Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / page / WikiPageTest.php
blob0a46f8a18c3e313378f441dfb81abae08d229e49
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 = 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|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
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.",
103 $title,
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();
120 $res->free();
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.",
134 $title,
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();
150 $res->free();
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();
184 $res->free();
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();
210 $res->free();
212 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
216 * @covers WikiPage::doQuickEditContent
218 public function testDoQuickEditContent() {
219 global $wgUser;
221 $page = $this->createPage(
222 "WikiPageTest_testDoQuickEditContent",
223 "original text",
224 CONTENT_MODEL_WIKITEXT
227 $content = ContentHandler::makeContent(
228 "quick text",
229 $page->getTitle(),
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" );
252 $this->assertFalse(
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" );
257 $this->assertFalse(
258 $page->exists(),
259 "WikiPage::exists should return false after page was deleted"
261 $this->assertNull(
262 $page->getContent(),
263 "WikiPage::getContent should return null after page was deleted"
265 $this->assertFalse(
266 $page->getText(),
267 "WikiPage::getText should return false after page was deleted"
270 $t = Title::newFromText( $page->getTitle()->getPrefixedText() );
271 $this->assertFalse(
272 $t->exists(),
273 "Title::exists should return false after page was deleted"
276 // Run the job queue
277 JobQueueGroup::destroySingletons();
278 $jobs = new RunJobs;
279 $jobs->loadParamsAndArgs( null, array( 'quiet' => true ), null );
280 $jobs->execute();
282 # ------------------------
283 $dbr = wfGetDB( DB_SLAVE );
284 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
285 $n = $res->numRows();
286 $res->free();
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 );
306 // Run the job queue
307 JobQueueGroup::destroySingletons();
308 $jobs = new RunJobs;
309 $jobs->loadParamsAndArgs( null, array( 'quiet' => true ), null );
310 $jobs->execute();
312 # ------------------------
313 $dbr = wfGetDB( DB_SLAVE );
314 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
315 $n = $res->numRows();
316 $res->free();
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 );
330 # -----------------
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 );
348 # -----------------
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 );
366 # -----------------
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 );
384 # -----------------
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",
403 "some text",
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",
423 "some text",
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() );
438 # -----------------
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() );
445 # -----------------
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() {
454 return array(
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() );
471 if ( $create ) {
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() {
481 return array(
482 array( 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT, "hello world", null ),
483 array(
484 'WikiPageTest_testGetRedirectTarget_2',
485 CONTENT_MODEL_WIKITEXT,
486 "#REDIRECT [[hello world]]",
487 "Hello world"
493 * @dataProvider provideGetRedirectTarget
494 * @covers WikiPage::getRedirectTarget
496 public function testGetRedirectTarget( $title, $model, $text, $target ) {
497 $this->setMwGlobals( array(
498 'wgCapitalLinks' => true,
499 ) );
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() {
522 return array(
524 // any
525 array( 'WikiPageTest_testIsCountable',
526 CONTENT_MODEL_WIKITEXT,
528 'any',
529 true
531 array( 'WikiPageTest_testIsCountable',
532 CONTENT_MODEL_WIKITEXT,
533 'Foo',
534 'any',
535 true
538 // comma
539 array( 'WikiPageTest_testIsCountable',
540 CONTENT_MODEL_WIKITEXT,
541 'Foo',
542 'comma',
543 false
545 array( 'WikiPageTest_testIsCountable',
546 CONTENT_MODEL_WIKITEXT,
547 'Foo, bar',
548 'comma',
549 true
552 // link
553 array( 'WikiPageTest_testIsCountable',
554 CONTENT_MODEL_WIKITEXT,
555 'Foo',
556 'link',
557 false
559 array( 'WikiPageTest_testIsCountable',
560 CONTENT_MODEL_WIKITEXT,
561 'Foo [[bar]]',
562 'link',
563 true
566 // redirects
567 array( 'WikiPageTest_testIsCountable',
568 CONTENT_MODEL_WIKITEXT,
569 '#REDIRECT [[bar]]',
570 'any',
571 false
573 array( 'WikiPageTest_testIsCountable',
574 CONTENT_MODEL_WIKITEXT,
575 '#REDIRECT [[bar]]',
576 'comma',
577 false
579 array( 'WikiPageTest_testIsCountable',
580 CONTENT_MODEL_WIKITEXT,
581 '#REDIRECT [[bar]]',
582 'link',
583 false
586 // not a content namespace
587 array( 'Talk:WikiPageTest_testIsCountable',
588 CONTENT_MODEL_WIKITEXT,
589 'Foo',
590 'any',
591 false
593 array( 'Talk:WikiPageTest_testIsCountable',
594 CONTENT_MODEL_WIKITEXT,
595 'Foo, bar',
596 'comma',
597 false
599 array( 'Talk:WikiPageTest_testIsCountable',
600 CONTENT_MODEL_WIKITEXT,
601 'Foo [[bar]]',
602 'link',
603 false
606 // not a content namespace, different model
607 array( 'MediaWiki:WikiPageTest_testIsCountable.js',
608 null,
609 'Foo',
610 'any',
611 false
613 array( 'MediaWiki:WikiPageTest_testIsCountable.js',
614 null,
615 'Foo, bar',
616 'comma',
617 false
619 array( 'MediaWiki:WikiPageTest_testIsCountable.js',
620 null,
621 'Foo [[bar]]',
622 'link',
623 false
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
640 && $model
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 );
654 $this->assertEquals(
655 $expected,
657 "isCountable( null ) returned unexpected value " . var_export( $v, true )
658 . " instead of " . var_export( $expected, true )
659 . " in mode `$mode` for text \"$text\""
662 $this->assertEquals(
663 $expected,
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() {
672 return array(
673 array( CONTENT_MODEL_WIKITEXT, "hello ''world''\n", "<p>hello <i>world</i></p>" ),
674 // @todo more...?
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 );
694 return $po;
698 * @covers WikiPage::getParserOutput
700 public function testGetParserOutput_nonexisting() {
701 static $count = 0;
702 $count++;
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 =
728 "Intro
730 == stuff ==
731 hello world
733 == test ==
734 just a test
736 == foo ==
737 more stuff
740 public function dataReplaceSection() {
741 // NOTE: assume the Help namespace to contain wikitext
742 return array(
743 array( 'Help:WikiPageTest_testReplaceSection',
744 CONTENT_MODEL_WIKITEXT,
745 WikiPageTest::$sections,
746 "0",
747 "No more",
748 null,
749 trim( preg_replace( '/^Intro/sm', 'No more', WikiPageTest::$sections ) )
751 array( 'Help:WikiPageTest_testReplaceSection',
752 CONTENT_MODEL_WIKITEXT,
753 WikiPageTest::$sections,
755 "No more",
756 null,
757 "No more"
759 array( 'Help:WikiPageTest_testReplaceSection',
760 CONTENT_MODEL_WIKITEXT,
761 WikiPageTest::$sections,
762 "2",
763 "== TEST ==\nmore fun",
764 null,
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,
772 "8",
773 "No more",
774 null,
775 trim( WikiPageTest::$sections )
777 array( 'Help:WikiPageTest_testReplaceSection',
778 CONTENT_MODEL_WIKITEXT,
779 WikiPageTest::$sections,
780 "new",
781 "No more",
782 "New",
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();
839 $text = "one";
840 $page = $this->createPage( "WikiPageTest_testGetUndoText", $text );
841 $rev1 = $page->getRevision();
843 $text .= "\n\ntwo";
844 $page->doEditContent(
845 ContentHandler::makeContent( $text, $page->getTitle() ),
846 "adding section two"
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();
857 $text .= "\n\nfour";
858 $page->doEditContent(
859 ContentHandler::makeContent( $text, $page->getTitle() ),
860 "adding section four"
862 $rev4 = $page->getRevision();
864 $text .= "\n\nfive";
865 $page->doEditContent(
866 ContentHandler::makeContent( $text, $page->getTitle() ),
867 "adding section five"
869 $rev5 = $page->getRevision();
871 $text .= "\n\nsix";
872 $page->doEditContent(
873 ContentHandler::makeContent( $text, $page->getTitle() ),
874 "adding section six"
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() {
897 $admin = new User();
898 $admin->setName( "Admin" );
900 $text = "one";
901 $page = $this->newPage( "WikiPageTest_testDoRollback" );
902 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
903 "section one", EDIT_NEW, false, $admin );
905 $user1 = new User();
906 $user1->setName( "127.0.1.11" );
907 $text .= "\n\ntwo";
908 $page = new WikiPage( $page->getTitle() );
909 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
910 "adding section two", 0, false, $user1 );
912 $user2 = new User();
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() ),
939 null
941 $errors = $page->doRollback(
942 $user2->getName(),
943 "testing revert",
944 $token,
945 false,
946 $details,
947 $admin
950 if ( $errors ) {
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() {
966 $admin = new User();
967 $admin->setName( "Admin" );
969 $text = "one";
970 $page = $this->newPage( "WikiPageTest_testDoRollback" );
971 $page->doEditContent(
972 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
973 "section one",
974 EDIT_NEW,
975 false,
976 $admin
978 $rev1 = $page->getRevision();
980 $user1 = new User();
981 $user1->setName( "127.0.1.11" );
982 $text .= "\n\ntwo";
983 $page = new WikiPage( $page->getTitle() );
984 $page->doEditContent(
985 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
986 "adding section two",
988 false,
989 $user1
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() ),
996 null
998 $errors = $page->doRollback(
999 $user1->getName(),
1000 "testing revert",
1001 $token,
1002 false,
1003 $details,
1004 $admin
1007 if ( $errors ) {
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...
1026 $text = "one";
1027 $page = $this->newPage( "WikiPageTest_testDoRollback" );
1028 $page->doEditContent(
1029 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
1030 "section one",
1031 EDIT_NEW,
1032 false,
1033 $admin
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...
1040 $text .= "\n\ntwo";
1041 $page = new WikiPage( $page->getTitle() );
1042 $page->doEditContent(
1043 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
1044 "adding section two",
1046 false,
1047 $user1
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() ),
1054 null
1056 $errors = $page->doRollback(
1057 $user1->getName(),
1058 "testing revert same user",
1059 $token,
1060 false,
1061 $resultDetails,
1062 $admin
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() ),
1071 null
1073 $errors = $page->doRollback(
1074 $user1->getName(),
1075 "testing revert",
1076 $token,
1077 false,
1078 $resultDetails,
1079 $admin
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() {
1092 return array(
1093 array(
1094 'Hello there, world!',
1095 '#REDIRECT [[Foo]]',
1097 '/^Redirected page .*Foo/'
1100 array(
1101 null,
1102 'Hello world!',
1103 EDIT_NEW,
1104 '/^Created page .*Hello/'
1107 array(
1108 'Hello there, world!',
1111 '/^Blanked/'
1114 array(
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.',
1119 'Hello world!',
1121 '/^Replaced .*Hello/'
1124 array(
1125 'foo',
1126 'bar',
1128 '/^$/'
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() {
1149 return array(
1150 array(
1151 array(),
1152 false,
1153 false
1156 array(
1157 array(
1158 array( "first edit", null ),
1160 "/first edit.*only contributor/",
1161 false
1164 array(
1165 array(
1166 array( "first edit", null ),
1167 array( "second edit", null ),
1169 "/second edit.*only contributor/",
1170 true
1173 array(
1174 array(
1175 array( "first edit", "127.0.2.22" ),
1176 array( "second edit", "127.0.3.33" ),
1178 "/second edit/",
1179 true
1182 array(
1183 array(
1184 array(
1185 "first edit: "
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.'",
1191 null
1194 '/first edit:.*\.\.\."/',
1195 false
1198 array(
1199 array(
1200 array( "first edit", "127.0.2.22" ),
1201 array( "", "127.0.3.33" ),
1203 "/before blanking.*first edit/",
1204 true
1211 * @dataProvider provideGetAutoDeleteReason
1212 * @covers WikiPage::getAutoDeleteReason
1214 public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
1215 global $wgUser;
1217 // NOTE: assume Help namespace to contain wikitext
1218 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
1220 $c = 1;
1222 foreach ( $edits as $edit ) {
1223 $user = new User();
1225 if ( !empty( $edit[1] ) ) {
1226 $user->setName( $edit[1] );
1227 } else {
1228 $user = $wgUser;
1231 $content = ContentHandler::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
1233 $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW : 0, false, $user );
1235 $c += 1;
1238 $reason = $page->getAutoDeleteReason( $hasHistory );
1240 if ( is_bool( $expectedResult ) || is_null( $expectedResult ) ) {
1241 $this->assertEquals( $expectedResult, $reason );
1242 } else {
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() {
1254 return array(
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' );
1270 $user = new User();
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 ) );