| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\Tests\Unit; |
| 4 |
|
| 5 |
use MediaWiki\Extension\AbuseFilter\EditStashCache; |
| 6 |
use MediaWiki\Extension\AbuseFilter\KeywordsManager; |
| 7 |
use MediaWiki\Extension\AbuseFilter\Variables\LazyVariableComputer; |
| 8 |
use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder; |
| 9 |
use MediaWiki\Extension\AbuseFilter\Variables\VariablesManager; |
| 10 |
use MediaWiki\Title\TitleValue; |
| 11 |
use MediaWikiUnitTestCase; |
| 12 |
use Psr\Log\LoggerInterface; |
| 13 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 14 |
use Wikimedia\Stats\NullStatsdDataFactory; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class EditStashCacheTest extends MediaWikiUnitTestCase { |
| 20 |
|
| 21 |
private function getVariablesManager(): VariablesManager { |
| 22 |
return new VariablesManager( |
| 23 |
$this->createMock( KeywordsManager::class ), |
| 24 |
$this->createMock( LazyVariableComputer::class ) |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
public function testStore() { |
| 29 |
$title = new TitleValue( NS_MAIN, 'Some title' ); |
| 30 |
$cache = $this->getMockBuilder( HashBagOStuff::class ) |
| 31 |
->onlyMethods( [ 'set' ] ) |
| 32 |
->getMock(); |
| 33 |
$cache->expects( $this->once() )->method( 'set' ); |
| 34 |
$logger = $this->createMock( LoggerInterface::class ); |
| 35 |
$logger->expects( $this->once() ) |
| 36 |
->method( 'debug' ) |
| 37 |
->willReturnCallback( function ( $msg, $args ) use ( $title ) { |
| 38 |
unset( $args['key'] ); |
| 39 |
$this->assertSame( [ 'logtype' => 'store', 'target' => $title ], $args ); |
| 40 |
} ); |
| 41 |
$stash = new EditStashCache( |
| 42 |
$cache, |
| 43 |
new NullStatsdDataFactory(), |
| 44 |
$this->getVariablesManager(), |
| 45 |
$logger, |
| 46 |
$title, |
| 47 |
'default' |
| 48 |
); |
| 49 |
$vars = VariableHolder::newFromArray( [ 'page_title' => 'Title' ] ); |
| 50 |
$data = [ 'foo' => 'bar' ]; |
| 51 |
$stash->store( $vars, $data ); |
| 52 |
$this->addToAssertionCount( 1 ); |
| 53 |
} |
| 54 |
|
| 55 |
public static function provideRoundTrip() { |
| 56 |
$simple = [ 'page_title' => 'Title', 'new_wikitext' => 'Foo Bar' ]; |
| 57 |
yield 'simple' => [ $simple, $simple ]; |
| 58 |
yield 'noisy' => [ |
| 59 |
$simple + [ 'user_age' => 100 ], |
| 60 |
$simple + [ 'user_age' => 200 ], |
| 61 |
]; |
| 62 |
$reverse = [ |
| 63 |
'new_wikitext' => $simple['new_wikitext'], |
| 64 |
'page_title' => $simple['page_title'], |
| 65 |
]; |
| 66 |
yield 'different order' => [ |
| 67 |
$reverse + [ 'page_age' => 100 ], |
| 68 |
$reverse + [ 'page_age' => 200 ], |
| 69 |
]; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function testRoundTrip( array $storeVars, array $seekVars ) { |
| 76 |
$title = new TitleValue( NS_MAIN, 'Some title' ); |
| 77 |
$cache = new HashBagOStuff(); |
| 78 |
$logger = $this->createMock( LoggerInterface::class ); |
| 79 |
$storeLogged = false; |
| 80 |
$logger->expects( $this->exactly( 2 ) ) |
| 81 |
->method( 'debug' ) |
| 82 |
->willReturnCallback( function ( $msg, $args ) use ( $title, &$storeLogged ) { |
| 83 |
unset( $args['key'] ); |
| 84 |
if ( !$storeLogged ) { |
| 85 |
$this->assertSame( [ 'logtype' => 'store', 'target' => $title ], $args ); |
| 86 |
$storeLogged = true; |
| 87 |
} else { |
| 88 |
$this->assertSame( [ 'logtype' => 'hit', 'target' => $title ], $args ); |
| 89 |
} |
| 90 |
} ); |
| 91 |
$stash = new EditStashCache( |
| 92 |
$cache, |
| 93 |
new NullStatsdDataFactory(), |
| 94 |
$this->getVariablesManager(), |
| 95 |
$logger, |
| 96 |
$title, |
| 97 |
'default' |
| 98 |
); |
| 99 |
$storeHolder = VariableHolder::newFromArray( $storeVars ); |
| 100 |
$data = [ 'foo' => 'bar' ]; |
| 101 |
$stash->store( $storeHolder, $data ); |
| 102 |
|
| 103 |
$seekHolder = VariableHolder::newFromArray( $seekVars ); |
| 104 |
$value = $stash->seek( $seekHolder ); |
| 105 |
$this->assertArrayEquals( $data, $value ); |
| 106 |
} |
| 107 |
|
| 108 |
public function testSeek_miss() { |
| 109 |
$title = new TitleValue( NS_MAIN, 'Some title' ); |
| 110 |
$cache = new HashBagOStuff(); |
| 111 |
$logger = $this->createMock( LoggerInterface::class ); |
| 112 |
$logger->expects( $this->once() ) |
| 113 |
->method( 'debug' ) |
| 114 |
->willReturnCallback( function ( $msg, $args ) use ( $title ) { |
| 115 |
unset( $args['key'] ); |
| 116 |
$this->assertSame( [ 'logtype' => 'miss', 'target' => $title ], $args ); |
| 117 |
} ); |
| 118 |
$stash = new EditStashCache( |
| 119 |
$cache, |
| 120 |
new NullStatsdDataFactory(), |
| 121 |
$this->getVariablesManager(), |
| 122 |
$logger, |
| 123 |
$title, |
| 124 |
'default' |
| 125 |
); |
| 126 |
$vars = VariableHolder::newFromArray( [ 'page_title' => 'Title' ] ); |
| 127 |
$value = $stash->seek( $vars ); |
| 128 |
$this->assertFalse( $value ); |
| 129 |
} |
| 130 |
|
| 131 |
} |