4 * @group ContentHandler
6 class RevisionUnitTest
extends MediaWikiTestCase
{
8 public function provideConstructFromArray() {
11 'text' => 'hello world.',
12 'content_model' => CONTENT_MODEL_JAVASCRIPT
15 yield
'with content' => [
17 'content' => new JavaScriptContent( 'hellow world.' )
23 * @dataProvider provideConstructFromArray
25 public function testConstructFromArray( $rowArray ) {
26 $rev = new Revision( $rowArray );
27 $this->assertNotNull( $rev->getContent(), 'no content object available' );
28 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
29 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
32 public function provideConstructFromArrayThrowsExceptions() {
33 yield
'content and text_id both not empty' => [
35 'content' => new WikitextContent( 'GOAT' ),
36 'text_id' => 'someid',
38 new MWException( "Text already stored in external store (id someid), " .
39 "can't serialize content object" )
41 yield
'with bad content object (class)' => [
42 [ 'content' => new stdClass() ],
43 new MWException( '`content` field must contain a Content object.' )
45 yield
'with bad content object (string)' => [
46 [ 'content' => 'ImAGoat' ],
47 new MWException( '`content` field must contain a Content object.' )
49 yield
'bad row format' => [
50 'imastring, not a row',
51 new MWException( 'Revision constructor passed invalid row format.' )
56 * @dataProvider provideConstructFromArrayThrowsExceptions
58 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception
$expectedException ) {
59 $this->setExpectedException(
60 get_class( $expectedException ),
61 $expectedException->getMessage(),
62 $expectedException->getCode()
64 new Revision( $rowArray );
67 public function provideGetRevisionText() {
68 yield
'Generic test' => [
69 'This is a goat of revision text.',
72 'old_text' => 'This is a goat of revision text.',
77 public function provideGetId() {
89 * @dataProvider provideGetId
90 * @covers Revision::getId
92 public function testGetId( $rowArray, $expectedId ) {
93 $rev = new Revision( $rowArray );
94 $this->assertEquals( $expectedId, $rev->getId() );
97 public function provideSetId() {
103 * @dataProvider provideSetId
104 * @covers Revision::setId
106 public function testSetId( $input, $expected ) {
107 $rev = new Revision( [] );
108 $rev->setId( $input );
109 $this->assertSame( $expected, $rev->getId() );
112 public function provideSetUserIdAndName() {
113 yield
[ '123', 123, 'GOaT' ];
114 yield
[ 456, 456, 'GOaT' ];
118 * @dataProvider provideSetUserIdAndName
119 * @covers Revision::setUserIdAndName
121 public function testSetUserIdAndName( $inputId, $expectedId, $name ) {
122 $rev = new Revision( [] );
123 $rev->setUserIdAndName( $inputId, $name );
124 $this->assertSame( $expectedId, $rev->getUser( Revision
::RAW
) );
125 $this->assertEquals( $name, $rev->getUserText( Revision
::RAW
) );
128 public function provideGetTextId() {
130 yield
[ [ 'text_id' => '123' ], 123 ];
131 yield
[ [ 'text_id' => 456 ], 456 ];
135 * @dataProvider provideGetTextId
136 * @covers Revision::getTextId()
138 public function testGetTextId( $rowArray, $expected ) {
139 $rev = new Revision( $rowArray );
140 $this->assertSame( $expected, $rev->getTextId() );
143 public function provideGetParentId() {
145 yield
[ [ 'parent_id' => '123' ], 123 ];
146 yield
[ [ 'parent_id' => 456 ], 456 ];
150 * @dataProvider provideGetParentId
151 * @covers Revision::getParentId()
153 public function testGetParentId( $rowArray, $expected ) {
154 $rev = new Revision( $rowArray );
155 $this->assertSame( $expected, $rev->getParentId() );
159 * @covers Revision::getRevisionText
160 * @dataProvider provideGetRevisionText
162 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
165 Revision
::getRevisionText( (object)$rowData, $prefix, $wiki ) );
168 public function provideGetRevisionTextWithZlibExtension() {
169 yield
'Generic gzip test' => [
170 'This is a small goat of revision text.',
172 'old_flags' => 'gzip',
173 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
179 * @covers Revision::getRevisionText
180 * @dataProvider provideGetRevisionTextWithZlibExtension
182 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
183 $this->checkPHPExtension( 'zlib' );
184 $this->testGetRevisionText( $expected, $rowData );
187 public function provideGetRevisionTextWithLegacyEncoding() {
188 yield
'Utf8Native' => [
189 "Wiki est l'\xc3\xa9cole superieur !",
192 'old_flags' => 'utf-8',
193 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
196 yield
'Utf8Legacy' => [
197 "Wiki est l'\xc3\xa9cole superieur !",
201 'old_text' => "Wiki est l'\xe9cole superieur !",
207 * @covers Revision::getRevisionText
208 * @dataProvider provideGetRevisionTextWithLegacyEncoding
210 public function testGetRevisionWithLegacyEncoding( $expected, $encoding, $rowData ) {
211 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
212 $this->testGetRevisionText( $expected, $rowData );
215 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
218 * Do not set the external flag!
219 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
221 yield
'Utf8NativeGzip' => [
222 "Wiki est l'\xc3\xa9cole superieur !",
225 'old_flags' => 'gzip,utf-8',
226 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
229 yield
'Utf8LegacyGzip' => [
230 "Wiki est l'\xc3\xa9cole superieur !",
233 'old_flags' => 'gzip',
234 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
240 * @covers Revision::getRevisionText
241 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
243 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $encoding, $rowData ) {
244 $this->checkPHPExtension( 'zlib' );
245 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
246 $this->testGetRevisionText( $expected, $rowData );
250 * @covers Revision::compressRevisionText
252 public function testCompressRevisionTextUtf8() {
254 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
255 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
256 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
257 "Flags should contain 'utf-8'" );
258 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
259 "Flags should not contain 'gzip'" );
260 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
261 $row->old_text
, "Direct check" );
262 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
263 Revision
::getRevisionText( $row ), "getRevisionText" );
267 * @covers Revision::compressRevisionText
269 public function testCompressRevisionTextUtf8Gzip() {
270 $this->checkPHPExtension( 'zlib' );
271 $this->setMwGlobals( 'wgCompressRevisions', true );
274 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
275 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
276 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
277 "Flags should contain 'utf-8'" );
278 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
279 "Flags should contain 'gzip'" );
280 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
281 gzinflate( $row->old_text
), "Direct check" );
282 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
283 Revision
::getRevisionText( $row ), "getRevisionText" );
287 * @covers Revision::userJoinCond
289 public function testUserJoinCond() {
291 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
292 Revision
::userJoinCond()
297 * @covers Revision::pageJoinCond
299 public function testPageJoinCond() {
301 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
302 Revision
::pageJoinCond()
306 public function provideSelectFields() {
321 'rev_comment_text' => 'rev_comment',
322 'rev_comment_data' => 'NULL',
323 'rev_comment_cid' => 'NULL',
324 'rev_content_format',
342 'rev_comment_text' => 'rev_comment',
343 'rev_comment_data' => 'NULL',
344 'rev_comment_cid' => 'NULL',
350 * @dataProvider provideSelectFields
351 * @covers Revision::selectFields
352 * @todo a true unit test would mock CommentStore
354 public function testSelectFields( $contentHandlerUseDB, $expected ) {
355 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
356 $this->assertEquals( $expected, Revision
::selectFields() );
359 public function provideSelectArchiveFields() {
376 'ar_comment_text' => 'ar_comment',
377 'ar_comment_data' => 'NULL',
378 'ar_comment_cid' => 'NULL',
399 'ar_comment_text' => 'ar_comment',
400 'ar_comment_data' => 'NULL',
401 'ar_comment_cid' => 'NULL',
407 * @dataProvider provideSelectArchiveFields
408 * @covers Revision::selectArchiveFields
409 * @todo a true unit test would mock CommentStore
411 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
412 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
413 $this->assertEquals( $expected, Revision
::selectArchiveFields() );
417 * @covers Revision::selectTextFields
419 public function testSelectTextFields() {
425 Revision
::selectTextFields()
430 * @covers Revision::selectPageFields
432 public function testSelectPageFields() {
442 Revision
::selectPageFields()
447 * @covers Revision::selectUserFields
449 public function testSelectUserFields() {
454 Revision
::selectUserFields()