Debian Codegraph @

extensions/AbuseFilter/tests/phpunit/unit/EmergencyCacheTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\AbuseFilter\Tests\Unit;
4
5 use MediaWiki\Extension\AbuseFilter\EmergencyCache;
6 use MediaWikiUnitTestCase;
7 use Wikimedia\ObjectCache\HashBagOStuff;
8
9 /**
10 * @covers \MediaWiki\Extension\AbuseFilter\EmergencyCache
11 */
12 class EmergencyCacheTest extends MediaWikiUnitTestCase {
13
14 public function testEmptyCache() {
15 $cache = new EmergencyCache( new HashBagOStuff(), [ 'default' => 86400 ] );
16 $this->assertSame( [], $cache->getFiltersToCheckInGroup( 'default' ) );
17 $this->assertFalse( $cache->getForFilter( 1 ) );
18 }
19
20 public function testSetNewForFilter() {
21 $time = microtime( true );
22 $stash = new HashBagOStuff();
23 $stash->setMockTime( $time );
24 $cache = new EmergencyCache( $stash, [ 'default' => 86400, 'other' => 3600 ] );
25 $cache->setNewForFilter( 2, 'other' );
26 $this->assertSame(
27 [ 'total' => 0, 'matches' => 0 ],
28 $cache->getForFilter( 2 )
29 );
30 $this->assertSame(
31 [ 2 ],
32 $cache->getFiltersToCheckInGroup( 'other' )
33 );
34 $this->assertSame(
35 [],
36 $cache->getFiltersToCheckInGroup( 'default' )
37 );
38
39 $time += 3599;
40 $this->assertNotFalse( $cache->getForFilter( 2 ) );
41 $time += 2;
42 $this->assertFalse( $cache->getForFilter( 2 ) );
43 $this->assertSame( [], $cache->getFiltersToCheckInGroup( 'other' ) );
44 }
45
46 public function testIncrementForFilter() {
47 $time = microtime( true );
48 $stash = new HashBagOStuff();
49 $stash->setMockTime( $time );
50 $cache = new EmergencyCache( $stash, [ 'default' => 86400 ] );
51 $cache->setNewForFilter( 1, 'default' );
52 $cache->incrementForFilter( 1, false );
53 $this->assertSame(
54 [ 'total' => 1, 'matches' => 0 ],
55 $cache->getForFilter( 1 )
56 );
57 $cache->incrementForFilter( 1, true );
58 $this->assertSame(
59 [ 'total' => 2, 'matches' => 1 ],
60 $cache->getForFilter( 1 )
61 );
62
63 $time += 86401;
64 $cache->incrementForFilter( 1, true );
65 $this->assertFalse( $cache->getForFilter( 1 ) );
66 }
67
68 public function testGetFiltersToCheckInGroup() {
69 $time = microtime( true );
70 $stash = new HashBagOStuff();
71 $stash->setMockTime( $time );
72 $cache = new EmergencyCache( $stash, [ 'default' => 3600 ] );
73 $cache->setNewForFilter( 1, 'default' );
74 $time += 1000;
75 $cache->setNewForFilter( 2, 'default' );
76 $this->assertArrayEquals(
77 [ 1, 2 ],
78 $cache->getFiltersToCheckInGroup( 'default' )
79 );
80 $time += 2601;
81 $this->assertArrayEquals(
82 [ 2 ],
83 $cache->getFiltersToCheckInGroup( 'default' )
84 );
85 }
86
87 }