4 * @group ContentHandler
6 * ^--- important, causes temporary tables to be used instead of the real database
9 class WikiPageTest
extends MediaWikiLangTestCase
{
11 protected $pages_to_delete;
13 function __construct( $name = null, array $data = array(), $dataName = '' ) {
14 parent
::__construct( $name, $data, $dataName );
16 $this->tablesUsed
= array_merge(
35 protected function setUp() {
37 $this->pages_to_delete
= array();
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 */
48 $p->doDeleteArticle( "testing done." );
50 } catch ( MWException
$ex ) {
58 * @param Title|string $title
59 * @param string|null $model
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;
76 * @param string|Title|WikiPage $page
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
);
94 * @covers WikiPage::doEditContent
96 public function testDoEditContent() {
97 $page = $this->newPage( "WikiPageTest_testDoEditContent" );
98 $title = $page->getTitle();
100 $content = ContentHandler
::makeContent(
101 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
102 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
104 CONTENT_MODEL_WIKITEXT
107 $page->doEditContent( $content, "[[testing]] 1" );
109 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
110 $this->assertTrue( $page->getId() > 0, "WikiPage should have new page id" );
111 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
112 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
114 $id = $page->getId();
116 # ------------------------
117 $dbr = wfGetDB( DB_SLAVE
);
118 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
119 $n = $res->numRows();
122 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
124 # ------------------------
125 $page = new WikiPage( $title );
127 $retrieved = $page->getContent();
128 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
130 # ------------------------
131 $content = ContentHandler
::makeContent(
132 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
133 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est.",
135 CONTENT_MODEL_WIKITEXT
138 $page->doEditContent( $content, "testing 2" );
140 # ------------------------
141 $page = new WikiPage( $title );
143 $retrieved = $page->getContent();
144 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
146 # ------------------------
147 $dbr = wfGetDB( DB_SLAVE
);
148 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
149 $n = $res->numRows();
152 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
156 * @covers WikiPage::doEdit
158 public function testDoEdit() {
159 $this->hideDeprecated( "WikiPage::doEdit" );
160 $this->hideDeprecated( "WikiPage::getText" );
161 $this->hideDeprecated( "Revision::getText" );
163 // NOTE: assume help namespace will default to wikitext
164 $title = Title
::newFromText( "Help:WikiPageTest_testDoEdit" );
166 $page = $this->newPage( $title );
168 $text = "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
169 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.";
171 $page->doEdit( $text, "[[testing]] 1" );
173 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
174 $this->assertTrue( $page->getId() > 0, "WikiPage should have new page id" );
175 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
176 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
178 $id = $page->getId();
180 # ------------------------
181 $dbr = wfGetDB( DB_SLAVE
);
182 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
183 $n = $res->numRows();
186 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
188 # ------------------------
189 $page = new WikiPage( $title );
191 $retrieved = $page->getText();
192 $this->assertEquals( $text, $retrieved, 'retrieved text doesn\'t equal original' );
194 # ------------------------
195 $text = "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
196 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est.";
198 $page->doEdit( $text, "testing 2" );
200 # ------------------------
201 $page = new WikiPage( $title );
203 $retrieved = $page->getText();
204 $this->assertEquals( $text, $retrieved, 'retrieved text doesn\'t equal original' );
206 # ------------------------
207 $dbr = wfGetDB( DB_SLAVE
);
208 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
209 $n = $res->numRows();
212 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
216 * @covers WikiPage::doQuickEditContent
218 public function testDoQuickEditContent() {
221 $page = $this->createPage(
222 "WikiPageTest_testDoQuickEditContent",
224 CONTENT_MODEL_WIKITEXT
227 $content = ContentHandler
::makeContent(
230 CONTENT_MODEL_WIKITEXT
232 $page->doQuickEditContent( $content, $wgUser, "testing q" );
234 # ---------------------
235 $page = new WikiPage( $page->getTitle() );
236 $this->assertTrue( $content->equals( $page->getContent() ) );
240 * @covers WikiPage::doDeleteArticle
242 public function testDoDeleteArticle() {
243 $page = $this->createPage(
244 "WikiPageTest_testDoDeleteArticle",
245 "[[original text]] foo",
246 CONTENT_MODEL_WIKITEXT
248 $id = $page->getId();
250 $page->doDeleteArticle( "testing deletion" );
253 $page->getTitle()->getArticleID() > 0,
254 "Title object should now have page id 0"
256 $this->assertFalse( $page->getId() > 0, "WikiPage should now have page id 0" );
259 "WikiPage::exists should return false after page was deleted"
263 "WikiPage::getContent should return null after page was deleted"
267 "WikiPage::getText should return false after page was deleted"
270 $t = Title
::newFromText( $page->getTitle()->getPrefixedText() );
273 "Title::exists should return false after page was deleted"
277 JobQueueGroup
::destroySingletons();
279 $jobs->loadParamsAndArgs( null, array( 'quiet' => true ), null );
282 # ------------------------
283 $dbr = wfGetDB( DB_SLAVE
);
284 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
285 $n = $res->numRows();
288 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
292 * @covers WikiPage::doDeleteUpdates
294 public function testDoDeleteUpdates() {
295 $page = $this->createPage(
296 "WikiPageTest_testDoDeleteArticle",
297 "[[original text]] foo",
298 CONTENT_MODEL_WIKITEXT
300 $id = $page->getId();
302 // Similar to MovePage logic
303 wfGetDB( DB_MASTER
)->delete( 'page', array( 'page_id' => $id ), __METHOD__
);
304 $page->doDeleteUpdates( $id );
307 JobQueueGroup
::destroySingletons();
309 $jobs->loadParamsAndArgs( null, array( 'quiet' => true ), null );
312 # ------------------------
313 $dbr = wfGetDB( DB_SLAVE
);
314 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
315 $n = $res->numRows();
318 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
322 * @covers WikiPage::getRevision
324 public function testGetRevision() {
325 $page = $this->newPage( "WikiPageTest_testGetRevision" );
327 $rev = $page->getRevision();
328 $this->assertNull( $rev );
331 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
333 $rev = $page->getRevision();
335 $this->assertEquals( $page->getLatest(), $rev->getId() );
336 $this->assertEquals( "some text", $rev->getContent()->getNativeData() );
340 * @covers WikiPage::getContent
342 public function testGetContent() {
343 $page = $this->newPage( "WikiPageTest_testGetContent" );
345 $content = $page->getContent();
346 $this->assertNull( $content );
349 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
351 $content = $page->getContent();
352 $this->assertEquals( "some text", $content->getNativeData() );
356 * @covers WikiPage::getText
358 public function testGetText() {
359 $this->hideDeprecated( "WikiPage::getText" );
361 $page = $this->newPage( "WikiPageTest_testGetText" );
363 $text = $page->getText();
364 $this->assertFalse( $text );
367 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
369 $text = $page->getText();
370 $this->assertEquals( "some text", $text );
374 * @covers WikiPage::getRawText
376 public function testGetRawText() {
377 $this->hideDeprecated( "WikiPage::getRawText" );
379 $page = $this->newPage( "WikiPageTest_testGetRawText" );
381 $text = $page->getRawText();
382 $this->assertFalse( $text );
385 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
387 $text = $page->getRawText();
388 $this->assertEquals( "some text", $text );
392 * @covers WikiPage::getContentModel
394 public function testGetContentModel() {
395 global $wgContentHandlerUseDB;
397 if ( !$wgContentHandlerUseDB ) {
398 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
401 $page = $this->createPage(
402 "WikiPageTest_testGetContentModel",
404 CONTENT_MODEL_JAVASCRIPT
407 $page = new WikiPage( $page->getTitle() );
408 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $page->getContentModel() );
412 * @covers WikiPage::getContentHandler
414 public function testGetContentHandler() {
415 global $wgContentHandlerUseDB;
417 if ( !$wgContentHandlerUseDB ) {
418 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
421 $page = $this->createPage(
422 "WikiPageTest_testGetContentHandler",
424 CONTENT_MODEL_JAVASCRIPT
427 $page = new WikiPage( $page->getTitle() );
428 $this->assertEquals( 'JavaScriptContentHandler', get_class( $page->getContentHandler() ) );
432 * @covers WikiPage::exists
434 public function testExists() {
435 $page = $this->newPage( "WikiPageTest_testExists" );
436 $this->assertFalse( $page->exists() );
439 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
440 $this->assertTrue( $page->exists() );
442 $page = new WikiPage( $page->getTitle() );
443 $this->assertTrue( $page->exists() );
446 $page->doDeleteArticle( "done testing" );
447 $this->assertFalse( $page->exists() );
449 $page = new WikiPage( $page->getTitle() );
450 $this->assertFalse( $page->exists() );
453 public static function provideHasViewableContent() {
455 array( 'WikiPageTest_testHasViewableContent', false, true ),
456 array( 'Special:WikiPageTest_testHasViewableContent', false ),
457 array( 'MediaWiki:WikiPageTest_testHasViewableContent', false ),
458 array( 'Special:Userlogin', true ),
459 array( 'MediaWiki:help', true ),
464 * @dataProvider provideHasViewableContent
465 * @covers WikiPage::hasViewableContent
467 public function testHasViewableContent( $title, $viewable, $create = false ) {
468 $page = $this->newPage( $title );
469 $this->assertEquals( $viewable, $page->hasViewableContent() );
472 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
473 $this->assertTrue( $page->hasViewableContent() );
475 $page = new WikiPage( $page->getTitle() );
476 $this->assertTrue( $page->hasViewableContent() );
480 public static function provideGetRedirectTarget() {
482 array( 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT
, "hello world", null ),
484 'WikiPageTest_testGetRedirectTarget_2',
485 CONTENT_MODEL_WIKITEXT
,
486 "#REDIRECT [[hello world]]",
493 * @dataProvider provideGetRedirectTarget
494 * @covers WikiPage::getRedirectTarget
496 public function testGetRedirectTarget( $title, $model, $text, $target ) {
497 $this->setMwGlobals( array(
498 'wgCapitalLinks' => true,
501 $page = $this->createPage( $title, $text, $model );
503 # sanity check, because this test seems to fail for no reason for some people.
504 $c = $page->getContent();
505 $this->assertEquals( 'WikitextContent', get_class( $c ) );
507 # now, test the actual redirect
508 $t = $page->getRedirectTarget();
509 $this->assertEquals( $target, is_null( $t ) ?
null : $t->getPrefixedText() );
513 * @dataProvider provideGetRedirectTarget
514 * @covers WikiPage::isRedirect
516 public function testIsRedirect( $title, $model, $text, $target ) {
517 $page = $this->createPage( $title, $text, $model );
518 $this->assertEquals( !is_null( $target ), $page->isRedirect() );
521 public static function provideIsCountable() {
525 array( 'WikiPageTest_testIsCountable',
526 CONTENT_MODEL_WIKITEXT
,
531 array( 'WikiPageTest_testIsCountable',
532 CONTENT_MODEL_WIKITEXT
,
539 array( 'WikiPageTest_testIsCountable',
540 CONTENT_MODEL_WIKITEXT
,
545 array( 'WikiPageTest_testIsCountable',
546 CONTENT_MODEL_WIKITEXT
,
553 array( 'WikiPageTest_testIsCountable',
554 CONTENT_MODEL_WIKITEXT
,
559 array( 'WikiPageTest_testIsCountable',
560 CONTENT_MODEL_WIKITEXT
,
567 array( 'WikiPageTest_testIsCountable',
568 CONTENT_MODEL_WIKITEXT
,
573 array( 'WikiPageTest_testIsCountable',
574 CONTENT_MODEL_WIKITEXT
,
579 array( 'WikiPageTest_testIsCountable',
580 CONTENT_MODEL_WIKITEXT
,
586 // not a content namespace
587 array( 'Talk:WikiPageTest_testIsCountable',
588 CONTENT_MODEL_WIKITEXT
,
593 array( 'Talk:WikiPageTest_testIsCountable',
594 CONTENT_MODEL_WIKITEXT
,
599 array( 'Talk:WikiPageTest_testIsCountable',
600 CONTENT_MODEL_WIKITEXT
,
606 // not a content namespace, different model
607 array( 'MediaWiki:WikiPageTest_testIsCountable.js',
613 array( 'MediaWiki:WikiPageTest_testIsCountable.js',
619 array( 'MediaWiki:WikiPageTest_testIsCountable.js',
629 * @dataProvider provideIsCountable
630 * @covers WikiPage::isCountable
632 public function testIsCountable( $title, $model, $text, $mode, $expected ) {
633 global $wgContentHandlerUseDB;
635 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
637 $title = Title
::newFromText( $title );
639 if ( !$wgContentHandlerUseDB
641 && ContentHandler
::getDefaultModelFor( $title ) != $model
643 $this->markTestSkipped( "Can not use non-default content model $model for "
644 . $title->getPrefixedDBkey() . " with \$wgContentHandlerUseDB disabled." );
647 $page = $this->createPage( $title, $text, $model );
649 $editInfo = $page->prepareContentForEdit( $page->getContent() );
651 $v = $page->isCountable();
652 $w = $page->isCountable( $editInfo );
657 "isCountable( null ) returned unexpected value " . var_export( $v, true )
658 . " instead of " . var_export( $expected, true )
659 . " in mode `$mode` for text \"$text\""
665 "isCountable( \$editInfo ) returned unexpected value " . var_export( $v, true )
666 . " instead of " . var_export( $expected, true )
667 . " in mode `$mode` for text \"$text\""
671 public static function provideGetParserOutput() {
673 array( CONTENT_MODEL_WIKITEXT
, "hello ''world''\n", "<p>hello <i>world</i></p>" ),
679 * @dataProvider provideGetParserOutput
680 * @covers WikiPage::getParserOutput
682 public function testGetParserOutput( $model, $text, $expectedHtml ) {
683 $page = $this->createPage( 'WikiPageTest_testGetParserOutput', $text, $model );
685 $opt = $page->makeParserOptions( 'canonical' );
686 $po = $page->getParserOutput( $opt );
687 $text = $po->getText();
689 $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments
690 $text = preg_replace( '!\s*(</p>)!sm', '\1', $text ); # don't let tidy confuse us
692 $this->assertEquals( $expectedHtml, $text );
698 * @covers WikiPage::getParserOutput
700 public function testGetParserOutput_nonexisting() {
704 $page = new WikiPage( new Title( "WikiPageTest_testGetParserOutput_nonexisting_$count" ) );
706 $opt = new ParserOptions();
707 $po = $page->getParserOutput( $opt );
709 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." );
713 * @covers WikiPage::getParserOutput
715 public function testGetParserOutput_badrev() {
716 $page = $this->createPage( 'WikiPageTest_testGetParserOutput', "dummy", CONTENT_MODEL_WIKITEXT
);
718 $opt = new ParserOptions();
719 $po = $page->getParserOutput( $opt, $page->getLatest() +
1234 );
721 // @todo would be neat to also test deleted revision
723 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." );
726 public static $sections =
740 public function dataReplaceSection() {
741 // NOTE: assume the Help namespace to contain wikitext
743 array( 'Help:WikiPageTest_testReplaceSection',
744 CONTENT_MODEL_WIKITEXT
,
745 WikiPageTest
::$sections,
749 trim( preg_replace( '/^Intro/sm', 'No more', WikiPageTest
::$sections ) )
751 array( 'Help:WikiPageTest_testReplaceSection',
752 CONTENT_MODEL_WIKITEXT
,
753 WikiPageTest
::$sections,
759 array( 'Help:WikiPageTest_testReplaceSection',
760 CONTENT_MODEL_WIKITEXT
,
761 WikiPageTest
::$sections,
763 "== TEST ==\nmore fun",
765 trim( preg_replace( '/^== test ==.*== foo ==/sm',
766 "== TEST ==\nmore fun\n\n== foo ==",
767 WikiPageTest
::$sections ) )
769 array( 'Help:WikiPageTest_testReplaceSection',
770 CONTENT_MODEL_WIKITEXT
,
771 WikiPageTest
::$sections,
775 trim( WikiPageTest
::$sections )
777 array( 'Help:WikiPageTest_testReplaceSection',
778 CONTENT_MODEL_WIKITEXT
,
779 WikiPageTest
::$sections,
783 trim( WikiPageTest
::$sections ) . "\n\n== New ==\n\nNo more"
789 * @dataProvider dataReplaceSection
790 * @covers WikiPage::replaceSection
792 public function testReplaceSection( $title, $model, $text, $section, $with,
793 $sectionTitle, $expected
795 $this->hideDeprecated( "WikiPage::replaceSection" );
797 $page = $this->createPage( $title, $text, $model );
798 $text = $page->replaceSection( $section, $with, $sectionTitle );
799 $text = trim( $text );
801 $this->assertEquals( $expected, $text );
805 * @dataProvider dataReplaceSection
806 * @covers WikiPage::replaceSectionContent
808 public function testReplaceSectionContent( $title, $model, $text, $section,
809 $with, $sectionTitle, $expected
811 $page = $this->createPage( $title, $text, $model );
813 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
814 $c = $page->replaceSectionContent( $section, $content, $sectionTitle );
816 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
820 * @dataProvider dataReplaceSection
821 * @covers WikiPage::replaceSectionAtRev
823 public function testReplaceSectionAtRev( $title, $model, $text, $section,
824 $with, $sectionTitle, $expected
826 $page = $this->createPage( $title, $text, $model );
827 $baseRevId = $page->getLatest();
829 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
830 $c = $page->replaceSectionAtRev( $section, $content, $sectionTitle, $baseRevId );
832 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
835 /* @todo FIXME: fix this!
836 public function testGetUndoText() {
837 $this->checkHasDiff3();
840 $page = $this->createPage( "WikiPageTest_testGetUndoText", $text );
841 $rev1 = $page->getRevision();
844 $page->doEditContent(
845 ContentHandler::makeContent( $text, $page->getTitle() ),
848 $rev2 = $page->getRevision();
850 $text .= "\n\nthree";
851 $page->doEditContent(
852 ContentHandler::makeContent( $text, $page->getTitle() ),
853 "adding section three"
855 $rev3 = $page->getRevision();
858 $page->doEditContent(
859 ContentHandler::makeContent( $text, $page->getTitle() ),
860 "adding section four"
862 $rev4 = $page->getRevision();
865 $page->doEditContent(
866 ContentHandler::makeContent( $text, $page->getTitle() ),
867 "adding section five"
869 $rev5 = $page->getRevision();
872 $page->doEditContent(
873 ContentHandler::makeContent( $text, $page->getTitle() ),
876 $rev6 = $page->getRevision();
878 $undo6 = $page->getUndoText( $rev6 );
879 if ( $undo6 === false ) $this->fail( "getUndoText failed for rev6" );
880 $this->assertEquals( "one\n\ntwo\n\nthree\n\nfour\n\nfive", $undo6 );
882 $undo3 = $page->getUndoText( $rev4, $rev2 );
883 if ( $undo3 === false ) $this->fail( "getUndoText failed for rev4..rev2" );
884 $this->assertEquals( "one\n\ntwo\n\nfive", $undo3 );
886 $undo2 = $page->getUndoText( $rev2 );
887 if ( $undo2 === false ) $this->fail( "getUndoText failed for rev2" );
888 $this->assertEquals( "one\n\nfive", $undo2 );
893 * @todo FIXME: this is a better rollback test than the one below, but it
894 * keeps failing in jenkins for some reason.
896 public function broken_testDoRollback() {
898 $admin->setName( "Admin" );
901 $page = $this->newPage( "WikiPageTest_testDoRollback" );
902 $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
903 "section one", EDIT_NEW
, false, $admin );
906 $user1->setName( "127.0.1.11" );
908 $page = new WikiPage( $page->getTitle() );
909 $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
910 "adding section two", 0, false, $user1 );
913 $user2->setName( "127.0.2.13" );
914 $text .= "\n\nthree";
915 $page = new WikiPage( $page->getTitle() );
916 $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
917 "adding section three", 0, false, $user2 );
919 # we are having issues with doRollback spuriously failing. Apparently
920 # the last revision somehow goes missing or not committed under some
921 # circumstances. So, make sure the last revision has the right user name.
922 $dbr = wfGetDB( DB_SLAVE
);
923 $this->assertEquals( 3, Revision
::countByPageId( $dbr, $page->getId() ) );
925 $page = new WikiPage( $page->getTitle() );
926 $rev3 = $page->getRevision();
927 $this->assertEquals( '127.0.2.13', $rev3->getUserText() );
929 $rev2 = $rev3->getPrevious();
930 $this->assertEquals( '127.0.1.11', $rev2->getUserText() );
932 $rev1 = $rev2->getPrevious();
933 $this->assertEquals( 'Admin', $rev1->getUserText() );
935 # now, try the actual rollback
936 $admin->addGroup( "sysop" ); # XXX: make the test user a sysop...
937 $token = $admin->getEditToken(
938 array( $page->getTitle()->getPrefixedText(), $user2->getName() ),
941 $errors = $page->doRollback(
951 $this->fail( "Rollback failed:\n" . print_r( $errors, true )
952 . ";\n" . print_r( $details, true ) );
955 $page = new WikiPage( $page->getTitle() );
956 $this->assertEquals( $rev2->getSha1(), $page->getRevision()->getSha1(),
957 "rollback did not revert to the correct revision" );
958 $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
962 * @todo FIXME: the above rollback test is better, but it keeps failing in jenkins for some reason.
963 * @covers WikiPage::doRollback
965 public function testDoRollback() {
967 $admin->setName( "Admin" );
970 $page = $this->newPage( "WikiPageTest_testDoRollback" );
971 $page->doEditContent(
972 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
978 $rev1 = $page->getRevision();
981 $user1->setName( "127.0.1.11" );
983 $page = new WikiPage( $page->getTitle() );
984 $page->doEditContent(
985 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
986 "adding section two",
992 # now, try the rollback
993 $admin->addGroup( "sysop" ); # XXX: make the test user a sysop...
994 $token = $admin->getEditToken(
995 array( $page->getTitle()->getPrefixedText(), $user1->getName() ),
998 $errors = $page->doRollback(
1008 $this->fail( "Rollback failed:\n" . print_r( $errors, true )
1009 . ";\n" . print_r( $details, true ) );
1012 $page = new WikiPage( $page->getTitle() );
1013 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
1014 "rollback did not revert to the correct revision" );
1015 $this->assertEquals( "one", $page->getContent()->getNativeData() );
1019 * @covers WikiPage::doRollback
1021 public function testDoRollbackFailureSameContent() {
1022 $admin = new User();
1023 $admin->setName( "Admin" );
1024 $admin->addGroup( "sysop" ); # XXX: make the test user a sysop...
1027 $page = $this->newPage( "WikiPageTest_testDoRollback" );
1028 $page->doEditContent(
1029 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1035 $rev1 = $page->getRevision();
1037 $user1 = new User();
1038 $user1->setName( "127.0.1.11" );
1039 $user1->addGroup( "sysop" ); # XXX: make the test user a sysop...
1041 $page = new WikiPage( $page->getTitle() );
1042 $page->doEditContent(
1043 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1044 "adding section two",
1050 # now, do a the rollback from the same user was doing the edit before
1051 $resultDetails = array();
1052 $token = $user1->getEditToken(
1053 array( $page->getTitle()->getPrefixedText(), $user1->getName() ),
1056 $errors = $page->doRollback(
1058 "testing revert same user",
1065 $this->assertEquals( array(), $errors, "Rollback failed same user" );
1067 # now, try the rollback
1068 $resultDetails = array();
1069 $token = $admin->getEditToken(
1070 array( $page->getTitle()->getPrefixedText(), $user1->getName() ),
1073 $errors = $page->doRollback(
1082 $this->assertEquals( array( array( 'alreadyrolled', 'WikiPageTest testDoRollback',
1083 '127.0.1.11', 'Admin' ) ), $errors, "Rollback not failed" );
1085 $page = new WikiPage( $page->getTitle() );
1086 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
1087 "rollback did not revert to the correct revision" );
1088 $this->assertEquals( "one", $page->getContent()->getNativeData() );
1091 public static function provideGetAutosummary() {
1094 'Hello there, world!',
1095 '#REDIRECT [[Foo]]',
1097 '/^Redirected page .*Foo/'
1104 '/^Created page .*Hello/'
1108 'Hello there, world!',
1115 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
1116 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
1117 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
1118 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
1121 '/^Replaced .*Hello/'
1134 * @dataProvider provideGetAutoSummary
1135 * @covers WikiPage::getAutosummary
1137 public function testGetAutosummary( $old, $new, $flags, $expected ) {
1138 $this->hideDeprecated( "WikiPage::getAutosummary" );
1140 $page = $this->newPage( "WikiPageTest_testGetAutosummary" );
1142 $summary = $page->getAutosummary( $old, $new, $flags );
1144 $this->assertTrue( (bool)preg_match( $expected, $summary ),
1145 "Autosummary didn't match expected pattern $expected: $summary" );
1148 public static function provideGetAutoDeleteReason() {
1158 array( "first edit", null ),
1160 "/first edit.*only contributor/",
1166 array( "first edit", null ),
1167 array( "second edit", null ),
1169 "/second edit.*only contributor/",
1175 array( "first edit", "127.0.2.22" ),
1176 array( "second edit", "127.0.3.33" ),
1186 . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
1187 . " nonumy eirmod tempor invidunt ut labore et dolore magna "
1188 . "aliquyam erat, sed diam voluptua. At vero eos et accusam "
1189 . "et justo duo dolores et ea rebum. Stet clita kasd gubergren, "
1190 . "no sea takimata sanctus est Lorem ipsum dolor sit amet.'",
1194 '/first edit:.*\.\.\."/',
1200 array( "first edit", "127.0.2.22" ),
1201 array( "", "127.0.3.33" ),
1203 "/before blanking.*first edit/",
1211 * @dataProvider provideGetAutoDeleteReason
1212 * @covers WikiPage::getAutoDeleteReason
1214 public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
1217 // NOTE: assume Help namespace to contain wikitext
1218 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
1222 foreach ( $edits as $edit ) {
1225 if ( !empty( $edit[1] ) ) {
1226 $user->setName( $edit[1] );
1231 $content = ContentHandler
::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
1233 $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW
: 0, false, $user );
1238 $reason = $page->getAutoDeleteReason( $hasHistory );
1240 if ( is_bool( $expectedResult ) ||
is_null( $expectedResult ) ) {
1241 $this->assertEquals( $expectedResult, $reason );
1243 $this->assertTrue( (bool)preg_match( $expectedResult, $reason ),
1244 "Autosummary didn't match expected pattern $expectedResult: $reason" );
1247 $this->assertEquals( $expectedHistory, $hasHistory,
1248 "expected \$hasHistory to be " . var_export( $expectedHistory, true ) );
1250 $page->doDeleteArticle( "done" );
1253 public static function providePreSaveTransform() {
1255 array( 'hello this is ~~~',
1256 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
1258 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1259 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1265 * @dataProvider providePreSaveTransform
1266 * @covers WikiPage::preSaveTransform
1268 public function testPreSaveTransform( $text, $expected ) {
1269 $this->hideDeprecated( 'WikiPage::preSaveTransform' );
1271 $user->setName( "127.0.0.1" );
1273 // NOTE: assume Help namespace to contain wikitext
1274 $page = $this->newPage( "Help:WikiPageTest_testPreloadTransform" );
1275 $text = $page->preSaveTransform( $text, $user );
1277 $this->assertEquals( $expected, $text );
1281 * @covers WikiPage::factory
1283 public function testWikiPageFactory() {
1284 $title = Title
::makeTitle( NS_FILE
, 'Someimage.png' );
1285 $page = WikiPage
::factory( $title );
1286 $this->assertEquals( 'WikiFilePage', get_class( $page ) );
1288 $title = Title
::makeTitle( NS_CATEGORY
, 'SomeCategory' );
1289 $page = WikiPage
::factory( $title );
1290 $this->assertEquals( 'WikiCategoryPage', get_class( $page ) );
1292 $title = Title
::makeTitle( NS_MAIN
, 'SomePage' );
1293 $page = WikiPage
::factory( $title );
1294 $this->assertEquals( 'WikiPage', get_class( $page ) );