Renamed confusing initial $status var in doEditContent()
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / MessageBlobStoreTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @group Cache
6 * @covers MessageBlobStore
7 */
8 class MessageBlobStoreTest extends ResourceLoaderTestCase {
9 protected $tablesUsed = array( 'msg_resource' );
10
11 protected function makeBlobStore( $methods = null, $rl = null ) {
12 $blobStore = $this->getMockBuilder( 'MessageBlobStore' )
13 ->setConstructorArgs( array( $rl ) )
14 ->setMethods( $methods )
15 ->getMock();
16
17 return $blobStore;
18 }
19
20 protected function makeModule( array $messages ) {
21 $module = new ResourceLoaderTestModule( array( 'messages' => $messages ) );
22 $module->setName( 'test.blobstore' );
23 return $module;
24 }
25
26 public function testGetBlob() {
27 $module = $this->makeModule( array( 'foo' ) );
28 $rl = new ResourceLoader();
29 $rl->register( $module->getName(), $module );
30
31 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
32 $blobStore->expects( $this->once() )
33 ->method( 'fetchMessage' )
34 ->will( $this->returnValue( 'Example' ) );
35
36 $blob = $blobStore->getBlob( $module, 'en' );
37
38 $this->assertEquals( '{"foo":"Example"}', $blob, 'Generated blob' );
39 }
40
41 public function testGetBlobCached() {
42 $module = $this->makeModule( array( 'example' ) );
43 $rl = new ResourceLoader();
44 $rl->register( $module->getName(), $module );
45
46 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
47 $blobStore->expects( $this->once() )
48 ->method( 'fetchMessage' )
49 ->will( $this->returnValue( 'First' ) );
50
51 $module = $this->makeModule( array( 'example' ) );
52 $blob = $blobStore->getBlob( $module, 'en' );
53 $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
54
55 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
56 $blobStore->expects( $this->never() )
57 ->method( 'fetchMessage' )
58 ->will( $this->returnValue( 'Second' ) );
59
60 $module = $this->makeModule( array( 'example' ) );
61 $blob = $blobStore->getBlob( $module, 'en' );
62 $this->assertEquals( '{"example":"First"}', $blob, 'Cache hit' );
63 }
64
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' ) );
73
74 $blob = $blobStore->getBlob( $module, 'en' );
75 $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
76
77 $blobStore->updateMessage( 'example' );
78
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' ) );
86
87 $blob = $blobStore->getBlob( $module, 'en' );
88 $this->assertEquals( '{"example":"Second"}', $blob, 'Updated blob' );
89 }
90
91 public function testValidation() {
92 $module = $this->makeModule( array( 'foo' ) );
93 $rl = new ResourceLoader();
94 $rl->register( $module->getName(), $module );
95
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' ),
101 ) ) );
102
103 $blob = $blobStore->getBlob( $module, 'en' );
104 $this->assertEquals( '{"foo":"Hello"}', $blob, 'Generated blob' );
105
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
109 // message keys.
110 $module = $this->makeModule( array( 'foo', 'bar' ) );
111 $rl = new ResourceLoader();
112 $rl->register( $module->getName(), $module );
113
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' ),
120 ) ) );
121
122 $blob = $blobStore->getBlob( $module, 'en' );
123 $this->assertEquals( '{"foo":"Hello","bar":"World"}', $blob, 'Updated blob' );
124 }
125 }