MediaWiki  1.33.0
SqlBlobStoreTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use InvalidArgumentException;
10 use stdClass;
12 
18 
22  public function getBlobStore( $legacyEncoding = false, $compressRevisions = false ) {
24 
25  $store = new SqlBlobStore(
26  $services->getDBLoadBalancer(),
27  $services->getMainWANObjectCache()
28  );
29 
30  if ( $compressRevisions ) {
31  $store->setCompressBlobs( $compressRevisions );
32  }
33  if ( $legacyEncoding ) {
34  $store->setLegacyEncoding( $legacyEncoding, Language::factory( 'en' ) );
35  }
36 
37  return $store;
38  }
39 
44  public function testGetSetCompressRevisions() {
45  $store = $this->getBlobStore();
46  $this->assertFalse( $store->getCompressBlobs() );
47  $store->setCompressBlobs( true );
48  $this->assertTrue( $store->getCompressBlobs() );
49  }
50 
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() );
64  }
65 
70  public function testGetSetCacheExpiry() {
71  $store = $this->getBlobStore();
72  $this->assertSame( 604800, $store->getCacheExpiry() );
73  $store->setCacheExpiry( 12 );
74  $this->assertSame( 12, $store->getCacheExpiry() );
75  }
76 
81  public function testGetSetUseExternalStore() {
82  $store = $this->getBlobStore();
83  $this->assertFalse( $store->getUseExternalStore() );
84  $store->setUseExternalStore( true );
85  $this->assertTrue( $store->getUseExternalStore() );
86  }
87 
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',
95  ];
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,
99  ];
100  yield '(no legacy encoding), serialized object in with object flag returns string' => [
101  false,
102  // Using a TitleValue object as it has a getText method (which is needed)
103  serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
104  [ 'object' ],
105  'HHJJDDFF',
106  ];
107  yield '(no legacy encoding), serialized object in with object & gzip flag returns string' => [
108  false,
109  // Using a TitleValue object as it has a getText method (which is needed)
110  gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
111  [ 'object', 'gzip' ],
112  '8219JJJ840',
113  ];
114  yield '(ISO-8859-1 encoding), string in string out' => [
115  'ISO-8859-1',
116  iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
117  [],
118  '1®Àþ1',
119  ];
120  yield '(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
121  'ISO-8859-1',
122  gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
123  [ 'gzip' ],
124  '4®Àþ4',
125  ];
126  yield '(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
127  'ISO-8859-1',
128  serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
129  [ 'object' ],
130  '3®Àþ3',
131  ];
132  yield '(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
133  'ISO-8859-1',
134  gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
135  [ 'gzip', 'object' ],
136  '2®Àþ2',
137  ];
138  yield 'T184749 (windows-1252 encoding), string in string out' => [
139  'windows-1252',
140  iconv( 'utf-8', 'windows-1252', "sammansättningar" ),
141  [],
142  'sammansättningar',
143  ];
144  yield 'T184749 (windows-1252 encoding), string in string out with gzip' => [
145  'windows-1252',
146  gzdeflate( iconv( 'utf-8', 'windows-1252', "sammansättningar" ) ),
147  [ 'gzip' ],
148  'sammansättningar',
149  ];
150  }
151 
161  public function testDecompressData( $legacyEncoding, $data, $flags, $expected ) {
162  $store = $this->getBlobStore( $legacyEncoding );
163  $this->assertSame(
164  $expected,
165  $store->decompressData( $data, $flags )
166  );
167  }
168 
173  $store = $this->getBlobStore();
174 
175  $this->setExpectedException( InvalidArgumentException::class );
176  $store->decompressData( false, [] );
177  }
178 
182  public function testCompressRevisionTextUtf8() {
183  $store = $this->getBlobStore();
184  $row = new stdClass;
185  $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
186  $row->old_flags = $store->compressData( $row->old_text );
187  $this->assertTrue( strpos( $row->old_flags, 'utf-8' ) !== false,
188  "Flags should contain 'utf-8'" );
189  $this->assertFalse( strpos( $row->old_flags, 'gzip' ) !== false,
190  "Flags should not contain 'gzip'" );
191  $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
192  $row->old_text, "Direct check" );
193  }
194 
199  $store = $this->getBlobStore( false, true );
200  $this->checkPHPExtension( 'zlib' );
201 
202  $row = new stdClass;
203  $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
204  $row->old_flags = $store->compressData( $row->old_text );
205  $this->assertTrue( strpos( $row->old_flags, 'utf-8' ) !== false,
206  "Flags should contain 'utf-8'" );
207  $this->assertTrue( strpos( $row->old_flags, 'gzip' ) !== false,
208  "Flags should contain 'gzip'" );
209  $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
210  gzinflate( $row->old_text ), "Direct check" );
211  }
212 
213  public function provideBlobs() {
214  yield [ '' ];
215  yield [ 'someText' ];
216  yield [ "sammansättningar" ];
217  }
218 
225  $store = $this->getBlobStore();
226  $address = $store->storeBlob( $blob );
227  $this->assertSame( $blob, $store->getBlob( $address ) );
228  }
229 
236  $store = $this->getBlobStore( 'windows-1252' );
237  $address = $store->storeBlob( $blob );
238  $this->assertSame( $blob, $store->getBlob( $address ) );
239  }
240 
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 ) );
252  }
253 
254  public function provideGetTextIdFromAddress() {
255  yield [ 'tt:17', 17 ];
256  yield [ 'xy:17', null ];
257  yield [ 'xy:xyzzy', null ];
258  }
259 
263  public function testGetTextIdFromAddress( $address, $textId ) {
264  $store = $this->getBlobStore();
265  $this->assertSame( $textId, $store->getTextIdFromAddress( $address ) );
266  }
267 
269  yield [ 'tt:-17' ];
270  yield [ 'tt:xy' ];
271  yield [ 'tt:0' ];
272  yield [ 'tt:' ];
273  yield [ 'xy' ];
274  yield [ '' ];
275  }
276 
281  $this->setExpectedException( InvalidArgumentException::class );
282  $store = $this->getBlobStore();
283  $store->getTextIdFromAddress( $address );
284  }
285 
286  public function testMakeAddressFromTextId() {
287  $this->assertSame( 'tt:17', SqlBlobStore::makeAddressFromTextId( 17 ) );
288  }
289 
290 }
MediaWiki\Tests\Storage\SqlBlobStoreTest\testGetTextIdFromAddress
testGetTextIdFromAddress( $address, $textId)
provideGetTextIdFromAddress
Definition: SqlBlobStoreTest.php:263
MediaWiki\Tests\Storage\SqlBlobStoreTest\testGetSetCompressRevisions
testGetSetCompressRevisions()
\MediaWiki\Storage\SqlBlobStore::getCompressBlobs() \MediaWiki\Storage\SqlBlobStore::setCompressBlobs...
Definition: SqlBlobStoreTest.php:44
MediaWiki\Tests\Storage\SqlBlobStoreTest\provideDecompress
provideDecompress()
Definition: SqlBlobStoreTest.php:88
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
MediaWiki\Tests\Storage\SqlBlobStoreTest\testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncoding
testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncoding( $blob)
provideBlobs \MediaWiki\Storage\SqlBlobStore::storeBlob \MediaWiki\Storage\SqlBlobStore::getBlob
Definition: SqlBlobStoreTest.php:235
MediaWiki\Tests\Storage\SqlBlobStoreTest\provideGetTextIdFromAddress
provideGetTextIdFromAddress()
Definition: SqlBlobStoreTest.php:254
MediaWiki\Tests\Storage\SqlBlobStoreTest\provideGetTextIdFromAddressInvalidArgumentException
provideGetTextIdFromAddressInvalidArgumentException()
Definition: SqlBlobStoreTest.php:268
MediaWiki\Tests\Storage\SqlBlobStoreTest
\MediaWiki\Storage\SqlBlobStore Database
Definition: SqlBlobStoreTest.php:17
MediaWiki\Storage\SqlBlobStore
Service for storing and loading Content objects.
Definition: SqlBlobStore.php:49
serialize
serialize()
Definition: ApiMessageTrait.php:134
MediaWiki\Tests\Storage\SqlBlobStoreTest\testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncodingGzip
testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncodingGzip( $blob)
provideBlobs \MediaWiki\Storage\SqlBlobStore::storeBlob \MediaWiki\Storage\SqlBlobStore::getBlob
Definition: SqlBlobStoreTest.php:246
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$data
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Definition: generatePhpCharToUpperMappings.php:13
MediaWiki\Tests\Storage
Definition: BlobStoreFactoryTest.php:3
MediaWiki\Tests\Storage\SqlBlobStoreTest\testCompressRevisionTextUtf8Gzip
testCompressRevisionTextUtf8Gzip()
\MediaWiki\Storage\SqlBlobStore::compressData
Definition: SqlBlobStoreTest.php:198
$blob
$blob
Definition: testCompression.php:65
MediaWiki\Tests\Storage\SqlBlobStoreTest\testGetTextIdFromAddressInvalidArgumentException
testGetTextIdFromAddressInvalidArgumentException( $address)
provideGetTextIdFromAddressInvalidArgumentException
Definition: SqlBlobStoreTest.php:280
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
MediaWiki\Storage\SqlBlobStore\makeAddressFromTextId
static makeAddressFromTextId( $id)
Returns an address referring to content stored in the text table row with the given ID.
Definition: SqlBlobStore.php:602
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MediaWiki\Tests\Storage\SqlBlobStoreTest\testGetSetUseExternalStore
testGetSetUseExternalStore()
\MediaWiki\Storage\SqlBlobStore::getUseExternalStore() \MediaWiki\Storage\SqlBlobStore::setUseExterna...
Definition: SqlBlobStoreTest.php:81
null
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:124
MediaWiki\Tests\Storage\SqlBlobStoreTest\testMakeAddressFromTextId
testMakeAddressFromTextId()
Definition: SqlBlobStoreTest.php:286
MediaWiki\Tests\Storage\SqlBlobStoreTest\testSimpleStoreGetBlobSimpleRoundtrip
testSimpleStoreGetBlobSimpleRoundtrip( $blob)
provideBlobs \MediaWiki\Storage\SqlBlobStore::storeBlob \MediaWiki\Storage\SqlBlobStore::getBlob
Definition: SqlBlobStoreTest.php:224
MediaWiki\Tests\Storage\SqlBlobStoreTest\testDecompressData
testDecompressData( $legacyEncoding, $data, $flags, $expected)
provideDecompress \MediaWiki\Storage\SqlBlobStore::decompressData
Definition: SqlBlobStoreTest.php:161
MediaWiki\Tests\Storage\SqlBlobStoreTest\provideBlobs
provideBlobs()
Definition: SqlBlobStoreTest.php:213
MediaWiki\Tests\Storage\SqlBlobStoreTest\testDecompressData_InvalidArgumentException
testDecompressData_InvalidArgumentException()
\MediaWiki\Storage\SqlBlobStore::decompressData
Definition: SqlBlobStoreTest.php:172
MediaWikiTestCase\checkPHPExtension
checkPHPExtension( $extName)
Check if $extName is a loaded PHP extension, will skip the test whenever it is not loaded.
Definition: MediaWikiTestCase.php:2288
MediaWiki\Tests\Storage\SqlBlobStoreTest\testGetSetLegacyEncoding
testGetSetLegacyEncoding()
\MediaWiki\Storage\SqlBlobStore::getLegacyEncoding() \MediaWiki\Storage\SqlBlobStore::getLegacyEncodi...
Definition: SqlBlobStoreTest.php:56
MediaWiki\Tests\Storage\SqlBlobStoreTest\testGetSetCacheExpiry
testGetSetCacheExpiry()
\MediaWiki\Storage\SqlBlobStore::getCacheExpiry() \MediaWiki\Storage\SqlBlobStore::setCacheExpiry()
Definition: SqlBlobStoreTest.php:70
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:215
MediaWiki\Storage\SqlBlobStore\setCompressBlobs
setCompressBlobs( $compressBlobs)
Definition: SqlBlobStore.php:140
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
$services
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition: hooks.txt:2220
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
MediaWiki\Tests\Storage\SqlBlobStoreTest\getBlobStore
getBlobStore( $legacyEncoding=false, $compressRevisions=false)
Definition: SqlBlobStoreTest.php:22
MediaWikiTestCase\markTestSkippedIfDbType
markTestSkippedIfDbType( $type)
Skip the test if using the specified database type.
Definition: MediaWikiTestCase.php:2303
MediaWiki\Tests\Storage\SqlBlobStoreTest\testCompressRevisionTextUtf8
testCompressRevisionTextUtf8()
\MediaWiki\Storage\SqlBlobStore::compressData
Definition: SqlBlobStoreTest.php:182
Language
Internationalisation code.
Definition: Language.php:36
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36