MediaWiki  1.28.0
MessageBlobStoreTest.php
Go to the documentation of this file.
1 <?php
2 
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( [ [
15  'cache' => new HashBagOStuff(),
16  'pool' => 'test',
17  'relayer' => new EventRelayerNull( [] )
18  ] ] )
19  ->setMethods( [ '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( [ $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( [ 'messages' => $messages ] );
43  $module->setName( 'test.blobstore' );
44  return $module;
45  }
46 
47  public function testGetBlob() {
48  $module = $this->makeModule( [ 'foo' ] );
49  $rl = new ResourceLoader();
50  $rl->register( $module->getName(), $module );
51 
52  $blobStore = $this->makeBlobStore( [ '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( [ 'example' ] );
64  $rl = new ResourceLoader();
65  $rl->register( $module->getName(), $module );
66 
67  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
68  $blobStore->expects( $this->once() )
69  ->method( 'fetchMessage' )
70  ->will( $this->returnValue( 'First' ) );
71 
72  $module = $this->makeModule( [ 'example' ] );
73  $blob = $blobStore->getBlob( $module, 'en' );
74  $this->assertEquals( '{"example":"First"}', $blob, 'Generated blob' );
75 
76  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
77  $blobStore->expects( $this->never() )
78  ->method( 'fetchMessage' )
79  ->will( $this->returnValue( 'Second' ) );
80 
81  $module = $this->makeModule( [ '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( [ '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' ) );
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( [ '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' ) );
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( [ 'foo' ] );
114  $rl = new ResourceLoader();
115  $rl->register( $module->getName(), $module );
116 
117  $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
118  $blobStore->expects( $this->once() )
119  ->method( 'fetchMessage' )
120  ->will( $this->returnValueMap( [
121  [ '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( [ 'foo', 'bar' ] );
132  $rl = new ResourceLoader();
133  $rl->register( $module->getName(), $module );
134 
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  ] ) );
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( [ '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' ) );
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 }
the array() calling protocol came about after MediaWiki 1.4rc1.
makeModule(array $messages)
No-op class for publishing messages into a PubSub system.
if($limit) $timestamp
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
makeBlobStore($methods=null, $rl=null)
$messages
static newFromObject($object)
Return the same object, without access restrictions.
Cache MessageBlobStore.
Dynamic JavaScript and CSS resource loading system.