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::doQuickEdit
218 public function testDoQuickEdit() {
221 $this->hideDeprecated( "WikiPage::doQuickEdit" );
223 //NOTE: assume help namespace will default to wikitext
224 $page = $this->createPage( "Help:WikiPageTest_testDoQuickEdit", "original text" );
226 $text = "quick text";
227 $page->doQuickEdit( $text, $wgUser, "testing q" );
229 # ---------------------
230 $page = new WikiPage( $page->getTitle() );
231 $this->assertEquals( $text, $page->getText() );
235 * @covers WikiPage::doQuickEditContent
237 public function testDoQuickEditContent() {
240 $page = $this->createPage(
241 "WikiPageTest_testDoQuickEditContent",
243 CONTENT_MODEL_WIKITEXT
246 $content = ContentHandler
::makeContent(
249 CONTENT_MODEL_WIKITEXT
251 $page->doQuickEditContent( $content, $wgUser, "testing q" );
253 # ---------------------
254 $page = new WikiPage( $page->getTitle() );
255 $this->assertTrue( $content->equals( $page->getContent() ) );
259 * @covers WikiPage::doDeleteArticle
261 public function testDoDeleteArticle() {
262 $page = $this->createPage(
263 "WikiPageTest_testDoDeleteArticle",
264 "[[original text]] foo",
265 CONTENT_MODEL_WIKITEXT
267 $id = $page->getId();
269 $page->doDeleteArticle( "testing deletion" );
272 $page->getTitle()->getArticleID() > 0,
273 "Title object should now have page id 0"
275 $this->assertFalse( $page->getId() > 0, "WikiPage should now have page id 0" );
278 "WikiPage::exists should return false after page was deleted"
282 "WikiPage::getContent should return null after page was deleted"
286 "WikiPage::getText should return false after page was deleted"
289 $t = Title
::newFromText( $page->getTitle()->getPrefixedText() );
292 "Title::exists should return false after page was deleted"
295 # ------------------------
296 $dbr = wfGetDB( DB_SLAVE
);
297 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
298 $n = $res->numRows();
301 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
305 * @covers WikiPage::doDeleteUpdates
307 public function testDoDeleteUpdates() {
308 $page = $this->createPage(
309 "WikiPageTest_testDoDeleteArticle",
310 "[[original text]] foo",
311 CONTENT_MODEL_WIKITEXT
313 $id = $page->getId();
315 $page->doDeleteUpdates( $id );
317 # ------------------------
318 $dbr = wfGetDB( DB_SLAVE
);
319 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
320 $n = $res->numRows();
323 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
327 * @covers WikiPage::getRevision
329 public function testGetRevision() {
330 $page = $this->newPage( "WikiPageTest_testGetRevision" );
332 $rev = $page->getRevision();
333 $this->assertNull( $rev );
336 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
338 $rev = $page->getRevision();
340 $this->assertEquals( $page->getLatest(), $rev->getId() );
341 $this->assertEquals( "some text", $rev->getContent()->getNativeData() );
345 * @covers WikiPage::getContent
347 public function testGetContent() {
348 $page = $this->newPage( "WikiPageTest_testGetContent" );
350 $content = $page->getContent();
351 $this->assertNull( $content );
354 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
356 $content = $page->getContent();
357 $this->assertEquals( "some text", $content->getNativeData() );
361 * @covers WikiPage::getText
363 public function testGetText() {
364 $this->hideDeprecated( "WikiPage::getText" );
366 $page = $this->newPage( "WikiPageTest_testGetText" );
368 $text = $page->getText();
369 $this->assertFalse( $text );
372 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
374 $text = $page->getText();
375 $this->assertEquals( "some text", $text );
379 * @covers WikiPage::getRawText
381 public function testGetRawText() {
382 $this->hideDeprecated( "WikiPage::getRawText" );
384 $page = $this->newPage( "WikiPageTest_testGetRawText" );
386 $text = $page->getRawText();
387 $this->assertFalse( $text );
390 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
392 $text = $page->getRawText();
393 $this->assertEquals( "some text", $text );
397 * @covers WikiPage::getContentModel
399 public function testGetContentModel() {
400 global $wgContentHandlerUseDB;
402 if ( !$wgContentHandlerUseDB ) {
403 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
406 $page = $this->createPage(
407 "WikiPageTest_testGetContentModel",
409 CONTENT_MODEL_JAVASCRIPT
412 $page = new WikiPage( $page->getTitle() );
413 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $page->getContentModel() );
417 * @covers WikiPage::getContentHandler
419 public function testGetContentHandler() {
420 global $wgContentHandlerUseDB;
422 if ( !$wgContentHandlerUseDB ) {
423 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
426 $page = $this->createPage(
427 "WikiPageTest_testGetContentHandler",
429 CONTENT_MODEL_JAVASCRIPT
432 $page = new WikiPage( $page->getTitle() );
433 $this->assertEquals( 'JavaScriptContentHandler', get_class( $page->getContentHandler() ) );
437 * @covers WikiPage::exists
439 public function testExists() {
440 $page = $this->newPage( "WikiPageTest_testExists" );
441 $this->assertFalse( $page->exists() );
444 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
445 $this->assertTrue( $page->exists() );
447 $page = new WikiPage( $page->getTitle() );
448 $this->assertTrue( $page->exists() );
451 $page->doDeleteArticle( "done testing" );
452 $this->assertFalse( $page->exists() );
454 $page = new WikiPage( $page->getTitle() );
455 $this->assertFalse( $page->exists() );
458 public static function provideHasViewableContent() {
460 array( 'WikiPageTest_testHasViewableContent', false, true ),
461 array( 'Special:WikiPageTest_testHasViewableContent', false ),
462 array( 'MediaWiki:WikiPageTest_testHasViewableContent', false ),
463 array( 'Special:Userlogin', true ),
464 array( 'MediaWiki:help', true ),
469 * @dataProvider provideHasViewableContent
470 * @covers WikiPage::hasViewableContent
472 public function testHasViewableContent( $title, $viewable, $create = false ) {
473 $page = $this->newPage( $title );
474 $this->assertEquals( $viewable, $page->hasViewableContent() );
477 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
478 $this->assertTrue( $page->hasViewableContent() );
480 $page = new WikiPage( $page->getTitle() );
481 $this->assertTrue( $page->hasViewableContent() );
485 public static function provideGetRedirectTarget() {
487 array( 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT
, "hello world", null ),
489 'WikiPageTest_testGetRedirectTarget_2',
490 CONTENT_MODEL_WIKITEXT
,
491 "#REDIRECT [[hello world]]",
498 * @dataProvider provideGetRedirectTarget
499 * @covers WikiPage::getRedirectTarget
501 public function testGetRedirectTarget( $title, $model, $text, $target ) {
502 $this->setMwGlobals( array(
503 'wgCapitalLinks' => true,
506 $page = $this->createPage( $title, $text, $model );
508 # sanity check, because this test seems to fail for no reason for some people.
509 $c = $page->getContent();
510 $this->assertEquals( 'WikitextContent', get_class( $c ) );
512 # now, test the actual redirect
513 $t = $page->getRedirectTarget();
514 $this->assertEquals( $target, is_null( $t ) ?
null : $t->getPrefixedText() );
518 * @dataProvider provideGetRedirectTarget
519 * @covers WikiPage::isRedirect
521 public function testIsRedirect( $title, $model, $text, $target ) {
522 $page = $this->createPage( $title, $text, $model );
523 $this->assertEquals( !is_null( $target ), $page->isRedirect() );
526 public static function provideIsCountable() {
530 array( 'WikiPageTest_testIsCountable',
531 CONTENT_MODEL_WIKITEXT
,
536 array( 'WikiPageTest_testIsCountable',
537 CONTENT_MODEL_WIKITEXT
,
544 array( 'WikiPageTest_testIsCountable',
545 CONTENT_MODEL_WIKITEXT
,
550 array( 'WikiPageTest_testIsCountable',
551 CONTENT_MODEL_WIKITEXT
,
558 array( 'WikiPageTest_testIsCountable',
559 CONTENT_MODEL_WIKITEXT
,
564 array( 'WikiPageTest_testIsCountable',
565 CONTENT_MODEL_WIKITEXT
,
572 array( 'WikiPageTest_testIsCountable',
573 CONTENT_MODEL_WIKITEXT
,
578 array( 'WikiPageTest_testIsCountable',
579 CONTENT_MODEL_WIKITEXT
,
584 array( 'WikiPageTest_testIsCountable',
585 CONTENT_MODEL_WIKITEXT
,
591 // not a content namespace
592 array( 'Talk:WikiPageTest_testIsCountable',
593 CONTENT_MODEL_WIKITEXT
,
598 array( 'Talk:WikiPageTest_testIsCountable',
599 CONTENT_MODEL_WIKITEXT
,
604 array( 'Talk:WikiPageTest_testIsCountable',
605 CONTENT_MODEL_WIKITEXT
,
611 // not a content namespace, different model
612 array( 'MediaWiki:WikiPageTest_testIsCountable.js',
618 array( 'MediaWiki:WikiPageTest_testIsCountable.js',
624 array( 'MediaWiki:WikiPageTest_testIsCountable.js',
634 * @dataProvider provideIsCountable
635 * @covers WikiPage::isCountable
637 public function testIsCountable( $title, $model, $text, $mode, $expected ) {
638 global $wgContentHandlerUseDB;
640 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
642 $title = Title
::newFromText( $title );
644 if ( !$wgContentHandlerUseDB
646 && ContentHandler
::getDefaultModelFor( $title ) != $model
648 $this->markTestSkipped( "Can not use non-default content model $model for "
649 . $title->getPrefixedDBkey() . " with \$wgContentHandlerUseDB disabled." );
652 $page = $this->createPage( $title, $text, $model );
654 $editInfo = $page->prepareContentForEdit( $page->getContent() );
656 $v = $page->isCountable();
657 $w = $page->isCountable( $editInfo );
662 "isCountable( null ) returned unexpected value " . var_export( $v, true )
663 . " instead of " . var_export( $expected, true )
664 . " in mode `$mode` for text \"$text\""
670 "isCountable( \$editInfo ) returned unexpected value " . var_export( $v, true )
671 . " instead of " . var_export( $expected, true )
672 . " in mode `$mode` for text \"$text\""
676 public static function provideGetParserOutput() {
678 array( CONTENT_MODEL_WIKITEXT
, "hello ''world''\n", "<p>hello <i>world</i></p>" ),
684 * @dataProvider provideGetParserOutput
685 * @covers WikiPage::getParserOutput
687 public function testGetParserOutput( $model, $text, $expectedHtml ) {
688 $page = $this->createPage( 'WikiPageTest_testGetParserOutput', $text, $model );
690 $opt = $page->makeParserOptions( 'canonical' );
691 $po = $page->getParserOutput( $opt );
692 $text = $po->getText();
694 $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments
695 $text = preg_replace( '!\s*(</p>)!sm', '\1', $text ); # don't let tidy confuse us
697 $this->assertEquals( $expectedHtml, $text );
703 * @covers WikiPage::getParserOutput
705 public function testGetParserOutput_nonexisting() {
709 $page = new WikiPage( new Title( "WikiPageTest_testGetParserOutput_nonexisting_$count" ) );
711 $opt = new ParserOptions();
712 $po = $page->getParserOutput( $opt );
714 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." );
718 * @covers WikiPage::getParserOutput
720 public function testGetParserOutput_badrev() {
721 $page = $this->createPage( 'WikiPageTest_testGetParserOutput', "dummy", CONTENT_MODEL_WIKITEXT
);
723 $opt = new ParserOptions();
724 $po = $page->getParserOutput( $opt, $page->getLatest() +
1234 );
726 // @todo would be neat to also test deleted revision
728 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." );
731 public static $sections =
745 public function dataReplaceSection() {
746 //NOTE: assume the Help namespace to contain wikitext
748 array( 'Help:WikiPageTest_testReplaceSection',
749 CONTENT_MODEL_WIKITEXT
,
750 WikiPageTest
::$sections,
754 trim( preg_replace( '/^Intro/sm', 'No more', WikiPageTest
::$sections ) )
756 array( 'Help:WikiPageTest_testReplaceSection',
757 CONTENT_MODEL_WIKITEXT
,
758 WikiPageTest
::$sections,
764 array( 'Help:WikiPageTest_testReplaceSection',
765 CONTENT_MODEL_WIKITEXT
,
766 WikiPageTest
::$sections,
768 "== TEST ==\nmore fun",
770 trim( preg_replace( '/^== test ==.*== foo ==/sm',
771 "== TEST ==\nmore fun\n\n== foo ==",
772 WikiPageTest
::$sections ) )
774 array( 'Help:WikiPageTest_testReplaceSection',
775 CONTENT_MODEL_WIKITEXT
,
776 WikiPageTest
::$sections,
780 trim( WikiPageTest
::$sections )
782 array( 'Help:WikiPageTest_testReplaceSection',
783 CONTENT_MODEL_WIKITEXT
,
784 WikiPageTest
::$sections,
788 trim( WikiPageTest
::$sections ) . "\n\n== New ==\n\nNo more"
794 * @dataProvider dataReplaceSection
795 * @covers WikiPage::replaceSection
797 public function testReplaceSection( $title, $model, $text, $section, $with,
798 $sectionTitle, $expected
800 $this->hideDeprecated( "WikiPage::replaceSection" );
802 $page = $this->createPage( $title, $text, $model );
803 $text = $page->replaceSection( $section, $with, $sectionTitle );
804 $text = trim( $text );
806 $this->assertEquals( $expected, $text );
810 * @dataProvider dataReplaceSection
811 * @covers WikiPage::replaceSectionContent
813 public function testReplaceSectionContent( $title, $model, $text, $section,
814 $with, $sectionTitle, $expected
816 $page = $this->createPage( $title, $text, $model );
818 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
819 $c = $page->replaceSectionContent( $section, $content, $sectionTitle );
821 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
825 * @dataProvider dataReplaceSection
826 * @covers WikiPage::replaceSectionAtRev
828 public function testReplaceSectionAtRev( $title, $model, $text, $section,
829 $with, $sectionTitle, $expected
831 $page = $this->createPage( $title, $text, $model );
832 $baseRevId = $page->getLatest();
834 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
835 $c = $page->replaceSectionAtRev( $section, $content, $sectionTitle, $baseRevId );
837 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
840 /* @todo FIXME: fix this!
841 public function testGetUndoText() {
842 $this->checkHasDiff3();
845 $page = $this->createPage( "WikiPageTest_testGetUndoText", $text );
846 $rev1 = $page->getRevision();
849 $page->doEditContent(
850 ContentHandler::makeContent( $text, $page->getTitle() ),
853 $rev2 = $page->getRevision();
855 $text .= "\n\nthree";
856 $page->doEditContent(
857 ContentHandler::makeContent( $text, $page->getTitle() ),
858 "adding section three"
860 $rev3 = $page->getRevision();
863 $page->doEditContent(
864 ContentHandler::makeContent( $text, $page->getTitle() ),
865 "adding section four"
867 $rev4 = $page->getRevision();
870 $page->doEditContent(
871 ContentHandler::makeContent( $text, $page->getTitle() ),
872 "adding section five"
874 $rev5 = $page->getRevision();
877 $page->doEditContent(
878 ContentHandler::makeContent( $text, $page->getTitle() ),
881 $rev6 = $page->getRevision();
883 $undo6 = $page->getUndoText( $rev6 );
884 if ( $undo6 === false ) $this->fail( "getUndoText failed for rev6" );
885 $this->assertEquals( "one\n\ntwo\n\nthree\n\nfour\n\nfive", $undo6 );
887 $undo3 = $page->getUndoText( $rev4, $rev2 );
888 if ( $undo3 === false ) $this->fail( "getUndoText failed for rev4..rev2" );
889 $this->assertEquals( "one\n\ntwo\n\nfive", $undo3 );
891 $undo2 = $page->getUndoText( $rev2 );
892 if ( $undo2 === false ) $this->fail( "getUndoText failed for rev2" );
893 $this->assertEquals( "one\n\nfive", $undo2 );
898 * @todo FIXME: this is a better rollback test than the one below, but it
899 * keeps failing in jenkins for some reason.
901 public function broken_testDoRollback() {
903 $admin->setName( "Admin" );
906 $page = $this->newPage( "WikiPageTest_testDoRollback" );
907 $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
908 "section one", EDIT_NEW
, false, $admin );
911 $user1->setName( "127.0.1.11" );
913 $page = new WikiPage( $page->getTitle() );
914 $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
915 "adding section two", 0, false, $user1 );
918 $user2->setName( "127.0.2.13" );
919 $text .= "\n\nthree";
920 $page = new WikiPage( $page->getTitle() );
921 $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
922 "adding section three", 0, false, $user2 );
924 # we are having issues with doRollback spuriously failing. Apparently
925 # the last revision somehow goes missing or not committed under some
926 # circumstances. So, make sure the last revision has the right user name.
927 $dbr = wfGetDB( DB_SLAVE
);
928 $this->assertEquals( 3, Revision
::countByPageId( $dbr, $page->getId() ) );
930 $page = new WikiPage( $page->getTitle() );
931 $rev3 = $page->getRevision();
932 $this->assertEquals( '127.0.2.13', $rev3->getUserText() );
934 $rev2 = $rev3->getPrevious();
935 $this->assertEquals( '127.0.1.11', $rev2->getUserText() );
937 $rev1 = $rev2->getPrevious();
938 $this->assertEquals( 'Admin', $rev1->getUserText() );
940 # now, try the actual rollback
941 $admin->addGroup( "sysop" ); #XXX: make the test user a sysop...
942 $token = $admin->getEditToken(
943 array( $page->getTitle()->getPrefixedText(), $user2->getName() ),
946 $errors = $page->doRollback(
956 $this->fail( "Rollback failed:\n" . print_r( $errors, true )
957 . ";\n" . print_r( $details, true ) );
960 $page = new WikiPage( $page->getTitle() );
961 $this->assertEquals( $rev2->getSha1(), $page->getRevision()->getSha1(),
962 "rollback did not revert to the correct revision" );
963 $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
967 * @todo FIXME: the above rollback test is better, but it keeps failing in jenkins for some reason.
968 * @covers WikiPage::doRollback
970 public function testDoRollback() {
972 $admin->setName( "Admin" );
975 $page = $this->newPage( "WikiPageTest_testDoRollback" );
976 $page->doEditContent(
977 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
983 $rev1 = $page->getRevision();
986 $user1->setName( "127.0.1.11" );
988 $page = new WikiPage( $page->getTitle() );
989 $page->doEditContent(
990 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
991 "adding section two",
997 # now, try the rollback
998 $admin->addGroup( "sysop" ); #XXX: make the test user a sysop...
999 $token = $admin->getEditToken(
1000 array( $page->getTitle()->getPrefixedText(), $user1->getName() ),
1003 $errors = $page->doRollback(
1013 $this->fail( "Rollback failed:\n" . print_r( $errors, true )
1014 . ";\n" . print_r( $details, true ) );
1017 $page = new WikiPage( $page->getTitle() );
1018 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
1019 "rollback did not revert to the correct revision" );
1020 $this->assertEquals( "one", $page->getContent()->getNativeData() );
1024 * @covers WikiPage::doRollback
1026 public function testDoRollbackFailureSameContent() {
1027 $admin = new User();
1028 $admin->setName( "Admin" );
1029 $admin->addGroup( "sysop" ); #XXX: make the test user a sysop...
1032 $page = $this->newPage( "WikiPageTest_testDoRollback" );
1033 $page->doEditContent(
1034 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1040 $rev1 = $page->getRevision();
1042 $user1 = new User();
1043 $user1->setName( "127.0.1.11" );
1044 $user1->addGroup( "sysop" ); #XXX: make the test user a sysop...
1046 $page = new WikiPage( $page->getTitle() );
1047 $page->doEditContent(
1048 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1049 "adding section two",
1055 # now, do a the rollback from the same user was doing the edit before
1056 $resultDetails = array();
1057 $token = $user1->getEditToken(
1058 array( $page->getTitle()->getPrefixedText(), $user1->getName() ),
1061 $errors = $page->doRollback(
1063 "testing revert same user",
1070 $this->assertEquals( array(), $errors, "Rollback failed same user" );
1072 # now, try the rollback
1073 $resultDetails = array();
1074 $token = $admin->getEditToken(
1075 array( $page->getTitle()->getPrefixedText(), $user1->getName() ),
1078 $errors = $page->doRollback(
1087 $this->assertEquals( array( array( 'alreadyrolled', 'WikiPageTest testDoRollback',
1088 '127.0.1.11', 'Admin' ) ), $errors, "Rollback not failed" );
1090 $page = new WikiPage( $page->getTitle() );
1091 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
1092 "rollback did not revert to the correct revision" );
1093 $this->assertEquals( "one", $page->getContent()->getNativeData() );
1096 public static function provideGetAutosummary() {
1099 'Hello there, world!',
1100 '#REDIRECT [[Foo]]',
1102 '/^Redirected page .*Foo/'
1109 '/^Created page .*Hello/'
1113 'Hello there, world!',
1120 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
1121 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
1122 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
1123 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
1126 '/^Replaced .*Hello/'
1139 * @dataProvider provideGetAutoSummary
1140 * @covers WikiPage::getAutosummary
1142 public function testGetAutosummary( $old, $new, $flags, $expected ) {
1143 $this->hideDeprecated( "WikiPage::getAutosummary" );
1145 $page = $this->newPage( "WikiPageTest_testGetAutosummary" );
1147 $summary = $page->getAutosummary( $old, $new, $flags );
1149 $this->assertTrue( (bool)preg_match( $expected, $summary ),
1150 "Autosummary didn't match expected pattern $expected: $summary" );
1153 public static function provideGetAutoDeleteReason() {
1163 array( "first edit", null ),
1165 "/first edit.*only contributor/",
1171 array( "first edit", null ),
1172 array( "second edit", null ),
1174 "/second edit.*only contributor/",
1180 array( "first edit", "127.0.2.22" ),
1181 array( "second edit", "127.0.3.33" ),
1191 . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
1192 . " nonumy eirmod tempor invidunt ut labore et dolore magna "
1193 . "aliquyam erat, sed diam voluptua. At vero eos et accusam "
1194 . "et justo duo dolores et ea rebum. Stet clita kasd gubergren, "
1195 . "no sea takimata sanctus est Lorem ipsum dolor sit amet.'",
1199 '/first edit:.*\.\.\."/',
1205 array( "first edit", "127.0.2.22" ),
1206 array( "", "127.0.3.33" ),
1208 "/before blanking.*first edit/",
1216 * @dataProvider provideGetAutoDeleteReason
1217 * @covers WikiPage::getAutoDeleteReason
1219 public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
1222 //NOTE: assume Help namespace to contain wikitext
1223 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
1227 foreach ( $edits as $edit ) {
1230 if ( !empty( $edit[1] ) ) {
1231 $user->setName( $edit[1] );
1236 $content = ContentHandler
::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
1238 $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW
: 0, false, $user );
1243 $reason = $page->getAutoDeleteReason( $hasHistory );
1245 if ( is_bool( $expectedResult ) ||
is_null( $expectedResult ) ) {
1246 $this->assertEquals( $expectedResult, $reason );
1248 $this->assertTrue( (bool)preg_match( $expectedResult, $reason ),
1249 "Autosummary didn't match expected pattern $expectedResult: $reason" );
1252 $this->assertEquals( $expectedHistory, $hasHistory,
1253 "expected \$hasHistory to be " . var_export( $expectedHistory, true ) );
1255 $page->doDeleteArticle( "done" );
1258 public static function providePreSaveTransform() {
1260 array( 'hello this is ~~~',
1261 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
1263 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1264 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1270 * @dataProvider providePreSaveTransform
1271 * @covers WikiPage::preSaveTransform
1273 public function testPreSaveTransform( $text, $expected ) {
1274 $this->hideDeprecated( 'WikiPage::preSaveTransform' );
1276 $user->setName( "127.0.0.1" );
1278 //NOTE: assume Help namespace to contain wikitext
1279 $page = $this->newPage( "Help:WikiPageTest_testPreloadTransform" );
1280 $text = $page->preSaveTransform( $text, $user );
1282 $this->assertEquals( $expected, $text );
1286 * @covers WikiPage::factory
1288 public function testWikiPageFactory() {
1289 $title = Title
::makeTitle( NS_FILE
, 'Someimage.png' );
1290 $page = WikiPage
::factory( $title );
1291 $this->assertEquals( 'WikiFilePage', get_class( $page ) );
1293 $title = Title
::makeTitle( NS_CATEGORY
, 'SomeCategory' );
1294 $page = WikiPage
::factory( $title );
1295 $this->assertEquals( 'WikiCategoryPage', get_class( $page ) );
1297 $title = Title
::makeTitle( NS_MAIN
, 'SomePage' );
1298 $page = WikiPage
::factory( $title );
1299 $this->assertEquals( 'WikiPage', get_class( $page ) );