From: Timo Tijhof Date: Fri, 20 Nov 2015 23:58:59 +0000 (+0000) Subject: Add unit tests for MessageBlobStore X-Git-Tag: 1.31.0-rc.0~8915^2 X-Git-Url: http://git.cyclocoop.org/fichier?a=commitdiff_plain;h=a4fa0229c1c3ed436f4cefcaa0365e02c176efe7;p=lhc%2Fweb%2Fwiklou.git Add unit tests for MessageBlobStore * Also added getBlob() and getBlobs() methods. These don't require passing ResourceLoader as argument. In preparation for T113092. Change-Id: I3239c45f0243eca1eaf85bc53fca736e4c9a209c --- diff --git a/includes/cache/MessageBlobStore.php b/includes/cache/MessageBlobStore.php index 624bbc9456..6054eccab6 100644 --- a/includes/cache/MessageBlobStore.php +++ b/includes/cache/MessageBlobStore.php @@ -50,15 +50,29 @@ class MessageBlobStore { $this->resourceloader = $resourceloader; } + /** + * Get the message blob for a module + * + * @since 1.27 + * @param ResourceLoaderModule $module + * @param string $lang Language code + * @return string JSON + */ + public function getBlob( ResourceLoaderModule $module, $lang ) { + $blobs = $this->getBlobs( array( $module->getName() => $module ), $lang ); + return $blobs[$module->getName()]; + } + /** * Get the message blobs for a set of modules * + * @since 1.27 * @param ResourceLoader $resourceLoader * @param array $modules Array of module objects keyed by module name * @param string $lang Language code * @return array An array mapping module names to message blobs */ - public function get( ResourceLoader $resourceLoader, $modules, $lang ) { + public function getBlobs( $modules, $lang ) { if ( !count( $modules ) ) { return array(); } @@ -99,6 +113,16 @@ class MessageBlobStore { return $blobs; } + /** + * Get the message blobs for a set of modules + * + * @deprecated since 1.27 Use getBlobs() instead + * @return array + */ + public function get( ResourceLoader $resourceLoader, $modules, $lang ) { + return $this->getBlobs( $modules, $lang ); + } + /** * Generate and insert a new message blob. If the blob was already * present, it is not regenerated; instead, the preexisting blob @@ -313,7 +337,7 @@ class MessageBlobStore { * @param string $lang Language code * @return string */ - private function fetchMessage( $key, $lang ) { + protected function fetchMessage( $key, $lang ) { $message = wfMessage( $key )->inLanguage( $lang ); if ( !$message->exists() ) { wfDebugLog( 'resourceloader', __METHOD__ . " failed to find: '$key' ($lang)" ); diff --git a/tests/phpunit/ResourceLoaderTestCase.php b/tests/phpunit/ResourceLoaderTestCase.php index f22f4a15a3..1863929cd2 100644 --- a/tests/phpunit/ResourceLoaderTestCase.php +++ b/tests/phpunit/ResourceLoaderTestCase.php @@ -60,6 +60,7 @@ abstract class ResourceLoaderTestCase extends MediaWikiTestCase { /* Stubs */ class ResourceLoaderTestModule extends ResourceLoaderModule { + protected $messages = array(); protected $dependencies = array(); protected $group = null; protected $source = 'local'; @@ -83,6 +84,10 @@ class ResourceLoaderTestModule extends ResourceLoaderModule { return array( '' => $this->styles ); } + public function getMessages() { + return $this->messages; + } + public function getDependencies( ResourceLoaderContext $context = null ) { return $this->dependencies; } diff --git a/tests/phpunit/includes/resourceloader/MessageBlobStoreTest.php b/tests/phpunit/includes/resourceloader/MessageBlobStoreTest.php new file mode 100644 index 0000000000..3de315a51a --- /dev/null +++ b/tests/phpunit/includes/resourceloader/MessageBlobStoreTest.php @@ -0,0 +1,125 @@ +getMockBuilder( 'MessageBlobStore' ) + ->setConstructorArgs( array( $rl ) ) + ->setMethods( $methods ) + ->getMock(); + + return $blobStore; + } + + protected function makeModule( array $messages ) { + $module = new ResourceLoaderTestModule( array( 'messages' => $messages ) ); + $module->setName( 'test.blobstore' ); + return $module; + } + + public function testGetBlob() { + $module = $this->makeModule( array( 'foo' ) ); + $rl = new ResourceLoader(); + $rl->register( $module->getName(), $module ); + + $blobStore = $this->makeBlobStore( array( '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( array( 'example' ) ); + $rl = new ResourceLoader(); + $rl->register( $module->getName(), $module ); + + $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl ); + $blobStore->expects( $this->once() ) + ->method( 'fetchMessage' ) + ->will( $this->returnValue( 'First' ) ); + + $module = $this->makeModule( array( 'example' ) ); + $blob = $blobStore->getBlob( $module, 'en' ); + $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' ); + + $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl ); + $blobStore->expects( $this->never() ) + ->method( 'fetchMessage' ) + ->will( $this->returnValue( 'Second' ) ); + + $module = $this->makeModule( array( 'example' ) ); + $blob = $blobStore->getBlob( $module, 'en' ); + $this->assertEquals( '{"example":"First"}', $blob, 'Cache hit' ); + } + + public function testUpdateMessage() { + $module = $this->makeModule( array( 'example' ) ); + $rl = new ResourceLoader(); + $rl->register( $module->getName(), $module ); + $blobStore = $this->makeBlobStore( array( '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' ); + + $blobStore->updateMessage( 'example' ); + + $module = $this->makeModule( array( 'example' ) ); + $rl = new ResourceLoader(); + $rl->register( $module->getName(), $module ); + $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl ); + $blobStore->expects( $this->never() ) + ->method( 'fetchMessage' ) + ->will( $this->returnValue( 'Wrong' ) ); + + $blob = $blobStore->getBlob( $module, 'en' ); + $this->assertEquals( '{"example":"Second"}', $blob, 'Updated blob' ); + } + + public function testValidation() { + $module = $this->makeModule( array( 'foo' ) ); + $rl = new ResourceLoader(); + $rl->register( $module->getName(), $module ); + + $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl ); + $blobStore->expects( $this->once() ) + ->method( 'fetchMessage' ) + ->will( $this->returnValueMap( array( + array( '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( array( 'foo', 'bar' ) ); + $rl = new ResourceLoader(); + $rl->register( $module->getName(), $module ); + + $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl ); + $blobStore->expects( $this->exactly( 2 ) ) + ->method( 'fetchMessage' ) + ->will( $this->returnValueMap( array( + array( 'foo', 'en', 'Hello' ), + array( 'bar', 'en', 'World' ), + ) ) ); + + $blob = $blobStore->getBlob( $module, 'en' ); + $this->assertEquals( '{"foo":"Hello","bar":"World"}', $blob, 'Updated blob' ); + } +}