2015-11-20 23:58:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
2017-04-19 19:37:35 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
2015-11-20 23:58:59 +00:00
|
|
|
/**
|
2019-03-27 20:23:02 +00:00
|
|
|
* @group ResourceLoader
|
2015-11-20 23:58:59 +00:00
|
|
|
* @covers MessageBlobStore
|
|
|
|
|
*/
|
2018-02-17 12:29:13 +00:00
|
|
|
class MessageBlobStoreTest extends PHPUnit\Framework\TestCase {
|
2015-11-13 00:04:12 +00:00
|
|
|
|
2017-12-29 23:22:37 +00:00
|
|
|
use MediaWikiCoversValidator;
|
2019-02-16 23:46:30 +00:00
|
|
|
use PHPUnit4And6Compat;
|
2017-12-29 23:22:37 +00:00
|
|
|
|
2015-11-13 00:04:12 +00:00
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
2019-03-27 20:23:02 +00:00
|
|
|
// MediaWiki's test wrapper sets $wgMainWANCache to CACHE_NONE.
|
|
|
|
|
// Use HashBagOStuff here so that we can observe caching.
|
|
|
|
|
$this->wanCache = new WANObjectCache( [
|
|
|
|
|
'cache' => new HashBagOStuff()
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->clock = 1301655600.000;
|
|
|
|
|
$this->wanCache->setMockTime( $this->clock );
|
2017-04-01 00:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
public function testBlobCreation() {
|
2017-04-01 00:58:43 +00:00
|
|
|
$module = $this->makeModule( [ 'mainpage' ] );
|
|
|
|
|
$rl = new ResourceLoader();
|
|
|
|
|
$rl->register( $module->getName(), $module );
|
|
|
|
|
|
|
|
|
|
$blobStore = $this->makeBlobStore( null, $rl );
|
|
|
|
|
$blob = $blobStore->getBlob( $module, 'en' );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( '{"mainpage":"Main Page"}', $blob, 'Generated blob' );
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 21:08:10 +00:00
|
|
|
public function testBlobCreation_empty() {
|
|
|
|
|
$module = $this->makeModule( [] );
|
|
|
|
|
$rl = new ResourceLoader();
|
|
|
|
|
$rl->register( $module->getName(), $module );
|
|
|
|
|
|
|
|
|
|
$blobStore = $this->makeBlobStore( null, $rl );
|
|
|
|
|
$blob = $blobStore->getBlob( $module, 'en' );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( '{}', $blob, 'Generated blob' );
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
public function testBlobCreation_unknownMessage() {
|
2019-04-17 21:08:10 +00:00
|
|
|
$module = $this->makeModule( [ 'i-dont-exist', 'mainpage', 'i-dont-exist2' ] );
|
2017-04-01 00:58:43 +00:00
|
|
|
$rl = new ResourceLoader();
|
|
|
|
|
$rl->register( $module->getName(), $module );
|
|
|
|
|
$blobStore = $this->makeBlobStore( null, $rl );
|
|
|
|
|
|
2019-04-17 21:08:10 +00:00
|
|
|
// Generating a blob should continue without errors,
|
|
|
|
|
// with keys of unknown messages excluded from the blob.
|
2019-03-27 20:23:02 +00:00
|
|
|
$blob = $blobStore->getBlob( $module, 'en' );
|
2019-04-17 21:08:10 +00:00
|
|
|
$this->assertEquals( '{"mainpage":"Main Page"}', $blob, 'Generated blob' );
|
2017-04-01 00:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
public function testMessageCachingAndPurging() {
|
|
|
|
|
$module = $this->makeModule( [ 'example' ] );
|
2015-11-20 23:58:59 +00:00
|
|
|
$rl = new ResourceLoader();
|
|
|
|
|
$rl->register( $module->getName(), $module );
|
2016-02-17 09:09:32 +00:00
|
|
|
$blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
|
2019-03-27 20:23:02 +00:00
|
|
|
|
|
|
|
|
// Advance this new WANObjectCache instance to a normal state,
|
|
|
|
|
// by doing one "get" and letting its hold off period expire.
|
|
|
|
|
// Without this, the first real "get" would lazy-initialise the
|
|
|
|
|
// checkKey and thus reject the first "set".
|
|
|
|
|
$blobStore->getBlob( $module, 'en' );
|
|
|
|
|
$this->clock += 20;
|
|
|
|
|
|
|
|
|
|
// Arrange version 1 of a message
|
2015-11-20 23:58:59 +00:00
|
|
|
$blobStore->expects( $this->once() )
|
|
|
|
|
->method( 'fetchMessage' )
|
2019-03-27 20:23:02 +00:00
|
|
|
->will( $this->returnValue( 'First version' ) );
|
2015-11-20 23:58:59 +00:00
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
// Assert
|
2015-11-20 23:58:59 +00:00
|
|
|
$blob = $blobStore->getBlob( $module, 'en' );
|
2019-03-27 20:23:02 +00:00
|
|
|
$this->assertEquals( '{"example":"First version"}', $blob, 'Blob for v1' );
|
2015-11-20 23:58:59 +00:00
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
// Arrange version 2
|
2016-02-17 09:09:32 +00:00
|
|
|
$blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
|
2015-11-20 23:58:59 +00:00
|
|
|
$blobStore->expects( $this->once() )
|
|
|
|
|
->method( 'fetchMessage' )
|
2019-03-27 20:23:02 +00:00
|
|
|
->will( $this->returnValue( 'Second version' ) );
|
|
|
|
|
$this->clock += 20;
|
2015-11-20 23:58:59 +00:00
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
// Assert
|
|
|
|
|
// We do not validate whether a cached message is up-to-date.
|
|
|
|
|
// Instead, changes to messages will send us a purge.
|
|
|
|
|
// When cache is not purged or expired, it must be used.
|
2015-11-20 23:58:59 +00:00
|
|
|
$blob = $blobStore->getBlob( $module, 'en' );
|
2019-03-27 20:23:02 +00:00
|
|
|
$this->assertEquals( '{"example":"First version"}', $blob, 'Reuse cached v1 blob' );
|
2015-11-20 23:58:59 +00:00
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
// Purge cache
|
|
|
|
|
$blobStore->updateMessage( 'example' );
|
|
|
|
|
$this->clock += 20;
|
2015-11-20 23:58:59 +00:00
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
// Assert
|
2015-11-20 23:58:59 +00:00
|
|
|
$blob = $blobStore->getBlob( $module, 'en' );
|
2019-03-27 20:23:02 +00:00
|
|
|
$this->assertEquals( '{"example":"Second version"}', $blob, 'Updated blob for v2' );
|
2015-11-20 23:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
public function testPurgeEverything() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$module = $this->makeModule( [ 'example' ] );
|
2015-11-20 23:58:59 +00:00
|
|
|
$rl = new ResourceLoader();
|
|
|
|
|
$rl->register( $module->getName(), $module );
|
2016-02-17 09:09:32 +00:00
|
|
|
$blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
|
2019-03-27 20:23:02 +00:00
|
|
|
// Advance this new WANObjectCache instance to a normal state.
|
|
|
|
|
$blobStore->getBlob( $module, 'en' );
|
|
|
|
|
$this->clock += 20;
|
|
|
|
|
|
|
|
|
|
// Arrange version 1 and 2
|
|
|
|
|
$blobStore->expects( $this->exactly( 2 ) )
|
2015-11-20 23:58:59 +00:00
|
|
|
->method( 'fetchMessage' )
|
2019-03-27 20:23:02 +00:00
|
|
|
->will( $this->onConsecutiveCalls( 'First', 'Second' ) );
|
2015-11-20 23:58:59 +00:00
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
// Assert
|
2015-11-20 23:58:59 +00:00
|
|
|
$blob = $blobStore->getBlob( $module, 'en' );
|
2019-03-27 20:23:02 +00:00
|
|
|
$this->assertEquals( '{"example":"First"}', $blob, 'Blob for v1' );
|
2015-11-20 23:58:59 +00:00
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
$this->clock += 20;
|
2015-11-20 23:58:59 +00:00
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
// Assert
|
|
|
|
|
$blob = $blobStore->getBlob( $module, 'en' );
|
|
|
|
|
$this->assertEquals( '{"example":"First"}', $blob, 'Blob for v1 again' );
|
|
|
|
|
|
|
|
|
|
// Purge everything
|
|
|
|
|
$blobStore->clear();
|
|
|
|
|
$this->clock += 20;
|
2015-11-20 23:58:59 +00:00
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
// Assert
|
2015-11-20 23:58:59 +00:00
|
|
|
$blob = $blobStore->getBlob( $module, 'en' );
|
2019-03-27 20:23:02 +00:00
|
|
|
$this->assertEquals( '{"example":"Second"}', $blob, 'Blob for v2' );
|
2015-11-20 23:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
public function testValidateAgainstModuleRegistry() {
|
|
|
|
|
// Arrange version 1 of a module
|
2016-02-17 09:09:32 +00:00
|
|
|
$module = $this->makeModule( [ 'foo' ] );
|
2015-11-20 23:58:59 +00:00
|
|
|
$rl = new ResourceLoader();
|
|
|
|
|
$rl->register( $module->getName(), $module );
|
2016-02-17 09:09:32 +00:00
|
|
|
$blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
|
2015-11-20 23:58:59 +00:00
|
|
|
$blobStore->expects( $this->once() )
|
|
|
|
|
->method( 'fetchMessage' )
|
2016-02-17 09:09:32 +00:00
|
|
|
->will( $this->returnValueMap( [
|
2019-03-27 20:23:02 +00:00
|
|
|
// message key, language code, message value
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'foo', 'en', 'Hello' ],
|
|
|
|
|
] ) );
|
2015-11-20 23:58:59 +00:00
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
// Assert
|
2015-11-20 23:58:59 +00:00
|
|
|
$blob = $blobStore->getBlob( $module, 'en' );
|
2019-03-27 20:23:02 +00:00
|
|
|
$this->assertEquals( '{"foo":"Hello"}', $blob, 'Blob for v1' );
|
2015-11-20 23:58:59 +00:00
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
// Arrange version 2 of module
|
|
|
|
|
// While message values may be out of date, the set of messages returned
|
|
|
|
|
// must always match the set of message keys required by the module.
|
|
|
|
|
// We do not receive purges for this because no messages were changed.
|
2016-02-17 09:09:32 +00:00
|
|
|
$module = $this->makeModule( [ 'foo', 'bar' ] );
|
2015-11-20 23:58:59 +00:00
|
|
|
$rl = new ResourceLoader();
|
|
|
|
|
$rl->register( $module->getName(), $module );
|
2016-02-17 09:09:32 +00:00
|
|
|
$blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
|
2015-11-20 23:58:59 +00:00
|
|
|
$blobStore->expects( $this->exactly( 2 ) )
|
|
|
|
|
->method( 'fetchMessage' )
|
2016-02-17 09:09:32 +00:00
|
|
|
->will( $this->returnValueMap( [
|
2019-03-27 20:23:02 +00:00
|
|
|
// message key, language code, message value
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'foo', 'en', 'Hello' ],
|
|
|
|
|
[ 'bar', 'en', 'World' ],
|
|
|
|
|
] ) );
|
2015-11-20 23:58:59 +00:00
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
// Assert
|
2015-11-20 23:58:59 +00:00
|
|
|
$blob = $blobStore->getBlob( $module, 'en' );
|
2019-03-27 20:23:02 +00:00
|
|
|
$this->assertEquals( '{"foo":"Hello","bar":"World"}', $blob, 'Blob for v2' );
|
2015-11-20 23:58:59 +00:00
|
|
|
}
|
2015-11-13 00:04:12 +00:00
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
public function testSetLoggedIsVoid() {
|
|
|
|
|
$blobStore = $this->makeBlobStore();
|
|
|
|
|
$this->assertSame( null, $blobStore->setLogger( new Psr\Log\NullLogger() ) );
|
|
|
|
|
}
|
2015-11-13 00:04:12 +00:00
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
private function makeBlobStore( $methods = null, $rl = null ) {
|
|
|
|
|
$blobStore = $this->getMockBuilder( MessageBlobStore::class )
|
|
|
|
|
->setConstructorArgs( [ $rl ?? $this->createMock( ResourceLoader::class ) ] )
|
|
|
|
|
->setMethods( $methods )
|
|
|
|
|
->getMock();
|
2015-11-13 00:04:12 +00:00
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
$access = TestingAccessWrapper::newFromObject( $blobStore );
|
|
|
|
|
$access->wanCache = $this->wanCache;
|
|
|
|
|
return $blobStore;
|
|
|
|
|
}
|
2015-11-13 00:04:12 +00:00
|
|
|
|
2019-03-27 20:23:02 +00:00
|
|
|
private function makeModule( array $messages ) {
|
|
|
|
|
$module = new ResourceLoaderTestModule( [ 'messages' => $messages ] );
|
|
|
|
|
$module->setName( 'test.blobstore' );
|
|
|
|
|
return $module;
|
2015-11-13 00:04:12 +00:00
|
|
|
}
|
2015-11-20 23:58:59 +00:00
|
|
|
}
|