Merge "mw.rcfilters.ui.SaveFiltersPopupButtonWidget: Remove pointless option"
[lhc/web/wiklou.git] / tests / phpunit / includes / page / WikiPageDbTestBase.php
1 <?php
2
3 abstract class WikiPageDbTestBase extends MediaWikiLangTestCase {
4
5 private $pagesToDelete;
6
7 public function __construct( $name = null, array $data = [], $dataName = '' ) {
8 parent::__construct( $name, $data, $dataName );
9
10 $this->tablesUsed = array_merge(
11 $this->tablesUsed,
12 [ 'page',
13 'revision',
14 'redirect',
15 'archive',
16 'category',
17 'ip_changes',
18 'text',
19
20 'recentchanges',
21 'logging',
22
23 'page_props',
24 'pagelinks',
25 'categorylinks',
26 'langlinks',
27 'externallinks',
28 'imagelinks',
29 'templatelinks',
30 'iwlinks' ] );
31 }
32
33 protected function setUp() {
34 parent::setUp();
35 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
36 $this->pagesToDelete = [];
37 }
38
39 protected function tearDown() {
40 foreach ( $this->pagesToDelete as $p ) {
41 /* @var $p WikiPage */
42
43 try {
44 if ( $p->exists() ) {
45 $p->doDeleteArticle( "testing done." );
46 }
47 } catch ( MWException $ex ) {
48 // fail silently
49 }
50 }
51 parent::tearDown();
52 }
53
54 abstract protected function getContentHandlerUseDB();
55
56 /**
57 * @param Title|string $title
58 * @param string|null $model
59 * @return WikiPage
60 */
61 private function newPage( $title, $model = null ) {
62 if ( is_string( $title ) ) {
63 $ns = $this->getDefaultWikitextNS();
64 $title = Title::newFromText( $title, $ns );
65 }
66
67 $p = new WikiPage( $title );
68
69 $this->pagesToDelete[] = $p;
70
71 return $p;
72 }
73
74 /**
75 * @param string|Title|WikiPage $page
76 * @param string $text
77 * @param int $model
78 *
79 * @return WikiPage
80 */
81 protected function createPage( $page, $text, $model = null ) {
82 if ( is_string( $page ) || $page instanceof Title ) {
83 $page = $this->newPage( $page, $model );
84 }
85
86 $content = ContentHandler::makeContent( $text, $page->getTitle(), $model );
87 $page->doEditContent( $content, "testing", EDIT_NEW );
88
89 return $page;
90 }
91
92 /**
93 * @covers WikiPage::doEditContent
94 * @covers WikiPage::doModify
95 * @covers WikiPage::doCreate
96 * @covers WikiPage::doEditUpdates
97 */
98 public function testDoEditContent() {
99 $page = $this->newPage( __METHOD__ );
100 $title = $page->getTitle();
101
102 $content = ContentHandler::makeContent(
103 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
104 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
105 $title,
106 CONTENT_MODEL_WIKITEXT
107 );
108
109 $page->doEditContent( $content, "[[testing]] 1" );
110
111 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
112 $this->assertTrue( $page->getId() > 0, "WikiPage should have new page id" );
113 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
114 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
115
116 $id = $page->getId();
117
118 # ------------------------
119 $dbr = wfGetDB( DB_REPLICA );
120 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
121 $n = $res->numRows();
122 $res->free();
123
124 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
125
126 # ------------------------
127 $page = new WikiPage( $title );
128
129 $retrieved = $page->getContent();
130 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
131
132 # ------------------------
133 $content = ContentHandler::makeContent(
134 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
135 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est.",
136 $title,
137 CONTENT_MODEL_WIKITEXT
138 );
139
140 $page->doEditContent( $content, "testing 2" );
141
142 # ------------------------
143 $page = new WikiPage( $title );
144
145 $retrieved = $page->getContent();
146 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
147
148 # ------------------------
149 $dbr = wfGetDB( DB_REPLICA );
150 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
151 $n = $res->numRows();
152 $res->free();
153
154 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
155 }
156
157 /**
158 * @covers WikiPage::doDeleteArticle
159 * @covers WikiPage::doDeleteArticleReal
160 */
161 public function testDoDeleteArticle() {
162 $page = $this->createPage(
163 __METHOD__,
164 "[[original text]] foo",
165 CONTENT_MODEL_WIKITEXT
166 );
167 $id = $page->getId();
168
169 $page->doDeleteArticle( "testing deletion" );
170
171 $this->assertFalse(
172 $page->getTitle()->getArticleID() > 0,
173 "Title object should now have page id 0"
174 );
175 $this->assertFalse( $page->getId() > 0, "WikiPage should now have page id 0" );
176 $this->assertFalse(
177 $page->exists(),
178 "WikiPage::exists should return false after page was deleted"
179 );
180 $this->assertNull(
181 $page->getContent(),
182 "WikiPage::getContent should return null after page was deleted"
183 );
184
185 $t = Title::newFromText( $page->getTitle()->getPrefixedText() );
186 $this->assertFalse(
187 $t->exists(),
188 "Title::exists should return false after page was deleted"
189 );
190
191 // Run the job queue
192 JobQueueGroup::destroySingletons();
193 $jobs = new RunJobs;
194 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
195 $jobs->execute();
196
197 # ------------------------
198 $dbr = wfGetDB( DB_REPLICA );
199 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
200 $n = $res->numRows();
201 $res->free();
202
203 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
204 }
205
206 /**
207 * @covers WikiPage::doDeleteUpdates
208 */
209 public function testDoDeleteUpdates() {
210 $page = $this->createPage(
211 __METHOD__,
212 "[[original text]] foo",
213 CONTENT_MODEL_WIKITEXT
214 );
215 $id = $page->getId();
216
217 // Similar to MovePage logic
218 wfGetDB( DB_MASTER )->delete( 'page', [ 'page_id' => $id ], __METHOD__ );
219 $page->doDeleteUpdates( $id );
220
221 // Run the job queue
222 JobQueueGroup::destroySingletons();
223 $jobs = new RunJobs;
224 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
225 $jobs->execute();
226
227 # ------------------------
228 $dbr = wfGetDB( DB_REPLICA );
229 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
230 $n = $res->numRows();
231 $res->free();
232
233 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
234 }
235
236 /**
237 * @covers WikiPage::getRevision
238 */
239 public function testGetRevision() {
240 $page = $this->newPage( __METHOD__ );
241
242 $rev = $page->getRevision();
243 $this->assertNull( $rev );
244
245 # -----------------
246 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
247
248 $rev = $page->getRevision();
249
250 $this->assertEquals( $page->getLatest(), $rev->getId() );
251 $this->assertEquals( "some text", $rev->getContent()->getNativeData() );
252 }
253
254 /**
255 * @covers WikiPage::getContent
256 */
257 public function testGetContent() {
258 $page = $this->newPage( __METHOD__ );
259
260 $content = $page->getContent();
261 $this->assertNull( $content );
262
263 # -----------------
264 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
265
266 $content = $page->getContent();
267 $this->assertEquals( "some text", $content->getNativeData() );
268 }
269
270 /**
271 * @covers WikiPage::exists
272 */
273 public function testExists() {
274 $page = $this->newPage( __METHOD__ );
275 $this->assertFalse( $page->exists() );
276
277 # -----------------
278 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
279 $this->assertTrue( $page->exists() );
280
281 $page = new WikiPage( $page->getTitle() );
282 $this->assertTrue( $page->exists() );
283
284 # -----------------
285 $page->doDeleteArticle( "done testing" );
286 $this->assertFalse( $page->exists() );
287
288 $page = new WikiPage( $page->getTitle() );
289 $this->assertFalse( $page->exists() );
290 }
291
292 public function provideHasViewableContent() {
293 return [
294 [ 'WikiPageTest_testHasViewableContent', false, true ],
295 [ 'Special:WikiPageTest_testHasViewableContent', false ],
296 [ 'MediaWiki:WikiPageTest_testHasViewableContent', false ],
297 [ 'Special:Userlogin', true ],
298 [ 'MediaWiki:help', true ],
299 ];
300 }
301
302 /**
303 * @dataProvider provideHasViewableContent
304 * @covers WikiPage::hasViewableContent
305 */
306 public function testHasViewableContent( $title, $viewable, $create = false ) {
307 $page = $this->newPage( $title );
308 $this->assertEquals( $viewable, $page->hasViewableContent() );
309
310 if ( $create ) {
311 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
312 $this->assertTrue( $page->hasViewableContent() );
313
314 $page = new WikiPage( $page->getTitle() );
315 $this->assertTrue( $page->hasViewableContent() );
316 }
317 }
318
319 public function provideGetRedirectTarget() {
320 return [
321 [ 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT, "hello world", null ],
322 [
323 'WikiPageTest_testGetRedirectTarget_2',
324 CONTENT_MODEL_WIKITEXT,
325 "#REDIRECT [[hello world]]",
326 "Hello world"
327 ],
328 ];
329 }
330
331 /**
332 * @dataProvider provideGetRedirectTarget
333 * @covers WikiPage::getRedirectTarget
334 */
335 public function testGetRedirectTarget( $title, $model, $text, $target ) {
336 $this->setMwGlobals( [
337 'wgCapitalLinks' => true,
338 ] );
339
340 $page = $this->createPage( $title, $text, $model );
341
342 # sanity check, because this test seems to fail for no reason for some people.
343 $c = $page->getContent();
344 $this->assertEquals( 'WikitextContent', get_class( $c ) );
345
346 # now, test the actual redirect
347 $t = $page->getRedirectTarget();
348 $this->assertEquals( $target, is_null( $t ) ? null : $t->getPrefixedText() );
349 }
350
351 /**
352 * @dataProvider provideGetRedirectTarget
353 * @covers WikiPage::isRedirect
354 */
355 public function testIsRedirect( $title, $model, $text, $target ) {
356 $page = $this->createPage( $title, $text, $model );
357 $this->assertEquals( !is_null( $target ), $page->isRedirect() );
358 }
359
360 public function provideIsCountable() {
361 return [
362
363 // any
364 [ 'WikiPageTest_testIsCountable',
365 CONTENT_MODEL_WIKITEXT,
366 '',
367 'any',
368 true
369 ],
370 [ 'WikiPageTest_testIsCountable',
371 CONTENT_MODEL_WIKITEXT,
372 'Foo',
373 'any',
374 true
375 ],
376
377 // comma
378 [ 'WikiPageTest_testIsCountable',
379 CONTENT_MODEL_WIKITEXT,
380 'Foo',
381 'comma',
382 false
383 ],
384 [ 'WikiPageTest_testIsCountable',
385 CONTENT_MODEL_WIKITEXT,
386 'Foo, bar',
387 'comma',
388 true
389 ],
390
391 // link
392 [ 'WikiPageTest_testIsCountable',
393 CONTENT_MODEL_WIKITEXT,
394 'Foo',
395 'link',
396 false
397 ],
398 [ 'WikiPageTest_testIsCountable',
399 CONTENT_MODEL_WIKITEXT,
400 'Foo [[bar]]',
401 'link',
402 true
403 ],
404
405 // redirects
406 [ 'WikiPageTest_testIsCountable',
407 CONTENT_MODEL_WIKITEXT,
408 '#REDIRECT [[bar]]',
409 'any',
410 false
411 ],
412 [ 'WikiPageTest_testIsCountable',
413 CONTENT_MODEL_WIKITEXT,
414 '#REDIRECT [[bar]]',
415 'comma',
416 false
417 ],
418 [ 'WikiPageTest_testIsCountable',
419 CONTENT_MODEL_WIKITEXT,
420 '#REDIRECT [[bar]]',
421 'link',
422 false
423 ],
424
425 // not a content namespace
426 [ 'Talk:WikiPageTest_testIsCountable',
427 CONTENT_MODEL_WIKITEXT,
428 'Foo',
429 'any',
430 false
431 ],
432 [ 'Talk:WikiPageTest_testIsCountable',
433 CONTENT_MODEL_WIKITEXT,
434 'Foo, bar',
435 'comma',
436 false
437 ],
438 [ 'Talk:WikiPageTest_testIsCountable',
439 CONTENT_MODEL_WIKITEXT,
440 'Foo [[bar]]',
441 'link',
442 false
443 ],
444
445 // not a content namespace, different model
446 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
447 null,
448 'Foo',
449 'any',
450 false
451 ],
452 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
453 null,
454 'Foo, bar',
455 'comma',
456 false
457 ],
458 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
459 null,
460 'Foo [[bar]]',
461 'link',
462 false
463 ],
464 ];
465 }
466
467 /**
468 * @dataProvider provideIsCountable
469 * @covers WikiPage::isCountable
470 */
471 public function testIsCountable( $title, $model, $text, $mode, $expected ) {
472 global $wgContentHandlerUseDB;
473
474 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
475
476 $title = Title::newFromText( $title );
477
478 if ( !$wgContentHandlerUseDB
479 && $model
480 && ContentHandler::getDefaultModelFor( $title ) != $model
481 ) {
482 $this->markTestSkipped( "Can not use non-default content model $model for "
483 . $title->getPrefixedDBkey() . " with \$wgContentHandlerUseDB disabled." );
484 }
485
486 $page = $this->createPage( $title, $text, $model );
487
488 $editInfo = $page->prepareContentForEdit( $page->getContent() );
489
490 $v = $page->isCountable();
491 $w = $page->isCountable( $editInfo );
492
493 $this->assertEquals(
494 $expected,
495 $v,
496 "isCountable( null ) returned unexpected value " . var_export( $v, true )
497 . " instead of " . var_export( $expected, true )
498 . " in mode `$mode` for text \"$text\""
499 );
500
501 $this->assertEquals(
502 $expected,
503 $w,
504 "isCountable( \$editInfo ) returned unexpected value " . var_export( $v, true )
505 . " instead of " . var_export( $expected, true )
506 . " in mode `$mode` for text \"$text\""
507 );
508 }
509
510 public function provideGetParserOutput() {
511 return [
512 [
513 CONTENT_MODEL_WIKITEXT,
514 "hello ''world''\n",
515 "<div class=\"mw-parser-output\"><p>hello <i>world</i></p></div>"
516 ],
517 // @todo more...?
518 ];
519 }
520
521 /**
522 * @dataProvider provideGetParserOutput
523 * @covers WikiPage::getParserOutput
524 */
525 public function testGetParserOutput( $model, $text, $expectedHtml ) {
526 $page = $this->createPage( __METHOD__, $text, $model );
527
528 $opt = $page->makeParserOptions( 'canonical' );
529 $po = $page->getParserOutput( $opt );
530 $text = $po->getText();
531
532 $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments
533 $text = preg_replace( '!\s*(</p>|</div>)!sm', '\1', $text ); # don't let tidy confuse us
534
535 $this->assertEquals( $expectedHtml, $text );
536
537 return $po;
538 }
539
540 /**
541 * @covers WikiPage::getParserOutput
542 */
543 public function testGetParserOutput_nonexisting() {
544 $page = new WikiPage( Title::newFromText( __METHOD__ ) );
545
546 $opt = new ParserOptions();
547 $po = $page->getParserOutput( $opt );
548
549 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." );
550 }
551
552 /**
553 * @covers WikiPage::getParserOutput
554 */
555 public function testGetParserOutput_badrev() {
556 $page = $this->createPage( __METHOD__, 'dummy', CONTENT_MODEL_WIKITEXT );
557
558 $opt = new ParserOptions();
559 $po = $page->getParserOutput( $opt, $page->getLatest() + 1234 );
560
561 // @todo would be neat to also test deleted revision
562
563 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." );
564 }
565
566 public static $sections =
567
568 "Intro
569
570 == stuff ==
571 hello world
572
573 == test ==
574 just a test
575
576 == foo ==
577 more stuff
578 ";
579
580 public function dataReplaceSection() {
581 // NOTE: assume the Help namespace to contain wikitext
582 return [
583 [ 'Help:WikiPageTest_testReplaceSection',
584 CONTENT_MODEL_WIKITEXT,
585 self::$sections,
586 "0",
587 "No more",
588 null,
589 trim( preg_replace( '/^Intro/sm', 'No more', self::$sections ) )
590 ],
591 [ 'Help:WikiPageTest_testReplaceSection',
592 CONTENT_MODEL_WIKITEXT,
593 self::$sections,
594 "",
595 "No more",
596 null,
597 "No more"
598 ],
599 [ 'Help:WikiPageTest_testReplaceSection',
600 CONTENT_MODEL_WIKITEXT,
601 self::$sections,
602 "2",
603 "== TEST ==\nmore fun",
604 null,
605 trim( preg_replace( '/^== test ==.*== foo ==/sm',
606 "== TEST ==\nmore fun\n\n== foo ==",
607 self::$sections ) )
608 ],
609 [ 'Help:WikiPageTest_testReplaceSection',
610 CONTENT_MODEL_WIKITEXT,
611 self::$sections,
612 "8",
613 "No more",
614 null,
615 trim( self::$sections )
616 ],
617 [ 'Help:WikiPageTest_testReplaceSection',
618 CONTENT_MODEL_WIKITEXT,
619 self::$sections,
620 "new",
621 "No more",
622 "New",
623 trim( self::$sections ) . "\n\n== New ==\n\nNo more"
624 ],
625 ];
626 }
627
628 /**
629 * @dataProvider dataReplaceSection
630 * @covers WikiPage::replaceSectionContent
631 */
632 public function testReplaceSectionContent( $title, $model, $text, $section,
633 $with, $sectionTitle, $expected
634 ) {
635 $page = $this->createPage( $title, $text, $model );
636
637 $content = ContentHandler::makeContent( $with, $page->getTitle(), $page->getContentModel() );
638 $c = $page->replaceSectionContent( $section, $content, $sectionTitle );
639
640 $this->assertEquals( $expected, is_null( $c ) ? null : trim( $c->getNativeData() ) );
641 }
642
643 /**
644 * @dataProvider dataReplaceSection
645 * @covers WikiPage::replaceSectionAtRev
646 */
647 public function testReplaceSectionAtRev( $title, $model, $text, $section,
648 $with, $sectionTitle, $expected
649 ) {
650 $page = $this->createPage( $title, $text, $model );
651 $baseRevId = $page->getLatest();
652
653 $content = ContentHandler::makeContent( $with, $page->getTitle(), $page->getContentModel() );
654 $c = $page->replaceSectionAtRev( $section, $content, $sectionTitle, $baseRevId );
655
656 $this->assertEquals( $expected, is_null( $c ) ? null : trim( $c->getNativeData() ) );
657 }
658
659 /**
660 * @covers WikiPage::getOldestRevision
661 */
662 public function testGetOldestRevision() {
663 $page = $this->newPage( __METHOD__ );
664 $page->doEditContent(
665 new WikitextContent( 'one' ),
666 "first edit",
667 EDIT_NEW
668 );
669 $rev1 = $page->getRevision();
670
671 $page = new WikiPage( $page->getTitle() );
672 $page->doEditContent(
673 new WikitextContent( 'two' ),
674 "second edit",
675 EDIT_UPDATE
676 );
677
678 $page = new WikiPage( $page->getTitle() );
679 $page->doEditContent(
680 new WikitextContent( 'three' ),
681 "third edit",
682 EDIT_UPDATE
683 );
684
685 // sanity check
686 $this->assertNotEquals(
687 $rev1->getId(),
688 $page->getRevision()->getId(),
689 '$page->getRevision()->getId()'
690 );
691
692 // actual test
693 $this->assertEquals(
694 $rev1->getId(),
695 $page->getOldestRevision()->getId(),
696 '$page->getOldestRevision()->getId()'
697 );
698 }
699
700 /**
701 * @covers WikiPage::doRollback
702 * @covers WikiPage::commitRollback
703 */
704 public function testDoRollback() {
705 $admin = $this->getTestSysop()->getUser();
706 $user1 = $this->getTestUser()->getUser();
707 // Use the confirmed group for user2 to make sure the user is different
708 $user2 = $this->getTestUser( [ 'confirmed' ] )->getUser();
709
710 $page = $this->newPage( __METHOD__ );
711
712 // Make some edits
713 $text = "one";
714 $status1 = $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
715 "section one", EDIT_NEW, false, $admin );
716
717 $text .= "\n\ntwo";
718 $status2 = $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
719 "adding section two", 0, false, $user1 );
720
721 $text .= "\n\nthree";
722 $status3 = $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
723 "adding section three", 0, false, $user2 );
724
725 /** @var Revision $rev1 */
726 /** @var Revision $rev2 */
727 /** @var Revision $rev3 */
728 $rev1 = $status1->getValue()['revision'];
729 $rev2 = $status2->getValue()['revision'];
730 $rev3 = $status3->getValue()['revision'];
731
732 /**
733 * We are having issues with doRollback spuriously failing. Apparently
734 * the last revision somehow goes missing or not committed under some
735 * circumstances. So, make sure the revisions have the correct usernames.
736 */
737 $this->assertEquals( 3, Revision::countByPageId( wfGetDB( DB_REPLICA ), $page->getId() ) );
738 $this->assertEquals( $admin->getName(), $rev1->getUserText() );
739 $this->assertEquals( $user1->getName(), $rev2->getUserText() );
740 $this->assertEquals( $user2->getName(), $rev3->getUserText() );
741
742 // Now, try the actual rollback
743 $token = $admin->getEditToken( 'rollback' );
744 $rollbackErrors = $page->doRollback(
745 $user2->getName(),
746 "testing rollback",
747 $token,
748 false,
749 $resultDetails,
750 $admin
751 );
752
753 if ( $rollbackErrors ) {
754 $this->fail(
755 "Rollback failed:\n" .
756 print_r( $rollbackErrors, true ) . ";\n" .
757 print_r( $resultDetails, true )
758 );
759 }
760
761 $page = new WikiPage( $page->getTitle() );
762 $this->assertEquals( $rev2->getSha1(), $page->getRevision()->getSha1(),
763 "rollback did not revert to the correct revision" );
764 $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
765 }
766
767 /**
768 * @covers WikiPage::doRollback
769 * @covers WikiPage::commitRollback
770 */
771 public function testDoRollback_simple() {
772 $admin = $this->getTestSysop()->getUser();
773
774 $text = "one";
775 $page = $this->newPage( __METHOD__ );
776 $page->doEditContent(
777 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
778 "section one",
779 EDIT_NEW,
780 false,
781 $admin
782 );
783 $rev1 = $page->getRevision();
784
785 $user1 = $this->getTestUser()->getUser();
786 $text .= "\n\ntwo";
787 $page = new WikiPage( $page->getTitle() );
788 $page->doEditContent(
789 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
790 "adding section two",
791 0,
792 false,
793 $user1
794 );
795
796 # now, try the rollback
797 $token = $admin->getEditToken( 'rollback' );
798 $errors = $page->doRollback(
799 $user1->getName(),
800 "testing revert",
801 $token,
802 false,
803 $details,
804 $admin
805 );
806
807 if ( $errors ) {
808 $this->fail( "Rollback failed:\n" . print_r( $errors, true )
809 . ";\n" . print_r( $details, true ) );
810 }
811
812 $page = new WikiPage( $page->getTitle() );
813 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
814 "rollback did not revert to the correct revision" );
815 $this->assertEquals( "one", $page->getContent()->getNativeData() );
816 }
817
818 /**
819 * @covers WikiPage::doRollback
820 * @covers WikiPage::commitRollback
821 */
822 public function testDoRollbackFailureSameContent() {
823 $admin = $this->getTestSysop()->getUser();
824
825 $text = "one";
826 $page = $this->newPage( __METHOD__ );
827 $page->doEditContent(
828 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
829 "section one",
830 EDIT_NEW,
831 false,
832 $admin
833 );
834 $rev1 = $page->getRevision();
835
836 $user1 = $this->getTestUser( [ 'sysop' ] )->getUser();
837 $text .= "\n\ntwo";
838 $page = new WikiPage( $page->getTitle() );
839 $page->doEditContent(
840 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
841 "adding section two",
842 0,
843 false,
844 $user1
845 );
846
847 # now, do a the rollback from the same user was doing the edit before
848 $resultDetails = [];
849 $token = $user1->getEditToken( 'rollback' );
850 $errors = $page->doRollback(
851 $user1->getName(),
852 "testing revert same user",
853 $token,
854 false,
855 $resultDetails,
856 $admin
857 );
858
859 $this->assertEquals( [], $errors, "Rollback failed same user" );
860
861 # now, try the rollback
862 $resultDetails = [];
863 $token = $admin->getEditToken( 'rollback' );
864 $errors = $page->doRollback(
865 $user1->getName(),
866 "testing revert",
867 $token,
868 false,
869 $resultDetails,
870 $admin
871 );
872
873 $this->assertEquals(
874 [
875 [
876 'alreadyrolled',
877 __METHOD__,
878 $user1->getName(),
879 $admin->getName(),
880 ],
881 ],
882 $errors,
883 "Rollback not failed"
884 );
885
886 $page = new WikiPage( $page->getTitle() );
887 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
888 "rollback did not revert to the correct revision" );
889 $this->assertEquals( "one", $page->getContent()->getNativeData() );
890 }
891
892 /**
893 * Tests tagging for edits that do rollback action
894 * @covers WikiPage::doRollback
895 */
896 public function testDoRollbackTagging() {
897 if ( !in_array( 'mw-rollback', ChangeTags::getSoftwareTags() ) ) {
898 $this->markTestSkipped( 'Rollback tag deactivated, skipped the test.' );
899 }
900
901 $admin = new User();
902 $admin->setName( 'Administrator' );
903 $admin->addToDatabase();
904
905 $text = 'First line';
906 $page = $this->newPage( 'WikiPageTest_testDoRollbackTagging' );
907 $page->doEditContent(
908 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
909 'Added first line',
910 EDIT_NEW,
911 false,
912 $admin
913 );
914
915 $secondUser = new User();
916 $secondUser->setName( '92.65.217.32' );
917 $text .= '\n\nSecond line';
918 $page = new WikiPage( $page->getTitle() );
919 $page->doEditContent(
920 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
921 'Adding second line',
922 0,
923 false,
924 $secondUser
925 );
926
927 // Now, try the rollback
928 $admin->addGroup( 'sysop' ); // Make the test user a sysop
929 $token = $admin->getEditToken( 'rollback' );
930 $errors = $page->doRollback(
931 $secondUser->getName(),
932 'testing rollback',
933 $token,
934 false,
935 $resultDetails,
936 $admin
937 );
938
939 // If doRollback completed without errors
940 if ( $errors === [] ) {
941 $tags = $resultDetails[ 'tags' ];
942 $this->assertContains( 'mw-rollback', $tags );
943 }
944 }
945
946 public function provideGetAutoDeleteReason() {
947 return [
948 [
949 [],
950 false,
951 false
952 ],
953
954 [
955 [
956 [ "first edit", null ],
957 ],
958 "/first edit.*only contributor/",
959 false
960 ],
961
962 [
963 [
964 [ "first edit", null ],
965 [ "second edit", null ],
966 ],
967 "/second edit.*only contributor/",
968 true
969 ],
970
971 [
972 [
973 [ "first edit", "127.0.2.22" ],
974 [ "second edit", "127.0.3.33" ],
975 ],
976 "/second edit/",
977 true
978 ],
979
980 [
981 [
982 [
983 "first edit: "
984 . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
985 . " nonumy eirmod tempor invidunt ut labore et dolore magna "
986 . "aliquyam erat, sed diam voluptua. At vero eos et accusam "
987 . "et justo duo dolores et ea rebum. Stet clita kasd gubergren, "
988 . "no sea takimata sanctus est Lorem ipsum dolor sit amet.'",
989 null
990 ],
991 ],
992 '/first edit:.*\.\.\."/',
993 false
994 ],
995
996 [
997 [
998 [ "first edit", "127.0.2.22" ],
999 [ "", "127.0.3.33" ],
1000 ],
1001 "/before blanking.*first edit/",
1002 true
1003 ],
1004
1005 ];
1006 }
1007
1008 /**
1009 * @dataProvider provideGetAutoDeleteReason
1010 * @covers WikiPage::getAutoDeleteReason
1011 */
1012 public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
1013 global $wgUser;
1014
1015 // NOTE: assume Help namespace to contain wikitext
1016 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
1017
1018 $c = 1;
1019
1020 foreach ( $edits as $edit ) {
1021 $user = new User();
1022
1023 if ( !empty( $edit[1] ) ) {
1024 $user->setName( $edit[1] );
1025 } else {
1026 $user = $wgUser;
1027 }
1028
1029 $content = ContentHandler::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
1030
1031 $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW : 0, false, $user );
1032
1033 $c += 1;
1034 }
1035
1036 $reason = $page->getAutoDeleteReason( $hasHistory );
1037
1038 if ( is_bool( $expectedResult ) || is_null( $expectedResult ) ) {
1039 $this->assertEquals( $expectedResult, $reason );
1040 } else {
1041 $this->assertTrue( (bool)preg_match( $expectedResult, $reason ),
1042 "Autosummary didn't match expected pattern $expectedResult: $reason" );
1043 }
1044
1045 $this->assertEquals( $expectedHistory, $hasHistory,
1046 "expected \$hasHistory to be " . var_export( $expectedHistory, true ) );
1047
1048 $page->doDeleteArticle( "done" );
1049 }
1050
1051 public function providePreSaveTransform() {
1052 return [
1053 [ 'hello this is ~~~',
1054 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
1055 ],
1056 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1057 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1058 ],
1059 ];
1060 }
1061
1062 /**
1063 * @covers WikiPage::factory
1064 */
1065 public function testWikiPageFactory() {
1066 $title = Title::makeTitle( NS_FILE, 'Someimage.png' );
1067 $page = WikiPage::factory( $title );
1068 $this->assertEquals( 'WikiFilePage', get_class( $page ) );
1069
1070 $title = Title::makeTitle( NS_CATEGORY, 'SomeCategory' );
1071 $page = WikiPage::factory( $title );
1072 $this->assertEquals( 'WikiCategoryPage', get_class( $page ) );
1073
1074 $title = Title::makeTitle( NS_MAIN, 'SomePage' );
1075 $page = WikiPage::factory( $title );
1076 $this->assertEquals( 'WikiPage', get_class( $page ) );
1077 }
1078
1079 /**
1080 * @dataProvider provideCommentMigrationOnDeletion
1081 *
1082 * @param int $writeStage
1083 * @param int $readStage
1084 */
1085 public function testCommentMigrationOnDeletion( $writeStage, $readStage ) {
1086 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', $writeStage );
1087 $dbr = wfGetDB( DB_REPLICA );
1088
1089 $page = $this->createPage(
1090 __METHOD__,
1091 "foo",
1092 CONTENT_MODEL_WIKITEXT
1093 );
1094 $revid = $page->getLatest();
1095 if ( $writeStage > MIGRATION_OLD ) {
1096 $comment_id = $dbr->selectField(
1097 'revision_comment_temp',
1098 'revcomment_comment_id',
1099 [ 'revcomment_rev' => $revid ],
1100 __METHOD__
1101 );
1102 }
1103
1104 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', $readStage );
1105
1106 $page->doDeleteArticle( "testing deletion" );
1107
1108 if ( $readStage > MIGRATION_OLD ) {
1109 // Didn't leave behind any 'revision_comment_temp' rows
1110 $n = $dbr->selectField(
1111 'revision_comment_temp', 'COUNT(*)', [ 'revcomment_rev' => $revid ], __METHOD__
1112 );
1113 $this->assertEquals( 0, $n, 'no entry in revision_comment_temp after deletion' );
1114
1115 // Copied or upgraded the comment_id, as applicable
1116 $ar_comment_id = $dbr->selectField(
1117 'archive',
1118 'ar_comment_id',
1119 [ 'ar_rev_id' => $revid ],
1120 __METHOD__
1121 );
1122 if ( $writeStage > MIGRATION_OLD ) {
1123 $this->assertSame( $comment_id, $ar_comment_id );
1124 } else {
1125 $this->assertNotEquals( 0, $ar_comment_id );
1126 }
1127 }
1128
1129 // Copied rev_comment, if applicable
1130 if ( $readStage <= MIGRATION_WRITE_BOTH && $writeStage <= MIGRATION_WRITE_BOTH ) {
1131 $ar_comment = $dbr->selectField(
1132 'archive',
1133 'ar_comment',
1134 [ 'ar_rev_id' => $revid ],
1135 __METHOD__
1136 );
1137 $this->assertSame( 'testing', $ar_comment );
1138 }
1139 }
1140
1141 public function provideCommentMigrationOnDeletion() {
1142 return [
1143 [ MIGRATION_OLD, MIGRATION_OLD ],
1144 [ MIGRATION_OLD, MIGRATION_WRITE_BOTH ],
1145 [ MIGRATION_OLD, MIGRATION_WRITE_NEW ],
1146 [ MIGRATION_WRITE_BOTH, MIGRATION_OLD ],
1147 [ MIGRATION_WRITE_BOTH, MIGRATION_WRITE_BOTH ],
1148 [ MIGRATION_WRITE_BOTH, MIGRATION_WRITE_NEW ],
1149 [ MIGRATION_WRITE_BOTH, MIGRATION_NEW ],
1150 [ MIGRATION_WRITE_NEW, MIGRATION_WRITE_BOTH ],
1151 [ MIGRATION_WRITE_NEW, MIGRATION_WRITE_NEW ],
1152 [ MIGRATION_WRITE_NEW, MIGRATION_NEW ],
1153 [ MIGRATION_NEW, MIGRATION_WRITE_BOTH ],
1154 [ MIGRATION_NEW, MIGRATION_WRITE_NEW ],
1155 [ MIGRATION_NEW, MIGRATION_NEW ],
1156 ];
1157 }
1158
1159 /**
1160 * @covers WikiPage::updateCategoryCounts
1161 */
1162 public function testUpdateCategoryCounts() {
1163 $page = new WikiPage( Title::newFromText( __METHOD__ ) );
1164
1165 // Add an initial category
1166 $page->updateCategoryCounts( [ 'A' ], [], 0 );
1167
1168 $this->assertEquals( 1, Category::newFromName( 'A' )->getPageCount() );
1169 $this->assertEquals( 0, Category::newFromName( 'B' )->getPageCount() );
1170 $this->assertEquals( 0, Category::newFromName( 'C' )->getPageCount() );
1171
1172 // Add a new category
1173 $page->updateCategoryCounts( [ 'B' ], [], 0 );
1174
1175 $this->assertEquals( 1, Category::newFromName( 'A' )->getPageCount() );
1176 $this->assertEquals( 1, Category::newFromName( 'B' )->getPageCount() );
1177 $this->assertEquals( 0, Category::newFromName( 'C' )->getPageCount() );
1178
1179 // Add and remove a category
1180 $page->updateCategoryCounts( [ 'C' ], [ 'A' ], 0 );
1181
1182 $this->assertEquals( 0, Category::newFromName( 'A' )->getPageCount() );
1183 $this->assertEquals( 1, Category::newFromName( 'B' )->getPageCount() );
1184 $this->assertEquals( 1, Category::newFromName( 'C' )->getPageCount() );
1185 }
1186
1187 public function provideUpdateRedirectOn() {
1188 yield [ '#REDIRECT [[Foo]]', true, null, true, true, 0 ];
1189 yield [ '#REDIRECT [[Foo]]', true, 'Foo', true, false, 1 ];
1190 yield [ 'SomeText', false, null, false, true, 0 ];
1191 yield [ 'SomeText', false, 'Foo', false, false, 1 ];
1192 }
1193
1194 /**
1195 * @dataProvider provideUpdateRedirectOn
1196 * @covers WikiPage::updateRedirectOn
1197 *
1198 * @param string $initialText
1199 * @param bool $initialRedirectState
1200 * @param string|null $redirectTitle
1201 * @param bool|null $lastRevIsRedirect
1202 * @param bool $expectedSuccess
1203 * @param int $expectedRowCount
1204 */
1205 public function testUpdateRedirectOn(
1206 $initialText,
1207 $initialRedirectState,
1208 $redirectTitle,
1209 $lastRevIsRedirect,
1210 $expectedSuccess,
1211 $expectedRowCount
1212 ) {
1213 static $pageCounter = 0;
1214 $pageCounter++;
1215
1216 $page = $this->createPage( Title::newFromText( __METHOD__ . $pageCounter ), $initialText );
1217 $this->assertSame( $initialRedirectState, $page->isRedirect() );
1218
1219 $redirectTitle = is_string( $redirectTitle )
1220 ? Title::newFromText( $redirectTitle )
1221 : $redirectTitle;
1222
1223 $success = $page->updateRedirectOn( $this->db, $redirectTitle, $lastRevIsRedirect );
1224 $this->assertSame( $expectedSuccess, $success, 'Success assertion' );
1225 /**
1226 * updateRedirectOn explicitly updates the redirect table (and not the page table).
1227 * Most of core checks the page table for redirect status, so we have to be ugly and
1228 * assert a select from the table here.
1229 */
1230 $this->assertRedirectTableCountForPageId( $page->getId(), $expectedRowCount );
1231 }
1232
1233 private function assertRedirectTableCountForPageId( $pageId, $expected ) {
1234 $this->assertSelect(
1235 'redirect',
1236 'COUNT(*)',
1237 [ 'rd_from' => $pageId ],
1238 [ [ strval( $expected ) ] ]
1239 );
1240 }
1241
1242 /**
1243 * @covers WikiPage::insertRedirectEntry
1244 */
1245 public function testInsertRedirectEntry_insertsRedirectEntry() {
1246 $page = $this->createPage( Title::newFromText( __METHOD__ ), 'A' );
1247 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1248
1249 $targetTitle = Title::newFromText( 'SomeTarget#Frag' );
1250 $targetTitle->mInterwiki = 'eninter';
1251 $page->insertRedirectEntry( $targetTitle, null );
1252
1253 $this->assertSelect(
1254 'redirect',
1255 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1256 [ 'rd_from' => $page->getId() ],
1257 [ [
1258 strval( $page->getId() ),
1259 strval( $targetTitle->getNamespace() ),
1260 strval( $targetTitle->getDBkey() ),
1261 strval( $targetTitle->getFragment() ),
1262 strval( $targetTitle->getInterwiki() ),
1263 ] ]
1264 );
1265 }
1266
1267 /**
1268 * @covers WikiPage::insertRedirectEntry
1269 */
1270 public function testInsertRedirectEntry_insertsRedirectEntryWithPageLatest() {
1271 $page = $this->createPage( Title::newFromText( __METHOD__ ), 'A' );
1272 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1273
1274 $targetTitle = Title::newFromText( 'SomeTarget#Frag' );
1275 $targetTitle->mInterwiki = 'eninter';
1276 $page->insertRedirectEntry( $targetTitle, $page->getLatest() );
1277
1278 $this->assertSelect(
1279 'redirect',
1280 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1281 [ 'rd_from' => $page->getId() ],
1282 [ [
1283 strval( $page->getId() ),
1284 strval( $targetTitle->getNamespace() ),
1285 strval( $targetTitle->getDBkey() ),
1286 strval( $targetTitle->getFragment() ),
1287 strval( $targetTitle->getInterwiki() ),
1288 ] ]
1289 );
1290 }
1291
1292 /**
1293 * @covers WikiPage::insertRedirectEntry
1294 */
1295 public function testInsertRedirectEntry_doesNotInsertIfPageLatestIncorrect() {
1296 $page = $this->createPage( Title::newFromText( __METHOD__ ), 'A' );
1297 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1298
1299 $targetTitle = Title::newFromText( 'SomeTarget#Frag' );
1300 $targetTitle->mInterwiki = 'eninter';
1301 $page->insertRedirectEntry( $targetTitle, 215251 );
1302
1303 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1304 }
1305
1306 private function getRow( array $overrides = [] ) {
1307 $row = [
1308 'page_id' => '44',
1309 'page_len' => '76',
1310 'page_is_redirect' => '1',
1311 'page_latest' => '99',
1312 'page_namespace' => '3',
1313 'page_title' => 'JaJaTitle',
1314 'page_restrictions' => 'edit=autoconfirmed,sysop:move=sysop',
1315 'page_touched' => '20120101020202',
1316 'page_links_updated' => '20140101020202',
1317 ];
1318 foreach ( $overrides as $key => $value ) {
1319 $row[$key] = $value;
1320 }
1321 return (object)$row;
1322 }
1323
1324 public function provideNewFromRowSuccess() {
1325 yield 'basic row' => [
1326 $this->getRow(),
1327 function ( WikiPage $wikiPage, self $test ) {
1328 $test->assertSame( 44, $wikiPage->getId() );
1329 $test->assertSame( 76, $wikiPage->getTitle()->getLength() );
1330 $test->assertTrue( $wikiPage->isRedirect() );
1331 $test->assertSame( 99, $wikiPage->getLatest() );
1332 $test->assertSame( 3, $wikiPage->getTitle()->getNamespace() );
1333 $test->assertSame( 'JaJaTitle', $wikiPage->getTitle()->getDBkey() );
1334 $test->assertSame(
1335 [
1336 'edit' => [ 'autoconfirmed', 'sysop' ],
1337 'move' => [ 'sysop' ],
1338 ],
1339 $wikiPage->getTitle()->getAllRestrictions()
1340 );
1341 $test->assertSame( '20120101020202', $wikiPage->getTouched() );
1342 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() );
1343 }
1344 ];
1345 yield 'different timestamp formats' => [
1346 $this->getRow( [
1347 'page_touched' => '2012-01-01 02:02:02',
1348 'page_links_updated' => '2014-01-01 02:02:02',
1349 ] ),
1350 function ( WikiPage $wikiPage, self $test ) {
1351 $test->assertSame( '20120101020202', $wikiPage->getTouched() );
1352 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() );
1353 }
1354 ];
1355 yield 'no restrictions' => [
1356 $this->getRow( [
1357 'page_restrictions' => '',
1358 ] ),
1359 function ( WikiPage $wikiPage, self $test ) {
1360 $test->assertSame(
1361 [
1362 'edit' => [],
1363 'move' => [],
1364 ],
1365 $wikiPage->getTitle()->getAllRestrictions()
1366 );
1367 }
1368 ];
1369 yield 'not redirect' => [
1370 $this->getRow( [
1371 'page_is_redirect' => '0',
1372 ] ),
1373 function ( WikiPage $wikiPage, self $test ) {
1374 $test->assertFalse( $wikiPage->isRedirect() );
1375 }
1376 ];
1377 }
1378
1379 /**
1380 * @covers WikiPage::newFromRow
1381 * @covers WikiPage::loadFromRow
1382 * @dataProvider provideNewFromRowSuccess
1383 *
1384 * @param object $row
1385 * @param callable $assertions
1386 */
1387 public function testNewFromRow( $row, $assertions ) {
1388 $page = WikiPage::newFromRow( $row, 'fromdb' );
1389 $assertions( $page, $this );
1390 }
1391
1392 public function provideTestNewFromId_returnsNullOnBadPageId() {
1393 yield[ 0 ];
1394 yield[ -11 ];
1395 }
1396
1397 /**
1398 * @covers WikiPage::newFromID
1399 * @dataProvider provideTestNewFromId_returnsNullOnBadPageId
1400 */
1401 public function testNewFromId_returnsNullOnBadPageId( $pageId ) {
1402 $this->assertNull( WikiPage::newFromID( $pageId ) );
1403 }
1404
1405 /**
1406 * @covers WikiPage::newFromID
1407 */
1408 public function testNewFromId_appearsToFetchCorrectRow() {
1409 $createdPage = $this->createPage( __METHOD__, 'Xsfaij09' );
1410 $fetchedPage = WikiPage::newFromID( $createdPage->getId() );
1411 $this->assertSame( $createdPage->getId(), $fetchedPage->getId() );
1412 $this->assertEquals(
1413 $createdPage->getContent()->getNativeData(),
1414 $fetchedPage->getContent()->getNativeData()
1415 );
1416 }
1417
1418 /**
1419 * @covers WikiPage::newFromID
1420 */
1421 public function testNewFromId_returnsNullOnNonExistingId() {
1422 $this->assertNull( WikiPage::newFromID( 73574757437437743743 ) );
1423 }
1424
1425 public function provideTestInsertProtectNullRevision() {
1426 // @codingStandardsIgnoreStart Generic.Files.LineLength
1427 yield [
1428 'goat-message-key',
1429 [ 'edit' => 'sysop' ],
1430 [ 'edit' => '20200101040404' ],
1431 false,
1432 'Goat Reason',
1433 true,
1434 '(goat-message-key: WikiPageDbTestBase::testInsertProtectNullRevision, UTSysop)(colon-separator)Goat Reason(word-separator)(parentheses: (protect-summary-desc: (restriction-edit), (protect-level-sysop), (protect-expiring: 04:04, 1 (january) 2020, 1 (january) 2020, 04:04)))'
1435 ];
1436 yield [
1437 'goat-key',
1438 [ 'edit' => 'sysop', 'move' => 'something' ],
1439 [ 'edit' => '20200101040404', 'move' => '20210101050505' ],
1440 false,
1441 'Goat Goat',
1442 true,
1443 '(goat-key: WikiPageDbTestBase::testInsertProtectNullRevision, UTSysop)(colon-separator)Goat Goat(word-separator)(parentheses: (protect-summary-desc: (restriction-edit), (protect-level-sysop), (protect-expiring: 04:04, 1 (january) 2020, 1 (january) 2020, 04:04))(word-separator)(protect-summary-desc: (restriction-move), (protect-level-something), (protect-expiring: 05:05, 1 (january) 2021, 1 (january) 2021, 05:05)))'
1444 ];
1445 // @codingStandardsIgnoreEnd Generic.Files.LineLength
1446 }
1447
1448 /**
1449 * @dataProvider provideTestInsertProtectNullRevision
1450 * @covers WikiPage::insertProtectNullRevision
1451 * @covers WikiPage::protectDescription
1452 *
1453 * @param string $revCommentMsg
1454 * @param array $limit
1455 * @param array $expiry
1456 * @param bool $cascade
1457 * @param string $reason
1458 * @param bool|null $user true if the test sysop should be used, or null
1459 * @param string $expectedComment
1460 */
1461 public function testInsertProtectNullRevision(
1462 $revCommentMsg,
1463 array $limit,
1464 array $expiry,
1465 $cascade,
1466 $reason,
1467 $user,
1468 $expectedComment
1469 ) {
1470 $this->setContentLang( 'qqx' );
1471
1472 $page = $this->createPage( __METHOD__, 'Goat' );
1473
1474 $user = $user === null ? $user : $this->getTestSysop()->getUser();
1475
1476 $result = $page->insertProtectNullRevision(
1477 $revCommentMsg,
1478 $limit,
1479 $expiry,
1480 $cascade,
1481 $reason,
1482 $user
1483 );
1484
1485 $this->assertTrue( $result instanceof Revision );
1486 $this->assertSame( $expectedComment, $result->getComment( Revision::RAW ) );
1487 }
1488
1489 /**
1490 * @covers WikiPage::updateRevisionOn
1491 */
1492 public function testUpdateRevisionOn_existingPage() {
1493 $user = $this->getTestSysop()->getUser();
1494 $page = $this->createPage( __METHOD__, 'StartText' );
1495
1496 $revision = new Revision(
1497 [
1498 'id' => 9989,
1499 'page' => $page->getId(),
1500 'title' => $page->getTitle(),
1501 'comment' => __METHOD__,
1502 'minor_edit' => true,
1503 'text' => __METHOD__ . '-text',
1504 'len' => strlen( __METHOD__ . '-text' ),
1505 'user' => $user->getId(),
1506 'user_text' => $user->getName(),
1507 'timestamp' => '20170707040404',
1508 'content_model' => CONTENT_MODEL_WIKITEXT,
1509 'content_format' => CONTENT_FORMAT_WIKITEXT,
1510 ]
1511 );
1512
1513 $result = $page->updateRevisionOn( $this->db, $revision );
1514 $this->assertTrue( $result );
1515 $this->assertSame( 9989, $page->getLatest() );
1516 $this->assertEquals( $revision, $page->getRevision() );
1517 }
1518
1519 /**
1520 * @covers WikiPage::updateRevisionOn
1521 */
1522 public function testUpdateRevisionOn_NonExistingPage() {
1523 $user = $this->getTestSysop()->getUser();
1524 $page = $this->createPage( __METHOD__, 'StartText' );
1525 $page->doDeleteArticle( 'reason' );
1526
1527 $revision = new Revision(
1528 [
1529 'id' => 9989,
1530 'page' => $page->getId(),
1531 'title' => $page->getTitle(),
1532 'comment' => __METHOD__,
1533 'minor_edit' => true,
1534 'text' => __METHOD__ . '-text',
1535 'len' => strlen( __METHOD__ . '-text' ),
1536 'user' => $user->getId(),
1537 'user_text' => $user->getName(),
1538 'timestamp' => '20170707040404',
1539 'content_model' => CONTENT_MODEL_WIKITEXT,
1540 'content_format' => CONTENT_FORMAT_WIKITEXT,
1541 ]
1542 );
1543
1544 $result = $page->updateRevisionOn( $this->db, $revision );
1545 $this->assertFalse( $result );
1546 }
1547
1548 /**
1549 * @covers WikiPage::updateIfNewerOn
1550 */
1551 public function testUpdateIfNewerOn_olderRevision() {
1552 $user = $this->getTestSysop()->getUser();
1553 $page = $this->createPage( __METHOD__, 'StartText' );
1554 $initialRevision = $page->getRevision();
1555
1556 $olderTimeStamp = wfTimestamp(
1557 TS_MW,
1558 wfTimestamp( TS_UNIX, $initialRevision->getTimestamp() ) - 1
1559 );
1560
1561 $olderRevison = new Revision(
1562 [
1563 'id' => 9989,
1564 'page' => $page->getId(),
1565 'title' => $page->getTitle(),
1566 'comment' => __METHOD__,
1567 'minor_edit' => true,
1568 'text' => __METHOD__ . '-text',
1569 'len' => strlen( __METHOD__ . '-text' ),
1570 'user' => $user->getId(),
1571 'user_text' => $user->getName(),
1572 'timestamp' => $olderTimeStamp,
1573 'content_model' => CONTENT_MODEL_WIKITEXT,
1574 'content_format' => CONTENT_FORMAT_WIKITEXT,
1575 ]
1576 );
1577
1578 $result = $page->updateIfNewerOn( $this->db, $olderRevison );
1579 $this->assertFalse( $result );
1580 }
1581
1582 /**
1583 * @covers WikiPage::updateIfNewerOn
1584 */
1585 public function testUpdateIfNewerOn_newerRevision() {
1586 $user = $this->getTestSysop()->getUser();
1587 $page = $this->createPage( __METHOD__, 'StartText' );
1588 $initialRevision = $page->getRevision();
1589
1590 $newerTimeStamp = wfTimestamp(
1591 TS_MW,
1592 wfTimestamp( TS_UNIX, $initialRevision->getTimestamp() ) + 1
1593 );
1594
1595 $newerRevision = new Revision(
1596 [
1597 'id' => 9989,
1598 'page' => $page->getId(),
1599 'title' => $page->getTitle(),
1600 'comment' => __METHOD__,
1601 'minor_edit' => true,
1602 'text' => __METHOD__ . '-text',
1603 'len' => strlen( __METHOD__ . '-text' ),
1604 'user' => $user->getId(),
1605 'user_text' => $user->getName(),
1606 'timestamp' => $newerTimeStamp,
1607 'content_model' => CONTENT_MODEL_WIKITEXT,
1608 'content_format' => CONTENT_FORMAT_WIKITEXT,
1609 ]
1610 );
1611 $result = $page->updateIfNewerOn( $this->db, $newerRevision );
1612 $this->assertTrue( $result );
1613 }
1614
1615 /**
1616 * @covers WikiPage::insertOn
1617 */
1618 public function testInsertOn() {
1619 $title = Title::newFromText( __METHOD__ );
1620 $page = new WikiPage( $title );
1621
1622 $startTimeStamp = wfTimestampNow();
1623 $result = $page->insertOn( $this->db );
1624 $endTimeStamp = wfTimestampNow();
1625
1626 $this->assertInternalType( 'int', $result );
1627 $this->assertTrue( $result > 0 );
1628
1629 $condition = [ 'page_id' => $result ];
1630
1631 // Check the default fields have been filled
1632 $this->assertSelect(
1633 'page',
1634 [
1635 'page_namespace',
1636 'page_title',
1637 'page_restrictions',
1638 'page_is_redirect',
1639 'page_is_new',
1640 'page_latest',
1641 'page_len',
1642 ],
1643 $condition,
1644 [ [
1645 '0',
1646 __METHOD__,
1647 '',
1648 '0',
1649 '1',
1650 '0',
1651 '0',
1652 ] ]
1653 );
1654
1655 // Check the page_random field has been filled
1656 $pageRandom = $this->db->selectField( 'page', 'page_random', $condition );
1657 $this->assertTrue( (float)$pageRandom < 1 && (float)$pageRandom > 0 );
1658
1659 // Assert the touched timestamp in the DB is roughly when we inserted the page
1660 $pageTouched = $this->db->selectField( 'page', 'page_touched', $condition );
1661 $this->assertTrue(
1662 wfTimestamp( TS_UNIX, $startTimeStamp )
1663 <= wfTimestamp( TS_UNIX, $pageTouched )
1664 );
1665 $this->assertTrue(
1666 wfTimestamp( TS_UNIX, $endTimeStamp )
1667 >= wfTimestamp( TS_UNIX, $pageTouched )
1668 );
1669
1670 // Try inserting the same page again and checking the result is false (no change)
1671 $result = $page->insertOn( $this->db );
1672 $this->assertFalse( $result );
1673 }
1674
1675 /**
1676 * @covers WikiPage::insertOn
1677 */
1678 public function testInsertOn_idSpecified() {
1679 $title = Title::newFromText( __METHOD__ );
1680 $page = new WikiPage( $title );
1681 $id = 1478952189;
1682
1683 $result = $page->insertOn( $this->db, $id );
1684
1685 $this->assertSame( $id, $result );
1686
1687 $condition = [ 'page_id' => $result ];
1688
1689 // Check there is actually a row in the db
1690 $this->assertSelect(
1691 'page',
1692 [ 'page_title' ],
1693 $condition,
1694 [ [ __METHOD__ ] ]
1695 );
1696 }
1697
1698 }