MediaWiki  1.30.1
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  protected function setUp() {
12  parent::setUp();
13  // MediaWiki tests defaults $wgMainWANCache to CACHE_NONE.
14  // Use hash instead so that caching is observed
15  $this->wanCache = $this->getMockBuilder( 'WANObjectCache' )
16  ->setConstructorArgs( [ [
17  'cache' => new HashBagOStuff(),
18  'pool' => 'test',
19  'relayer' => new EventRelayerNull( [] )
20  ] ] )
21  ->setMethods( [ 'makePurgeValue' ] )
22  ->getMock();
23 
24  $this->wanCache->expects( $this->any() )
25  ->method( 'makePurgeValue' )
26  ->will( $this->returnCallback( function ( $timestamp, $holdoff ) {
27  // Disable holdoff as it messes with testing
28  return WANObjectCache::PURGE_VAL_PREFIX . (float)$timestamp . ':0';
29  } ) );
30  }
31 
32  protected function makeBlobStore( $methods = null, $rl = null ) {
33  $blobStore = $this->getMockBuilder( 'MessageBlobStore' )
34  ->setConstructorArgs( [ $rl ] )
35  ->setMethods( $methods )
36  ->getMock();
37 
38  $access = TestingAccessWrapper::newFromObject( $blobStore );
39  $access->wanCache = $this->wanCache;
40  return $blobStore;
41  }
42 
43  protected function makeModule( array $messages ) {
44  $module = new ResourceLoaderTestModule( [ 'messages' => $messages ] );
45  $module->setName( 'test.blobstore' );
46  return $module;
47  }
48 
50  public function testSetLogger() {
51  $blobStore = $this->makeBlobStore();
52  $this->assertSame( null, $blobStore->setLogger( new Psr\Log\NullLogger() ) );
53  }
54 
56  public function testGetResourceLoader() {
57  // Call protected method
58  $blobStore = TestingAccessWrapper::newFromObject( $this->makeBlobStore() );
59  $this->assertInstanceOf(
61  $blobStore->getResourceLoader()
62  );
63  }
64 
66  public function testFetchMessage() {
67  $module = $this->makeModule( [ 'mainpage' ] );
68  $rl = new ResourceLoader();
69  $rl->register( $module->getName(), $module );
70 
71  $blobStore = $this->makeBlobStore( null, $rl );
72  $blob = $blobStore->getBlob( $module, 'en' );
73 
74  $this->assertEquals( '{"mainpage":"Main Page"}', $blob, 'Generated blob' );
75  }
76 
78  public function testFetchMessageFail() {
79  $module = $this->makeModule( [ 'i-dont-exist' ] );
80  $rl = new ResourceLoader();
81  $rl->register( $module->getName(), $module );
82 
83  $blobStore = $this->makeBlobStore( null, $rl );
84  $blob = $blobStore->getBlob( $module, 'en' );
85 
86  $this->assertEquals( '{"i-dont-exist":"\u29fci-dont-exist\u29fd"}', $blob, 'Generated blob' );
87  }
88 
89  public function testGetBlob() {
90  $module = $this->makeModule( [ 'foo' ] );
91  $rl = new ResourceLoader();
92  $rl->register( $module->getName(), $module );
93 
94  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
95  $blobStore->expects( $this->once() )
96  ->method( 'fetchMessage' )
97  ->will( $this->returnValue( 'Example' ) );
98 
99  $blob = $blobStore->getBlob( $module, 'en' );
100 
101  $this->assertEquals( '{"foo":"Example"}', $blob, 'Generated blob' );
102  }
103 
109  public function testGetBlobCached() {
110  $module = $this->makeModule( [ 'example' ] );
111  $rl = new ResourceLoader();
112  $rl->register( $module->getName(), $module );
113 
114  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
115  $blobStore->expects( $this->once() )
116  ->method( 'fetchMessage' )
117  ->will( $this->returnValue( 'First' ) );
118 
119  $module = $this->makeModule( [ 'example' ] );
120  $blob = $blobStore->getBlob( $module, 'en' );
121  $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
122 
123  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
124  $blobStore->expects( $this->never() )
125  ->method( 'fetchMessage' )
126  ->will( $this->returnValue( 'Second' ) );
127 
128  $module = $this->makeModule( [ 'example' ] );
129  $blob = $blobStore->getBlob( $module, 'en' );
130  $this->assertEquals( '{"example":"First"}', $blob, 'Cache hit' );
131  }
132 
133  public function testUpdateMessage() {
134  $module = $this->makeModule( [ 'example' ] );
135  $rl = new ResourceLoader();
136  $rl->register( $module->getName(), $module );
137  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
138  $blobStore->expects( $this->once() )
139  ->method( 'fetchMessage' )
140  ->will( $this->returnValue( 'First' ) );
141 
142  $blob = $blobStore->getBlob( $module, 'en' );
143  $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
144 
145  $blobStore->updateMessage( 'example' );
146 
147  $module = $this->makeModule( [ 'example' ] );
148  $rl = new ResourceLoader();
149  $rl->register( $module->getName(), $module );
150  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
151  $blobStore->expects( $this->once() )
152  ->method( 'fetchMessage' )
153  ->will( $this->returnValue( 'Second' ) );
154 
155  $blob = $blobStore->getBlob( $module, 'en' );
156  $this->assertEquals( '{"example":"Second"}', $blob, 'Updated blob' );
157  }
158 
159  public function testValidation() {
160  $module = $this->makeModule( [ 'foo' ] );
161  $rl = new ResourceLoader();
162  $rl->register( $module->getName(), $module );
163 
164  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
165  $blobStore->expects( $this->once() )
166  ->method( 'fetchMessage' )
167  ->will( $this->returnValueMap( [
168  [ 'foo', 'en', 'Hello' ],
169  ] ) );
170 
171  $blob = $blobStore->getBlob( $module, 'en' );
172  $this->assertEquals( '{"foo":"Hello"}', $blob, 'Generated blob' );
173 
174  // Now, imagine a change to the module is deployed. The module now contains
175  // message 'foo' and 'bar'. While updateMessage() was not called (since no
176  // message values were changed) it should detect the change in list of
177  // message keys.
178  $module = $this->makeModule( [ 'foo', 'bar' ] );
179  $rl = new ResourceLoader();
180  $rl->register( $module->getName(), $module );
181 
182  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
183  $blobStore->expects( $this->exactly( 2 ) )
184  ->method( 'fetchMessage' )
185  ->will( $this->returnValueMap( [
186  [ 'foo', 'en', 'Hello' ],
187  [ 'bar', 'en', 'World' ],
188  ] ) );
189 
190  $blob = $blobStore->getBlob( $module, 'en' );
191  $this->assertEquals( '{"foo":"Hello","bar":"World"}', $blob, 'Updated blob' );
192  }
193 
194  public function testClear() {
195  $module = $this->makeModule( [ 'example' ] );
196  $rl = new ResourceLoader();
197  $rl->register( $module->getName(), $module );
198  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
199  $blobStore->expects( $this->exactly( 2 ) )
200  ->method( 'fetchMessage' )
201  ->will( $this->onConsecutiveCalls( 'First', 'Second' ) );
202 
203  $blob = $blobStore->getBlob( $module, 'en' );
204  $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
205 
206  $blob = $blobStore->getBlob( $module, 'en' );
207  $this->assertEquals( '{"example":"First"}', $blob, 'Cache-hit' );
208 
209  $blobStore->clear();
210 
211  $blob = $blobStore->getBlob( $module, 'en' );
212  $this->assertEquals( '{"example":"Second"}', $blob, 'Updated blob' );
213  }
214 }
MessageBlobStoreTest\setUp
setUp()
Definition: MessageBlobStoreTest.php:11
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
MessageBlobStoreTest\testGetBlobCached
testGetBlobCached()
Seems to fail sometimes (T176097).
Definition: MessageBlobStoreTest.php:109
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
EventRelayerNull
No-op class for publishing messages into a PubSub system.
Definition: EventRelayerNull.php:24
MessageBlobStoreTest\testClear
testClear()
Definition: MessageBlobStoreTest.php:194
$messages
$messages
Definition: LogTests.i18n.php:8
MessageBlobStoreTest\makeBlobStore
makeBlobStore( $methods=null, $rl=null)
Definition: MessageBlobStoreTest.php:32
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:35
MessageBlobStoreTest\testFetchMessage
testFetchMessage()
MessageBlobStore::fetchMessage.
Definition: MessageBlobStoreTest.php:66
$blob
$blob
Definition: testCompression.php:63
MessageBlobStoreTest\makeModule
makeModule(array $messages)
Definition: MessageBlobStoreTest.php:43
MessageBlobStoreTest\testValidation
testValidation()
Definition: MessageBlobStoreTest.php:159
ResourceLoaderTestModule
Definition: ResourceLoaderTestCase.php:85
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition: COPYING.txt:326
MessageBlobStoreTest\testGetBlob
testGetBlob()
Definition: MessageBlobStoreTest.php:89
MessageBlobStoreTest\testUpdateMessage
testUpdateMessage()
Definition: MessageBlobStoreTest.php:133
MessageBlobStoreTest\testSetLogger
testSetLogger()
MessageBlobStore::setLogger.
Definition: MessageBlobStoreTest.php:50
WANObjectCache\PURGE_VAL_PREFIX
const PURGE_VAL_PREFIX
Definition: WANObjectCache.php:165
MessageBlobStoreTest\testGetResourceLoader
testGetResourceLoader()
MessageBlobStore::getResourceLoader.
Definition: MessageBlobStoreTest.php:56
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:52
MessageBlobStoreTest
Cache MessageBlobStore.
Definition: MessageBlobStoreTest.php:9
MessageBlobStoreTest\testFetchMessageFail
testFetchMessageFail()
MessageBlobStore::fetchMessage.
Definition: MessageBlobStoreTest.php:78
array
the array() calling protocol came about after MediaWiki 1.4rc1.