Tests for Revision::decompressRevisionText
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
6 * Test cases in RevisionTest should not interact with the Database.
7 * For test cases that need Database interaction see RevisionDbTestBase.
8 */
9 class RevisionTest extends MediaWikiTestCase {
10
11 public function provideConstructFromArray() {
12 yield 'with text' => [
13 [
14 'text' => 'hello world.',
15 'content_model' => CONTENT_MODEL_JAVASCRIPT
16 ],
17 ];
18 yield 'with content' => [
19 [
20 'content' => new JavaScriptContent( 'hellow world.' )
21 ],
22 ];
23 }
24
25 /**
26 * @dataProvider provideConstructFromArray
27 * @covers Revision::__construct
28 * @covers Revision::constructFromRowArray
29 */
30 public function testConstructFromArray( array $rowArray ) {
31 $rev = new Revision( $rowArray );
32 $this->assertNotNull( $rev->getContent(), 'no content object available' );
33 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
34 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
35 }
36
37 public function provideConstructFromArrayThrowsExceptions() {
38 yield 'content and text_id both not empty' => [
39 [
40 'content' => new WikitextContent( 'GOAT' ),
41 'text_id' => 'someid',
42 ],
43 new MWException( "Text already stored in external store (id someid), " .
44 "can't serialize content object" )
45 ];
46 yield 'with bad content object (class)' => [
47 [ 'content' => new stdClass() ],
48 new MWException( '`content` field must contain a Content object.' )
49 ];
50 yield 'with bad content object (string)' => [
51 [ 'content' => 'ImAGoat' ],
52 new MWException( '`content` field must contain a Content object.' )
53 ];
54 yield 'bad row format' => [
55 'imastring, not a row',
56 new MWException( 'Revision constructor passed invalid row format.' )
57 ];
58 }
59
60 /**
61 * @dataProvider provideConstructFromArrayThrowsExceptions
62 * @covers Revision::__construct
63 * @covers Revision::constructFromRowArray
64 */
65 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception $expectedException ) {
66 $this->setExpectedException(
67 get_class( $expectedException ),
68 $expectedException->getMessage(),
69 $expectedException->getCode()
70 );
71 new Revision( $rowArray );
72 }
73
74 public function provideConstructFromRow() {
75 yield 'Full construction' => [
76 [
77 'rev_id' => '2',
78 'rev_page' => '1',
79 'rev_text_id' => '2',
80 'rev_timestamp' => '20171017114835',
81 'rev_user_text' => '127.0.0.1',
82 'rev_user' => '0',
83 'rev_minor_edit' => '0',
84 'rev_deleted' => '0',
85 'rev_len' => '46',
86 'rev_parent_id' => '1',
87 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
88 'rev_comment_text' => 'Goat Comment!',
89 'rev_comment_data' => null,
90 'rev_comment_cid' => null,
91 'rev_content_format' => 'GOATFORMAT',
92 'rev_content_model' => 'GOATMODEL',
93 ],
94 function ( RevisionTest $testCase, Revision $rev ) {
95 $testCase->assertSame( 2, $rev->getId() );
96 $testCase->assertSame( 1, $rev->getPage() );
97 $testCase->assertSame( 2, $rev->getTextId() );
98 $testCase->assertSame( '20171017114835', $rev->getTimestamp() );
99 $testCase->assertSame( '127.0.0.1', $rev->getUserText() );
100 $testCase->assertSame( 0, $rev->getUser() );
101 $testCase->assertSame( false, $rev->isMinor() );
102 $testCase->assertSame( false, $rev->isDeleted( Revision::DELETED_TEXT ) );
103 $testCase->assertSame( 46, $rev->getSize() );
104 $testCase->assertSame( 1, $rev->getParentId() );
105 $testCase->assertSame( 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z', $rev->getSha1() );
106 $testCase->assertSame( 'Goat Comment!', $rev->getComment() );
107 $testCase->assertSame( 'GOATFORMAT', $rev->getContentFormat() );
108 $testCase->assertSame( 'GOATMODEL', $rev->getContentModel() );
109 }
110 ];
111 yield 'null fields' => [
112 [
113 'rev_id' => '2',
114 'rev_page' => '1',
115 'rev_text_id' => '2',
116 'rev_timestamp' => '20171017114835',
117 'rev_user_text' => '127.0.0.1',
118 'rev_user' => '0',
119 'rev_minor_edit' => '0',
120 'rev_deleted' => '0',
121 'rev_comment_text' => 'Goat Comment!',
122 'rev_comment_data' => null,
123 'rev_comment_cid' => null,
124 ],
125 function ( RevisionTest $testCase, Revision $rev ) {
126 $testCase->assertNull( $rev->getSize() );
127 $testCase->assertNull( $rev->getParentId() );
128 $testCase->assertNull( $rev->getSha1() );
129 $testCase->assertSame( 'text/x-wiki', $rev->getContentFormat() );
130 $testCase->assertSame( 'wikitext', $rev->getContentModel() );
131 }
132 ];
133 }
134
135 /**
136 * @dataProvider provideConstructFromRow
137 * @covers Revision::__construct
138 * @covers Revision::constructFromDbRowObject
139 */
140 public function testConstructFromRow( array $arrayData, $assertions ) {
141 $row = (object)$arrayData;
142 $rev = new Revision( $row );
143 $assertions( $this, $rev );
144 }
145
146 public function provideGetRevisionText() {
147 yield 'Generic test' => [
148 'This is a goat of revision text.',
149 [
150 'old_flags' => '',
151 'old_text' => 'This is a goat of revision text.',
152 ],
153 ];
154 }
155
156 public function provideGetId() {
157 yield [
158 [],
159 null
160 ];
161 yield [
162 [ 'id' => 998 ],
163 998
164 ];
165 }
166
167 /**
168 * @dataProvider provideGetId
169 * @covers Revision::getId
170 */
171 public function testGetId( $rowArray, $expectedId ) {
172 $rev = new Revision( $rowArray );
173 $this->assertEquals( $expectedId, $rev->getId() );
174 }
175
176 public function provideSetId() {
177 yield [ '123', 123 ];
178 yield [ 456, 456 ];
179 }
180
181 /**
182 * @dataProvider provideSetId
183 * @covers Revision::setId
184 */
185 public function testSetId( $input, $expected ) {
186 $rev = new Revision( [] );
187 $rev->setId( $input );
188 $this->assertSame( $expected, $rev->getId() );
189 }
190
191 public function provideSetUserIdAndName() {
192 yield [ '123', 123, 'GOaT' ];
193 yield [ 456, 456, 'GOaT' ];
194 }
195
196 /**
197 * @dataProvider provideSetUserIdAndName
198 * @covers Revision::setUserIdAndName
199 */
200 public function testSetUserIdAndName( $inputId, $expectedId, $name ) {
201 $rev = new Revision( [] );
202 $rev->setUserIdAndName( $inputId, $name );
203 $this->assertSame( $expectedId, $rev->getUser( Revision::RAW ) );
204 $this->assertEquals( $name, $rev->getUserText( Revision::RAW ) );
205 }
206
207 public function provideGetTextId() {
208 yield [ [], null ];
209 yield [ [ 'text_id' => '123' ], 123 ];
210 yield [ [ 'text_id' => 456 ], 456 ];
211 }
212
213 /**
214 * @dataProvider provideGetTextId
215 * @covers Revision::getTextId()
216 */
217 public function testGetTextId( $rowArray, $expected ) {
218 $rev = new Revision( $rowArray );
219 $this->assertSame( $expected, $rev->getTextId() );
220 }
221
222 public function provideGetParentId() {
223 yield [ [], null ];
224 yield [ [ 'parent_id' => '123' ], 123 ];
225 yield [ [ 'parent_id' => 456 ], 456 ];
226 }
227
228 /**
229 * @dataProvider provideGetParentId
230 * @covers Revision::getParentId()
231 */
232 public function testGetParentId( $rowArray, $expected ) {
233 $rev = new Revision( $rowArray );
234 $this->assertSame( $expected, $rev->getParentId() );
235 }
236
237 /**
238 * @covers Revision::getRevisionText
239 * @dataProvider provideGetRevisionText
240 */
241 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
242 $this->assertEquals(
243 $expected,
244 Revision::getRevisionText( (object)$rowData, $prefix, $wiki ) );
245 }
246
247 public function provideGetRevisionTextWithZlibExtension() {
248 yield 'Generic gzip test' => [
249 'This is a small goat of revision text.',
250 [
251 'old_flags' => 'gzip',
252 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
253 ],
254 ];
255 }
256
257 /**
258 * @covers Revision::getRevisionText
259 * @dataProvider provideGetRevisionTextWithZlibExtension
260 */
261 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
262 $this->checkPHPExtension( 'zlib' );
263 $this->testGetRevisionText( $expected, $rowData );
264 }
265
266 public function provideGetRevisionTextWithLegacyEncoding() {
267 yield 'Utf8Native' => [
268 "Wiki est l'\xc3\xa9cole superieur !",
269 'iso-8859-1',
270 [
271 'old_flags' => 'utf-8',
272 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
273 ]
274 ];
275 yield 'Utf8Legacy' => [
276 "Wiki est l'\xc3\xa9cole superieur !",
277 'iso-8859-1',
278 [
279 'old_flags' => '',
280 'old_text' => "Wiki est l'\xe9cole superieur !",
281 ]
282 ];
283 }
284
285 /**
286 * @covers Revision::getRevisionText
287 * @dataProvider provideGetRevisionTextWithLegacyEncoding
288 */
289 public function testGetRevisionWithLegacyEncoding( $expected, $encoding, $rowData ) {
290 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
291 $this->testGetRevisionText( $expected, $rowData );
292 }
293
294 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
295 /**
296 * WARNING!
297 * Do not set the external flag!
298 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
299 */
300 yield 'Utf8NativeGzip' => [
301 "Wiki est l'\xc3\xa9cole superieur !",
302 'iso-8859-1',
303 [
304 'old_flags' => 'gzip,utf-8',
305 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
306 ]
307 ];
308 yield 'Utf8LegacyGzip' => [
309 "Wiki est l'\xc3\xa9cole superieur !",
310 'iso-8859-1',
311 [
312 'old_flags' => 'gzip',
313 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
314 ]
315 ];
316 }
317
318 /**
319 * @covers Revision::getRevisionText
320 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
321 */
322 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $encoding, $rowData ) {
323 $this->checkPHPExtension( 'zlib' );
324 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
325 $this->testGetRevisionText( $expected, $rowData );
326 }
327
328 /**
329 * @covers Revision::compressRevisionText
330 */
331 public function testCompressRevisionTextUtf8() {
332 $row = new stdClass;
333 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
334 $row->old_flags = Revision::compressRevisionText( $row->old_text );
335 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
336 "Flags should contain 'utf-8'" );
337 $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
338 "Flags should not contain 'gzip'" );
339 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
340 $row->old_text, "Direct check" );
341 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
342 Revision::getRevisionText( $row ), "getRevisionText" );
343 }
344
345 /**
346 * @covers Revision::compressRevisionText
347 */
348 public function testCompressRevisionTextUtf8Gzip() {
349 $this->checkPHPExtension( 'zlib' );
350 $this->setMwGlobals( 'wgCompressRevisions', true );
351
352 $row = new stdClass;
353 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
354 $row->old_flags = Revision::compressRevisionText( $row->old_text );
355 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
356 "Flags should contain 'utf-8'" );
357 $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
358 "Flags should contain 'gzip'" );
359 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
360 gzinflate( $row->old_text ), "Direct check" );
361 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
362 Revision::getRevisionText( $row ), "getRevisionText" );
363 }
364
365 public function provideFetchFromConds() {
366 yield [ 0, [] ];
367 yield [ Revision::READ_LOCKING, [ 'FOR UPDATE' ] ];
368 }
369
370 /**
371 * @dataProvider provideFetchFromConds
372 * @covers Revision::fetchFromConds
373 */
374 public function testFetchFromConds( $flags, array $options ) {
375 $conditions = [ 'conditionsArray' ];
376
377 $db = $this->getMock( IDatabase::class );
378 $db->expects( $this->once() )
379 ->method( 'selectRow' )
380 ->with(
381 $this->equalTo( [ 'revision', 'page', 'user' ] ),
382 // We don't really care about the fields are they come from the selectField methods
383 $this->isType( 'array' ),
384 $this->equalTo( $conditions ),
385 // Method name
386 $this->equalTo( 'Revision::fetchFromConds' ),
387 $this->equalTo( $options ),
388 // We don't really care about the join conds are they come from the joinCond methods
389 $this->isType( 'array' )
390 )
391 ->willReturn( 'RETURNVALUE' );
392
393 $wrapper = TestingAccessWrapper::newFromClass( Revision::class );
394 $result = $wrapper->fetchFromConds( $db, $conditions, $flags );
395
396 $this->assertEquals( 'RETURNVALUE', $result );
397 }
398
399 public function provideDecompressRevisionText() {
400 yield '(no legacy encoding), false in false out' => [ false, false, [], false ];
401 yield '(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
402 yield '(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
403 yield '(no legacy encoding), string in with gzip flag returns string' => [
404 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
405 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
406 ];
407 yield '(no legacy encoding), string in with object flag returns false' => [
408 // gzip string below generated with serialize( 'JOJO' )
409 false, "s:4:\"JOJO\";", [ 'object' ], false,
410 ];
411 yield '(no legacy encoding), serialized object in with object flag returns string' => [
412 false,
413 // Using a TitleValue object as it has a getText method (which is needed)
414 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
415 [ 'object' ],
416 'HHJJDDFF',
417 ];
418 yield '(no legacy encoding), serialized object in with object & gzip flag returns string' => [
419 false,
420 // Using a TitleValue object as it has a getText method (which is needed)
421 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
422 [ 'object', 'gzip' ],
423 '8219JJJ840',
424 ];
425 yield '(ISO-8859-1 encoding), string in string out' => [
426 'ISO-8859-1',
427 iconv( 'utf8', 'ISO-8859-1', "1®Àþ1" ),
428 [],
429 '1®Àþ1',
430 ];
431 yield '(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
432 'ISO-8859-1',
433 gzdeflate( iconv( 'utf8', 'ISO-8859-1', "4®Àþ4" ) ),
434 [ 'gzip' ],
435 '4®Àþ4',
436 ];
437 yield '(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
438 'ISO-8859-1',
439 serialize( new TitleValue( 0, iconv( 'utf8', 'ISO-8859-1', "3®Àþ3" ) ) ),
440 [ 'object' ],
441 '3®Àþ3',
442 ];
443 yield '(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
444 'ISO-8859-1',
445 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
446 [ 'gzip', 'object' ],
447 '2®Àþ2',
448 ];
449 }
450
451 /**
452 * @dataProvider provideDecompressRevisionText
453 * @covers Revision::decompressRevisionText
454 *
455 * @param bool $legacyEncoding
456 * @param mixed $text
457 * @param array $flags
458 * @param mixed $expected
459 */
460 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
461 $this->setMwGlobals( 'wgLegacyEncoding', $legacyEncoding );
462 $this->setMwGlobals( 'wgLanguageCode', 'en' );
463 $this->assertSame(
464 $expected,
465 Revision::decompressRevisionText( $text, $flags )
466 );
467 }
468
469 }