3de315a51ac3bcc574696e9fe881271ce9d80376
6 * @covers MessageBlobStore
8 class MessageBlobStoreTest
extends ResourceLoaderTestCase
{
9 protected $tablesUsed = array( 'msg_resource' );
11 protected function makeBlobStore( $methods = null, $rl = null ) {
12 $blobStore = $this->getMockBuilder( 'MessageBlobStore' )
13 ->setConstructorArgs( array( $rl ) )
14 ->setMethods( $methods )
20 protected function makeModule( array $messages ) {
21 $module = new ResourceLoaderTestModule( array( 'messages' => $messages ) );
22 $module->setName( 'test.blobstore' );
26 public function testGetBlob() {
27 $module = $this->makeModule( array( 'foo' ) );
28 $rl = new ResourceLoader();
29 $rl->register( $module->getName(), $module );
31 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
32 $blobStore->expects( $this->once() )
33 ->method( 'fetchMessage' )
34 ->will( $this->returnValue( 'Example' ) );
36 $blob = $blobStore->getBlob( $module, 'en' );
38 $this->assertEquals( '{"foo":"Example"}', $blob, 'Generated blob' );
41 public function testGetBlobCached() {
42 $module = $this->makeModule( array( 'example' ) );
43 $rl = new ResourceLoader();
44 $rl->register( $module->getName(), $module );
46 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
47 $blobStore->expects( $this->once() )
48 ->method( 'fetchMessage' )
49 ->will( $this->returnValue( 'First' ) );
51 $module = $this->makeModule( array( 'example' ) );
52 $blob = $blobStore->getBlob( $module, 'en' );
53 $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
55 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
56 $blobStore->expects( $this->never() )
57 ->method( 'fetchMessage' )
58 ->will( $this->returnValue( 'Second' ) );
60 $module = $this->makeModule( array( 'example' ) );
61 $blob = $blobStore->getBlob( $module, 'en' );
62 $this->assertEquals( '{"example":"First"}', $blob, 'Cache hit' );
65 public function testUpdateMessage() {
66 $module = $this->makeModule( array( 'example' ) );
67 $rl = new ResourceLoader();
68 $rl->register( $module->getName(), $module );
69 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
70 $blobStore->expects( $this->exactly( 2 ) )
71 ->method( 'fetchMessage' )
72 ->will( $this->onConsecutiveCalls( 'First', 'Second' ) );
74 $blob = $blobStore->getBlob( $module, 'en' );
75 $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
77 $blobStore->updateMessage( 'example' );
79 $module = $this->makeModule( array( 'example' ) );
80 $rl = new ResourceLoader();
81 $rl->register( $module->getName(), $module );
82 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
83 $blobStore->expects( $this->never() )
84 ->method( 'fetchMessage' )
85 ->will( $this->returnValue( 'Wrong' ) );
87 $blob = $blobStore->getBlob( $module, 'en' );
88 $this->assertEquals( '{"example":"Second"}', $blob, 'Updated blob' );
91 public function testValidation() {
92 $module = $this->makeModule( array( 'foo' ) );
93 $rl = new ResourceLoader();
94 $rl->register( $module->getName(), $module );
96 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
97 $blobStore->expects( $this->once() )
98 ->method( 'fetchMessage' )
99 ->will( $this->returnValueMap( array(
100 array( 'foo', 'en', 'Hello' ),
103 $blob = $blobStore->getBlob( $module, 'en' );
104 $this->assertEquals( '{"foo":"Hello"}', $blob, 'Generated blob' );
106 // Now, imagine a change to the module is deployed. The module now contains
107 // message 'foo' and 'bar'. While updateMessage() was not called (since no
108 // message values were changed) it should detect the change in list of
110 $module = $this->makeModule( array( 'foo', 'bar' ) );
111 $rl = new ResourceLoader();
112 $rl->register( $module->getName(), $module );
114 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
115 $blobStore->expects( $this->exactly( 2 ) )
116 ->method( 'fetchMessage' )
117 ->will( $this->returnValueMap( array(
118 array( 'foo', 'en', 'Hello' ),
119 array( 'bar', 'en', 'World' ),
122 $blob = $blobStore->getBlob( $module, 'en' );
123 $this->assertEquals( '{"foo":"Hello","bar":"World"}', $blob, 'Updated blob' );