| 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 |
|
| 15 |
|
| 16 |
|
| 17 |
class MessageBlobStoreTest extends TestCase { |
| 18 |
|
| 19 |
use MediaWikiCoversValidator; |
| 20 |
|
| 21 |
private const NAME = 'test.blobstore'; |
| 22 |
|
| 23 |
|
| 24 |
private $wanCache; |
| 25 |
|
| 26 |
|
| 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 |
|
| 69 |
|
| 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 |
|
| 77 |
|
| 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 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
$blobStore->getBlob( $module, 'en' ); |
| 91 |
$this->clock += 20; |
| 92 |
|
| 93 |
|
| 94 |
$blobStore->expects( $this->once() ) |
| 95 |
->method( 'fetchMessage' ) |
| 96 |
->willReturn( 'First version' ); |
| 97 |
|
| 98 |
|
| 99 |
$blob = $blobStore->getBlob( $module, 'en' ); |
| 100 |
$this->assertEquals( '{"example":"First version"}', $blob, 'Blob for v1' ); |
| 101 |
|
| 102 |
|
| 103 |
$blobStore = $this->makeBlobStore( [ 'fetchMessage' ], $rl ); |
| 104 |
$blobStore->expects( $this->once() ) |
| 105 |
->method( 'fetchMessage' ) |
| 106 |
->willReturn( 'Second version' ); |
| 107 |
$this->clock += 20; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
$blob = $blobStore->getBlob( $module, 'en' ); |
| 114 |
$this->assertEquals( '{"example":"First version"}', $blob, 'Reuse cached v1 blob' ); |
| 115 |
|
| 116 |
|
| 117 |
$blobStore->updateMessage( 'example' ); |
| 118 |
$this->clock += 20; |
| 119 |
|
| 120 |
|
| 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 |
|
| 130 |
$blobStore->getBlob( $module, 'en' ); |
| 131 |
$this->clock += 20; |
| 132 |
|
| 133 |
|
| 134 |
$blobStore->expects( $this->exactly( 2 ) ) |
| 135 |
->method( 'fetchMessage' ) |
| 136 |
->willReturnOnConsecutiveCalls( 'First', 'Second' ); |
| 137 |
|
| 138 |
|
| 139 |
$blob = $blobStore->getBlob( $module, 'en' ); |
| 140 |
$this->assertEquals( '{"example":"First"}', $blob, 'Blob for v1' ); |
| 141 |
|
| 142 |
$this->clock += 20; |
| 143 |
|
| 144 |
|
| 145 |
$blob = $blobStore->getBlob( $module, 'en' ); |
| 146 |
$this->assertEquals( '{"example":"First"}', $blob, 'Blob for v1 again' ); |
| 147 |
|
| 148 |
|
| 149 |
$blobStore->clear(); |
| 150 |
$this->clock += 20; |
| 151 |
|
| 152 |
|
| 153 |
$blob = $blobStore->getBlob( $module, 'en' ); |
| 154 |
$this->assertEquals( '{"example":"Second"}', $blob, 'Blob for v2' ); |
| 155 |
} |
| 156 |
|
| 157 |
public function testValidateAgainstModuleRegistry() { |
| 158 |
|
| 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 |
|
| 166 |
[ 'foo', 'en', 'Hello' ], |
| 167 |
] ); |
| 168 |
|
| 169 |
|
| 170 |
$blob = $blobStore->getBlob( $module, 'en' ); |
| 171 |
$this->assertEquals( '{"foo":"Hello"}', $blob, 'Blob for v1' ); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 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 |
|
| 184 |
[ 'foo', 'en', 'Hello' ], |
| 185 |
[ 'bar', 'en', 'World' ], |
| 186 |
] ); |
| 187 |
|
| 188 |
|
| 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 |
} |