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->getMainWANObjectCache()
30 if ( $compressRevisions ) {
31 $store->setCompressBlobs( $compressRevisions );
33 if ( $legacyEncoding ) {
34 $store->setLegacyEncoding( $legacyEncoding, Language
::factory( 'en' ) );
41 * @covers \MediaWiki\Storage\SqlBlobStore::getCompressBlobs()
42 * @covers \MediaWiki\Storage\SqlBlobStore::setCompressBlobs()
44 public function testGetSetCompressRevisions() {
45 $store = $this->getBlobStore();
46 $this->assertFalse( $store->getCompressBlobs() );
47 $store->setCompressBlobs( true );
48 $this->assertTrue( $store->getCompressBlobs() );
52 * @covers \MediaWiki\Storage\SqlBlobStore::getLegacyEncoding()
53 * @covers \MediaWiki\Storage\SqlBlobStore::getLegacyEncodingConversionLang()
54 * @covers \MediaWiki\Storage\SqlBlobStore::setLegacyEncoding()
56 public function testGetSetLegacyEncoding() {
57 $store = $this->getBlobStore();
58 $this->assertFalse( $store->getLegacyEncoding() );
59 $this->assertNull( $store->getLegacyEncodingConversionLang() );
60 $en = Language
::factory( 'en' );
61 $store->setLegacyEncoding( 'foo', $en );
62 $this->assertSame( 'foo', $store->getLegacyEncoding() );
63 $this->assertSame( $en, $store->getLegacyEncodingConversionLang() );
67 * @covers \MediaWiki\Storage\SqlBlobStore::getCacheExpiry()
68 * @covers \MediaWiki\Storage\SqlBlobStore::setCacheExpiry()
70 public function testGetSetCacheExpiry() {
71 $store = $this->getBlobStore();
72 $this->assertSame( 604800, $store->getCacheExpiry() );
73 $store->setCacheExpiry( 12 );
74 $this->assertSame( 12, $store->getCacheExpiry() );
78 * @covers \MediaWiki\Storage\SqlBlobStore::getUseExternalStore()
79 * @covers \MediaWiki\Storage\SqlBlobStore::setUseExternalStore()
81 public function testGetSetUseExternalStore() {
82 $store = $this->getBlobStore();
83 $this->assertFalse( $store->getUseExternalStore() );
84 $store->setUseExternalStore( true );
85 $this->assertTrue( $store->getUseExternalStore() );
88 public function provideDecompress() {
89 yield
'(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
90 yield
'(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
91 yield
'(no legacy encoding), error flag -> false' => [ false, 'X', [ 'error' ], false ];
92 yield
'(no legacy encoding), string in with gzip flag returns string' => [
93 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
94 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
96 yield
'(no legacy encoding), string in with object flag returns false' => [
97 // gzip string below generated with serialize( 'JOJO' )
98 false, "s:4:\"JOJO\";", [ 'object' ], false,
100 yield
'(no legacy encoding), serialized object in with object flag returns string' => [
102 // Using a TitleValue object as it has a getText method (which is needed)
103 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
107 yield
'(no legacy encoding), serialized object in with object & gzip flag returns string' => [
109 // Using a TitleValue object as it has a getText method (which is needed)
110 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
111 [ 'object', 'gzip' ],
114 yield
'(ISO-8859-1 encoding), string in string out' => [
116 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
120 yield
'(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
122 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
126 yield
'(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
128 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
132 yield
'(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
134 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
135 [ 'gzip', 'object' ],
138 yield
'T184749 (windows-1252 encoding), string in string out' => [
140 iconv( 'utf-8', 'windows-1252', "sammansättningar" ),
144 yield
'T184749 (windows-1252 encoding), string in string out with gzip' => [
146 gzdeflate( iconv( 'utf-8', 'windows-1252', "sammansättningar" ) ),
153 * @dataProvider provideDecompress
154 * @covers \MediaWiki\Storage\SqlBlobStore::decompressData
156 * @param string|bool $legacyEncoding
158 * @param array $flags
159 * @param mixed $expected
161 public function testDecompressData( $legacyEncoding, $data, $flags, $expected ) {
162 $store = $this->getBlobStore( $legacyEncoding );
165 $store->decompressData( $data, $flags )
170 * @covers \MediaWiki\Storage\SqlBlobStore::decompressData
172 public function testDecompressData_InvalidArgumentException() {
173 $store = $this->getBlobStore();
175 $this->setExpectedException( InvalidArgumentException
::class );
176 $store->decompressData( false, [] );
180 * @covers \MediaWiki\Storage\SqlBlobStore::compressData
182 public function testCompressRevisionTextUtf8() {
183 $store = $this->getBlobStore();
185 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
186 $row->old_flags
= $store->compressData( $row->old_text
);
187 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
188 "Flags should contain 'utf-8'" );
189 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
190 "Flags should not contain 'gzip'" );
191 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
192 $row->old_text
, "Direct check" );
196 * @covers \MediaWiki\Storage\SqlBlobStore::compressData
198 public function testCompressRevisionTextUtf8Gzip() {
199 $store = $this->getBlobStore( false, true );
200 $this->checkPHPExtension( 'zlib' );
203 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
204 $row->old_flags
= $store->compressData( $row->old_text
);
205 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
206 "Flags should contain 'utf-8'" );
207 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
208 "Flags should contain 'gzip'" );
209 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
210 gzinflate( $row->old_text
), "Direct check" );
213 public function provideBlobs() {
215 yield
[ 'someText' ];
216 yield
[ "sammansättningar" ];
220 * @dataProvider provideBlobs
221 * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
222 * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
224 public function testSimpleStoreGetBlobSimpleRoundtrip( $blob ) {
225 $store = $this->getBlobStore();
226 $address = $store->storeBlob( $blob );
227 $this->assertSame( $blob, $store->getBlob( $address ) );
231 * @dataProvider provideBlobs
232 * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
233 * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
235 public function testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncoding( $blob ) {
236 $store = $this->getBlobStore( 'windows-1252' );
237 $address = $store->storeBlob( $blob );
238 $this->assertSame( $blob, $store->getBlob( $address ) );
242 * @dataProvider provideBlobs
243 * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
244 * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
246 public function testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncodingGzip( $blob ) {
247 // FIXME: fails under postgres
248 $this->markTestSkippedIfDbType( 'postgres' );
249 $store = $this->getBlobStore( 'windows-1252', true );
250 $address = $store->storeBlob( $blob );
251 $this->assertSame( $blob, $store->getBlob( $address ) );
254 public function provideGetTextIdFromAddress() {
255 yield
[ 'tt:17', 17 ];
256 yield
[ 'xy:17', null ];
257 yield
[ 'xy:xyzzy', null ];
261 * @dataProvider provideGetTextIdFromAddress
263 public function testGetTextIdFromAddress( $address, $textId ) {
264 $store = $this->getBlobStore();
265 $this->assertSame( $textId, $store->getTextIdFromAddress( $address ) );
268 public function provideGetTextIdFromAddressInvalidArgumentException() {
278 * @dataProvider provideGetTextIdFromAddressInvalidArgumentException
280 public function testGetTextIdFromAddressInvalidArgumentException( $address ) {
281 $this->setExpectedException( InvalidArgumentException
::class );
282 $store = $this->getBlobStore();
283 $store->getTextIdFromAddress( $address );
286 public function testMakeAddressFromTextId() {
287 $this->assertSame( 'tt:17', SqlBlobStore
::makeAddressFromTextId( 17 ) );