Merge "Support all values for exif PhotometricInterpretation"
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / MessageBlobStoreTest.php
1 <?php
2
3 /**
4 * @group Cache
5 * @covers MessageBlobStore
6 */
7 class MessageBlobStoreTest extends PHPUnit_Framework_TestCase {
8
9 protected function setUp() {
10 parent::setUp();
11 // MediaWiki tests defaults $wgMainWANCache to CACHE_NONE.
12 // Use hash instead so that caching is observed
13 $this->wanCache = $this->getMockBuilder( 'WANObjectCache' )
14 ->setConstructorArgs( array( array(
15 'cache' => new HashBagOStuff(),
16 'pool' => 'test',
17 'relayer' => new EventRelayerNull( array() )
18 ) ) )
19 ->setMethods( array( 'makePurgeValue' ) )
20 ->getMock();
21
22 $this->wanCache->expects( $this->any() )
23 ->method( 'makePurgeValue' )
24 ->will( $this->returnCallback( function ( $timestamp, $holdoff ) {
25 // Disable holdoff as it messes with testing
26 return WANObjectCache::PURGE_VAL_PREFIX . (float)$timestamp . ':0';
27 } ) );
28 }
29
30 protected function makeBlobStore( $methods = null, $rl = null ) {
31 $blobStore = $this->getMockBuilder( 'MessageBlobStore' )
32 ->setConstructorArgs( array( $rl ) )
33 ->setMethods( $methods )
34 ->getMock();
35
36 $access = TestingAccessWrapper::newFromObject( $blobStore );
37 $access->wanCache = $this->wanCache;
38 return $blobStore;
39 }
40
41 protected function makeModule( array $messages ) {
42 $module = new ResourceLoaderTestModule( array( 'messages' => $messages ) );
43 $module->setName( 'test.blobstore' );
44 return $module;
45 }
46
47 public function testGetBlob() {
48 $module = $this->makeModule( array( 'foo' ) );
49 $rl = new ResourceLoader();
50 $rl->register( $module->getName(), $module );
51
52 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
53 $blobStore->expects( $this->once() )
54 ->method( 'fetchMessage' )
55 ->will( $this->returnValue( 'Example' ) );
56
57 $blob = $blobStore->getBlob( $module, 'en' );
58
59 $this->assertEquals( '{"foo":"Example"}', $blob, 'Generated blob' );
60 }
61
62 public function testGetBlobCached() {
63 $module = $this->makeModule( array( 'example' ) );
64 $rl = new ResourceLoader();
65 $rl->register( $module->getName(), $module );
66
67 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
68 $blobStore->expects( $this->once() )
69 ->method( 'fetchMessage' )
70 ->will( $this->returnValue( 'First' ) );
71
72 $module = $this->makeModule( array( 'example' ) );
73 $blob = $blobStore->getBlob( $module, 'en' );
74 $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
75
76 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
77 $blobStore->expects( $this->never() )
78 ->method( 'fetchMessage' )
79 ->will( $this->returnValue( 'Second' ) );
80
81 $module = $this->makeModule( array( 'example' ) );
82 $blob = $blobStore->getBlob( $module, 'en' );
83 $this->assertEquals( '{"example":"First"}', $blob, 'Cache hit' );
84 }
85
86 public function testUpdateMessage() {
87 $module = $this->makeModule( array( 'example' ) );
88 $rl = new ResourceLoader();
89 $rl->register( $module->getName(), $module );
90 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
91 $blobStore->expects( $this->once() )
92 ->method( 'fetchMessage' )
93 ->will( $this->returnValue( 'First' ) );
94
95 $blob = $blobStore->getBlob( $module, 'en' );
96 $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
97
98 $blobStore->updateMessage( 'example' );
99
100 $module = $this->makeModule( array( 'example' ) );
101 $rl = new ResourceLoader();
102 $rl->register( $module->getName(), $module );
103 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
104 $blobStore->expects( $this->once() )
105 ->method( 'fetchMessage' )
106 ->will( $this->returnValue( 'Second' ) );
107
108 $blob = $blobStore->getBlob( $module, 'en' );
109 $this->assertEquals( '{"example":"Second"}', $blob, 'Updated blob' );
110 }
111
112 public function testValidation() {
113 $module = $this->makeModule( array( 'foo' ) );
114 $rl = new ResourceLoader();
115 $rl->register( $module->getName(), $module );
116
117 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
118 $blobStore->expects( $this->once() )
119 ->method( 'fetchMessage' )
120 ->will( $this->returnValueMap( array(
121 array( 'foo', 'en', 'Hello' ),
122 ) ) );
123
124 $blob = $blobStore->getBlob( $module, 'en' );
125 $this->assertEquals( '{"foo":"Hello"}', $blob, 'Generated blob' );
126
127 // Now, imagine a change to the module is deployed. The module now contains
128 // message 'foo' and 'bar'. While updateMessage() was not called (since no
129 // message values were changed) it should detect the change in list of
130 // message keys.
131 $module = $this->makeModule( array( 'foo', 'bar' ) );
132 $rl = new ResourceLoader();
133 $rl->register( $module->getName(), $module );
134
135 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
136 $blobStore->expects( $this->exactly( 2 ) )
137 ->method( 'fetchMessage' )
138 ->will( $this->returnValueMap( array(
139 array( 'foo', 'en', 'Hello' ),
140 array( 'bar', 'en', 'World' ),
141 ) ) );
142
143 $blob = $blobStore->getBlob( $module, 'en' );
144 $this->assertEquals( '{"foo":"Hello","bar":"World"}', $blob, 'Updated blob' );
145 }
146
147 public function testClear() {
148 $module = $this->makeModule( array( 'example' ) );
149 $rl = new ResourceLoader();
150 $rl->register( $module->getName(), $module );
151 $blobStore = $this->makeBlobStore( array( 'fetchMessage' ), $rl );
152 $blobStore->expects( $this->exactly( 2 ) )
153 ->method( 'fetchMessage' )
154 ->will( $this->onConsecutiveCalls( 'First', 'Second' ) );
155
156 $blob = $blobStore->getBlob( $module, 'en' );
157 $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
158
159 $blob = $blobStore->getBlob( $module, 'en' );
160 $this->assertEquals( '{"example":"First"}', $blob, 'Cache-hit' );
161
162 $blobStore->clear();
163
164 $blob = $blobStore->getBlob( $module, 'en' );
165 $this->assertEquals( '{"example":"Second"}', $blob, 'Updated blob' );
166 }
167 }