wiki.techinc.nl/tests/phpunit/includes/resourceloader/MessageBlobStoreTest.php
Kunal Mehta 6e9b4f0e9c Convert all array() syntax to []
Per wikitech-l consensus:
 https://lists.wikimedia.org/pipermail/wikitech-l/2016-February/084821.html

Notes:
* Disabled CallTimePassByReference due to false positives (T127163)

Change-Id: I2c8ce713ce6600a0bb7bf67537c87044c7a45c4b
2016-02-17 01:33:00 -08:00

167 lines
5.5 KiB
PHP

<?php
/**
* @group Cache
* @covers MessageBlobStore
*/
class MessageBlobStoreTest extends PHPUnit_Framework_TestCase {
protected function setUp() {
parent::setUp();
// MediaWiki tests defaults $wgMainWANCache to CACHE_NONE.
// Use hash instead so that caching is observed
$this->wanCache = $this->getMockBuilder( 'WANObjectCache' )
->setConstructorArgs( [ [
'cache' => new HashBagOStuff(),
'pool' => 'test',
'relayer' => new EventRelayerNull( [] )
] ] )
->setMethods( [ 'makePurgeValue' ] )
->getMock();
$this->wanCache->expects( $this->any() )
->method( 'makePurgeValue' )
->will( $this->returnCallback( function ( $timestamp, $holdoff ) {
// Disable holdoff as it messes with testing
return WANObjectCache::PURGE_VAL_PREFIX . (float)$timestamp . ':0';
} ) );
}
protected function makeBlobStore( $methods = null, $rl = null ) {
$blobStore = $this->getMockBuilder( 'MessageBlobStore' )
->setConstructorArgs( [ $rl ] )
->setMethods( $methods )
->getMock();
$access = TestingAccessWrapper::newFromObject( $blobStore );
$access->wanCache = $this->wanCache;
return $blobStore;
}
protected function makeModule( array $messages ) {
$module = new ResourceLoaderTestModule( [ 'messages' => $messages ] );
$module->setName( 'test.blobstore' );
return $module;
}
public function testGetBlob() {
$module = $this->makeModule( [ 'foo' ] );
$rl = new ResourceLoader();
$rl->register( $module->getName(), $module );
$blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
$blobStore->expects( $this->once() )
->method( 'fetchMessage' )
->will( $this->returnValue( 'Example' ) );
$blob = $blobStore->getBlob( $module, 'en' );
$this->assertEquals( '{"foo":"Example"}', $blob, 'Generated blob' );
}
public function testGetBlobCached() {
$module = $this->makeModule( [ 'example' ] );
$rl = new ResourceLoader();
$rl->register( $module->getName(), $module );
$blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
$blobStore->expects( $this->once() )
->method( 'fetchMessage' )
->will( $this->returnValue( 'First' ) );
$module = $this->makeModule( [ 'example' ] );
$blob = $blobStore->getBlob( $module, 'en' );
$this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
$blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
$blobStore->expects( $this->never() )
->method( 'fetchMessage' )
->will( $this->returnValue( 'Second' ) );
$module = $this->makeModule( [ 'example' ] );
$blob = $blobStore->getBlob( $module, 'en' );
$this->assertEquals( '{"example":"First"}', $blob, 'Cache hit' );
}
public function testUpdateMessage() {
$module = $this->makeModule( [ 'example' ] );
$rl = new ResourceLoader();
$rl->register( $module->getName(), $module );
$blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
$blobStore->expects( $this->once() )
->method( 'fetchMessage' )
->will( $this->returnValue( 'First' ) );
$blob = $blobStore->getBlob( $module, 'en' );
$this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
$blobStore->updateMessage( 'example' );
$module = $this->makeModule( [ 'example' ] );
$rl = new ResourceLoader();
$rl->register( $module->getName(), $module );
$blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
$blobStore->expects( $this->once() )
->method( 'fetchMessage' )
->will( $this->returnValue( 'Second' ) );
$blob = $blobStore->getBlob( $module, 'en' );
$this->assertEquals( '{"example":"Second"}', $blob, 'Updated blob' );
}
public function testValidation() {
$module = $this->makeModule( [ 'foo' ] );
$rl = new ResourceLoader();
$rl->register( $module->getName(), $module );
$blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
$blobStore->expects( $this->once() )
->method( 'fetchMessage' )
->will( $this->returnValueMap( [
[ 'foo', 'en', 'Hello' ],
] ) );
$blob = $blobStore->getBlob( $module, 'en' );
$this->assertEquals( '{"foo":"Hello"}', $blob, 'Generated blob' );
// Now, imagine a change to the module is deployed. The module now contains
// message 'foo' and 'bar'. While updateMessage() was not called (since no
// message values were changed) it should detect the change in list of
// message keys.
$module = $this->makeModule( [ 'foo', 'bar' ] );
$rl = new ResourceLoader();
$rl->register( $module->getName(), $module );
$blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
$blobStore->expects( $this->exactly( 2 ) )
->method( 'fetchMessage' )
->will( $this->returnValueMap( [
[ 'foo', 'en', 'Hello' ],
[ 'bar', 'en', 'World' ],
] ) );
$blob = $blobStore->getBlob( $module, 'en' );
$this->assertEquals( '{"foo":"Hello","bar":"World"}', $blob, 'Updated blob' );
}
public function testClear() {
$module = $this->makeModule( [ 'example' ] );
$rl = new ResourceLoader();
$rl->register( $module->getName(), $module );
$blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
$blobStore->expects( $this->exactly( 2 ) )
->method( 'fetchMessage' )
->will( $this->onConsecutiveCalls( 'First', 'Second' ) );
$blob = $blobStore->getBlob( $module, 'en' );
$this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
$blob = $blobStore->getBlob( $module, 'en' );
$this->assertEquals( '{"example":"First"}', $blob, 'Cache-hit' );
$blobStore->clear();
$blob = $blobStore->getBlob( $module, 'en' );
$this->assertEquals( '{"example":"Second"}', $blob, 'Updated blob' );
}
}