RevisionUnitTest::testConstructFromArray @covers tags
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionUnitTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
6 * @group ContentHandler
7 */
8 class RevisionUnitTest extends MediaWikiTestCase {
9
10 public function provideConstructFromArray() {
11 yield 'with text' => [
12 [
13 'text' => 'hello world.',
14 'content_model' => CONTENT_MODEL_JAVASCRIPT
15 ],
16 ];
17 yield 'with content' => [
18 [
19 'content' => new JavaScriptContent( 'hellow world.' )
20 ],
21 ];
22 }
23
24 /**
25 * @dataProvider provideConstructFromArray
26 * @covers Revision::__construct
27 * @covers Revision::constructFromRowArray
28 */
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() );
34 }
35
36 public function provideConstructFromArrayThrowsExceptions() {
37 yield 'content and text_id both not empty' => [
38 [
39 'content' => new WikitextContent( 'GOAT' ),
40 'text_id' => 'someid',
41 ],
42 new MWException( "Text already stored in external store (id someid), " .
43 "can't serialize content object" )
44 ];
45 yield 'with bad content object (class)' => [
46 [ 'content' => new stdClass() ],
47 new MWException( '`content` field must contain a Content object.' )
48 ];
49 yield 'with bad content object (string)' => [
50 [ 'content' => 'ImAGoat' ],
51 new MWException( '`content` field must contain a Content object.' )
52 ];
53 yield 'bad row format' => [
54 'imastring, not a row',
55 new MWException( 'Revision constructor passed invalid row format.' )
56 ];
57 }
58
59 /**
60 * @dataProvider provideConstructFromArrayThrowsExceptions
61 * @covers Revision::__construct
62 * @covers Revision::constructFromRowArray
63 */
64 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception $expectedException ) {
65 $this->setExpectedException(
66 get_class( $expectedException ),
67 $expectedException->getMessage(),
68 $expectedException->getCode()
69 );
70 new Revision( $rowArray );
71 }
72
73 public function provideGetRevisionText() {
74 yield 'Generic test' => [
75 'This is a goat of revision text.',
76 [
77 'old_flags' => '',
78 'old_text' => 'This is a goat of revision text.',
79 ],
80 ];
81 }
82
83 public function provideGetId() {
84 yield [
85 [],
86 null
87 ];
88 yield [
89 [ 'id' => 998 ],
90 998
91 ];
92 }
93
94 /**
95 * @dataProvider provideGetId
96 * @covers Revision::getId
97 */
98 public function testGetId( $rowArray, $expectedId ) {
99 $rev = new Revision( $rowArray );
100 $this->assertEquals( $expectedId, $rev->getId() );
101 }
102
103 public function provideSetId() {
104 yield [ '123', 123 ];
105 yield [ 456, 456 ];
106 }
107
108 /**
109 * @dataProvider provideSetId
110 * @covers Revision::setId
111 */
112 public function testSetId( $input, $expected ) {
113 $rev = new Revision( [] );
114 $rev->setId( $input );
115 $this->assertSame( $expected, $rev->getId() );
116 }
117
118 public function provideSetUserIdAndName() {
119 yield [ '123', 123, 'GOaT' ];
120 yield [ 456, 456, 'GOaT' ];
121 }
122
123 /**
124 * @dataProvider provideSetUserIdAndName
125 * @covers Revision::setUserIdAndName
126 */
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 ) );
132 }
133
134 public function provideGetTextId() {
135 yield [ [], null ];
136 yield [ [ 'text_id' => '123' ], 123 ];
137 yield [ [ 'text_id' => 456 ], 456 ];
138 }
139
140 /**
141 * @dataProvider provideGetTextId
142 * @covers Revision::getTextId()
143 */
144 public function testGetTextId( $rowArray, $expected ) {
145 $rev = new Revision( $rowArray );
146 $this->assertSame( $expected, $rev->getTextId() );
147 }
148
149 public function provideGetParentId() {
150 yield [ [], null ];
151 yield [ [ 'parent_id' => '123' ], 123 ];
152 yield [ [ 'parent_id' => 456 ], 456 ];
153 }
154
155 /**
156 * @dataProvider provideGetParentId
157 * @covers Revision::getParentId()
158 */
159 public function testGetParentId( $rowArray, $expected ) {
160 $rev = new Revision( $rowArray );
161 $this->assertSame( $expected, $rev->getParentId() );
162 }
163
164 /**
165 * @covers Revision::getRevisionText
166 * @dataProvider provideGetRevisionText
167 */
168 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
169 $this->assertEquals(
170 $expected,
171 Revision::getRevisionText( (object)$rowData, $prefix, $wiki ) );
172 }
173
174 public function provideGetRevisionTextWithZlibExtension() {
175 yield 'Generic gzip test' => [
176 'This is a small goat of revision text.',
177 [
178 'old_flags' => 'gzip',
179 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
180 ],
181 ];
182 }
183
184 /**
185 * @covers Revision::getRevisionText
186 * @dataProvider provideGetRevisionTextWithZlibExtension
187 */
188 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
189 $this->checkPHPExtension( 'zlib' );
190 $this->testGetRevisionText( $expected, $rowData );
191 }
192
193 public function provideGetRevisionTextWithLegacyEncoding() {
194 yield 'Utf8Native' => [
195 "Wiki est l'\xc3\xa9cole superieur !",
196 'iso-8859-1',
197 [
198 'old_flags' => 'utf-8',
199 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
200 ]
201 ];
202 yield 'Utf8Legacy' => [
203 "Wiki est l'\xc3\xa9cole superieur !",
204 'iso-8859-1',
205 [
206 'old_flags' => '',
207 'old_text' => "Wiki est l'\xe9cole superieur !",
208 ]
209 ];
210 }
211
212 /**
213 * @covers Revision::getRevisionText
214 * @dataProvider provideGetRevisionTextWithLegacyEncoding
215 */
216 public function testGetRevisionWithLegacyEncoding( $expected, $encoding, $rowData ) {
217 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
218 $this->testGetRevisionText( $expected, $rowData );
219 }
220
221 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
222 /**
223 * WARNING!
224 * Do not set the external flag!
225 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
226 */
227 yield 'Utf8NativeGzip' => [
228 "Wiki est l'\xc3\xa9cole superieur !",
229 'iso-8859-1',
230 [
231 'old_flags' => 'gzip,utf-8',
232 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
233 ]
234 ];
235 yield 'Utf8LegacyGzip' => [
236 "Wiki est l'\xc3\xa9cole superieur !",
237 'iso-8859-1',
238 [
239 'old_flags' => 'gzip',
240 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
241 ]
242 ];
243 }
244
245 /**
246 * @covers Revision::getRevisionText
247 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
248 */
249 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $encoding, $rowData ) {
250 $this->checkPHPExtension( 'zlib' );
251 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
252 $this->testGetRevisionText( $expected, $rowData );
253 }
254
255 /**
256 * @covers Revision::compressRevisionText
257 */
258 public function testCompressRevisionTextUtf8() {
259 $row = new stdClass;
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" );
270 }
271
272 /**
273 * @covers Revision::compressRevisionText
274 */
275 public function testCompressRevisionTextUtf8Gzip() {
276 $this->checkPHPExtension( 'zlib' );
277 $this->setMwGlobals( 'wgCompressRevisions', true );
278
279 $row = new stdClass;
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" );
290 }
291
292 /**
293 * @covers Revision::userJoinCond
294 */
295 public function testUserJoinCond() {
296 $this->assertEquals(
297 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
298 Revision::userJoinCond()
299 );
300 }
301
302 /**
303 * @covers Revision::pageJoinCond
304 */
305 public function testPageJoinCond() {
306 $this->assertEquals(
307 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
308 Revision::pageJoinCond()
309 );
310 }
311
312 public function provideSelectFields() {
313 yield [
314 true,
315 [
316 'rev_id',
317 'rev_page',
318 'rev_text_id',
319 'rev_timestamp',
320 'rev_user_text',
321 'rev_user',
322 'rev_minor_edit',
323 'rev_deleted',
324 'rev_len',
325 'rev_parent_id',
326 'rev_sha1',
327 'rev_comment_text' => 'rev_comment',
328 'rev_comment_data' => 'NULL',
329 'rev_comment_cid' => 'NULL',
330 'rev_content_format',
331 'rev_content_model',
332 ]
333 ];
334 yield [
335 false,
336 [
337 'rev_id',
338 'rev_page',
339 'rev_text_id',
340 'rev_timestamp',
341 'rev_user_text',
342 'rev_user',
343 'rev_minor_edit',
344 'rev_deleted',
345 'rev_len',
346 'rev_parent_id',
347 'rev_sha1',
348 'rev_comment_text' => 'rev_comment',
349 'rev_comment_data' => 'NULL',
350 'rev_comment_cid' => 'NULL',
351 ]
352 ];
353 }
354
355 /**
356 * @dataProvider provideSelectFields
357 * @covers Revision::selectFields
358 * @todo a true unit test would mock CommentStore
359 */
360 public function testSelectFields( $contentHandlerUseDB, $expected ) {
361 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
362 $this->assertEquals( $expected, Revision::selectFields() );
363 }
364
365 public function provideSelectArchiveFields() {
366 yield [
367 true,
368 [
369 'ar_id',
370 'ar_page_id',
371 'ar_rev_id',
372 'ar_text',
373 'ar_text_id',
374 'ar_timestamp',
375 'ar_user_text',
376 'ar_user',
377 'ar_minor_edit',
378 'ar_deleted',
379 'ar_len',
380 'ar_parent_id',
381 'ar_sha1',
382 'ar_comment_text' => 'ar_comment',
383 'ar_comment_data' => 'NULL',
384 'ar_comment_cid' => 'NULL',
385 'ar_content_format',
386 'ar_content_model',
387 ]
388 ];
389 yield [
390 false,
391 [
392 'ar_id',
393 'ar_page_id',
394 'ar_rev_id',
395 'ar_text',
396 'ar_text_id',
397 'ar_timestamp',
398 'ar_user_text',
399 'ar_user',
400 'ar_minor_edit',
401 'ar_deleted',
402 'ar_len',
403 'ar_parent_id',
404 'ar_sha1',
405 'ar_comment_text' => 'ar_comment',
406 'ar_comment_data' => 'NULL',
407 'ar_comment_cid' => 'NULL',
408 ]
409 ];
410 }
411
412 /**
413 * @dataProvider provideSelectArchiveFields
414 * @covers Revision::selectArchiveFields
415 * @todo a true unit test would mock CommentStore
416 */
417 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
418 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
419 $this->assertEquals( $expected, Revision::selectArchiveFields() );
420 }
421
422 /**
423 * @covers Revision::selectTextFields
424 */
425 public function testSelectTextFields() {
426 $this->assertEquals(
427 [
428 'old_text',
429 'old_flags',
430 ],
431 Revision::selectTextFields()
432 );
433 }
434
435 /**
436 * @covers Revision::selectPageFields
437 */
438 public function testSelectPageFields() {
439 $this->assertEquals(
440 [
441 'page_namespace',
442 'page_title',
443 'page_id',
444 'page_latest',
445 'page_is_redirect',
446 'page_len',
447 ],
448 Revision::selectPageFields()
449 );
450 }
451
452 /**
453 * @covers Revision::selectUserFields
454 */
455 public function testSelectUserFields() {
456 $this->assertEquals(
457 [
458 'user_name',
459 ],
460 Revision::selectUserFields()
461 );
462 }
463
464 public function provideFetchFromConds() {
465 yield [ 0, [] ];
466 yield [ Revision::READ_LOCKING, [ 'FOR UPDATE' ] ];
467 }
468
469 /**
470 * @dataProvider provideFetchFromConds
471 * @covers Revision::fetchFromConds
472 */
473 public function testFetchFromConds( $flags, array $options ) {
474 $conditions = [ 'conditionsArray' ];
475
476 $db = $this->getMock( IDatabase::class );
477 $db->expects( $this->once() )
478 ->method( 'selectRow' )
479 ->with(
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 ),
484 // Method name
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' )
489 )
490 ->willReturn( 'RETURNVALUE' );
491
492 $wrapper = TestingAccessWrapper::newFromClass( Revision::class );
493 $result = $wrapper->fetchFromConds( $db, $conditions, $flags );
494
495 $this->assertEquals( 'RETURNVALUE', $result );
496 }
497 }