MediaWiki  REL1_31
MessageBlobStoreTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\TestingAccessWrapper;
4 
9 class MessageBlobStoreTest extends PHPUnit\Framework\TestCase {
10 
11  use MediaWikiCoversValidator;
12 
13  protected function setUp() {
14  parent::setUp();
15  // MediaWiki tests defaults $wgMainWANCache to CACHE_NONE.
16  // Use hash instead so that caching is observed
17  $this->wanCache = $this->getMockBuilder( WANObjectCache::class )
18  ->setConstructorArgs( [ [
19  'cache' => new HashBagOStuff(),
20  'pool' => 'test',
21  'relayer' => new EventRelayerNull( [] )
22  ] ] )
23  ->setMethods( [ 'makePurgeValue' ] )
24  ->getMock();
25 
26  $this->wanCache->expects( $this->any() )
27  ->method( 'makePurgeValue' )
28  ->will( $this->returnCallback( function ( $timestamp, $holdoff ) {
29  // Disable holdoff as it messes with testing. Aside from a 0-second holdoff,
30  // make sure that "time" passes between getMulti() check init and the set()
31  // in recacheMessageBlob(). This especially matters for Windows clocks.
32  $ts = (float)$timestamp - 0.0001;
33 
34  return WANObjectCache::PURGE_VAL_PREFIX . $ts . ':0';
35  } ) );
36  }
37 
38  protected function makeBlobStore( $methods = null, $rl = null ) {
39  $blobStore = $this->getMockBuilder( MessageBlobStore::class )
40  ->setConstructorArgs( [ $rl ] )
41  ->setMethods( $methods )
42  ->getMock();
43 
44  $access = TestingAccessWrapper::newFromObject( $blobStore );
45  $access->wanCache = $this->wanCache;
46  return $blobStore;
47  }
48 
49  protected function makeModule( array $messages ) {
50  $module = new ResourceLoaderTestModule( [ 'messages' => $messages ] );
51  $module->setName( 'test.blobstore' );
52  return $module;
53  }
54 
56  public function testSetLogger() {
57  $blobStore = $this->makeBlobStore();
58  $this->assertSame( null, $blobStore->setLogger( new Psr\Log\NullLogger() ) );
59  }
60 
62  public function testGetResourceLoader() {
63  // Call protected method
64  $blobStore = TestingAccessWrapper::newFromObject( $this->makeBlobStore() );
65  $this->assertInstanceOf(
67  $blobStore->getResourceLoader()
68  );
69  }
70 
72  public function testFetchMessage() {
73  $module = $this->makeModule( [ 'mainpage' ] );
74  $rl = new ResourceLoader();
75  $rl->register( $module->getName(), $module );
76 
77  $blobStore = $this->makeBlobStore( null, $rl );
78  $blob = $blobStore->getBlob( $module, 'en' );
79 
80  $this->assertEquals( '{"mainpage":"Main Page"}', $blob, 'Generated blob' );
81  }
82 
84  public function testFetchMessageFail() {
85  $module = $this->makeModule( [ 'i-dont-exist' ] );
86  $rl = new ResourceLoader();
87  $rl->register( $module->getName(), $module );
88 
89  $blobStore = $this->makeBlobStore( null, $rl );
90  $blob = $blobStore->getBlob( $module, 'en' );
91 
92  $this->assertEquals( '{"i-dont-exist":"\u29fci-dont-exist\u29fd"}', $blob, 'Generated blob' );
93  }
94 
95  public function testGetBlob() {
96  $module = $this->makeModule( [ 'foo' ] );
97  $rl = new ResourceLoader();
98  $rl->register( $module->getName(), $module );
99 
100  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
101  $blobStore->expects( $this->once() )
102  ->method( 'fetchMessage' )
103  ->will( $this->returnValue( 'Example' ) );
104 
105  $blob = $blobStore->getBlob( $module, 'en' );
106 
107  $this->assertEquals( '{"foo":"Example"}', $blob, 'Generated blob' );
108  }
109 
115  public function testGetBlobCached() {
116  $module = $this->makeModule( [ 'example' ] );
117  $rl = new ResourceLoader();
118  $rl->register( $module->getName(), $module );
119 
120  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
121  $blobStore->expects( $this->once() )
122  ->method( 'fetchMessage' )
123  ->will( $this->returnValue( 'First' ) );
124 
125  $module = $this->makeModule( [ 'example' ] );
126  $blob = $blobStore->getBlob( $module, 'en' );
127  $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
128 
129  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
130  $blobStore->expects( $this->never() )
131  ->method( 'fetchMessage' )
132  ->will( $this->returnValue( 'Second' ) );
133 
134  $module = $this->makeModule( [ 'example' ] );
135  $blob = $blobStore->getBlob( $module, 'en' );
136  $this->assertEquals( '{"example":"First"}', $blob, 'Cache hit' );
137  }
138 
139  public function testUpdateMessage() {
140  $module = $this->makeModule( [ 'example' ] );
141  $rl = new ResourceLoader();
142  $rl->register( $module->getName(), $module );
143  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
144  $blobStore->expects( $this->once() )
145  ->method( 'fetchMessage' )
146  ->will( $this->returnValue( 'First' ) );
147 
148  $blob = $blobStore->getBlob( $module, 'en' );
149  $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
150 
151  $blobStore->updateMessage( 'example' );
152 
153  $module = $this->makeModule( [ 'example' ] );
154  $rl = new ResourceLoader();
155  $rl->register( $module->getName(), $module );
156  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
157  $blobStore->expects( $this->once() )
158  ->method( 'fetchMessage' )
159  ->will( $this->returnValue( 'Second' ) );
160 
161  $blob = $blobStore->getBlob( $module, 'en' );
162  $this->assertEquals( '{"example":"Second"}', $blob, 'Updated blob' );
163  }
164 
165  public function testValidation() {
166  $module = $this->makeModule( [ 'foo' ] );
167  $rl = new ResourceLoader();
168  $rl->register( $module->getName(), $module );
169 
170  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
171  $blobStore->expects( $this->once() )
172  ->method( 'fetchMessage' )
173  ->will( $this->returnValueMap( [
174  [ 'foo', 'en', 'Hello' ],
175  ] ) );
176 
177  $blob = $blobStore->getBlob( $module, 'en' );
178  $this->assertEquals( '{"foo":"Hello"}', $blob, 'Generated blob' );
179 
180  // Now, imagine a change to the module is deployed. The module now contains
181  // message 'foo' and 'bar'. While updateMessage() was not called (since no
182  // message values were changed) it should detect the change in list of
183  // message keys.
184  $module = $this->makeModule( [ 'foo', 'bar' ] );
185  $rl = new ResourceLoader();
186  $rl->register( $module->getName(), $module );
187 
188  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
189  $blobStore->expects( $this->exactly( 2 ) )
190  ->method( 'fetchMessage' )
191  ->will( $this->returnValueMap( [
192  [ 'foo', 'en', 'Hello' ],
193  [ 'bar', 'en', 'World' ],
194  ] ) );
195 
196  $blob = $blobStore->getBlob( $module, 'en' );
197  $this->assertEquals( '{"foo":"Hello","bar":"World"}', $blob, 'Updated blob' );
198  }
199 
200  public function testClear() {
201  $module = $this->makeModule( [ 'example' ] );
202  $rl = new ResourceLoader();
203  $rl->register( $module->getName(), $module );
204  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
205  $blobStore->expects( $this->exactly( 2 ) )
206  ->method( 'fetchMessage' )
207  ->will( $this->onConsecutiveCalls( 'First', 'Second' ) );
208 
209  $now = microtime( true );
210  $this->wanCache->setMockTime( $now );
211 
212  $blob = $blobStore->getBlob( $module, 'en' );
213  $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
214 
215  $blob = $blobStore->getBlob( $module, 'en' );
216  $this->assertEquals( '{"example":"First"}', $blob, 'Cache-hit' );
217 
218  $now += 1;
219  $blobStore->clear();
220 
221  $blob = $blobStore->getBlob( $module, 'en' );
222  $this->assertEquals( '{"example":"Second"}', $blob, 'Updated blob' );
223  }
224 }
MessageBlobStoreTest\setUp
setUp()
Definition: MessageBlobStoreTest.php:13
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
array
the array() calling protocol came about after MediaWiki 1.4rc1.
MessageBlobStoreTest\testGetBlobCached
testGetBlobCached()
Seems to fail sometimes (T176097).
Definition: MessageBlobStoreTest.php:115
EventRelayerNull
No-op class for publishing messages into a PubSub system.
Definition: EventRelayerNull.php:24
MessageBlobStoreTest\testClear
testClear()
Definition: MessageBlobStoreTest.php:200
MessageBlobStoreTest\makeBlobStore
makeBlobStore( $methods=null, $rl=null)
Definition: MessageBlobStoreTest.php:38
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
MessageBlobStoreTest\testFetchMessage
testFetchMessage()
MessageBlobStore::fetchMessage.
Definition: MessageBlobStoreTest.php:72
$blob
$blob
Definition: testCompression.php:65
MessageBlobStoreTest\makeModule
makeModule(array $messages)
Definition: MessageBlobStoreTest.php:49
MessageBlobStoreTest\testValidation
testValidation()
Definition: MessageBlobStoreTest.php:165
ResourceLoaderTestModule
Definition: ResourceLoaderTestCase.php:84
MessageBlobStoreTest\testGetBlob
testGetBlob()
Definition: MessageBlobStoreTest.php:95
MessageBlobStoreTest\testUpdateMessage
testUpdateMessage()
Definition: MessageBlobStoreTest.php:139
ResourceLoader
Dynamic JavaScript and CSS resource loading system.
Definition: ResourceLoader.php:38
MessageBlobStoreTest\testSetLogger
testSetLogger()
MessageBlobStore::setLogger.
Definition: MessageBlobStoreTest.php:56
WANObjectCache\PURGE_VAL_PREFIX
const PURGE_VAL_PREFIX
Definition: WANObjectCache.php:193
MessageBlobStoreTest\testGetResourceLoader
testGetResourceLoader()
MessageBlobStore::getResourceLoader.
Definition: MessageBlobStoreTest.php:62
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56
MessageBlobStoreTest
Cache MessageBlobStore.
Definition: MessageBlobStoreTest.php:9
MessageBlobStoreTest\testFetchMessageFail
testFetchMessageFail()
MessageBlobStore::fetchMessage.
Definition: MessageBlobStoreTest.php:84
$messages
$messages
Definition: LogTests.i18n.php:8