3 use Wikimedia\TestingAccessWrapper
;
6 * @group ContentHandler
8 class RevisionUnitTest
extends MediaWikiTestCase
{
10 public function provideConstructFromArray() {
11 yield
'with text' => [
13 'text' => 'hello world.',
14 'content_model' => CONTENT_MODEL_JAVASCRIPT
17 yield
'with content' => [
19 'content' => new JavaScriptContent( 'hellow world.' )
25 * @dataProvider provideConstructFromArray
26 * @covers Revision::__construct
27 * @covers Revision::constructFromRowArray
29 public function testConstructFromArray( array $rowArray ) {
30 $rev = new Revision( $rowArray );
31 $this->assertNotNull( $rev->getContent(), 'no content object available' );
32 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
33 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
36 public function provideConstructFromArrayThrowsExceptions() {
37 yield
'content and text_id both not empty' => [
39 'content' => new WikitextContent( 'GOAT' ),
40 'text_id' => 'someid',
42 new MWException( "Text already stored in external store (id someid), " .
43 "can't serialize content object" )
45 yield
'with bad content object (class)' => [
46 [ 'content' => new stdClass() ],
47 new MWException( '`content` field must contain a Content object.' )
49 yield
'with bad content object (string)' => [
50 [ 'content' => 'ImAGoat' ],
51 new MWException( '`content` field must contain a Content object.' )
53 yield
'bad row format' => [
54 'imastring, not a row',
55 new MWException( 'Revision constructor passed invalid row format.' )
60 * @dataProvider provideConstructFromArrayThrowsExceptions
61 * @covers Revision::__construct
62 * @covers Revision::constructFromRowArray
64 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception
$expectedException ) {
65 $this->setExpectedException(
66 get_class( $expectedException ),
67 $expectedException->getMessage(),
68 $expectedException->getCode()
70 new Revision( $rowArray );
73 public function provideGetRevisionText() {
74 yield
'Generic test' => [
75 'This is a goat of revision text.',
78 'old_text' => 'This is a goat of revision text.',
83 public function provideGetId() {
95 * @dataProvider provideGetId
96 * @covers Revision::getId
98 public function testGetId( $rowArray, $expectedId ) {
99 $rev = new Revision( $rowArray );
100 $this->assertEquals( $expectedId, $rev->getId() );
103 public function provideSetId() {
104 yield
[ '123', 123 ];
109 * @dataProvider provideSetId
110 * @covers Revision::setId
112 public function testSetId( $input, $expected ) {
113 $rev = new Revision( [] );
114 $rev->setId( $input );
115 $this->assertSame( $expected, $rev->getId() );
118 public function provideSetUserIdAndName() {
119 yield
[ '123', 123, 'GOaT' ];
120 yield
[ 456, 456, 'GOaT' ];
124 * @dataProvider provideSetUserIdAndName
125 * @covers Revision::setUserIdAndName
127 public function testSetUserIdAndName( $inputId, $expectedId, $name ) {
128 $rev = new Revision( [] );
129 $rev->setUserIdAndName( $inputId, $name );
130 $this->assertSame( $expectedId, $rev->getUser( Revision
::RAW
) );
131 $this->assertEquals( $name, $rev->getUserText( Revision
::RAW
) );
134 public function provideGetTextId() {
136 yield
[ [ 'text_id' => '123' ], 123 ];
137 yield
[ [ 'text_id' => 456 ], 456 ];
141 * @dataProvider provideGetTextId
142 * @covers Revision::getTextId()
144 public function testGetTextId( $rowArray, $expected ) {
145 $rev = new Revision( $rowArray );
146 $this->assertSame( $expected, $rev->getTextId() );
149 public function provideGetParentId() {
151 yield
[ [ 'parent_id' => '123' ], 123 ];
152 yield
[ [ 'parent_id' => 456 ], 456 ];
156 * @dataProvider provideGetParentId
157 * @covers Revision::getParentId()
159 public function testGetParentId( $rowArray, $expected ) {
160 $rev = new Revision( $rowArray );
161 $this->assertSame( $expected, $rev->getParentId() );
165 * @covers Revision::getRevisionText
166 * @dataProvider provideGetRevisionText
168 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
171 Revision
::getRevisionText( (object)$rowData, $prefix, $wiki ) );
174 public function provideGetRevisionTextWithZlibExtension() {
175 yield
'Generic gzip test' => [
176 'This is a small goat of revision text.',
178 'old_flags' => 'gzip',
179 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
185 * @covers Revision::getRevisionText
186 * @dataProvider provideGetRevisionTextWithZlibExtension
188 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
189 $this->checkPHPExtension( 'zlib' );
190 $this->testGetRevisionText( $expected, $rowData );
193 public function provideGetRevisionTextWithLegacyEncoding() {
194 yield
'Utf8Native' => [
195 "Wiki est l'\xc3\xa9cole superieur !",
198 'old_flags' => 'utf-8',
199 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
202 yield
'Utf8Legacy' => [
203 "Wiki est l'\xc3\xa9cole superieur !",
207 'old_text' => "Wiki est l'\xe9cole superieur !",
213 * @covers Revision::getRevisionText
214 * @dataProvider provideGetRevisionTextWithLegacyEncoding
216 public function testGetRevisionWithLegacyEncoding( $expected, $encoding, $rowData ) {
217 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
218 $this->testGetRevisionText( $expected, $rowData );
221 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
224 * Do not set the external flag!
225 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
227 yield
'Utf8NativeGzip' => [
228 "Wiki est l'\xc3\xa9cole superieur !",
231 'old_flags' => 'gzip,utf-8',
232 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
235 yield
'Utf8LegacyGzip' => [
236 "Wiki est l'\xc3\xa9cole superieur !",
239 'old_flags' => 'gzip',
240 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
246 * @covers Revision::getRevisionText
247 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
249 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $encoding, $rowData ) {
250 $this->checkPHPExtension( 'zlib' );
251 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
252 $this->testGetRevisionText( $expected, $rowData );
256 * @covers Revision::compressRevisionText
258 public function testCompressRevisionTextUtf8() {
260 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
261 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
262 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
263 "Flags should contain 'utf-8'" );
264 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
265 "Flags should not contain 'gzip'" );
266 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
267 $row->old_text
, "Direct check" );
268 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
269 Revision
::getRevisionText( $row ), "getRevisionText" );
273 * @covers Revision::compressRevisionText
275 public function testCompressRevisionTextUtf8Gzip() {
276 $this->checkPHPExtension( 'zlib' );
277 $this->setMwGlobals( 'wgCompressRevisions', true );
280 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
281 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
282 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
283 "Flags should contain 'utf-8'" );
284 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
285 "Flags should contain 'gzip'" );
286 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
287 gzinflate( $row->old_text
), "Direct check" );
288 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
289 Revision
::getRevisionText( $row ), "getRevisionText" );
293 * @covers Revision::userJoinCond
295 public function testUserJoinCond() {
297 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
298 Revision
::userJoinCond()
303 * @covers Revision::pageJoinCond
305 public function testPageJoinCond() {
307 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
308 Revision
::pageJoinCond()
312 public function provideSelectFields() {
327 'rev_comment_text' => 'rev_comment',
328 'rev_comment_data' => 'NULL',
329 'rev_comment_cid' => 'NULL',
330 'rev_content_format',
348 'rev_comment_text' => 'rev_comment',
349 'rev_comment_data' => 'NULL',
350 'rev_comment_cid' => 'NULL',
356 * @dataProvider provideSelectFields
357 * @covers Revision::selectFields
358 * @todo a true unit test would mock CommentStore
360 public function testSelectFields( $contentHandlerUseDB, $expected ) {
361 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
362 $this->assertEquals( $expected, Revision
::selectFields() );
365 public function provideSelectArchiveFields() {
382 'ar_comment_text' => 'ar_comment',
383 'ar_comment_data' => 'NULL',
384 'ar_comment_cid' => 'NULL',
405 'ar_comment_text' => 'ar_comment',
406 'ar_comment_data' => 'NULL',
407 'ar_comment_cid' => 'NULL',
413 * @dataProvider provideSelectArchiveFields
414 * @covers Revision::selectArchiveFields
415 * @todo a true unit test would mock CommentStore
417 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
418 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
419 $this->assertEquals( $expected, Revision
::selectArchiveFields() );
423 * @covers Revision::selectTextFields
425 public function testSelectTextFields() {
431 Revision
::selectTextFields()
436 * @covers Revision::selectPageFields
438 public function testSelectPageFields() {
448 Revision
::selectPageFields()
453 * @covers Revision::selectUserFields
455 public function testSelectUserFields() {
460 Revision
::selectUserFields()
464 public function provideFetchFromConds() {
466 yield
[ Revision
::READ_LOCKING
, [ 'FOR UPDATE' ] ];
470 * @dataProvider provideFetchFromConds
471 * @covers Revision::fetchFromConds
473 public function testFetchFromConds( $flags, array $options ) {
474 $conditions = [ 'conditionsArray' ];
476 $db = $this->getMock( IDatabase
::class );
477 $db->expects( $this->once() )
478 ->method( 'selectRow' )
480 $this->equalTo( [ 'revision', 'page', 'user' ] ),
481 // We don't really care about the fields are they come from the selectField methods
482 $this->isType( 'array' ),
483 $this->equalTo( $conditions ),
485 $this->equalTo( 'Revision::fetchFromConds' ),
486 $this->equalTo( $options ),
487 // We don't really care about the join conds are they come from the joinCond methods
488 $this->isType( 'array' )
490 ->willReturn( 'RETURNVALUE' );
492 $wrapper = TestingAccessWrapper
::newFromClass( Revision
::class );
493 $result = $wrapper->fetchFromConds( $db, $conditions, $flags );
495 $this->assertEquals( 'RETURNVALUE', $result );