3 namespace MediaWiki\Tests\Storage
;
6 use MediaWiki\MediaWikiServices
;
7 use MediaWiki\Storage\SqlBlobStore
;
13 * @covers \MediaWiki\Storage\SqlBlobStore
16 class SqlBlobStoreTest
extends MediaWikiTestCase
{
19 * @return SqlBlobStore
21 public function getBlobStore( $legacyEncoding = false, $compressRevisions = false ) {
22 $services = MediaWikiServices
::getInstance();
24 $store = new SqlBlobStore(
25 $services->getDBLoadBalancer(),
26 $services->getMainWANObjectCache()
29 if ( $compressRevisions ) {
30 $store->setCompressBlobs( $compressRevisions );
32 if ( $legacyEncoding ) {
33 $store->setLegacyEncoding( $legacyEncoding, Language
::factory( 'en' ) );
40 * @covers \MediaWiki\Storage\SqlBlobStore::getCompressBlobs()
41 * @covers \MediaWiki\Storage\SqlBlobStore::setCompressBlobs()
43 public function testGetSetCompressRevisions() {
44 $store = $this->getBlobStore();
45 $this->assertFalse( $store->getCompressBlobs() );
46 $store->setCompressBlobs( true );
47 $this->assertTrue( $store->getCompressBlobs() );
51 * @covers \MediaWiki\Storage\SqlBlobStore::getLegacyEncoding()
52 * @covers \MediaWiki\Storage\SqlBlobStore::getLegacyEncodingConversionLang()
53 * @covers \MediaWiki\Storage\SqlBlobStore::setLegacyEncoding()
55 public function testGetSetLegacyEncoding() {
56 $store = $this->getBlobStore();
57 $this->assertFalse( $store->getLegacyEncoding() );
58 $this->assertNull( $store->getLegacyEncodingConversionLang() );
59 $en = Language
::factory( 'en' );
60 $store->setLegacyEncoding( 'foo', $en );
61 $this->assertSame( 'foo', $store->getLegacyEncoding() );
62 $this->assertSame( $en, $store->getLegacyEncodingConversionLang() );
66 * @covers \MediaWiki\Storage\SqlBlobStore::getCacheExpiry()
67 * @covers \MediaWiki\Storage\SqlBlobStore::setCacheExpiry()
69 public function testGetSetCacheExpiry() {
70 $store = $this->getBlobStore();
71 $this->assertSame( 604800, $store->getCacheExpiry() );
72 $store->setCacheExpiry( 12 );
73 $this->assertSame( 12, $store->getCacheExpiry() );
77 * @covers \MediaWiki\Storage\SqlBlobStore::getUseExternalStore()
78 * @covers \MediaWiki\Storage\SqlBlobStore::setUseExternalStore()
80 public function testGetSetUseExternalStore() {
81 $store = $this->getBlobStore();
82 $this->assertFalse( $store->getUseExternalStore() );
83 $store->setUseExternalStore( true );
84 $this->assertTrue( $store->getUseExternalStore() );
87 public function provideDecompress() {
88 yield
'(no legacy encoding), false in false out' => [ false, false, [], false ];
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), string in with gzip flag returns string' => [
92 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
93 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
95 yield
'(no legacy encoding), string in with object flag returns false' => [
96 // gzip string below generated with serialize( 'JOJO' )
97 false, "s:4:\"JOJO\";", [ 'object' ], false,
99 yield
'(no legacy encoding), serialized object in with object flag returns string' => [
101 // Using a TitleValue object as it has a getText method (which is needed)
102 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
106 yield
'(no legacy encoding), serialized object in with object & gzip flag returns string' => [
108 // Using a TitleValue object as it has a getText method (which is needed)
109 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
110 [ 'object', 'gzip' ],
113 yield
'(ISO-8859-1 encoding), string in string out' => [
115 iconv( 'utf8', 'ISO-8859-1', "1®Àþ1" ),
119 yield
'(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
121 gzdeflate( iconv( 'utf8', 'ISO-8859-1', "4®Àþ4" ) ),
125 yield
'(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
127 serialize( new TitleValue( 0, iconv( 'utf8', 'ISO-8859-1', "3®Àþ3" ) ) ),
131 yield
'(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
133 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
134 [ 'gzip', 'object' ],
140 * @dataProvider provideDecompress
141 * @covers \MediaWiki\Storage\SqlBlobStore::decompressData
143 * @param string|bool $legacyEncoding
145 * @param array $flags
146 * @param mixed $expected
148 public function testDecompressData( $legacyEncoding, $data, $flags, $expected ) {
149 $store = $this->getBlobStore( $legacyEncoding );
152 $store->decompressData( $data, $flags )
157 * @covers \MediaWiki\Storage\SqlBlobStore::compressData
159 public function testCompressRevisionTextUtf8() {
160 $store = $this->getBlobStore();
162 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
163 $row->old_flags
= $store->compressData( $row->old_text
);
164 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
165 "Flags should contain 'utf-8'" );
166 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
167 "Flags should not contain 'gzip'" );
168 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
169 $row->old_text
, "Direct check" );
173 * @covers \MediaWiki\Storage\SqlBlobStore::compressData
175 public function testCompressRevisionTextUtf8Gzip() {
176 $store = $this->getBlobStore( false, true );
177 $this->checkPHPExtension( 'zlib' );
180 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
181 $row->old_flags
= $store->compressData( $row->old_text
);
182 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
183 "Flags should contain 'utf-8'" );
184 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
185 "Flags should contain 'gzip'" );
186 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
187 gzinflate( $row->old_text
), "Direct check" );
190 public function provideBlobs() {
192 yield
[ 'someText' ];
196 * @dataProvider provideBlobs
197 * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
198 * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
200 public function testSimpleStoreGetBlobSimpleRoundtrip( $blob ) {
201 $store = $this->getBlobStore();
202 $address = $store->storeBlob( $blob );
203 $this->assertSame( $blob, $store->getBlob( $address ) );