Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / page / WikiPageTest.php
blobe7782704aaf23dcedb916c79a5f8e2a5aabd9e51
1 <?php
3 /**
4 * @group ContentHandler
5 * @group Database
6 * ^--- important, causes temporary tables to be used instead of the real database
7 * @group medium
8 */
9 class WikiPageTest extends MediaWikiLangTestCase {
11 protected $pages_to_delete;
13 function __construct( $name = null, array $data = [], $dataName = '' ) {
14 parent::__construct( $name, $data, $dataName );
16 $this->tablesUsed = array_merge(
17 $this->tablesUsed,
18 [ 'page',
19 'revision',
20 'text',
22 'recentchanges',
23 'logging',
25 'page_props',
26 'pagelinks',
27 'categorylinks',
28 'langlinks',
29 'externallinks',
30 'imagelinks',
31 'templatelinks',
32 'iwlinks' ] );
35 protected function setUp() {
36 parent::setUp();
37 $this->pages_to_delete = [];
39 LinkCache::singleton()->clear(); # avoid cached redirect status, etc
42 protected function tearDown() {
43 foreach ( $this->pages_to_delete as $p ) {
44 /* @var $p WikiPage */
46 try {
47 if ( $p->exists() ) {
48 $p->doDeleteArticle( "testing done." );
50 } catch ( MWException $ex ) {
51 // fail silently
54 parent::tearDown();
57 /**
58 * @param Title|string $title
59 * @param string|null $model
60 * @return WikiPage
62 protected function newPage( $title, $model = null ) {
63 if ( is_string( $title ) ) {
64 $ns = $this->getDefaultWikitextNS();
65 $title = Title::newFromText( $title, $ns );
68 $p = new WikiPage( $title );
70 $this->pages_to_delete[] = $p;
72 return $p;
75 /**
76 * @param string|Title|WikiPage $page
77 * @param string $text
78 * @param int $model
80 * @return WikiPage
82 protected function createPage( $page, $text, $model = null ) {
83 if ( is_string( $page ) || $page instanceof Title ) {
84 $page = $this->newPage( $page, $model );
87 $content = ContentHandler::makeContent( $text, $page->getTitle(), $model );
88 $page->doEditContent( $content, "testing", EDIT_NEW );
90 return $page;
93 /**
94 * @covers WikiPage::doEditContent
95 * @covers WikiPage::doModify
96 * @covers WikiPage::doCreate
97 * @covers WikiPage::doEditUpdates
99 public function testDoEditContent() {
100 $page = $this->newPage( "WikiPageTest_testDoEditContent" );
101 $title = $page->getTitle();
103 $content = ContentHandler::makeContent(
104 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
105 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
106 $title,
107 CONTENT_MODEL_WIKITEXT
110 $page->doEditContent( $content, "[[testing]] 1" );
112 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
113 $this->assertTrue( $page->getId() > 0, "WikiPage should have new page id" );
114 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
115 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
117 $id = $page->getId();
119 # ------------------------
120 $dbr = wfGetDB( DB_SLAVE );
121 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
122 $n = $res->numRows();
123 $res->free();
125 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
127 # ------------------------
128 $page = new WikiPage( $title );
130 $retrieved = $page->getContent();
131 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
133 # ------------------------
134 $content = ContentHandler::makeContent(
135 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
136 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est.",
137 $title,
138 CONTENT_MODEL_WIKITEXT
141 $page->doEditContent( $content, "testing 2" );
143 # ------------------------
144 $page = new WikiPage( $title );
146 $retrieved = $page->getContent();
147 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
149 # ------------------------
150 $dbr = wfGetDB( DB_SLAVE );
151 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
152 $n = $res->numRows();
153 $res->free();
155 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
159 * @covers WikiPage::doDeleteArticle
161 public function testDoDeleteArticle() {
162 $page = $this->createPage(
163 "WikiPageTest_testDoDeleteArticle",
164 "[[original text]] foo",
165 CONTENT_MODEL_WIKITEXT
167 $id = $page->getId();
169 $page->doDeleteArticle( "testing deletion" );
171 $this->assertFalse(
172 $page->getTitle()->getArticleID() > 0,
173 "Title object should now have page id 0"
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"
180 $this->assertNull(
181 $page->getContent(),
182 "WikiPage::getContent should return null after page was deleted"
185 $t = Title::newFromText( $page->getTitle()->getPrefixedText() );
186 $this->assertFalse(
187 $t->exists(),
188 "Title::exists should return false after page was deleted"
191 // Run the job queue
192 JobQueueGroup::destroySingletons();
193 $jobs = new RunJobs;
194 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
195 $jobs->execute();
197 # ------------------------
198 $dbr = wfGetDB( DB_SLAVE );
199 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
200 $n = $res->numRows();
201 $res->free();
203 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
207 * @covers WikiPage::doDeleteUpdates
209 public function testDoDeleteUpdates() {
210 $page = $this->createPage(
211 "WikiPageTest_testDoDeleteArticle",
212 "[[original text]] foo",
213 CONTENT_MODEL_WIKITEXT
215 $id = $page->getId();
217 // Similar to MovePage logic
218 wfGetDB( DB_MASTER )->delete( 'page', [ 'page_id' => $id ], __METHOD__ );
219 $page->doDeleteUpdates( $id );
221 // Run the job queue
222 JobQueueGroup::destroySingletons();
223 $jobs = new RunJobs;
224 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
225 $jobs->execute();
227 # ------------------------
228 $dbr = wfGetDB( DB_SLAVE );
229 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
230 $n = $res->numRows();
231 $res->free();
233 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
237 * @covers WikiPage::getRevision
239 public function testGetRevision() {
240 $page = $this->newPage( "WikiPageTest_testGetRevision" );
242 $rev = $page->getRevision();
243 $this->assertNull( $rev );
245 # -----------------
246 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
248 $rev = $page->getRevision();
250 $this->assertEquals( $page->getLatest(), $rev->getId() );
251 $this->assertEquals( "some text", $rev->getContent()->getNativeData() );
255 * @covers WikiPage::getContent
257 public function testGetContent() {
258 $page = $this->newPage( "WikiPageTest_testGetContent" );
260 $content = $page->getContent();
261 $this->assertNull( $content );
263 # -----------------
264 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
266 $content = $page->getContent();
267 $this->assertEquals( "some text", $content->getNativeData() );
271 * @covers WikiPage::getContentModel
273 public function testGetContentModel() {
274 global $wgContentHandlerUseDB;
276 if ( !$wgContentHandlerUseDB ) {
277 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
280 $page = $this->createPage(
281 "WikiPageTest_testGetContentModel",
282 "some text",
283 CONTENT_MODEL_JAVASCRIPT
286 $page = new WikiPage( $page->getTitle() );
287 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $page->getContentModel() );
291 * @covers WikiPage::getContentHandler
293 public function testGetContentHandler() {
294 global $wgContentHandlerUseDB;
296 if ( !$wgContentHandlerUseDB ) {
297 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
300 $page = $this->createPage(
301 "WikiPageTest_testGetContentHandler",
302 "some text",
303 CONTENT_MODEL_JAVASCRIPT
306 $page = new WikiPage( $page->getTitle() );
307 $this->assertEquals( 'JavaScriptContentHandler', get_class( $page->getContentHandler() ) );
311 * @covers WikiPage::exists
313 public function testExists() {
314 $page = $this->newPage( "WikiPageTest_testExists" );
315 $this->assertFalse( $page->exists() );
317 # -----------------
318 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
319 $this->assertTrue( $page->exists() );
321 $page = new WikiPage( $page->getTitle() );
322 $this->assertTrue( $page->exists() );
324 # -----------------
325 $page->doDeleteArticle( "done testing" );
326 $this->assertFalse( $page->exists() );
328 $page = new WikiPage( $page->getTitle() );
329 $this->assertFalse( $page->exists() );
332 public static function provideHasViewableContent() {
333 return [
334 [ 'WikiPageTest_testHasViewableContent', false, true ],
335 [ 'Special:WikiPageTest_testHasViewableContent', false ],
336 [ 'MediaWiki:WikiPageTest_testHasViewableContent', false ],
337 [ 'Special:Userlogin', true ],
338 [ 'MediaWiki:help', true ],
343 * @dataProvider provideHasViewableContent
344 * @covers WikiPage::hasViewableContent
346 public function testHasViewableContent( $title, $viewable, $create = false ) {
347 $page = $this->newPage( $title );
348 $this->assertEquals( $viewable, $page->hasViewableContent() );
350 if ( $create ) {
351 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
352 $this->assertTrue( $page->hasViewableContent() );
354 $page = new WikiPage( $page->getTitle() );
355 $this->assertTrue( $page->hasViewableContent() );
359 public static function provideGetRedirectTarget() {
360 return [
361 [ 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT, "hello world", null ],
363 'WikiPageTest_testGetRedirectTarget_2',
364 CONTENT_MODEL_WIKITEXT,
365 "#REDIRECT [[hello world]]",
366 "Hello world"
372 * @dataProvider provideGetRedirectTarget
373 * @covers WikiPage::getRedirectTarget
375 public function testGetRedirectTarget( $title, $model, $text, $target ) {
376 $this->setMwGlobals( [
377 'wgCapitalLinks' => true,
378 ] );
380 $page = $this->createPage( $title, $text, $model );
382 # sanity check, because this test seems to fail for no reason for some people.
383 $c = $page->getContent();
384 $this->assertEquals( 'WikitextContent', get_class( $c ) );
386 # now, test the actual redirect
387 $t = $page->getRedirectTarget();
388 $this->assertEquals( $target, is_null( $t ) ? null : $t->getPrefixedText() );
392 * @dataProvider provideGetRedirectTarget
393 * @covers WikiPage::isRedirect
395 public function testIsRedirect( $title, $model, $text, $target ) {
396 $page = $this->createPage( $title, $text, $model );
397 $this->assertEquals( !is_null( $target ), $page->isRedirect() );
400 public static function provideIsCountable() {
401 return [
403 // any
404 [ 'WikiPageTest_testIsCountable',
405 CONTENT_MODEL_WIKITEXT,
407 'any',
408 true
410 [ 'WikiPageTest_testIsCountable',
411 CONTENT_MODEL_WIKITEXT,
412 'Foo',
413 'any',
414 true
417 // comma
418 [ 'WikiPageTest_testIsCountable',
419 CONTENT_MODEL_WIKITEXT,
420 'Foo',
421 'comma',
422 false
424 [ 'WikiPageTest_testIsCountable',
425 CONTENT_MODEL_WIKITEXT,
426 'Foo, bar',
427 'comma',
428 true
431 // link
432 [ 'WikiPageTest_testIsCountable',
433 CONTENT_MODEL_WIKITEXT,
434 'Foo',
435 'link',
436 false
438 [ 'WikiPageTest_testIsCountable',
439 CONTENT_MODEL_WIKITEXT,
440 'Foo [[bar]]',
441 'link',
442 true
445 // redirects
446 [ 'WikiPageTest_testIsCountable',
447 CONTENT_MODEL_WIKITEXT,
448 '#REDIRECT [[bar]]',
449 'any',
450 false
452 [ 'WikiPageTest_testIsCountable',
453 CONTENT_MODEL_WIKITEXT,
454 '#REDIRECT [[bar]]',
455 'comma',
456 false
458 [ 'WikiPageTest_testIsCountable',
459 CONTENT_MODEL_WIKITEXT,
460 '#REDIRECT [[bar]]',
461 'link',
462 false
465 // not a content namespace
466 [ 'Talk:WikiPageTest_testIsCountable',
467 CONTENT_MODEL_WIKITEXT,
468 'Foo',
469 'any',
470 false
472 [ 'Talk:WikiPageTest_testIsCountable',
473 CONTENT_MODEL_WIKITEXT,
474 'Foo, bar',
475 'comma',
476 false
478 [ 'Talk:WikiPageTest_testIsCountable',
479 CONTENT_MODEL_WIKITEXT,
480 'Foo [[bar]]',
481 'link',
482 false
485 // not a content namespace, different model
486 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
487 null,
488 'Foo',
489 'any',
490 false
492 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
493 null,
494 'Foo, bar',
495 'comma',
496 false
498 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
499 null,
500 'Foo [[bar]]',
501 'link',
502 false
508 * @dataProvider provideIsCountable
509 * @covers WikiPage::isCountable
511 public function testIsCountable( $title, $model, $text, $mode, $expected ) {
512 global $wgContentHandlerUseDB;
514 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
516 $title = Title::newFromText( $title );
518 if ( !$wgContentHandlerUseDB
519 && $model
520 && ContentHandler::getDefaultModelFor( $title ) != $model
522 $this->markTestSkipped( "Can not use non-default content model $model for "
523 . $title->getPrefixedDBkey() . " with \$wgContentHandlerUseDB disabled." );
526 $page = $this->createPage( $title, $text, $model );
528 $editInfo = $page->prepareContentForEdit( $page->getContent() );
530 $v = $page->isCountable();
531 $w = $page->isCountable( $editInfo );
533 $this->assertEquals(
534 $expected,
536 "isCountable( null ) returned unexpected value " . var_export( $v, true )
537 . " instead of " . var_export( $expected, true )
538 . " in mode `$mode` for text \"$text\""
541 $this->assertEquals(
542 $expected,
544 "isCountable( \$editInfo ) returned unexpected value " . var_export( $v, true )
545 . " instead of " . var_export( $expected, true )
546 . " in mode `$mode` for text \"$text\""
550 public static function provideGetParserOutput() {
551 return [
552 [ CONTENT_MODEL_WIKITEXT, "hello ''world''\n", "<p>hello <i>world</i></p>" ],
553 // @todo more...?
558 * @dataProvider provideGetParserOutput
559 * @covers WikiPage::getParserOutput
561 public function testGetParserOutput( $model, $text, $expectedHtml ) {
562 $page = $this->createPage( 'WikiPageTest_testGetParserOutput', $text, $model );
564 $opt = $page->makeParserOptions( 'canonical' );
565 $po = $page->getParserOutput( $opt );
566 $text = $po->getText();
568 $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments
569 $text = preg_replace( '!\s*(</p>)!sm', '\1', $text ); # don't let tidy confuse us
571 $this->assertEquals( $expectedHtml, $text );
573 return $po;
577 * @covers WikiPage::getParserOutput
579 public function testGetParserOutput_nonexisting() {
580 static $count = 0;
581 $count++;
583 $page = new WikiPage( new Title( "WikiPageTest_testGetParserOutput_nonexisting_$count" ) );
585 $opt = new ParserOptions();
586 $po = $page->getParserOutput( $opt );
588 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." );
592 * @covers WikiPage::getParserOutput
594 public function testGetParserOutput_badrev() {
595 $page = $this->createPage( 'WikiPageTest_testGetParserOutput', "dummy", CONTENT_MODEL_WIKITEXT );
597 $opt = new ParserOptions();
598 $po = $page->getParserOutput( $opt, $page->getLatest() + 1234 );
600 // @todo would be neat to also test deleted revision
602 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." );
605 public static $sections =
607 "Intro
609 == stuff ==
610 hello world
612 == test ==
613 just a test
615 == foo ==
616 more stuff
619 public function dataReplaceSection() {
620 // NOTE: assume the Help namespace to contain wikitext
621 return [
622 [ 'Help:WikiPageTest_testReplaceSection',
623 CONTENT_MODEL_WIKITEXT,
624 WikiPageTest::$sections,
625 "0",
626 "No more",
627 null,
628 trim( preg_replace( '/^Intro/sm', 'No more', WikiPageTest::$sections ) )
630 [ 'Help:WikiPageTest_testReplaceSection',
631 CONTENT_MODEL_WIKITEXT,
632 WikiPageTest::$sections,
634 "No more",
635 null,
636 "No more"
638 [ 'Help:WikiPageTest_testReplaceSection',
639 CONTENT_MODEL_WIKITEXT,
640 WikiPageTest::$sections,
641 "2",
642 "== TEST ==\nmore fun",
643 null,
644 trim( preg_replace( '/^== test ==.*== foo ==/sm',
645 "== TEST ==\nmore fun\n\n== foo ==",
646 WikiPageTest::$sections ) )
648 [ 'Help:WikiPageTest_testReplaceSection',
649 CONTENT_MODEL_WIKITEXT,
650 WikiPageTest::$sections,
651 "8",
652 "No more",
653 null,
654 trim( WikiPageTest::$sections )
656 [ 'Help:WikiPageTest_testReplaceSection',
657 CONTENT_MODEL_WIKITEXT,
658 WikiPageTest::$sections,
659 "new",
660 "No more",
661 "New",
662 trim( WikiPageTest::$sections ) . "\n\n== New ==\n\nNo more"
668 * @dataProvider dataReplaceSection
669 * @covers WikiPage::replaceSectionContent
671 public function testReplaceSectionContent( $title, $model, $text, $section,
672 $with, $sectionTitle, $expected
674 $page = $this->createPage( $title, $text, $model );
676 $content = ContentHandler::makeContent( $with, $page->getTitle(), $page->getContentModel() );
677 $c = $page->replaceSectionContent( $section, $content, $sectionTitle );
679 $this->assertEquals( $expected, is_null( $c ) ? null : trim( $c->getNativeData() ) );
683 * @dataProvider dataReplaceSection
684 * @covers WikiPage::replaceSectionAtRev
686 public function testReplaceSectionAtRev( $title, $model, $text, $section,
687 $with, $sectionTitle, $expected
689 $page = $this->createPage( $title, $text, $model );
690 $baseRevId = $page->getLatest();
692 $content = ContentHandler::makeContent( $with, $page->getTitle(), $page->getContentModel() );
693 $c = $page->replaceSectionAtRev( $section, $content, $sectionTitle, $baseRevId );
695 $this->assertEquals( $expected, is_null( $c ) ? null : trim( $c->getNativeData() ) );
698 /* @todo FIXME: fix this!
699 public function testGetUndoText() {
700 $this->markTestSkippedIfNoDiff3();
702 $text = "one";
703 $page = $this->createPage( "WikiPageTest_testGetUndoText", $text );
704 $rev1 = $page->getRevision();
706 $text .= "\n\ntwo";
707 $page->doEditContent(
708 ContentHandler::makeContent( $text, $page->getTitle() ),
709 "adding section two"
711 $rev2 = $page->getRevision();
713 $text .= "\n\nthree";
714 $page->doEditContent(
715 ContentHandler::makeContent( $text, $page->getTitle() ),
716 "adding section three"
718 $rev3 = $page->getRevision();
720 $text .= "\n\nfour";
721 $page->doEditContent(
722 ContentHandler::makeContent( $text, $page->getTitle() ),
723 "adding section four"
725 $rev4 = $page->getRevision();
727 $text .= "\n\nfive";
728 $page->doEditContent(
729 ContentHandler::makeContent( $text, $page->getTitle() ),
730 "adding section five"
732 $rev5 = $page->getRevision();
734 $text .= "\n\nsix";
735 $page->doEditContent(
736 ContentHandler::makeContent( $text, $page->getTitle() ),
737 "adding section six"
739 $rev6 = $page->getRevision();
741 $undo6 = $page->getUndoText( $rev6 );
742 if ( $undo6 === false ) $this->fail( "getUndoText failed for rev6" );
743 $this->assertEquals( "one\n\ntwo\n\nthree\n\nfour\n\nfive", $undo6 );
745 $undo3 = $page->getUndoText( $rev4, $rev2 );
746 if ( $undo3 === false ) $this->fail( "getUndoText failed for rev4..rev2" );
747 $this->assertEquals( "one\n\ntwo\n\nfive", $undo3 );
749 $undo2 = $page->getUndoText( $rev2 );
750 if ( $undo2 === false ) $this->fail( "getUndoText failed for rev2" );
751 $this->assertEquals( "one\n\nfive", $undo2 );
756 * @todo FIXME: this is a better rollback test than the one below, but it
757 * keeps failing in jenkins for some reason.
759 public function broken_testDoRollback() {
760 $admin = new User();
761 $admin->setName( "Admin" );
763 $text = "one";
764 $page = $this->newPage( "WikiPageTest_testDoRollback" );
765 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
766 "section one", EDIT_NEW, false, $admin );
768 $user1 = new User();
769 $user1->setName( "127.0.1.11" );
770 $text .= "\n\ntwo";
771 $page = new WikiPage( $page->getTitle() );
772 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
773 "adding section two", 0, false, $user1 );
775 $user2 = new User();
776 $user2->setName( "127.0.2.13" );
777 $text .= "\n\nthree";
778 $page = new WikiPage( $page->getTitle() );
779 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
780 "adding section three", 0, false, $user2 );
782 # we are having issues with doRollback spuriously failing. Apparently
783 # the last revision somehow goes missing or not committed under some
784 # circumstances. So, make sure the last revision has the right user name.
785 $dbr = wfGetDB( DB_SLAVE );
786 $this->assertEquals( 3, Revision::countByPageId( $dbr, $page->getId() ) );
788 $page = new WikiPage( $page->getTitle() );
789 $rev3 = $page->getRevision();
790 $this->assertEquals( '127.0.2.13', $rev3->getUserText() );
792 $rev2 = $rev3->getPrevious();
793 $this->assertEquals( '127.0.1.11', $rev2->getUserText() );
795 $rev1 = $rev2->getPrevious();
796 $this->assertEquals( 'Admin', $rev1->getUserText() );
798 # now, try the actual rollback
799 $admin->addGroup( "sysop" ); # XXX: make the test user a sysop...
800 $token = $admin->getEditToken(
801 [ $page->getTitle()->getPrefixedText(), $user2->getName() ],
802 null
804 $errors = $page->doRollback(
805 $user2->getName(),
806 "testing revert",
807 $token,
808 false,
809 $details,
810 $admin
813 if ( $errors ) {
814 $this->fail( "Rollback failed:\n" . print_r( $errors, true )
815 . ";\n" . print_r( $details, true ) );
818 $page = new WikiPage( $page->getTitle() );
819 $this->assertEquals( $rev2->getSha1(), $page->getRevision()->getSha1(),
820 "rollback did not revert to the correct revision" );
821 $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
825 * @todo FIXME: the above rollback test is better, but it keeps failing in jenkins for some reason.
826 * @covers WikiPage::doRollback
828 public function testDoRollback() {
829 $admin = new User();
830 $admin->setName( "Admin" );
832 $text = "one";
833 $page = $this->newPage( "WikiPageTest_testDoRollback" );
834 $page->doEditContent(
835 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
836 "section one",
837 EDIT_NEW,
838 false,
839 $admin
841 $rev1 = $page->getRevision();
843 $user1 = new User();
844 $user1->setName( "127.0.1.11" );
845 $text .= "\n\ntwo";
846 $page = new WikiPage( $page->getTitle() );
847 $page->doEditContent(
848 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
849 "adding section two",
851 false,
852 $user1
855 # now, try the rollback
856 $admin->addGroup( "sysop" ); # XXX: make the test user a sysop...
857 $token = $admin->getEditToken(
858 [ $page->getTitle()->getPrefixedText(), $user1->getName() ],
859 null
861 $errors = $page->doRollback(
862 $user1->getName(),
863 "testing revert",
864 $token,
865 false,
866 $details,
867 $admin
870 if ( $errors ) {
871 $this->fail( "Rollback failed:\n" . print_r( $errors, true )
872 . ";\n" . print_r( $details, true ) );
875 $page = new WikiPage( $page->getTitle() );
876 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
877 "rollback did not revert to the correct revision" );
878 $this->assertEquals( "one", $page->getContent()->getNativeData() );
882 * @covers WikiPage::doRollback
884 public function testDoRollbackFailureSameContent() {
885 $admin = new User();
886 $admin->setName( "Admin" );
887 $admin->addGroup( "sysop" ); # XXX: make the test user a sysop...
889 $text = "one";
890 $page = $this->newPage( "WikiPageTest_testDoRollback" );
891 $page->doEditContent(
892 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
893 "section one",
894 EDIT_NEW,
895 false,
896 $admin
898 $rev1 = $page->getRevision();
900 $user1 = new User();
901 $user1->setName( "127.0.1.11" );
902 $user1->addGroup( "sysop" ); # XXX: make the test user a sysop...
903 $text .= "\n\ntwo";
904 $page = new WikiPage( $page->getTitle() );
905 $page->doEditContent(
906 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
907 "adding section two",
909 false,
910 $user1
913 # now, do a the rollback from the same user was doing the edit before
914 $resultDetails = [];
915 $token = $user1->getEditToken(
916 [ $page->getTitle()->getPrefixedText(), $user1->getName() ],
917 null
919 $errors = $page->doRollback(
920 $user1->getName(),
921 "testing revert same user",
922 $token,
923 false,
924 $resultDetails,
925 $admin
928 $this->assertEquals( [], $errors, "Rollback failed same user" );
930 # now, try the rollback
931 $resultDetails = [];
932 $token = $admin->getEditToken(
933 [ $page->getTitle()->getPrefixedText(), $user1->getName() ],
934 null
936 $errors = $page->doRollback(
937 $user1->getName(),
938 "testing revert",
939 $token,
940 false,
941 $resultDetails,
942 $admin
945 $this->assertEquals( [ [ 'alreadyrolled', 'WikiPageTest testDoRollback',
946 '127.0.1.11', 'Admin' ] ], $errors, "Rollback not failed" );
948 $page = new WikiPage( $page->getTitle() );
949 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
950 "rollback did not revert to the correct revision" );
951 $this->assertEquals( "one", $page->getContent()->getNativeData() );
954 public static function provideGetAutoDeleteReason() {
955 return [
958 false,
959 false
964 [ "first edit", null ],
966 "/first edit.*only contributor/",
967 false
972 [ "first edit", null ],
973 [ "second edit", null ],
975 "/second edit.*only contributor/",
976 true
981 [ "first edit", "127.0.2.22" ],
982 [ "second edit", "127.0.3.33" ],
984 "/second edit/",
985 true
991 "first edit: "
992 . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
993 . " nonumy eirmod tempor invidunt ut labore et dolore magna "
994 . "aliquyam erat, sed diam voluptua. At vero eos et accusam "
995 . "et justo duo dolores et ea rebum. Stet clita kasd gubergren, "
996 . "no sea takimata sanctus est Lorem ipsum dolor sit amet.'",
997 null
1000 '/first edit:.*\.\.\."/',
1001 false
1006 [ "first edit", "127.0.2.22" ],
1007 [ "", "127.0.3.33" ],
1009 "/before blanking.*first edit/",
1010 true
1017 * @dataProvider provideGetAutoDeleteReason
1018 * @covers WikiPage::getAutoDeleteReason
1020 public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
1021 global $wgUser;
1023 // NOTE: assume Help namespace to contain wikitext
1024 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
1026 $c = 1;
1028 foreach ( $edits as $edit ) {
1029 $user = new User();
1031 if ( !empty( $edit[1] ) ) {
1032 $user->setName( $edit[1] );
1033 } else {
1034 $user = $wgUser;
1037 $content = ContentHandler::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
1039 $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW : 0, false, $user );
1041 $c += 1;
1044 $reason = $page->getAutoDeleteReason( $hasHistory );
1046 if ( is_bool( $expectedResult ) || is_null( $expectedResult ) ) {
1047 $this->assertEquals( $expectedResult, $reason );
1048 } else {
1049 $this->assertTrue( (bool)preg_match( $expectedResult, $reason ),
1050 "Autosummary didn't match expected pattern $expectedResult: $reason" );
1053 $this->assertEquals( $expectedHistory, $hasHistory,
1054 "expected \$hasHistory to be " . var_export( $expectedHistory, true ) );
1056 $page->doDeleteArticle( "done" );
1059 public static function providePreSaveTransform() {
1060 return [
1061 [ 'hello this is ~~~',
1062 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
1064 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1065 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1071 * @covers WikiPage::factory
1073 public function testWikiPageFactory() {
1074 $title = Title::makeTitle( NS_FILE, 'Someimage.png' );
1075 $page = WikiPage::factory( $title );
1076 $this->assertEquals( 'WikiFilePage', get_class( $page ) );
1078 $title = Title::makeTitle( NS_CATEGORY, 'SomeCategory' );
1079 $page = WikiPage::factory( $title );
1080 $this->assertEquals( 'WikiCategoryPage', get_class( $page ) );
1082 $title = Title::makeTitle( NS_MAIN, 'SomePage' );
1083 $page = WikiPage::factory( $title );
1084 $this->assertEquals( 'WikiPage', get_class( $page ) );