Fix 'Tags' padding to keep it farther from the edge and document the source of the...
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / SqlBlobStoreTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use InvalidArgumentException;
6 use Language;
7 use MediaWiki\MediaWikiServices;
8 use MediaWiki\Storage\SqlBlobStore;
9 use MediaWikiTestCase;
10 use stdClass;
11 use TitleValue;
12
13 /**
14 * @covers \MediaWiki\Storage\SqlBlobStore
15 * @group Database
16 */
17 class SqlBlobStoreTest extends MediaWikiTestCase {
18
19 /**
20 * @return SqlBlobStore
21 */
22 public function getBlobStore( $legacyEncoding = false, $compressRevisions = false ) {
23 $services = MediaWikiServices::getInstance();
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
40 /**
41 * @covers \MediaWiki\Storage\SqlBlobStore::getCompressBlobs()
42 * @covers \MediaWiki\Storage\SqlBlobStore::setCompressBlobs()
43 */
44 public function testGetSetCompressRevisions() {
45 $store = $this->getBlobStore();
46 $this->assertFalse( $store->getCompressBlobs() );
47 $store->setCompressBlobs( true );
48 $this->assertTrue( $store->getCompressBlobs() );
49 }
50
51 /**
52 * @covers \MediaWiki\Storage\SqlBlobStore::getLegacyEncoding()
53 * @covers \MediaWiki\Storage\SqlBlobStore::getLegacyEncodingConversionLang()
54 * @covers \MediaWiki\Storage\SqlBlobStore::setLegacyEncoding()
55 */
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
66 /**
67 * @covers \MediaWiki\Storage\SqlBlobStore::getCacheExpiry()
68 * @covers \MediaWiki\Storage\SqlBlobStore::setCacheExpiry()
69 */
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
77 /**
78 * @covers \MediaWiki\Storage\SqlBlobStore::getUseExternalStore()
79 * @covers \MediaWiki\Storage\SqlBlobStore::setUseExternalStore()
80 */
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), false in false out' => [ false, false, [], false ];
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), 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
152 /**
153 * @dataProvider provideDecompress
154 * @covers \MediaWiki\Storage\SqlBlobStore::decompressData
155 *
156 * @param string|bool $legacyEncoding
157 * @param mixed $data
158 * @param array $flags
159 * @param mixed $expected
160 */
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
169 /**
170 * @covers \MediaWiki\Storage\SqlBlobStore::compressData
171 */
172 public function testCompressRevisionTextUtf8() {
173 $store = $this->getBlobStore();
174 $row = new stdClass;
175 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
176 $row->old_flags = $store->compressData( $row->old_text );
177 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
178 "Flags should contain 'utf-8'" );
179 $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
180 "Flags should not contain 'gzip'" );
181 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
182 $row->old_text, "Direct check" );
183 }
184
185 /**
186 * @covers \MediaWiki\Storage\SqlBlobStore::compressData
187 */
188 public function testCompressRevisionTextUtf8Gzip() {
189 $store = $this->getBlobStore( false, true );
190 $this->checkPHPExtension( 'zlib' );
191
192 $row = new stdClass;
193 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
194 $row->old_flags = $store->compressData( $row->old_text );
195 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
196 "Flags should contain 'utf-8'" );
197 $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
198 "Flags should contain 'gzip'" );
199 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
200 gzinflate( $row->old_text ), "Direct check" );
201 }
202
203 public function provideBlobs() {
204 yield [ '' ];
205 yield [ 'someText' ];
206 yield [ "sammansättningar" ];
207 }
208
209 /**
210 * @dataProvider provideBlobs
211 * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
212 * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
213 */
214 public function testSimpleStoreGetBlobSimpleRoundtrip( $blob ) {
215 $store = $this->getBlobStore();
216 $address = $store->storeBlob( $blob );
217 $this->assertSame( $blob, $store->getBlob( $address ) );
218 }
219
220 /**
221 * @dataProvider provideBlobs
222 * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
223 * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
224 */
225 public function testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncoding( $blob ) {
226 $store = $this->getBlobStore( 'windows-1252' );
227 $address = $store->storeBlob( $blob );
228 $this->assertSame( $blob, $store->getBlob( $address ) );
229 }
230
231 /**
232 * @dataProvider provideBlobs
233 * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
234 * @covers \MediaWiki\Storage\SqlBlobStore::getBlob
235 */
236 public function testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncodingGzip( $blob ) {
237 $store = $this->getBlobStore( 'windows-1252', true );
238 $address = $store->storeBlob( $blob );
239 $this->assertSame( $blob, $store->getBlob( $address ) );
240 }
241
242 public function provideGetTextIdFromAddress() {
243 yield [ 'tt:17', 17 ];
244 yield [ 'xy:17', null ];
245 yield [ 'xy:xyzzy', null ];
246 }
247
248 /**
249 * @dataProvider provideGetTextIdFromAddress
250 */
251 public function testGetTextIdFromAddress( $address, $textId ) {
252 $store = $this->getBlobStore();
253 $this->assertSame( $textId, $store->getTextIdFromAddress( $address ) );
254 }
255
256 public function provideGetTextIdFromAddressInvalidArgumentException() {
257 yield [ 'tt:-17' ];
258 yield [ 'tt:xy' ];
259 yield [ 'tt:0' ];
260 yield [ 'tt:' ];
261 yield [ 'xy' ];
262 yield [ '' ];
263 }
264
265 /**
266 * @dataProvider provideGetTextIdFromAddressInvalidArgumentException
267 */
268 public function testGetTextIdFromAddressInvalidArgumentException( $address ) {
269 $this->setExpectedException( InvalidArgumentException::class );
270 $store = $this->getBlobStore();
271 $store->getTextIdFromAddress( $address );
272 }
273
274 public function testMakeAddressFromTextId() {
275 $this->assertSame( 'tt:17', SqlBlobStore::makeAddressFromTextId( 17 ) );
276 }
277
278 }