Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / resourceloader / MessageBlobStoreTest.php
blobbf80845976414b983a88e65ca6d2da7e76bc22f2
1 <?php
3 /**
4 * @group Cache
5 * @covers MessageBlobStore
6 */
7 class MessageBlobStoreTest extends PHPUnit_Framework_TestCase {
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( [ [
15 'cache' => new HashBagOStuff(),
16 'pool' => 'test',
17 'relayer' => new EventRelayerNull( [] )
18 ] ] )
19 ->setMethods( [ 'makePurgeValue' ] )
20 ->getMock();
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 } ) );
30 protected function makeBlobStore( $methods = null, $rl = null ) {
31 $blobStore = $this->getMockBuilder( 'MessageBlobStore' )
32 ->setConstructorArgs( [ $rl ] )
33 ->setMethods( $methods )
34 ->getMock();
36 $access = TestingAccessWrapper::newFromObject( $blobStore );
37 $access->wanCache = $this->wanCache;
38 return $blobStore;
41 protected function makeModule( array $messages ) {
42 $module = new ResourceLoaderTestModule( [ 'messages' => $messages ] );
43 $module->setName( 'test.blobstore' );
44 return $module;
47 public function testGetBlob() {
48 $module = $this->makeModule( [ 'foo' ] );
49 $rl = new ResourceLoader();
50 $rl->register( $module->getName(), $module );
52 $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
53 $blobStore->expects( $this->once() )
54 ->method( 'fetchMessage' )
55 ->will( $this->returnValue( 'Example' ) );
57 $blob = $blobStore->getBlob( $module, 'en' );
59 $this->assertEquals( '{"foo":"Example"}', $blob, 'Generated blob' );
62 public function testGetBlobCached() {
63 $module = $this->makeModule( [ 'example' ] );
64 $rl = new ResourceLoader();
65 $rl->register( $module->getName(), $module );
67 $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
68 $blobStore->expects( $this->once() )
69 ->method( 'fetchMessage' )
70 ->will( $this->returnValue( 'First' ) );
72 $module = $this->makeModule( [ 'example' ] );
73 $blob = $blobStore->getBlob( $module, 'en' );
74 $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
76 $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
77 $blobStore->expects( $this->never() )
78 ->method( 'fetchMessage' )
79 ->will( $this->returnValue( 'Second' ) );
81 $module = $this->makeModule( [ 'example' ] );
82 $blob = $blobStore->getBlob( $module, 'en' );
83 $this->assertEquals( '{"example":"First"}', $blob, 'Cache hit' );
86 public function testUpdateMessage() {
87 $module = $this->makeModule( [ 'example' ] );
88 $rl = new ResourceLoader();
89 $rl->register( $module->getName(), $module );
90 $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
91 $blobStore->expects( $this->once() )
92 ->method( 'fetchMessage' )
93 ->will( $this->returnValue( 'First' ) );
95 $blob = $blobStore->getBlob( $module, 'en' );
96 $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
98 $blobStore->updateMessage( 'example' );
100 $module = $this->makeModule( [ 'example' ] );
101 $rl = new ResourceLoader();
102 $rl->register( $module->getName(), $module );
103 $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
104 $blobStore->expects( $this->once() )
105 ->method( 'fetchMessage' )
106 ->will( $this->returnValue( 'Second' ) );
108 $blob = $blobStore->getBlob( $module, 'en' );
109 $this->assertEquals( '{"example":"Second"}', $blob, 'Updated blob' );
112 public function testValidation() {
113 $module = $this->makeModule( [ 'foo' ] );
114 $rl = new ResourceLoader();
115 $rl->register( $module->getName(), $module );
117 $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
118 $blobStore->expects( $this->once() )
119 ->method( 'fetchMessage' )
120 ->will( $this->returnValueMap( [
121 [ 'foo', 'en', 'Hello' ],
122 ] ) );
124 $blob = $blobStore->getBlob( $module, 'en' );
125 $this->assertEquals( '{"foo":"Hello"}', $blob, 'Generated blob' );
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( [ 'foo', 'bar' ] );
132 $rl = new ResourceLoader();
133 $rl->register( $module->getName(), $module );
135 $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
136 $blobStore->expects( $this->exactly( 2 ) )
137 ->method( 'fetchMessage' )
138 ->will( $this->returnValueMap( [
139 [ 'foo', 'en', 'Hello' ],
140 [ 'bar', 'en', 'World' ],
141 ] ) );
143 $blob = $blobStore->getBlob( $module, 'en' );
144 $this->assertEquals( '{"foo":"Hello","bar":"World"}', $blob, 'Updated blob' );
147 public function testClear() {
148 $module = $this->makeModule( [ 'example' ] );
149 $rl = new ResourceLoader();
150 $rl->register( $module->getName(), $module );
151 $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
152 $blobStore->expects( $this->exactly( 2 ) )
153 ->method( 'fetchMessage' )
154 ->will( $this->onConsecutiveCalls( 'First', 'Second' ) );
156 $blob = $blobStore->getBlob( $module, 'en' );
157 $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
159 $blob = $blobStore->getBlob( $module, 'en' );
160 $this->assertEquals( '{"example":"First"}', $blob, 'Cache-hit' );
162 $blobStore->clear();
164 $blob = $blobStore->getBlob( $module, 'en' );
165 $this->assertEquals( '{"example":"Second"}', $blob, 'Updated blob' );