1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
<?php
namespace MediaWiki\Tests\Storage;
use InvalidArgumentException;
use Language;
use MediaWiki\MediaWikiServices;
use MediaWiki\Storage\SqlBlobStore;
use MediaWikiTestCase;
use stdClass;
use TitleValue;
/**
* @covers \MediaWiki\Storage\SqlBlobStore
* @group Database
*/
class SqlBlobStoreTest extends MediaWikiTestCase {
/**
* @return SqlBlobStore
*/
public function getBlobStore( $legacyEncoding = false, $compressRevisions = false ) {
$services = MediaWikiServices::getInstance();
$store = new SqlBlobStore(
$services->getDBLoadBalancer(),
$services->getMainWANObjectCache()
);
if ( $compressRevisions ) {
$store->setCompressBlobs( $compressRevisions );
}
if ( $legacyEncoding ) {
$store->setLegacyEncoding( $legacyEncoding, Language::factory( 'en' ) );
}
return $store;
}
/**
* @covers \MediaWiki\Storage\SqlBlobStore::getCompressBlobs()
* @covers \MediaWiki\Storage\SqlBlobStore::setCompressBlobs()
*/
public function testGetSetCompressRevisions() {
$store = $this->getBlobStore();
$this->assertFalse( $store->getCompressBlobs() );
$store->setCompressBlobs( true );
$this->assertTrue( $store->getCompressBlobs() );
}
/**
* @covers \MediaWiki\Storage\SqlBlobStore::getLegacyEncoding()
* @covers \MediaWiki\Storage\SqlBlobStore::getLegacyEncodingConversionLang()
* @covers \MediaWiki\Storage\SqlBlobStore::setLegacyEncoding()
*/
public function testGetSetLegacyEncoding() {
$store = $this->getBlobStore();
$this->assertFalse( $store->getLegacyEncoding() );
$this->assertNull( $store->getLegacyEncodingConversionLang() );
$en = Language::factory( 'en' );
$store->setLegacyEncoding( 'foo', $en );
$this->assertSame( 'foo', $store->getLegacyEncoding() );
$this->assertSame( $en, $store->getLegacyEncodingConversionLang() );
}
/**
* @covers \MediaWiki\Storage\SqlBlobStore::getCacheExpiry()
* @covers \MediaWiki\Storage\SqlBlobStore::setCacheExpiry()
*/
public function testGetSetCacheExpiry() {
$store = $this->getBlobStore();
$this->assertSame( 604800, $store->getCacheExpiry() );
$store->setCacheExpiry( 12 );
$this->assertSame( 12, $store->getCacheExpiry() );
}
/**
* @covers \MediaWiki\Storage\SqlBlobStore::getUseExternalStore()
* @covers \MediaWiki\Storage\SqlBlobStore::setUseExternalStore()
*/
public function testGetSetUseExternalStore() {
$store = $this->getBlobStore();
$this->assertFalse( $store->getUseExternalStore() );
$store->setUseExternalStore( true );
$this->assertTrue( $store->getUseExternalStore() );
}
public function provideDecompress() {
yield '(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
yield '(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
yield '(no legacy encoding), error flag -> false' => [ false, 'X', [ 'error' ], false ];
yield '(no legacy encoding), string in with gzip flag returns string' => [
// gzip string below generated with gzdeflate( 'AAAABBAAA' )
false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
];
yield '(no legacy encoding), string in with object flag returns false' => [
// gzip string below generated with serialize( 'JOJO' )
false, "s:4:\"JOJO\";", [ 'object' ], false,
];
yield '(no legacy encoding), serialized object in with object flag returns string' => [
false,
// Using a TitleValue object as it has a getText method (which is needed)
serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
[ 'object' ],
'HHJJDDFF',
];
yield '(no legacy encoding), serialized object in with object & gzip flag returns string' => [
false,
// Using a TitleValue object as it has a getText method (which is needed)
gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
[ 'object', 'gzip' ],
'8219JJJ840',
];
yield '(ISO-8859-1 encoding), string in string out' => [
'ISO-8859-1',
iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
[],
'1®Àþ1',
];
yield '(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
'ISO-8859-1',
gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
[ 'gzip' ],
'4®Àþ4',
];
yield '(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
'ISO-8859-1',
serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
[ 'object' ],
'3®Àþ3',
];
yield '(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
'ISO-8859-1',
gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
[ 'gzip', 'object' ],
'2®Àþ2',
];
yield 'T184749 (windows-1252 encoding), string in string out' => [
'windows-1252',
iconv( 'utf-8', 'windows-1252', "sammansättningar" ),
[],
'sammansättningar',
];
yield 'T184749 (windows-1252 encoding), string in string out with gzip' => [
'windows-1252',
gzdeflate( iconv( 'utf-8', 'windows-1252', "sammansättningar" ) ),
[ 'gzip' ],
'sammansättningar',
];
}
/**
* @dataProvider provideDecompress
* @covers \MediaWiki\Storage\SqlBlobStore::decompressData
*
* @param string|bool $legacyEncoding
* @param mixed $data
* @param array $flags
* @param mixed $expected
*/
public function testDecompressData( $legacyEncoding, $data, $flags, $expected ) {
$store = $this->getBlobStore( $legacyEncoding );
$this->assertSame(
$expected,
$store->decompressData( $data, $flags )
);
}
/**
* @covers \MediaWiki\Storage\SqlBlobStore::decompressData
*/
public function testDecompressData_InvalidArgumentException() {
$store = $this->getBlobStore();
$this->setExpectedException( InvalidArgumentException::class );
$store->decompressData( false, [] );
}
/**
* @covers \MediaWiki\Storage\SqlBlobStore::compressData
*/
public function testCompressRevisionTextUtf8() {
$store = $this->getBlobStore();
$row = new stdClass;
$row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
$row->old_flags = $store->compressData( $row->old_text );
$this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
"Flags should contain 'utf-8'" );
$this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
"Flags should not contain 'gzip'" );
$this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
$row->old_text, "Direct check" );
}
/**
* @covers \MediaWiki\Storage\SqlBlobStore::compressData
*/
public function testCompressRevisionTextUtf8Gzip() {
$store = $this->getBlobStore( false, true );
$this->checkPHPExtension( 'zlib' );
$row = new stdClass;
$row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
$row->old_flags = $store->compressData( $row->old_text );
$this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
"Flags should contain 'utf-8'" );
$this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
"Flags should contain 'gzip'" );
$this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
gzinflate( $row->old_text ), "Direct check" );
}
public function provideBlobs() {
yield [ '' ];
yield [ 'someText' ];
yield [ "sammansättningar" ];
}
/**
* @dataProvider provideBlobs
* @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
* @covers \MediaWiki\Storage\SqlBlobStore::getBlob
*/
public function testSimpleStoreGetBlobSimpleRoundtrip( $blob ) {
$store = $this->getBlobStore();
$address = $store->storeBlob( $blob );
$this->assertSame( $blob, $store->getBlob( $address ) );
}
/**
* @dataProvider provideBlobs
* @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
* @covers \MediaWiki\Storage\SqlBlobStore::getBlob
*/
public function testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncoding( $blob ) {
$store = $this->getBlobStore( 'windows-1252' );
$address = $store->storeBlob( $blob );
$this->assertSame( $blob, $store->getBlob( $address ) );
}
/**
* @dataProvider provideBlobs
* @covers \MediaWiki\Storage\SqlBlobStore::storeBlob
* @covers \MediaWiki\Storage\SqlBlobStore::getBlob
*/
public function testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncodingGzip( $blob ) {
// FIXME: fails under postgres
$this->markTestSkippedIfDbType( 'postgres' );
$store = $this->getBlobStore( 'windows-1252', true );
$address = $store->storeBlob( $blob );
$this->assertSame( $blob, $store->getBlob( $address ) );
}
public function provideGetTextIdFromAddress() {
yield [ 'tt:17', 17 ];
yield [ 'xy:17', null ];
yield [ 'xy:xyzzy', null ];
}
/**
* @dataProvider provideGetTextIdFromAddress
*/
public function testGetTextIdFromAddress( $address, $textId ) {
$store = $this->getBlobStore();
$this->assertSame( $textId, $store->getTextIdFromAddress( $address ) );
}
public function provideGetTextIdFromAddressInvalidArgumentException() {
yield [ 'tt:-17' ];
yield [ 'tt:xy' ];
yield [ 'tt:0' ];
yield [ 'tt:' ];
yield [ 'xy' ];
yield [ '' ];
}
/**
* @dataProvider provideGetTextIdFromAddressInvalidArgumentException
*/
public function testGetTextIdFromAddressInvalidArgumentException( $address ) {
$this->setExpectedException( InvalidArgumentException::class );
$store = $this->getBlobStore();
$store->getTextIdFromAddress( $address );
}
public function testMakeAddressFromTextId() {
$this->assertSame( 'tt:17', SqlBlobStore::makeAddressFromTextId( 17 ) );
}
}
|