test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / WikiPageTest.php
blobbf8cd37e9300b126e899c415d444d93087d17b49
1 <?php
2 /**
3 * @group ContentHandler
4 * @group Database
5 * ^--- important, causes temporary tables to be used instead of the real database
6 * @group medium
7 **/
9 class WikiPageTest extends MediaWikiLangTestCase {
11 var $pages_to_delete;
13 function __construct( $name = null, array $data = array(), $dataName = '' ) {
14 parent::__construct( $name, $data, $dataName );
16 $this->tablesUsed = array_merge(
17 $this->tablesUsed,
18 array( '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 = 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 */
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 $title
59 * @param String $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 public function testDoEditContent() {
94 $page = $this->newPage( "WikiPageTest_testDoEditContent" );
95 $title = $page->getTitle();
97 $content = ContentHandler::makeContent( "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
98 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
99 $title, CONTENT_MODEL_WIKITEXT );
101 $page->doEditContent( $content, "[[testing]] 1" );
103 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
104 $this->assertTrue( $page->getId() > 0, "WikiPage should have new page id" );
105 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
106 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
108 $id = $page->getId();
110 # ------------------------
111 $dbr = wfGetDB( DB_SLAVE );
112 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
113 $n = $res->numRows();
114 $res->free();
116 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
118 # ------------------------
119 $page = new WikiPage( $title );
121 $retrieved = $page->getContent();
122 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
124 # ------------------------
125 $content = ContentHandler::makeContent( "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
126 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est.",
127 $title, CONTENT_MODEL_WIKITEXT );
129 $page->doEditContent( $content, "testing 2" );
131 # ------------------------
132 $page = new WikiPage( $title );
134 $retrieved = $page->getContent();
135 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
137 # ------------------------
138 $dbr = wfGetDB( DB_SLAVE );
139 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
140 $n = $res->numRows();
141 $res->free();
143 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
146 public function testDoEdit() {
147 $this->hideDeprecated( "WikiPage::doEdit" );
148 $this->hideDeprecated( "WikiPage::getText" );
149 $this->hideDeprecated( "Revision::getText" );
151 //NOTE: assume help namespace will default to wikitext
152 $title = Title::newFromText( "Help:WikiPageTest_testDoEdit" );
154 $page = $this->newPage( $title );
156 $text = "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
157 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.";
159 $page->doEdit( $text, "[[testing]] 1" );
161 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
162 $this->assertTrue( $page->getId() > 0, "WikiPage should have new page id" );
163 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
164 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
166 $id = $page->getId();
168 # ------------------------
169 $dbr = wfGetDB( DB_SLAVE );
170 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
171 $n = $res->numRows();
172 $res->free();
174 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
176 # ------------------------
177 $page = new WikiPage( $title );
179 $retrieved = $page->getText();
180 $this->assertEquals( $text, $retrieved, 'retrieved text doesn\'t equal original' );
182 # ------------------------
183 $text = "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
184 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est.";
186 $page->doEdit( $text, "testing 2" );
188 # ------------------------
189 $page = new WikiPage( $title );
191 $retrieved = $page->getText();
192 $this->assertEquals( $text, $retrieved, 'retrieved text doesn\'t equal original' );
194 # ------------------------
195 $dbr = wfGetDB( DB_SLAVE );
196 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
197 $n = $res->numRows();
198 $res->free();
200 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
203 public function testDoQuickEdit() {
204 global $wgUser;
206 $this->hideDeprecated( "WikiPage::doQuickEdit" );
208 //NOTE: assume help namespace will default to wikitext
209 $page = $this->createPage( "Help:WikiPageTest_testDoQuickEdit", "original text" );
211 $text = "quick text";
212 $page->doQuickEdit( $text, $wgUser, "testing q" );
214 # ---------------------
215 $page = new WikiPage( $page->getTitle() );
216 $this->assertEquals( $text, $page->getText() );
219 public function testDoQuickEditContent() {
220 global $wgUser;
222 $page = $this->createPage( "WikiPageTest_testDoQuickEditContent", "original text", CONTENT_MODEL_WIKITEXT );
224 $content = ContentHandler::makeContent( "quick text", $page->getTitle(), CONTENT_MODEL_WIKITEXT );
225 $page->doQuickEditContent( $content, $wgUser, "testing q" );
227 # ---------------------
228 $page = new WikiPage( $page->getTitle() );
229 $this->assertTrue( $content->equals( $page->getContent() ) );
232 public function testDoDeleteArticle() {
233 $page = $this->createPage( "WikiPageTest_testDoDeleteArticle", "[[original text]] foo", CONTENT_MODEL_WIKITEXT );
234 $id = $page->getId();
236 $page->doDeleteArticle( "testing deletion" );
238 $this->assertFalse( $page->getTitle()->getArticleID() > 0, "Title object should now have page id 0" );
239 $this->assertFalse( $page->getId() > 0, "WikiPage should now have page id 0" );
240 $this->assertFalse( $page->exists(), "WikiPage::exists should return false after page was deleted" );
241 $this->assertNull( $page->getContent(), "WikiPage::getContent should return null after page was deleted" );
242 $this->assertFalse( $page->getText(), "WikiPage::getText should return false after page was deleted" );
244 $t = Title::newFromText( $page->getTitle()->getPrefixedText() );
245 $this->assertFalse( $t->exists(), "Title::exists should return false after page was deleted" );
247 # ------------------------
248 $dbr = wfGetDB( DB_SLAVE );
249 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
250 $n = $res->numRows();
251 $res->free();
253 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
256 public function testDoDeleteUpdates() {
257 $page = $this->createPage( "WikiPageTest_testDoDeleteArticle", "[[original text]] foo", CONTENT_MODEL_WIKITEXT );
258 $id = $page->getId();
260 $page->doDeleteUpdates( $id );
262 # ------------------------
263 $dbr = wfGetDB( DB_SLAVE );
264 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
265 $n = $res->numRows();
266 $res->free();
268 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
271 public function testGetRevision() {
272 $page = $this->newPage( "WikiPageTest_testGetRevision" );
274 $rev = $page->getRevision();
275 $this->assertNull( $rev );
277 # -----------------
278 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
280 $rev = $page->getRevision();
282 $this->assertEquals( $page->getLatest(), $rev->getId() );
283 $this->assertEquals( "some text", $rev->getContent()->getNativeData() );
286 public function testGetContent() {
287 $page = $this->newPage( "WikiPageTest_testGetContent" );
289 $content = $page->getContent();
290 $this->assertNull( $content );
292 # -----------------
293 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
295 $content = $page->getContent();
296 $this->assertEquals( "some text", $content->getNativeData() );
299 public function testGetText() {
300 $this->hideDeprecated( "WikiPage::getText" );
302 $page = $this->newPage( "WikiPageTest_testGetText" );
304 $text = $page->getText();
305 $this->assertFalse( $text );
307 # -----------------
308 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
310 $text = $page->getText();
311 $this->assertEquals( "some text", $text );
314 public function testGetRawText() {
315 $this->hideDeprecated( "WikiPage::getRawText" );
317 $page = $this->newPage( "WikiPageTest_testGetRawText" );
319 $text = $page->getRawText();
320 $this->assertFalse( $text );
322 # -----------------
323 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
325 $text = $page->getRawText();
326 $this->assertEquals( "some text", $text );
329 public function testGetContentModel() {
330 global $wgContentHandlerUseDB;
332 if ( !$wgContentHandlerUseDB ) {
333 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
336 $page = $this->createPage( "WikiPageTest_testGetContentModel", "some text", CONTENT_MODEL_JAVASCRIPT );
338 $page = new WikiPage( $page->getTitle() );
339 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $page->getContentModel() );
342 public function testGetContentHandler() {
343 global $wgContentHandlerUseDB;
345 if ( !$wgContentHandlerUseDB ) {
346 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
349 $page = $this->createPage( "WikiPageTest_testGetContentHandler", "some text", CONTENT_MODEL_JAVASCRIPT );
351 $page = new WikiPage( $page->getTitle() );
352 $this->assertEquals( 'JavaScriptContentHandler', get_class( $page->getContentHandler() ) );
355 public function testExists() {
356 $page = $this->newPage( "WikiPageTest_testExists" );
357 $this->assertFalse( $page->exists() );
359 # -----------------
360 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
361 $this->assertTrue( $page->exists() );
363 $page = new WikiPage( $page->getTitle() );
364 $this->assertTrue( $page->exists() );
366 # -----------------
367 $page->doDeleteArticle( "done testing" );
368 $this->assertFalse( $page->exists() );
370 $page = new WikiPage( $page->getTitle() );
371 $this->assertFalse( $page->exists() );
374 public static function provideHasViewableContent() {
375 return array(
376 array( 'WikiPageTest_testHasViewableContent', false, true ),
377 array( 'Special:WikiPageTest_testHasViewableContent', false ),
378 array( 'MediaWiki:WikiPageTest_testHasViewableContent', false ),
379 array( 'Special:Userlogin', true ),
380 array( 'MediaWiki:help', true ),
385 * @dataProvider provideHasViewableContent
387 public function testHasViewableContent( $title, $viewable, $create = false ) {
388 $page = $this->newPage( $title );
389 $this->assertEquals( $viewable, $page->hasViewableContent() );
391 if ( $create ) {
392 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
393 $this->assertTrue( $page->hasViewableContent() );
395 $page = new WikiPage( $page->getTitle() );
396 $this->assertTrue( $page->hasViewableContent() );
400 public static function provideGetRedirectTarget() {
401 return array(
402 array( 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT, "hello world", null ),
403 array( 'WikiPageTest_testGetRedirectTarget_2', CONTENT_MODEL_WIKITEXT, "#REDIRECT [[hello world]]", "Hello world" ),
408 * @dataProvider provideGetRedirectTarget
410 public function testGetRedirectTarget( $title, $model, $text, $target ) {
411 $page = $this->createPage( $title, $text, $model );
413 # sanity check, because this test seems to fail for no reason for some people.
414 $c = $page->getContent();
415 $this->assertEquals( 'WikitextContent', get_class( $c ) );
417 # now, test the actual redirect
418 $t = $page->getRedirectTarget();
419 $this->assertEquals( $target, is_null( $t ) ? null : $t->getPrefixedText() );
423 * @dataProvider provideGetRedirectTarget
425 public function testIsRedirect( $title, $model, $text, $target ) {
426 $page = $this->createPage( $title, $text, $model );
427 $this->assertEquals( !is_null( $target ), $page->isRedirect() );
430 public static function provideIsCountable() {
431 return array(
433 // any
434 array( 'WikiPageTest_testIsCountable',
435 CONTENT_MODEL_WIKITEXT,
437 'any',
438 true
440 array( 'WikiPageTest_testIsCountable',
441 CONTENT_MODEL_WIKITEXT,
442 'Foo',
443 'any',
444 true
447 // comma
448 array( 'WikiPageTest_testIsCountable',
449 CONTENT_MODEL_WIKITEXT,
450 'Foo',
451 'comma',
452 false
454 array( 'WikiPageTest_testIsCountable',
455 CONTENT_MODEL_WIKITEXT,
456 'Foo, bar',
457 'comma',
458 true
461 // link
462 array( 'WikiPageTest_testIsCountable',
463 CONTENT_MODEL_WIKITEXT,
464 'Foo',
465 'link',
466 false
468 array( 'WikiPageTest_testIsCountable',
469 CONTENT_MODEL_WIKITEXT,
470 'Foo [[bar]]',
471 'link',
472 true
475 // redirects
476 array( 'WikiPageTest_testIsCountable',
477 CONTENT_MODEL_WIKITEXT,
478 '#REDIRECT [[bar]]',
479 'any',
480 false
482 array( 'WikiPageTest_testIsCountable',
483 CONTENT_MODEL_WIKITEXT,
484 '#REDIRECT [[bar]]',
485 'comma',
486 false
488 array( 'WikiPageTest_testIsCountable',
489 CONTENT_MODEL_WIKITEXT,
490 '#REDIRECT [[bar]]',
491 'link',
492 false
495 // not a content namespace
496 array( 'Talk:WikiPageTest_testIsCountable',
497 CONTENT_MODEL_WIKITEXT,
498 'Foo',
499 'any',
500 false
502 array( 'Talk:WikiPageTest_testIsCountable',
503 CONTENT_MODEL_WIKITEXT,
504 'Foo, bar',
505 'comma',
506 false
508 array( 'Talk:WikiPageTest_testIsCountable',
509 CONTENT_MODEL_WIKITEXT,
510 'Foo [[bar]]',
511 'link',
512 false
515 // not a content namespace, different model
516 array( 'MediaWiki:WikiPageTest_testIsCountable.js',
517 null,
518 'Foo',
519 'any',
520 false
522 array( 'MediaWiki:WikiPageTest_testIsCountable.js',
523 null,
524 'Foo, bar',
525 'comma',
526 false
528 array( 'MediaWiki:WikiPageTest_testIsCountable.js',
529 null,
530 'Foo [[bar]]',
531 'link',
532 false
539 * @dataProvider provideIsCountable
541 public function testIsCountable( $title, $model, $text, $mode, $expected ) {
542 global $wgContentHandlerUseDB;
544 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
546 $title = Title::newFromText( $title );
548 if ( !$wgContentHandlerUseDB && $model && ContentHandler::getDefaultModelFor( $title ) != $model ) {
549 $this->markTestSkipped( "Can not use non-default content model $model for "
550 . $title->getPrefixedDBkey() . " with \$wgContentHandlerUseDB disabled." );
553 $page = $this->createPage( $title, $text, $model );
554 $hasLinks = wfGetDB( DB_SLAVE )->selectField( 'pagelinks', 1,
555 array( 'pl_from' => $page->getId() ), __METHOD__ );
557 $editInfo = $page->prepareContentForEdit( $page->getContent() );
559 $v = $page->isCountable();
560 $w = $page->isCountable( $editInfo );
562 $this->assertEquals( $expected, $v, "isCountable( null ) returned unexpected value " . var_export( $v, true )
563 . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
565 $this->assertEquals( $expected, $w, "isCountable( \$editInfo ) returned unexpected value " . var_export( $v, true )
566 . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
569 public static function provideGetParserOutput() {
570 return array(
571 array( CONTENT_MODEL_WIKITEXT, "hello ''world''\n", "<p>hello <i>world</i></p>" ),
572 // @todo more...?
577 * @dataProvider provideGetParserOutput
579 public function testGetParserOutput( $model, $text, $expectedHtml ) {
580 $page = $this->createPage( 'WikiPageTest_testGetParserOutput', $text, $model );
582 $opt = $page->makeParserOptions( 'canonical' );
583 $po = $page->getParserOutput( $opt );
584 $text = $po->getText();
586 $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments
587 $text = preg_replace( '!\s*(</p>)!sm', '\1', $text ); # don't let tidy confuse us
589 $this->assertEquals( $expectedHtml, $text );
591 return $po;
594 public function testGetParserOutput_nonexisting() {
595 static $count = 0;
596 $count++;
598 $page = new WikiPage( new Title( "WikiPageTest_testGetParserOutput_nonexisting_$count" ) );
600 $opt = new ParserOptions();
601 $po = $page->getParserOutput( $opt );
603 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." );
606 public function testGetParserOutput_badrev() {
607 $page = $this->createPage( 'WikiPageTest_testGetParserOutput', "dummy", CONTENT_MODEL_WIKITEXT );
609 $opt = new ParserOptions();
610 $po = $page->getParserOutput( $opt, $page->getLatest() + 1234 );
612 // @todo would be neat to also test deleted revision
614 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." );
617 static $sections =
619 "Intro
621 == stuff ==
622 hello world
624 == test ==
625 just a test
627 == foo ==
628 more stuff
632 public function dataReplaceSection() {
633 //NOTE: assume the Help namespace to contain wikitext
634 return array(
635 array( 'Help:WikiPageTest_testReplaceSection',
636 CONTENT_MODEL_WIKITEXT,
637 WikiPageTest::$sections,
638 "0",
639 "No more",
640 null,
641 trim( preg_replace( '/^Intro/sm', 'No more', WikiPageTest::$sections ) )
643 array( 'Help:WikiPageTest_testReplaceSection',
644 CONTENT_MODEL_WIKITEXT,
645 WikiPageTest::$sections,
647 "No more",
648 null,
649 "No more"
651 array( 'Help:WikiPageTest_testReplaceSection',
652 CONTENT_MODEL_WIKITEXT,
653 WikiPageTest::$sections,
654 "2",
655 "== TEST ==\nmore fun",
656 null,
657 trim( preg_replace( '/^== test ==.*== foo ==/sm',
658 "== TEST ==\nmore fun\n\n== foo ==",
659 WikiPageTest::$sections ) )
661 array( 'Help:WikiPageTest_testReplaceSection',
662 CONTENT_MODEL_WIKITEXT,
663 WikiPageTest::$sections,
664 "8",
665 "No more",
666 null,
667 trim( WikiPageTest::$sections )
669 array( 'Help:WikiPageTest_testReplaceSection',
670 CONTENT_MODEL_WIKITEXT,
671 WikiPageTest::$sections,
672 "new",
673 "No more",
674 "New",
675 trim( WikiPageTest::$sections ) . "\n\n== New ==\n\nNo more"
681 * @dataProvider dataReplaceSection
683 public function testReplaceSection( $title, $model, $text, $section, $with, $sectionTitle, $expected ) {
684 $this->hideDeprecated( "WikiPage::replaceSection" );
686 $page = $this->createPage( $title, $text, $model );
687 $text = $page->replaceSection( $section, $with, $sectionTitle );
688 $text = trim( $text );
690 $this->assertEquals( $expected, $text );
694 * @dataProvider dataReplaceSection
696 public function testReplaceSectionContent( $title, $model, $text, $section, $with, $sectionTitle, $expected ) {
697 $page = $this->createPage( $title, $text, $model );
699 $content = ContentHandler::makeContent( $with, $page->getTitle(), $page->getContentModel() );
700 $c = $page->replaceSectionContent( $section, $content, $sectionTitle );
702 $this->assertEquals( $expected, is_null( $c ) ? null : trim( $c->getNativeData() ) );
705 /* @todo FIXME: fix this!
706 public function testGetUndoText() {
707 $this->checkHasDiff3();
709 $text = "one";
710 $page = $this->createPage( "WikiPageTest_testGetUndoText", $text );
711 $rev1 = $page->getRevision();
713 $text .= "\n\ntwo";
714 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ), "adding section two");
715 $rev2 = $page->getRevision();
717 $text .= "\n\nthree";
718 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ), "adding section three");
719 $rev3 = $page->getRevision();
721 $text .= "\n\nfour";
722 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ), "adding section four");
723 $rev4 = $page->getRevision();
725 $text .= "\n\nfive";
726 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ), "adding section five");
727 $rev5 = $page->getRevision();
729 $text .= "\n\nsix";
730 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ), "adding section six");
731 $rev6 = $page->getRevision();
733 $undo6 = $page->getUndoText( $rev6 );
734 if ( $undo6 === false ) $this->fail( "getUndoText failed for rev6" );
735 $this->assertEquals( "one\n\ntwo\n\nthree\n\nfour\n\nfive", $undo6 );
737 $undo3 = $page->getUndoText( $rev4, $rev2 );
738 if ( $undo3 === false ) $this->fail( "getUndoText failed for rev4..rev2" );
739 $this->assertEquals( "one\n\ntwo\n\nfive", $undo3 );
741 $undo2 = $page->getUndoText( $rev2 );
742 if ( $undo2 === false ) $this->fail( "getUndoText failed for rev2" );
743 $this->assertEquals( "one\n\nfive", $undo2 );
748 * @todo FIXME: this is a better rollback test than the one below, but it keeps failing in jenkins for some reason.
750 public function broken_testDoRollback() {
751 $admin = new User();
752 $admin->setName( "Admin" );
754 $text = "one";
755 $page = $this->newPage( "WikiPageTest_testDoRollback" );
756 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
757 "section one", EDIT_NEW, false, $admin );
759 $user1 = new User();
760 $user1->setName( "127.0.1.11" );
761 $text .= "\n\ntwo";
762 $page = new WikiPage( $page->getTitle() );
763 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
764 "adding section two", 0, false, $user1 );
766 $user2 = new User();
767 $user2->setName( "127.0.2.13" );
768 $text .= "\n\nthree";
769 $page = new WikiPage( $page->getTitle() );
770 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
771 "adding section three", 0, false, $user2 );
773 # we are having issues with doRollback spuriously failing. apparently the last revision somehow goes missing
774 # or not committed under some circumstances. so, make sure the last revision has the right user name.
775 $dbr = wfGetDB( DB_SLAVE );
776 $this->assertEquals( 3, Revision::countByPageId( $dbr, $page->getId() ) );
778 $page = new WikiPage( $page->getTitle() );
779 $rev3 = $page->getRevision();
780 $this->assertEquals( '127.0.2.13', $rev3->getUserText() );
782 $rev2 = $rev3->getPrevious();
783 $this->assertEquals( '127.0.1.11', $rev2->getUserText() );
785 $rev1 = $rev2->getPrevious();
786 $this->assertEquals( 'Admin', $rev1->getUserText() );
788 # now, try the actual rollback
789 $admin->addGroup( "sysop" ); #XXX: make the test user a sysop...
790 $token = $admin->getEditToken( array( $page->getTitle()->getPrefixedText(), $user2->getName() ), null );
791 $errors = $page->doRollback( $user2->getName(), "testing revert", $token, false, $details, $admin );
793 if ( $errors ) {
794 $this->fail( "Rollback failed:\n" . print_r( $errors, true ) . ";\n" . print_r( $details, true ) );
797 $page = new WikiPage( $page->getTitle() );
798 $this->assertEquals( $rev2->getSha1(), $page->getRevision()->getSha1(),
799 "rollback did not revert to the correct revision" );
800 $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
804 * @todo FIXME: the above rollback test is better, but it keeps failing in jenkins for some reason.
806 public function testDoRollback() {
807 $admin = new User();
808 $admin->setName( "Admin" );
810 $text = "one";
811 $page = $this->newPage( "WikiPageTest_testDoRollback" );
812 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
813 "section one", EDIT_NEW, false, $admin );
814 $rev1 = $page->getRevision();
816 $user1 = new User();
817 $user1->setName( "127.0.1.11" );
818 $text .= "\n\ntwo";
819 $page = new WikiPage( $page->getTitle() );
820 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
821 "adding section two", 0, false, $user1 );
823 # now, try the rollback
824 $admin->addGroup( "sysop" ); #XXX: make the test user a sysop...
825 $token = $admin->getEditToken( array( $page->getTitle()->getPrefixedText(), $user1->getName() ), null );
826 $errors = $page->doRollback( $user1->getName(), "testing revert", $token, false, $details, $admin );
828 if ( $errors ) {
829 $this->fail( "Rollback failed:\n" . print_r( $errors, true ) . ";\n" . print_r( $details, true ) );
832 $page = new WikiPage( $page->getTitle() );
833 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
834 "rollback did not revert to the correct revision" );
835 $this->assertEquals( "one", $page->getContent()->getNativeData() );
838 public static function provideGetAutosummary() {
839 return array(
840 array(
841 'Hello there, world!',
842 '#REDIRECT [[Foo]]',
844 '/^Redirected page .*Foo/'
847 array(
848 null,
849 'Hello world!',
850 EDIT_NEW,
851 '/^Created page .*Hello/'
854 array(
855 'Hello there, world!',
858 '/^Blanked/'
861 array(
862 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
863 labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et
864 ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
865 'Hello world!',
867 '/^Replaced .*Hello/'
870 array(
871 'foo',
872 'bar',
874 '/^$/'
880 * @dataProvider provideGetAutoSummary
882 public function testGetAutosummary( $old, $new, $flags, $expected ) {
883 $this->hideDeprecated( "WikiPage::getAutosummary" );
885 $page = $this->newPage( "WikiPageTest_testGetAutosummary" );
887 $summary = $page->getAutosummary( $old, $new, $flags );
889 $this->assertTrue( (bool)preg_match( $expected, $summary ),
890 "Autosummary didn't match expected pattern $expected: $summary" );
893 public static function provideGetAutoDeleteReason() {
894 return array(
895 array(
896 array(),
897 false,
898 false
901 array(
902 array(
903 array( "first edit", null ),
905 "/first edit.*only contributor/",
906 false
909 array(
910 array(
911 array( "first edit", null ),
912 array( "second edit", null ),
914 "/second edit.*only contributor/",
915 true
918 array(
919 array(
920 array( "first edit", "127.0.2.22" ),
921 array( "second edit", "127.0.3.33" ),
923 "/second edit/",
924 true
927 array(
928 array(
929 array( "first edit: "
930 . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
931 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. "
932 . "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea "
933 . "takimata sanctus est Lorem ipsum dolor sit amet.'", null ),
935 '/first edit:.*\.\.\."/',
936 false
939 array(
940 array(
941 array( "first edit", "127.0.2.22" ),
942 array( "", "127.0.3.33" ),
944 "/before blanking.*first edit/",
945 true
952 * @dataProvider provideGetAutoDeleteReason
954 public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
955 global $wgUser;
957 //NOTE: assume Help namespace to contain wikitext
958 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
960 $c = 1;
962 foreach ( $edits as $edit ) {
963 $user = new User();
965 if ( !empty( $edit[1] ) ) {
966 $user->setName( $edit[1] );
967 } else {
968 $user = $wgUser;
971 $content = ContentHandler::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
973 $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW : 0, false, $user );
975 $c += 1;
978 $reason = $page->getAutoDeleteReason( $hasHistory );
980 if ( is_bool( $expectedResult ) || is_null( $expectedResult ) ) {
981 $this->assertEquals( $expectedResult, $reason );
982 } else {
983 $this->assertTrue( (bool)preg_match( $expectedResult, $reason ),
984 "Autosummary didn't match expected pattern $expectedResult: $reason" );
987 $this->assertEquals( $expectedHistory, $hasHistory,
988 "expected \$hasHistory to be " . var_export( $expectedHistory, true ) );
990 $page->doDeleteArticle( "done" );
993 public static function providePreSaveTransform() {
994 return array(
995 array( 'hello this is ~~~',
996 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
998 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
999 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1005 * @dataProvider providePreSaveTransform
1007 public function testPreSaveTransform( $text, $expected ) {
1008 $this->hideDeprecated( 'WikiPage::preSaveTransform' );
1009 $user = new User();
1010 $user->setName( "127.0.0.1" );
1012 //NOTE: assume Help namespace to contain wikitext
1013 $page = $this->newPage( "Help:WikiPageTest_testPreloadTransform" );
1014 $text = $page->preSaveTransform( $text, $user );
1016 $this->assertEquals( $expected, $text );