3 use Wikimedia\TestingAccessWrapper
;
6 * Test cases in RevisionTest should not interact with the Database.
7 * For test cases that need Database interaction see RevisionDbTestBase.
9 class RevisionTest
extends MediaWikiTestCase
{
11 public function provideConstructFromArray() {
12 yield
'with text' => [
14 'text' => 'hello world.',
15 'content_model' => CONTENT_MODEL_JAVASCRIPT
18 yield
'with content' => [
20 'content' => new JavaScriptContent( 'hellow world.' )
26 * @dataProvider provideConstructFromArray
27 * @covers Revision::__construct
28 * @covers Revision::constructFromRowArray
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() );
37 public function provideConstructFromArrayThrowsExceptions() {
38 yield
'content and text_id both not empty' => [
40 'content' => new WikitextContent( 'GOAT' ),
41 'text_id' => 'someid',
43 new MWException( "Text already stored in external store (id someid), " .
44 "can't serialize content object" )
46 yield
'with bad content object (class)' => [
47 [ 'content' => new stdClass() ],
48 new MWException( '`content` field must contain a Content object.' )
50 yield
'with bad content object (string)' => [
51 [ 'content' => 'ImAGoat' ],
52 new MWException( '`content` field must contain a Content object.' )
54 yield
'bad row format' => [
55 'imastring, not a row',
56 new MWException( 'Revision constructor passed invalid row format.' )
61 * @dataProvider provideConstructFromArrayThrowsExceptions
62 * @covers Revision::__construct
63 * @covers Revision::constructFromRowArray
65 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception
$expectedException ) {
66 $this->setExpectedException(
67 get_class( $expectedException ),
68 $expectedException->getMessage(),
69 $expectedException->getCode()
71 new Revision( $rowArray );
74 public function provideConstructFromRow() {
75 yield
'Full construction' => [
80 'rev_timestamp' => '20171017114835',
81 'rev_user_text' => '127.0.0.1',
83 'rev_minor_edit' => '0',
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',
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() );
111 yield
'null fields' => [
115 'rev_text_id' => '2',
116 'rev_timestamp' => '20171017114835',
117 'rev_user_text' => '127.0.0.1',
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,
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() );
136 * @dataProvider provideConstructFromRow
137 * @covers Revision::__construct
138 * @covers Revision::constructFromDbRowObject
140 public function testConstructFromRow( array $arrayData, $assertions ) {
141 $row = (object)$arrayData;
142 $rev = new Revision( $row );
143 $assertions( $this, $rev );
146 public function provideGetRevisionText() {
147 yield
'Generic test' => [
148 'This is a goat of revision text.',
151 'old_text' => 'This is a goat of revision text.',
156 public function provideGetId() {
168 * @dataProvider provideGetId
169 * @covers Revision::getId
171 public function testGetId( $rowArray, $expectedId ) {
172 $rev = new Revision( $rowArray );
173 $this->assertEquals( $expectedId, $rev->getId() );
176 public function provideSetId() {
177 yield
[ '123', 123 ];
182 * @dataProvider provideSetId
183 * @covers Revision::setId
185 public function testSetId( $input, $expected ) {
186 $rev = new Revision( [] );
187 $rev->setId( $input );
188 $this->assertSame( $expected, $rev->getId() );
191 public function provideSetUserIdAndName() {
192 yield
[ '123', 123, 'GOaT' ];
193 yield
[ 456, 456, 'GOaT' ];
197 * @dataProvider provideSetUserIdAndName
198 * @covers Revision::setUserIdAndName
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
) );
207 public function provideGetTextId() {
209 yield
[ [ 'text_id' => '123' ], 123 ];
210 yield
[ [ 'text_id' => 456 ], 456 ];
214 * @dataProvider provideGetTextId
215 * @covers Revision::getTextId()
217 public function testGetTextId( $rowArray, $expected ) {
218 $rev = new Revision( $rowArray );
219 $this->assertSame( $expected, $rev->getTextId() );
222 public function provideGetParentId() {
224 yield
[ [ 'parent_id' => '123' ], 123 ];
225 yield
[ [ 'parent_id' => 456 ], 456 ];
229 * @dataProvider provideGetParentId
230 * @covers Revision::getParentId()
232 public function testGetParentId( $rowArray, $expected ) {
233 $rev = new Revision( $rowArray );
234 $this->assertSame( $expected, $rev->getParentId() );
238 * @covers Revision::getRevisionText
239 * @dataProvider provideGetRevisionText
241 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
244 Revision
::getRevisionText( (object)$rowData, $prefix, $wiki ) );
247 public function provideGetRevisionTextWithZlibExtension() {
248 yield
'Generic gzip test' => [
249 'This is a small goat of revision text.',
251 'old_flags' => 'gzip',
252 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
258 * @covers Revision::getRevisionText
259 * @dataProvider provideGetRevisionTextWithZlibExtension
261 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
262 $this->checkPHPExtension( 'zlib' );
263 $this->testGetRevisionText( $expected, $rowData );
266 public function provideGetRevisionTextWithLegacyEncoding() {
267 yield
'Utf8Native' => [
268 "Wiki est l'\xc3\xa9cole superieur !",
271 'old_flags' => 'utf-8',
272 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
275 yield
'Utf8Legacy' => [
276 "Wiki est l'\xc3\xa9cole superieur !",
280 'old_text' => "Wiki est l'\xe9cole superieur !",
286 * @covers Revision::getRevisionText
287 * @dataProvider provideGetRevisionTextWithLegacyEncoding
289 public function testGetRevisionWithLegacyEncoding( $expected, $encoding, $rowData ) {
290 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
291 $this->testGetRevisionText( $expected, $rowData );
294 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
297 * Do not set the external flag!
298 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
300 yield
'Utf8NativeGzip' => [
301 "Wiki est l'\xc3\xa9cole superieur !",
304 'old_flags' => 'gzip,utf-8',
305 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
308 yield
'Utf8LegacyGzip' => [
309 "Wiki est l'\xc3\xa9cole superieur !",
312 'old_flags' => 'gzip',
313 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
319 * @covers Revision::getRevisionText
320 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
322 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $encoding, $rowData ) {
323 $this->checkPHPExtension( 'zlib' );
324 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
325 $this->testGetRevisionText( $expected, $rowData );
329 * @covers Revision::compressRevisionText
331 public function testCompressRevisionTextUtf8() {
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" );
346 * @covers Revision::compressRevisionText
348 public function testCompressRevisionTextUtf8Gzip() {
349 $this->checkPHPExtension( 'zlib' );
350 $this->setMwGlobals( 'wgCompressRevisions', true );
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" );
365 public function provideFetchFromConds() {
367 yield
[ Revision
::READ_LOCKING
, [ 'FOR UPDATE' ] ];
371 * @dataProvider provideFetchFromConds
372 * @covers Revision::fetchFromConds
374 public function testFetchFromConds( $flags, array $options ) {
375 $conditions = [ 'conditionsArray' ];
377 $db = $this->getMock( IDatabase
::class );
378 $db->expects( $this->once() )
379 ->method( 'selectRow' )
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 ),
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' )
391 ->willReturn( 'RETURNVALUE' );
393 $wrapper = TestingAccessWrapper
::newFromClass( Revision
::class );
394 $result = $wrapper->fetchFromConds( $db, $conditions, $flags );
396 $this->assertEquals( 'RETURNVALUE', $result );
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',
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,
411 yield
'(no legacy encoding), serialized object in with object flag returns string' => [
413 // Using a TitleValue object as it has a getText method (which is needed)
414 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
418 yield
'(no legacy encoding), serialized object in with object & gzip flag returns string' => [
420 // Using a TitleValue object as it has a getText method (which is needed)
421 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
422 [ 'object', 'gzip' ],
425 yield
'(ISO-8859-1 encoding), string in string out' => [
427 iconv( 'utf8', 'ISO-8859-1', "1®Àþ1" ),
431 yield
'(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
433 gzdeflate( iconv( 'utf8', 'ISO-8859-1', "4®Àþ4" ) ),
437 yield
'(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
439 serialize( new TitleValue( 0, iconv( 'utf8', 'ISO-8859-1', "3®Àþ3" ) ) ),
443 yield
'(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
445 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
446 [ 'gzip', 'object' ],
452 * @dataProvider provideDecompressRevisionText
453 * @covers Revision::decompressRevisionText
455 * @param bool $legacyEncoding
457 * @param array $flags
458 * @param mixed $expected
460 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
461 $this->setMwGlobals( 'wgLegacyEncoding', $legacyEncoding );
462 $this->setMwGlobals( 'wgLanguageCode', 'en' );
465 Revision
::decompressRevisionText( $text, $flags )
470 * @covers Revision::getRevisionText
472 public function testGetRevisionText_returnsFalseWhenNoTextField() {
473 $this->assertFalse( Revision
::getRevisionText( new stdClass() ) );
476 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
477 yield
'Just text' => [
478 (object)[ 'old_text' => 'SomeText' ],
482 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
483 yield
'gzip text' => [
485 'old_text' => "sttttr\002\022\000",
486 'old_flags' => 'gzip'
491 yield
'gzip text and different prefix' => [
493 'jojo_text' => "sttttr\002\022\000",
494 'jojo_flags' => 'gzip'
502 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
503 * @covers Revision::getRevisionText
505 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
510 $this->assertSame( $expected, Revision
::getRevisionText( $row, $prefix ) );
513 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
514 yield
'Just some text' => [ 'someNonUrlText' ];
515 yield
'No second URL part' => [ 'someProtocol://' ];
519 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
520 * @covers Revision::getRevisionText
522 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
526 Revision
::getRevisionText(
529 'old_flags' => 'external',