MediaWiki  REL1_31
SqlBlobStoreTest.php
Go to the documentation of this file.
1 <?php
2 
4 
9 use stdClass;
11 
17 
21  public function getBlobStore( $legacyEncoding = false, $compressRevisions = false ) {
23 
24  $store = new SqlBlobStore(
25  $services->getDBLoadBalancer(),
26  $services->getMainWANObjectCache()
27  );
28 
29  if ( $compressRevisions ) {
30  $store->setCompressBlobs( $compressRevisions );
31  }
32  if ( $legacyEncoding ) {
33  $store->setLegacyEncoding( $legacyEncoding, Language::factory( 'en' ) );
34  }
35 
36  return $store;
37  }
38 
43  public function testGetSetCompressRevisions() {
44  $store = $this->getBlobStore();
45  $this->assertFalse( $store->getCompressBlobs() );
46  $store->setCompressBlobs( true );
47  $this->assertTrue( $store->getCompressBlobs() );
48  }
49 
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() );
63  }
64 
69  public function testGetSetCacheExpiry() {
70  $store = $this->getBlobStore();
71  $this->assertSame( 604800, $store->getCacheExpiry() );
72  $store->setCacheExpiry( 12 );
73  $this->assertSame( 12, $store->getCacheExpiry() );
74  }
75 
80  public function testGetSetUseExternalStore() {
81  $store = $this->getBlobStore();
82  $this->assertFalse( $store->getUseExternalStore() );
83  $store->setUseExternalStore( true );
84  $this->assertTrue( $store->getUseExternalStore() );
85  }
86 
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',
94  ];
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,
98  ];
99  yield '(no legacy encoding), serialized object in with object flag returns string' => [
100  false,
101  // Using a TitleValue object as it has a getText method (which is needed)
102  serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
103  [ 'object' ],
104  'HHJJDDFF',
105  ];
106  yield '(no legacy encoding), serialized object in with object & gzip flag returns string' => [
107  false,
108  // Using a TitleValue object as it has a getText method (which is needed)
109  gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
110  [ 'object', 'gzip' ],
111  '8219JJJ840',
112  ];
113  yield '(ISO-8859-1 encoding), string in string out' => [
114  'ISO-8859-1',
115  iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
116  [],
117  '1®Àþ1',
118  ];
119  yield '(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
120  'ISO-8859-1',
121  gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
122  [ 'gzip' ],
123  '4®Àþ4',
124  ];
125  yield '(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
126  'ISO-8859-1',
127  serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
128  [ 'object' ],
129  '3®Àþ3',
130  ];
131  yield '(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
132  'ISO-8859-1',
133  gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
134  [ 'gzip', 'object' ],
135  '2®Àþ2',
136  ];
137  yield 'T184749 (windows-1252 encoding), string in string out' => [
138  'windows-1252',
139  iconv( 'utf-8', 'windows-1252', "sammansättningar" ),
140  [],
141  'sammansättningar',
142  ];
143  yield 'T184749 (windows-1252 encoding), string in string out with gzip' => [
144  'windows-1252',
145  gzdeflate( iconv( 'utf-8', 'windows-1252', "sammansättningar" ) ),
146  [ 'gzip' ],
147  'sammansättningar',
148  ];
149  }
150 
160  public function testDecompressData( $legacyEncoding, $data, $flags, $expected ) {
161  $store = $this->getBlobStore( $legacyEncoding );
162  $this->assertSame(
163  $expected,
164  $store->decompressData( $data, $flags )
165  );
166  }
167 
171  public function testCompressRevisionTextUtf8() {
172  $store = $this->getBlobStore();
173  $row = new stdClass;
174  $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
175  $row->old_flags = $store->compressData( $row->old_text );
176  $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
177  "Flags should contain 'utf-8'" );
178  $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
179  "Flags should not contain 'gzip'" );
180  $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
181  $row->old_text, "Direct check" );
182  }
183 
188  $store = $this->getBlobStore( false, true );
189  $this->checkPHPExtension( 'zlib' );
190 
191  $row = new stdClass;
192  $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
193  $row->old_flags = $store->compressData( $row->old_text );
194  $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
195  "Flags should contain 'utf-8'" );
196  $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
197  "Flags should contain 'gzip'" );
198  $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
199  gzinflate( $row->old_text ), "Direct check" );
200  }
201 
202  public function provideBlobs() {
203  yield [ '' ];
204  yield [ 'someText' ];
205  yield [ "sammansättningar" ];
206  }
207 
214  $store = $this->getBlobStore();
215  $address = $store->storeBlob( $blob );
216  $this->assertSame( $blob, $store->getBlob( $address ) );
217  }
218 
225  $store = $this->getBlobStore( 'windows-1252' );
226  $address = $store->storeBlob( $blob );
227  $this->assertSame( $blob, $store->getBlob( $address ) );
228  }
229 
236  $store = $this->getBlobStore( 'windows-1252', true );
237  $address = $store->storeBlob( $blob );
238  $this->assertSame( $blob, $store->getBlob( $address ) );
239  }
240 
241 }
MediaWiki\Tests\Storage\SqlBlobStoreTest\testGetSetCompressRevisions
testGetSetCompressRevisions()
\MediaWiki\Storage\SqlBlobStore::getCompressBlobs() \MediaWiki\Storage\SqlBlobStore::setCompressBlobs...
Definition: SqlBlobStoreTest.php:43
MediaWiki\Tests\Storage\SqlBlobStoreTest\provideDecompress
provideDecompress()
Definition: SqlBlobStoreTest.php:87
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
MediaWiki\Tests\Storage\SqlBlobStoreTest\testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncoding
testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncoding( $blob)
provideBlobs \MediaWiki\Storage\SqlBlobStore::storeBlob \MediaWiki\Storage\SqlBlobStore::getBlob
Definition: SqlBlobStoreTest.php:224
MediaWiki\Tests\Storage\SqlBlobStoreTest
\MediaWiki\Storage\SqlBlobStore Database
Definition: SqlBlobStoreTest.php:16
MediaWiki\Storage\SqlBlobStore
Service for storing and loading Content objects.
Definition: SqlBlobStore.php:50
serialize
serialize()
Definition: ApiMessage.php:184
MediaWiki\Tests\Storage\SqlBlobStoreTest\testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncodingGzip
testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncodingGzip( $blob)
provideBlobs \MediaWiki\Storage\SqlBlobStore::storeBlob \MediaWiki\Storage\SqlBlobStore::getBlob
Definition: SqlBlobStoreTest.php:235
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:37
MediaWiki\Tests\Storage
Definition: BlobStoreFactoryTest.php:3
MediaWiki\Tests\Storage\SqlBlobStoreTest\testCompressRevisionTextUtf8Gzip
testCompressRevisionTextUtf8Gzip()
\MediaWiki\Storage\SqlBlobStore::compressData
Definition: SqlBlobStoreTest.php:187
$blob
$blob
Definition: testCompression.php:65
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
MediaWiki\Tests\Storage\SqlBlobStoreTest\testGetSetUseExternalStore
testGetSetUseExternalStore()
\MediaWiki\Storage\SqlBlobStore::getUseExternalStore() \MediaWiki\Storage\SqlBlobStore::setUseExterna...
Definition: SqlBlobStoreTest.php:80
$services
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title 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:2273
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:109
MediaWiki\Tests\Storage\SqlBlobStoreTest\testSimpleStoreGetBlobSimpleRoundtrip
testSimpleStoreGetBlobSimpleRoundtrip( $blob)
provideBlobs \MediaWiki\Storage\SqlBlobStore::storeBlob \MediaWiki\Storage\SqlBlobStore::getBlob
Definition: SqlBlobStoreTest.php:213
MediaWiki\Tests\Storage\SqlBlobStoreTest\testDecompressData
testDecompressData( $legacyEncoding, $data, $flags, $expected)
provideDecompress \MediaWiki\Storage\SqlBlobStore::decompressData
Definition: SqlBlobStoreTest.php:160
MediaWiki\Tests\Storage\SqlBlobStoreTest\provideBlobs
provideBlobs()
Definition: SqlBlobStoreTest.php:202
MediaWikiTestCase\checkPHPExtension
checkPHPExtension( $extName)
Check if $extName is a loaded PHP extension, will skip the test whenever it is not loaded.
Definition: MediaWikiTestCase.php:1989
MediaWiki\Tests\Storage\SqlBlobStoreTest\testGetSetLegacyEncoding
testGetSetLegacyEncoding()
\MediaWiki\Storage\SqlBlobStore::getLegacyEncoding() \MediaWiki\Storage\SqlBlobStore::getLegacyEncodi...
Definition: SqlBlobStoreTest.php:55
MediaWiki\Tests\Storage\SqlBlobStoreTest\testGetSetCacheExpiry
testGetSetCacheExpiry()
\MediaWiki\Storage\SqlBlobStore::getCacheExpiry() \MediaWiki\Storage\SqlBlobStore::setCacheExpiry()
Definition: SqlBlobStoreTest.php:69
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:183
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:25
MediaWiki\Tests\Storage\SqlBlobStoreTest\getBlobStore
getBlobStore( $legacyEncoding=false, $compressRevisions=false)
Definition: SqlBlobStoreTest.php:21
MediaWiki\Tests\Storage\SqlBlobStoreTest\testCompressRevisionTextUtf8
testCompressRevisionTextUtf8()
\MediaWiki\Storage\SqlBlobStore::compressData
Definition: SqlBlobStoreTest.php:171
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
Language
Internationalisation code.
Definition: Language.php:35
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:35