3 namespace MediaWiki\Tests\Storage
;
5 use InvalidArgumentException
;
7 use MediaWiki\MediaWikiServices
;
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, Language
::factory( 'en' ) );
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 $this->assertNull( $store->getLegacyEncodingConversionLang() );
61 $en = Language
::factory( 'en' );
62 $store->setLegacyEncoding( 'foo', $en );
63 $this->assertSame( 'foo', $store->getLegacyEncoding() );
64 $this->assertSame( $en, $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 * @dataProvider provideBlobs
222 * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
223 * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
225 public function testSimpleStoreGetBlobSimpleRoundtrip( $blob ) {
226 $store = $this->getBlobStore();
227 $address = $store->storeBlob( $blob );
228 $this->assertSame( $blob, $store->getBlob( $address ) );
232 * @dataProvider provideBlobs
233 * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
234 * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
236 public function testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncoding( $blob ) {
237 $store = $this->getBlobStore( 'windows-1252' );
238 $address = $store->storeBlob( $blob );
239 $this->assertSame( $blob, $store->getBlob( $address ) );
243 * @dataProvider provideBlobs
244 * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
245 * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
247 public function testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncodingGzip( $blob ) {
248 // FIXME: fails under postgres
249 $this->markTestSkippedIfDbType( 'postgres' );
250 $store = $this->getBlobStore( 'windows-1252', true );
251 $address = $store->storeBlob( $blob );
252 $this->assertSame( $blob, $store->getBlob( $address ) );
255 public function provideGetTextIdFromAddress() {
256 yield
[ 'tt:17', 17 ];
257 yield
[ 'xy:17', null ];
258 yield
[ 'xy:xyzzy', null ];
262 * @dataProvider provideGetTextIdFromAddress
264 public function testGetTextIdFromAddress( $address, $textId ) {
265 $store = $this->getBlobStore();
266 $this->assertSame( $textId, $store->getTextIdFromAddress( $address ) );
269 public function provideGetTextIdFromAddressInvalidArgumentException() {
279 * @dataProvider provideGetTextIdFromAddressInvalidArgumentException
281 public function testGetTextIdFromAddressInvalidArgumentException( $address ) {
282 $this->setExpectedException( InvalidArgumentException
::class );
283 $store = $this->getBlobStore();
284 $store->getTextIdFromAddress( $address );
287 public function testMakeAddressFromTextId() {
288 $this->assertSame( 'tt:17', SqlBlobStore
::makeAddressFromTextId( 17 ) );