Debian Codegraph @

tests/phpunit/includes/ResourceLoader/MessageBlobStoreTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\ResourceLoader;
4
5 use MediaWiki\ResourceLoader\MessageBlobStore;
6 use MediaWiki\ResourceLoader\ResourceLoader;
7 use MediaWikiCoversValidator;
8 use PHPUnit\Framework\TestCase;
9 use Psr\Log\NullLogger;
10 use Wikimedia\ObjectCache\HashBagOStuff;
11 use Wikimedia\ObjectCache\WANObjectCache;
12
13 /**
14 * @group ResourceLoader
15 * @covers \MediaWiki\ResourceLoader\MessageBlobStore
16 */
17 class MessageBlobStoreTest extends TestCase {
18
19 use MediaWikiCoversValidator;
20
21 private const NAME = 'test.blobstore';
22
23 /** @var WANObjectCache */
24 private $wanCache;
25
26 /** @var float */
27 private $clock;
28
29 protected function setUp(): void {
30 parent::setUp();
31 $this->wanCache = new WANObjectCache( [
32 'cache' => new HashBagOStuff()
33 ] );
34
35 $this->clock = 1301655600.000;
36 $this->wanCache->setMockTime( $this->clock );
37 }
38
39 public function testBlobCreation() {
40 $rl = new EmptyResourceLoader();
41 $rl->register( self::NAME, [
42 'factory' => function () {
43 return $this->makeModule( [ 'mainpage' ] );
44 }
45 ] );
46
47 $blobStore = $this->makeBlobStore( null, $rl );
48 $blob = $blobStore->getBlob( $rl->getModule( self::NAME ), 'en' );
49
50 $this->assertEquals( '{"mainpage":"Main Page"}', $blob, 'Generated blob' );
51 }
52
53 public function testBlobCreation_empty() {
54 $module = $this->makeModule( [] );
55 $rl = new EmptyResourceLoader();
56
57 $blobStore = $this->makeBlobStore( null, $rl );
58 $blob = $blobStore->getBlob( $module, 'en' );
59
60 $this->assertEquals( '{}', $blob, 'Generated blob' );
61 }
62
63 public function testBlobCreation_unknownMessage() {
64 $module = $this->makeModule( [ 'i-dont-exist', 'mainpage', 'i-dont-exist2' ] );
65 $rl = new EmptyResourceLoader();
66 $blobStore = $this->makeBlobStore( null, $rl );
67
68 // Generating a blob should continue without errors,
69 // with keys of unknown messages excluded from the blob.
70 $blob = $blobStore->getBlob( $module, 'en' );
71 $this->assertEquals( '{"mainpage":"Main Page"}', $blob, 'Generated blob' );
72 }
73
74 public function testMessageCachingAndPurging() {
75 $rl = new EmptyResourceLoader();
76 // Register it so that MessageBlobStore::updateMessage can
77 // discover it from the registry as a module that uses this message.
78 $rl->register( self::NAME, [
79 'factory' => function () {
80 return $this->makeModule( [ 'example' ] );
81 }
82 ] );
83 $module = $rl->getModule( self::NAME );
84 $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
85
86 // Advance this new WANObjectCache instance to a normal state,
87 // by doing one "get" and letting its hold off period expire.
88 // Without this, the first real "get" would lazy-initialise the
89 // checkKey and thus reject the first "set".
90 $blobStore->getBlob( $module, 'en' );
91 $this->clock += 20;
92
93 // Arrange version 1 of a message
94 $blobStore->expects( $this->once() )
95 ->method( 'fetchMessage' )
96 ->willReturn( 'First version' );
97
98 // Assert
99 $blob = $blobStore->getBlob( $module, 'en' );
100 $this->assertEquals( '{"example":"First version"}', $blob, 'Blob for v1' );
101
102 // Arrange version 2
103 $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
104 $blobStore->expects( $this->once() )
105 ->method( 'fetchMessage' )
106 ->willReturn( 'Second version' );
107 $this->clock += 20;
108
109 // Assert
110 // We do not validate whether a cached message is up-to-date.
111 // Instead, changes to messages will send us a purge.
112 // When cache is not purged or expired, it must be used.
113 $blob = $blobStore->getBlob( $module, 'en' );
114 $this->assertEquals( '{"example":"First version"}', $blob, 'Reuse cached v1 blob' );
115
116 // Purge cache
117 $blobStore->updateMessage( 'example' );
118 $this->clock += 20;
119
120 // Assert
121 $blob = $blobStore->getBlob( $module, 'en' );
122 $this->assertEquals( '{"example":"Second version"}', $blob, 'Updated blob for v2' );
123 }
124
125 public function testPurgeEverything() {
126 $module = $this->makeModule( [ 'example' ] );
127 $rl = new EmptyResourceLoader();
128 $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
129 // Advance this new WANObjectCache instance to a normal state.
130 $blobStore->getBlob( $module, 'en' );
131 $this->clock += 20;
132
133 // Arrange version 1 and 2
134 $blobStore->expects( $this->exactly( 2 ) )
135 ->method( 'fetchMessage' )
136 ->willReturnOnConsecutiveCalls( 'First', 'Second' );
137
138 // Assert
139 $blob = $blobStore->getBlob( $module, 'en' );
140 $this->assertEquals( '{"example":"First"}', $blob, 'Blob for v1' );
141
142 $this->clock += 20;
143
144 // Assert
145 $blob = $blobStore->getBlob( $module, 'en' );
146 $this->assertEquals( '{"example":"First"}', $blob, 'Blob for v1 again' );
147
148 // Purge everything
149 $blobStore->clear();
150 $this->clock += 20;
151
152 // Assert
153 $blob = $blobStore->getBlob( $module, 'en' );
154 $this->assertEquals( '{"example":"Second"}', $blob, 'Blob for v2' );
155 }
156
157 public function testValidateAgainstModuleRegistry() {
158 // Arrange version 1 of a module
159 $module = $this->makeModule( [ 'foo' ] );
160 $rl = new EmptyResourceLoader();
161 $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
162 $blobStore->expects( $this->once() )
163 ->method( 'fetchMessage' )
164 ->willReturnMap( [
165 // message key, language code, message value
166 [ 'foo', 'en', 'Hello' ],
167 ] );
168
169 // Assert
170 $blob = $blobStore->getBlob( $module, 'en' );
171 $this->assertEquals( '{"foo":"Hello"}', $blob, 'Blob for v1' );
172
173 // Arrange version 2 of module
174 // While message values may be out of date, the set of messages returned
175 // must always match the set of message keys required by the module.
176 // We do not receive purges for this because no messages were changed.
177 $module = $this->makeModule( [ 'foo', 'bar' ] );
178 $rl = new EmptyResourceLoader();
179 $blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl );
180 $blobStore->expects( $this->exactly( 2 ) )
181 ->method( 'fetchMessage' )
182 ->willReturnMap( [
183 // message key, language code, message value
184 [ 'foo', 'en', 'Hello' ],
185 [ 'bar', 'en', 'World' ],
186 ] );
187
188 // Assert
189 $blob = $blobStore->getBlob( $module, 'en' );
190 $this->assertEquals( '{"foo":"Hello","bar":"World"}', $blob, 'Blob for v2' );
191 }
192
193 public function testSetLoggedIsVoid() {
194 $blobStore = $this->makeBlobStore();
195 $this->assertNull( $blobStore->setLogger( new NullLogger() ) );
196 }
197
198 private function makeBlobStore( $methods = null, $rl = null ) {
199 $blobStore = $this->getMockBuilder( MessageBlobStore::class )
200 ->setConstructorArgs( [
201 $rl ?? $this->createMock( ResourceLoader::class ),
202 null,
203 $this->wanCache
204 ] )
205 ->onlyMethods( $methods ?: [] )
206 ->getMock();
207
208 return $blobStore;
209 }
210
211 private function makeModule( array $messages ) {
212 $module = new ResourceLoaderTestModule( [ 'messages' => $messages ] );
213 $module->setName( self::NAME );
214 return $module;
215 }
216 }