3 namespace MediaWiki\Tests\Storage
;
5 use InvalidArgumentException
;
6 use MediaWiki\MediaWikiServices
;
7 use MediaWiki\Storage\BlobAccessException
;
8 use MediaWiki\Storage\SqlBlobStore
;
14 * @covers \MediaWiki\Storage\SqlBlobStore
17 class SqlBlobStoreTest
extends MediaWikiTestCase
{
20 * @return SqlBlobStore
22 public function getBlobStore( $legacyEncoding = false, $compressRevisions = false ) {
23 $services = MediaWikiServices
::getInstance();
25 $store = new SqlBlobStore(
26 $services->getDBLoadBalancer(),
27 $services->getExternalStoreAccess(),
28 $services->getMainWANObjectCache()
31 if ( $compressRevisions ) {
32 $store->setCompressBlobs( $compressRevisions );
34 if ( $legacyEncoding ) {
35 $store->setLegacyEncoding( $legacyEncoding );
42 * @covers \MediaWiki\Storage\SqlBlobStore::getCompressBlobs()
43 * @covers \MediaWiki\Storage\SqlBlobStore::setCompressBlobs()
45 public function testGetSetCompressRevisions() {
46 $store = $this->getBlobStore();
47 $this->assertFalse( $store->getCompressBlobs() );
48 $store->setCompressBlobs( true );
49 $this->assertTrue( $store->getCompressBlobs() );
53 * @covers \MediaWiki\Storage\SqlBlobStore::getLegacyEncoding()
54 * @covers \MediaWiki\Storage\SqlBlobStore::getLegacyEncodingConversionLang()
55 * @covers \MediaWiki\Storage\SqlBlobStore::setLegacyEncoding()
57 public function testGetSetLegacyEncoding() {
58 $store = $this->getBlobStore();
59 $this->assertFalse( $store->getLegacyEncoding() );
60 $store->setLegacyEncoding( 'foo' );
61 $this->assertSame( 'foo', $store->getLegacyEncoding() );
63 $this->hideDeprecated( SqlBlobStore
::class . '::getLegacyEncodingConversionLang' );
64 $this->assertNull( $store->getLegacyEncodingConversionLang() );
68 * @covers \MediaWiki\Storage\SqlBlobStore::getCacheExpiry()
69 * @covers \MediaWiki\Storage\SqlBlobStore::setCacheExpiry()
71 public function testGetSetCacheExpiry() {
72 $store = $this->getBlobStore();
73 $this->assertSame( 604800, $store->getCacheExpiry() );
74 $store->setCacheExpiry( 12 );
75 $this->assertSame( 12, $store->getCacheExpiry() );
79 * @covers \MediaWiki\Storage\SqlBlobStore::getUseExternalStore()
80 * @covers \MediaWiki\Storage\SqlBlobStore::setUseExternalStore()
82 public function testGetSetUseExternalStore() {
83 $store = $this->getBlobStore();
84 $this->assertFalse( $store->getUseExternalStore() );
85 $store->setUseExternalStore( true );
86 $this->assertTrue( $store->getUseExternalStore() );
89 public function provideDecompress() {
90 yield
'(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
91 yield
'(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
92 yield
'(no legacy encoding), error flag -> false' => [ false, 'X', [ 'error' ], false ];
93 yield
'(no legacy encoding), string in with gzip flag returns string' => [
94 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
95 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
97 yield
'(no legacy encoding), string in with object flag returns false' => [
98 // gzip string below generated with serialize( 'JOJO' )
99 false, "s:4:\"JOJO\";", [ 'object' ], false,
101 yield
'(no legacy encoding), serialized object in with object flag returns string' => [
103 // Using a TitleValue object as it has a getText method (which is needed)
104 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
108 yield
'(no legacy encoding), serialized object in with object & gzip flag returns string' => [
110 // Using a TitleValue object as it has a getText method (which is needed)
111 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
112 [ 'object', 'gzip' ],
115 yield
'(ISO-8859-1 encoding), string in string out' => [
117 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
121 yield
'(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
123 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
127 yield
'(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
129 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
133 yield
'(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
135 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
136 [ 'gzip', 'object' ],
139 yield
'T184749 (windows-1252 encoding), string in string out' => [
141 iconv( 'utf-8', 'windows-1252', "sammansättningar" ),
145 yield
'T184749 (windows-1252 encoding), string in string out with gzip' => [
147 gzdeflate( iconv( 'utf-8', 'windows-1252', "sammansättningar" ) ),
154 * @dataProvider provideDecompress
155 * @covers \MediaWiki\Storage\SqlBlobStore::decompressData
157 * @param string|bool $legacyEncoding
159 * @param array $flags
160 * @param mixed $expected
162 public function testDecompressData( $legacyEncoding, $data, $flags, $expected ) {
163 $store = $this->getBlobStore( $legacyEncoding );
166 $store->decompressData( $data, $flags )
171 * @covers \MediaWiki\Storage\SqlBlobStore::decompressData
173 public function testDecompressData_InvalidArgumentException() {
174 $store = $this->getBlobStore();
176 $this->setExpectedException( InvalidArgumentException
::class );
177 $store->decompressData( false, [] );
181 * @covers \MediaWiki\Storage\SqlBlobStore::compressData
183 public function testCompressRevisionTextUtf8() {
184 $store = $this->getBlobStore();
186 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
187 $row->old_flags
= $store->compressData( $row->old_text
);
188 $this->assertTrue( strpos( $row->old_flags
, 'utf-8' ) !== false,
189 "Flags should contain 'utf-8'" );
190 $this->assertFalse( strpos( $row->old_flags
, 'gzip' ) !== false,
191 "Flags should not contain 'gzip'" );
192 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
193 $row->old_text
, "Direct check" );
197 * @covers \MediaWiki\Storage\SqlBlobStore::compressData
199 public function testCompressRevisionTextUtf8Gzip() {
200 $store = $this->getBlobStore( false, true );
201 $this->checkPHPExtension( 'zlib' );
204 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
205 $row->old_flags
= $store->compressData( $row->old_text
);
206 $this->assertTrue( strpos( $row->old_flags
, 'utf-8' ) !== false,
207 "Flags should contain 'utf-8'" );
208 $this->assertTrue( strpos( $row->old_flags
, 'gzip' ) !== false,
209 "Flags should contain 'gzip'" );
210 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
211 gzinflate( $row->old_text
), "Direct check" );
214 public function provideBlobs() {
216 yield
[ 'someText' ];
217 yield
[ "sammansättningar" ];
221 * @param string $blob
222 * @dataProvider provideBlobs
223 * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
224 * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
226 public function testSimpleStoreGetBlobSimpleRoundtrip( $blob ) {
227 $store = $this->getBlobStore();
228 $address = $store->storeBlob( $blob );
229 $this->assertSame( $blob, $store->getBlob( $address ) );
233 * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
234 * @covers \MediaWiki\Storage\SqlBlobStore::getBlobBatch
236 public function testSimpleStorageGetBlobBatchSimpleEmpty() {
237 $store = $this->getBlobStore();
238 $this->assertArrayEquals(
240 $store->getBlobBatch( [] )->getValue()
245 * @param string $blob
246 * @dataProvider provideBlobs
247 * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
248 * @covers \MediaWiki\Storage\SqlBlobStore::getBlobBatch
250 public function testSimpleStorageGetBlobBatchSimpleRoundtrip( $blob ) {
251 $store = $this->getBlobStore();
253 $store->storeBlob( $blob ),
254 $store->storeBlob( $blob . '1' )
256 $this->assertArrayEquals(
257 array_combine( $addresses, [ $blob, $blob . '1' ] ),
258 $store->getBlobBatch( $addresses )->getValue()
263 * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
265 public function testSimpleStorageNonExistentBlob() {
266 $this->setExpectedException( BlobAccessException
::class );
267 $store = $this->getBlobStore();
268 $store->getBlob( 'tt:this_will_not_exist' );
272 * @covers \MediaWiki\Storage\SqlBlobStore::getBlobBatch
274 public function testSimpleStorageNonExistentBlobBatch() {
275 $store = $this->getBlobStore();
276 $result = $store->getBlobBatch( [ 'tt:this_will_not_exist', 'tt:1000', 'bla:1001' ] );
279 'tt:this_will_not_exist' => null,
288 'message' => 'internalerror',
290 'Bad blob address: tt:this_will_not_exist'
295 'message' => 'internalerror',
297 'Unknown blob address schema: bla'
302 'message' => 'internalerror',
304 'Unable to fetch blob at tt:1000'
307 ], $result->getErrors() );
311 * @covers \MediaWiki\Storage\SqlBlobStore::getBlobBatch
313 public function testSimpleStoragePartialNonExistentBlobBatch() {
314 $store = $this->getBlobStore();
315 $address = $store->storeBlob( 'test_data' );
316 $result = $store->getBlobBatch( [ $address, 'tt:this_will_not_exist_too' ] );
319 $address => 'test_data',
320 'tt:this_will_not_exist_too' => null
327 'message' => 'internalerror',
329 'Bad blob address: tt:this_will_not_exist_too'
332 ], $result->getErrors() );
336 * @dataProvider provideBlobs
337 * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
338 * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
340 public function testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncoding( $blob ) {
341 $store = $this->getBlobStore( 'windows-1252' );
342 $address = $store->storeBlob( $blob );
343 $this->assertSame( $blob, $store->getBlob( $address ) );
347 * @dataProvider provideBlobs
348 * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
349 * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
351 public function testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncodingGzip( $blob ) {
352 // FIXME: fails under postgres
353 $this->markTestSkippedIfDbType( 'postgres' );
354 $store = $this->getBlobStore( 'windows-1252', true );
355 $address = $store->storeBlob( $blob );
356 $this->assertSame( $blob, $store->getBlob( $address ) );
359 public function provideGetTextIdFromAddress() {
360 yield
[ 'tt:17', 17 ];
361 yield
[ 'xy:17', null ];
362 yield
[ 'xy:xyzzy', null ];
366 * @dataProvider provideGetTextIdFromAddress
368 public function testGetTextIdFromAddress( $address, $textId ) {
369 $store = $this->getBlobStore();
370 $this->assertSame( $textId, $store->getTextIdFromAddress( $address ) );
373 public function provideGetTextIdFromAddressInvalidArgumentException() {
383 * @dataProvider provideGetTextIdFromAddressInvalidArgumentException
385 public function testGetTextIdFromAddressInvalidArgumentException( $address ) {
386 $this->setExpectedException( InvalidArgumentException
::class );
387 $store = $this->getBlobStore();
388 $store->getTextIdFromAddress( $address );
391 public function testMakeAddressFromTextId() {
392 $this->assertSame( 'tt:17', SqlBlobStore
::makeAddressFromTextId( 17 ) );