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 provideConstructFromRow() {
74 yield
'Full construction' => [
79 'rev_timestamp' => '20171017114835',
80 'rev_user_text' => '127.0.0.1',
82 'rev_minor_edit' => '0',
85 'rev_parent_id' => '1',
86 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
87 'rev_comment_text' => 'Goat Comment!',
88 'rev_comment_data' => null,
89 'rev_comment_cid' => null,
90 'rev_content_format' => 'GOATFORMAT',
91 'rev_content_model' => 'GOATMODEL',
93 function ( RevisionUnitTest
$testCase, Revision
$rev ) {
94 $testCase->assertSame( 2, $rev->getId() );
95 $testCase->assertSame( 1, $rev->getPage() );
96 $testCase->assertSame( 2, $rev->getTextId() );
97 $testCase->assertSame( '20171017114835', $rev->getTimestamp() );
98 $testCase->assertSame( '127.0.0.1', $rev->getUserText() );
99 $testCase->assertSame( 0, $rev->getUser() );
100 $testCase->assertSame( false, $rev->isMinor() );
101 $testCase->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
102 $testCase->assertSame( 46, $rev->getSize() );
103 $testCase->assertSame( 1, $rev->getParentId() );
104 $testCase->assertSame( 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z', $rev->getSha1() );
105 $testCase->assertSame( 'Goat Comment!', $rev->getComment() );
106 $testCase->assertSame( 'GOATFORMAT', $rev->getContentFormat() );
107 $testCase->assertSame( 'GOATMODEL', $rev->getContentModel() );
110 yield
'null fields' => [
114 'rev_text_id' => '2',
115 'rev_timestamp' => '20171017114835',
116 'rev_user_text' => '127.0.0.1',
118 'rev_minor_edit' => '0',
119 'rev_deleted' => '0',
120 'rev_comment_text' => 'Goat Comment!',
121 'rev_comment_data' => null,
122 'rev_comment_cid' => null,
124 function ( RevisionUnitTest
$testCase, Revision
$rev ) {
125 $testCase->assertNull( $rev->getSize() );
126 $testCase->assertNull( $rev->getParentId() );
127 $testCase->assertNull( $rev->getSha1() );
128 $testCase->assertSame( 'text/x-wiki', $rev->getContentFormat() );
129 $testCase->assertSame( 'wikitext', $rev->getContentModel() );
135 * @dataProvider provideConstructFromRow
136 * @covers Revision::__construct
137 * @covers Revision::constructFromDbRowObject
139 public function testConstructFromRow( array $arrayData, $assertions ) {
140 $row = (object)$arrayData;
141 $rev = new Revision( $row );
142 $assertions( $this, $rev );
145 public function provideGetRevisionText() {
146 yield
'Generic test' => [
147 'This is a goat of revision text.',
150 'old_text' => 'This is a goat of revision text.',
155 public function provideGetId() {
167 * @dataProvider provideGetId
168 * @covers Revision::getId
170 public function testGetId( $rowArray, $expectedId ) {
171 $rev = new Revision( $rowArray );
172 $this->assertEquals( $expectedId, $rev->getId() );
175 public function provideSetId() {
176 yield
[ '123', 123 ];
181 * @dataProvider provideSetId
182 * @covers Revision::setId
184 public function testSetId( $input, $expected ) {
185 $rev = new Revision( [] );
186 $rev->setId( $input );
187 $this->assertSame( $expected, $rev->getId() );
190 public function provideSetUserIdAndName() {
191 yield
[ '123', 123, 'GOaT' ];
192 yield
[ 456, 456, 'GOaT' ];
196 * @dataProvider provideSetUserIdAndName
197 * @covers Revision::setUserIdAndName
199 public function testSetUserIdAndName( $inputId, $expectedId, $name ) {
200 $rev = new Revision( [] );
201 $rev->setUserIdAndName( $inputId, $name );
202 $this->assertSame( $expectedId, $rev->getUser( Revision
::RAW
) );
203 $this->assertEquals( $name, $rev->getUserText( Revision
::RAW
) );
206 public function provideGetTextId() {
208 yield
[ [ 'text_id' => '123' ], 123 ];
209 yield
[ [ 'text_id' => 456 ], 456 ];
213 * @dataProvider provideGetTextId
214 * @covers Revision::getTextId()
216 public function testGetTextId( $rowArray, $expected ) {
217 $rev = new Revision( $rowArray );
218 $this->assertSame( $expected, $rev->getTextId() );
221 public function provideGetParentId() {
223 yield
[ [ 'parent_id' => '123' ], 123 ];
224 yield
[ [ 'parent_id' => 456 ], 456 ];
228 * @dataProvider provideGetParentId
229 * @covers Revision::getParentId()
231 public function testGetParentId( $rowArray, $expected ) {
232 $rev = new Revision( $rowArray );
233 $this->assertSame( $expected, $rev->getParentId() );
237 * @covers Revision::getRevisionText
238 * @dataProvider provideGetRevisionText
240 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
243 Revision
::getRevisionText( (object)$rowData, $prefix, $wiki ) );
246 public function provideGetRevisionTextWithZlibExtension() {
247 yield
'Generic gzip test' => [
248 'This is a small goat of revision text.',
250 'old_flags' => 'gzip',
251 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
257 * @covers Revision::getRevisionText
258 * @dataProvider provideGetRevisionTextWithZlibExtension
260 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
261 $this->checkPHPExtension( 'zlib' );
262 $this->testGetRevisionText( $expected, $rowData );
265 public function provideGetRevisionTextWithLegacyEncoding() {
266 yield
'Utf8Native' => [
267 "Wiki est l'\xc3\xa9cole superieur !",
270 'old_flags' => 'utf-8',
271 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
274 yield
'Utf8Legacy' => [
275 "Wiki est l'\xc3\xa9cole superieur !",
279 'old_text' => "Wiki est l'\xe9cole superieur !",
285 * @covers Revision::getRevisionText
286 * @dataProvider provideGetRevisionTextWithLegacyEncoding
288 public function testGetRevisionWithLegacyEncoding( $expected, $encoding, $rowData ) {
289 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
290 $this->testGetRevisionText( $expected, $rowData );
293 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
296 * Do not set the external flag!
297 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
299 yield
'Utf8NativeGzip' => [
300 "Wiki est l'\xc3\xa9cole superieur !",
303 'old_flags' => 'gzip,utf-8',
304 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
307 yield
'Utf8LegacyGzip' => [
308 "Wiki est l'\xc3\xa9cole superieur !",
311 'old_flags' => 'gzip',
312 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
318 * @covers Revision::getRevisionText
319 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
321 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $encoding, $rowData ) {
322 $this->checkPHPExtension( 'zlib' );
323 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
324 $this->testGetRevisionText( $expected, $rowData );
328 * @covers Revision::compressRevisionText
330 public function testCompressRevisionTextUtf8() {
332 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
333 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
334 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
335 "Flags should contain 'utf-8'" );
336 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
337 "Flags should not contain 'gzip'" );
338 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
339 $row->old_text
, "Direct check" );
340 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
341 Revision
::getRevisionText( $row ), "getRevisionText" );
345 * @covers Revision::compressRevisionText
347 public function testCompressRevisionTextUtf8Gzip() {
348 $this->checkPHPExtension( 'zlib' );
349 $this->setMwGlobals( 'wgCompressRevisions', true );
352 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
353 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
354 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
355 "Flags should contain 'utf-8'" );
356 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
357 "Flags should contain 'gzip'" );
358 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
359 gzinflate( $row->old_text
), "Direct check" );
360 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
361 Revision
::getRevisionText( $row ), "getRevisionText" );
364 public function provideFetchFromConds() {
366 yield
[ Revision
::READ_LOCKING
, [ 'FOR UPDATE' ] ];
370 * @dataProvider provideFetchFromConds
371 * @covers Revision::fetchFromConds
373 public function testFetchFromConds( $flags, array $options ) {
374 $conditions = [ 'conditionsArray' ];
376 $db = $this->getMock( IDatabase
::class );
377 $db->expects( $this->once() )
378 ->method( 'selectRow' )
380 $this->equalTo( [ 'revision', 'page', 'user' ] ),
381 // We don't really care about the fields are they come from the selectField methods
382 $this->isType( 'array' ),
383 $this->equalTo( $conditions ),
385 $this->equalTo( 'Revision::fetchFromConds' ),
386 $this->equalTo( $options ),
387 // We don't really care about the join conds are they come from the joinCond methods
388 $this->isType( 'array' )
390 ->willReturn( 'RETURNVALUE' );
392 $wrapper = TestingAccessWrapper
::newFromClass( Revision
::class );
393 $result = $wrapper->fetchFromConds( $db, $conditions, $flags );
395 $this->assertEquals( 'RETURNVALUE', $result );