7 public function __construct( $name =
null, array $data = [], $dataName =
'' ) {
8 parent::__construct( $name, $data, $dataName );
10 $this->tablesUsed = array_merge(
36 $this->pagesToDelete = [];
40 foreach ( $this->pagesToDelete as $p ) {
45 $p->doDeleteArticle(
"testing done." );
61 private function newPage( $title, $model =
null ) {
62 if ( is_string( $title ) ) {
64 $title = Title::newFromText( $title, $ns );
69 $this->pagesToDelete[] = $p;
81 protected function createPage( $page, $text, $model =
null ) {
82 if ( is_string( $page ) || $page instanceof
Title ) {
83 $page = $this->
newPage( $page, $model );
86 $content = ContentHandler::makeContent( $text, $page->getTitle(), $model );
87 $page->doEditContent( $content,
"testing",
EDIT_NEW );
99 $page = $this->
newPage( __METHOD__ );
100 $title = $page->getTitle();
102 $content = ContentHandler::makeContent(
103 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
104 .
" nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
109 $page->doEditContent( $content,
"[[testing]] 1" );
111 $this->assertTrue( $title->getArticleID() > 0,
"Title object should have new page id" );
112 $this->assertTrue( $page->getId() > 0,
"WikiPage should have new page id" );
113 $this->assertTrue( $title->exists(),
"Title object should indicate that the page now exists" );
114 $this->assertTrue( $page->exists(),
"WikiPage object should indicate that the page now exists" );
116 $id = $page->getId();
118 # ------------------------
120 $res =
$dbr->select(
'pagelinks',
'*', [
'pl_from' => $id ] );
121 $n =
$res->numRows();
124 $this->assertEquals( 1, $n,
'pagelinks should contain one link from the page' );
126 # ------------------------
129 $retrieved = $page->getContent();
130 $this->assertTrue( $content->equals( $retrieved ),
'retrieved content doesn\'t equal original' );
132 # ------------------------
133 $content = ContentHandler::makeContent(
134 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
135 .
"Stet clita kasd [[gubergren]], no sea takimata sanctus est.",
140 $page->doEditContent( $content,
"testing 2" );
142 # ------------------------
145 $retrieved = $page->getContent();
146 $this->assertTrue( $content->equals( $retrieved ),
'retrieved content doesn\'t equal original' );
148 # ------------------------
150 $res =
$dbr->select(
'pagelinks',
'*', [
'pl_from' => $id ] );
151 $n =
$res->numRows();
154 $this->assertEquals( 2, $n,
'pagelinks should contain two links from the page' );
164 "[[original text]] foo",
167 $id = $page->getId();
169 $page->doDeleteArticle(
"testing deletion" );
172 $page->getTitle()->getArticleID() > 0,
173 "Title object should now have page id 0"
175 $this->assertFalse( $page->getId() > 0,
"WikiPage should now have page id 0" );
178 "WikiPage::exists should return false after page was deleted"
182 "WikiPage::getContent should return null after page was deleted"
185 $t = Title::newFromText( $page->getTitle()->getPrefixedText() );
188 "Title::exists should return false after page was deleted"
197 # ------------------------
199 $res =
$dbr->select(
'pagelinks',
'*', [
'pl_from' => $id ] );
200 $n =
$res->numRows();
203 $this->assertEquals( 0, $n,
'pagelinks should contain no more links from the page' );
212 "[[original text]] foo",
215 $id = $page->getId();
219 $page->doDeleteUpdates( $id );
227 # ------------------------
229 $res =
$dbr->select(
'pagelinks',
'*', [
'pl_from' => $id ] );
230 $n =
$res->numRows();
233 $this->assertEquals( 0, $n,
'pagelinks should contain no more links from the page' );
240 $page = $this->
newPage( __METHOD__ );
242 $rev = $page->getRevision();
243 $this->assertNull(
$rev );
248 $rev = $page->getRevision();
250 $this->assertEquals( $page->getLatest(),
$rev->getId() );
251 $this->assertEquals(
"some text",
$rev->getContent()->getNativeData() );
258 $page = $this->
newPage( __METHOD__ );
260 $content = $page->getContent();
261 $this->assertNull( $content );
266 $content = $page->getContent();
267 $this->assertEquals(
"some text", $content->getNativeData() );
274 $page = $this->
newPage( __METHOD__ );
275 $this->assertFalse( $page->exists() );
279 $this->assertTrue( $page->exists() );
281 $page =
new WikiPage( $page->getTitle() );
282 $this->assertTrue( $page->exists() );
285 $page->doDeleteArticle(
"done testing" );
286 $this->assertFalse( $page->exists() );
288 $page =
new WikiPage( $page->getTitle() );
289 $this->assertFalse( $page->exists() );
294 [
'WikiPageTest_testHasViewableContent',
false,
true ],
295 [
'Special:WikiPageTest_testHasViewableContent',
false ],
296 [
'MediaWiki:WikiPageTest_testHasViewableContent',
false ],
297 [
'Special:Userlogin',
true ],
298 [
'MediaWiki:help',
true ],
307 $page = $this->
newPage( $title );
308 $this->assertEquals( $viewable, $page->hasViewableContent() );
312 $this->assertTrue( $page->hasViewableContent() );
314 $page =
new WikiPage( $page->getTitle() );
315 $this->assertTrue( $page->hasViewableContent() );
323 'WikiPageTest_testGetRedirectTarget_2',
325 "#REDIRECT [[hello world]]",
337 'wgCapitalLinks' =>
true,
340 $page = $this->
createPage( $title, $text, $model );
342 # sanity check, because this test seems to fail for no reason for some people.
343 $c = $page->getContent();
344 $this->assertEquals( WikitextContent::class, get_class( $c ) );
346 # now, test the actual redirect
347 $t = $page->getRedirectTarget();
348 $this->assertEquals( $target, is_null(
$t ) ?
null :
$t->getPrefixedText() );
356 $page = $this->
createPage( $title, $text, $model );
357 $this->assertEquals( !is_null( $target ), $page->isRedirect() );
364 [
'WikiPageTest_testIsCountable',
370 [
'WikiPageTest_testIsCountable',
378 [
'WikiPageTest_testIsCountable',
384 [
'WikiPageTest_testIsCountable',
392 [
'WikiPageTest_testIsCountable',
398 [
'WikiPageTest_testIsCountable',
406 [
'Talk:WikiPageTest_testIsCountable',
412 [
'Talk:WikiPageTest_testIsCountable',
420 [
'MediaWiki:WikiPageTest_testIsCountable.js',
426 [
'MediaWiki:WikiPageTest_testIsCountable.js',
444 $title = Title::newFromText( $title );
448 && ContentHandler::getDefaultModelFor( $title ) != $model
450 $this->markTestSkipped(
"Can not use non-default content model $model for "
451 . $title->getPrefixedDBkey() .
" with \$wgContentHandlerUseDB disabled." );
454 $page = $this->
createPage( $title, $text, $model );
456 $editInfo = $page->prepareContentForEdit( $page->getContent() );
458 $v = $page->isCountable();
459 $w = $page->isCountable( $editInfo );
464 "isCountable( null ) returned unexpected value " . var_export( $v,
true )
465 .
" instead of " . var_export( $expected,
true )
466 .
" in mode `$mode` for text \"$text\""
472 "isCountable( \$editInfo ) returned unexpected value " . var_export( $v,
true )
473 .
" instead of " . var_export( $expected,
true )
474 .
" in mode `$mode` for text \"$text\""
483 "<div class=\"mw-parser-output\"><p>hello <i>world</i></p></div>"
494 $page = $this->
createPage( __METHOD__, $text, $model );
496 $opt = $page->makeParserOptions(
'canonical' );
497 $po = $page->getParserOutput(
$opt );
498 $text = $po->getText();
500 $text = trim( preg_replace(
'/<!--.*?-->/sm',
'', $text ) ); # strip injected comments
501 $text = preg_replace(
'!\s*(</p>|</div>)!sm',
'\1', $text ); # don
't let tidy confuse us
503 $this->assertEquals( $expectedHtml, $text );
511 public function testGetParserOutput_nonexisting() {
512 $page = new WikiPage( Title::newFromText( __METHOD__ ) );
514 $opt = new ParserOptions();
515 $po = $page->getParserOutput( $opt );
517 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." );
523 public function testGetParserOutput_badrev() {
524 $page = $this->createPage( __METHOD__, 'dummy
', CONTENT_MODEL_WIKITEXT );
526 $opt = new ParserOptions();
527 $po = $page->getParserOutput( $opt, $page->getLatest() + 1234 );
529 // @todo would be neat to also test deleted revision
531 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." );
534 public static $sections =
548 public function dataReplaceSection() {
549 // NOTE: assume the Help namespace to contain wikitext
551 [ 'Help:WikiPageTest_testReplaceSection
',
552 CONTENT_MODEL_WIKITEXT,
557 trim( preg_replace( '/^Intro/sm
', 'No more
', self::$sections ) )
559 [ 'Help:WikiPageTest_testReplaceSection
',
560 CONTENT_MODEL_WIKITEXT,
567 [ 'Help:WikiPageTest_testReplaceSection
',
568 CONTENT_MODEL_WIKITEXT,
571 "== TEST ==\nmore fun",
573 trim( preg_replace( '/^== test ==.*== foo ==/sm
',
574 "== TEST ==\nmore fun\n\n== foo ==",
577 [ 'Help:WikiPageTest_testReplaceSection
',
578 CONTENT_MODEL_WIKITEXT,
583 trim( self::$sections )
585 [ 'Help:WikiPageTest_testReplaceSection
',
586 CONTENT_MODEL_WIKITEXT,
591 trim( self::$sections ) . "\n\n== New ==\n\nNo more"
600 public function testReplaceSectionContent( $title, $model, $text, $section,
601 $with, $sectionTitle, $expected
603 $page = $this->createPage( $title, $text, $model );
605 $content = ContentHandler::makeContent( $with, $page->getTitle(), $page->getContentModel() );
606 $c = $page->replaceSectionContent( $section, $content, $sectionTitle );
608 $this->assertEquals( $expected, is_null( $c ) ? null : trim( $c->getNativeData() ) );
615 public function testReplaceSectionAtRev( $title, $model, $text, $section,
616 $with, $sectionTitle, $expected
618 $page = $this->createPage( $title, $text, $model );
619 $baseRevId = $page->getLatest();
621 $content = ContentHandler::makeContent( $with, $page->getTitle(), $page->getContentModel() );
622 $c = $page->replaceSectionAtRev( $section, $content, $sectionTitle, $baseRevId );
624 $this->assertEquals( $expected, is_null( $c ) ? null : trim( $c->getNativeData() ) );
630 public function testGetOldestRevision() {
631 $page = $this->newPage( __METHOD__ );
632 $page->doEditContent(
633 new WikitextContent( 'one
' ),
637 $rev1 = $page->getRevision();
639 $page = new WikiPage( $page->getTitle() );
640 $page->doEditContent(
641 new WikitextContent( 'two
' ),
646 $page = new WikiPage( $page->getTitle() );
647 $page->doEditContent(
648 new WikitextContent( 'three
' ),
654 $this->assertNotEquals(
656 $page->getRevision()->getId(),
657 '$page->getRevision()->getId()
'
663 $page->getOldestRevision()->getId(),
664 '$page->getOldestRevision()->getId()
'
672 public function testDoRollback() {
673 $admin = $this->getTestSysop()->getUser();
674 $user1 = $this->getTestUser()->getUser();
675 // Use the confirmed group for user2 to make sure the user is different
676 $user2 = $this->getTestUser( [ 'confirmed
' ] )->getUser();
678 $page = $this->newPage( __METHOD__ );
682 $status1 = $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
683 "section one", EDIT_NEW, false, $admin );
686 $status2 = $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
687 "adding section two", 0, false, $user1 );
689 $text .= "\n\nthree";
690 $status3 = $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
691 "adding section three", 0, false, $user2 );
696 $rev1 = $status1->getValue()['revision
'];
697 $rev2 = $status2->getValue()['revision
'];
698 $rev3 = $status3->getValue()['revision
'];
705 $this->assertEquals( 3, Revision::countByPageId( wfGetDB( DB_REPLICA ), $page->getId() ) );
706 $this->assertEquals( $admin->getName(), $rev1->getUserText() );
707 $this->assertEquals( $user1->getName(), $rev2->getUserText() );
708 $this->assertEquals( $user2->getName(), $rev3->getUserText() );
710 // Now, try the actual rollback
711 $token = $admin->getEditToken( '
rollback' );
712 $rollbackErrors = $page->doRollback(
721 if ( $rollbackErrors ) {
723 "Rollback failed:\n" .
724 print_r( $rollbackErrors, true ) . ";\n" .
725 print_r( $resultDetails, true )
729 $page = new WikiPage( $page->getTitle() );
730 $this->assertEquals( $rev2->getSha1(), $page->getRevision()->getSha1(),
731 "rollback did not revert to the correct revision" );
732 $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
739 public function testDoRollback_simple() {
740 $admin = $this->getTestSysop()->getUser();
743 $page = $this->newPage( __METHOD__ );
744 $page->doEditContent(
745 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
751 $rev1 = $page->getRevision();
753 $user1 = $this->getTestUser()->getUser();
755 $page = new WikiPage( $page->getTitle() );
756 $page->doEditContent(
757 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
758 "adding section two",
764 # now, try the rollback
765 $token = $admin->getEditToken( '
rollback' );
766 $errors = $page->doRollback(
776 $this->fail( "Rollback failed:\n" . print_r( $errors, true )
777 . ";\n" . print_r( $details, true ) );
780 $page = new WikiPage( $page->getTitle() );
781 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
782 "rollback did not revert to the correct revision" );
783 $this->assertEquals( "one", $page->getContent()->getNativeData() );
790 public function testDoRollbackFailureSameContent() {
791 $admin = $this->getTestSysop()->getUser();
794 $page = $this->newPage( __METHOD__ );
795 $page->doEditContent(
796 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
802 $rev1 = $page->getRevision();
804 $user1 = $this->getTestUser( [ '
sysop' ] )->getUser();
806 $page = new WikiPage( $page->getTitle() );
807 $page->doEditContent(
808 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
809 "adding section two",
815 # now, do a the rollback from the same user was doing the edit before
817 $token = $user1->getEditToken( 'rollback' );
818 $errors = $page->doRollback(
820 "testing revert same user",
827 $this->assertEquals( [], $errors, "Rollback failed same user" );
829 # now, try the rollback
831 $token = $admin->getEditToken( 'rollback' );
832 $errors = $page->doRollback(
851 "Rollback not failed"
854 $page = new WikiPage( $page->getTitle() );
855 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
856 "rollback did not revert to the correct revision" );
857 $this->assertEquals( "one", $page->getContent()->getNativeData() );
864 public function testDoRollbackTagging() {
865 if ( !in_array( 'mw-
rollback', ChangeTags::getSoftwareTags() ) ) {
866 $this->markTestSkipped( 'Rollback tag deactivated, skipped the test.
' );
870 $admin->setName( 'Administrator
' );
871 $admin->addToDatabase();
874 $page = $this->newPage( 'WikiPageTest_testDoRollbackTagging
' );
875 $page->doEditContent(
876 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
883 $secondUser = new User();
884 $secondUser->setName( '92.65.217.32
' );
885 $text .= '\n\nSecond
line';
886 $page = new WikiPage( $page->getTitle() );
887 $page->doEditContent(
888 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
889 'Adding second
line',
895 // Now, try the rollback
896 $admin->addGroup( 'sysop' ); // Make the test user a sysop
897 $token = $admin->getEditToken( 'rollback' );
898 $errors = $page->doRollback(
899 $secondUser->getName(),
907 // If doRollback completed without errors
908 if ( $errors === [] ) {
909 $tags = $resultDetails[ 'tags
' ];
910 $this->assertContains( 'mw-
rollback', $tags );
914 public function provideGetAutoDeleteReason() {
924 [ "first edit", null ],
926 "/first edit.*only contributor/",
932 [ "first edit", null ],
933 [ "second edit", null ],
935 "/second edit.*only contributor/",
941 [ "first edit", "127.0.2.22" ],
942 [ "second edit", "127.0.3.33" ],
952 . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
953 . " nonumy eirmod tempor invidunt ut labore et dolore magna "
954 . "aliquyam erat, sed diam voluptua. At vero eos et accusam "
955 . "et justo duo dolores et ea rebum. Stet clita kasd gubergren, "
956 . "no sea takimata sanctus est Lorem ipsum dolor sit amet.'",
960 '/first edit:.*\.\.\."/
',
966 [ "first edit", "127.0.2.22" ],
967 [ "", "127.0.3.33" ],
969 "/before blanking.*first edit/",
980 public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
983 // NOTE: assume Help namespace to contain wikitext
984 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
988 foreach ( $edits as $edit ) {
991 if ( !empty( $edit[1] ) ) {
992 $user->setName( $edit[1] );
997 $content = ContentHandler::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
999 $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW : 0, false, $user );
1004 $reason = $page->getAutoDeleteReason( $hasHistory );
1006 if ( is_bool( $expectedResult ) || is_null( $expectedResult ) ) {
1007 $this->assertEquals( $expectedResult, $reason );
1009 $this->assertTrue( (bool)preg_match( $expectedResult, $reason ),
1010 "Autosummary didn't match expected pattern $expectedResult: $reason
" );
1013 $this->assertEquals( $expectedHistory, $hasHistory,
1014 "expected \$hasHistory to be
" . var_export( $expectedHistory, true ) );
1016 $page->doDeleteArticle( "done
" );
1019 public function providePreSaveTransform() {
1021 [ 'hello this is ~~~',
1022 "hello
this is [[
Special:Contributions/127.0.0.1|127.0.0.1]]
",
1024 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1025 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1033 public function testWikiPageFactory() {
1034 $title = Title::makeTitle( NS_FILE, 'Someimage.png' );
1035 $page = WikiPage::factory( $title );
1036 $this->assertEquals( WikiFilePage::class, get_class( $page ) );
1038 $title = Title::makeTitle( NS_CATEGORY, 'SomeCategory' );
1039 $page = WikiPage::factory( $title );
1040 $this->assertEquals( WikiCategoryPage::class, get_class( $page ) );
1042 $title = Title::makeTitle( NS_MAIN, 'SomePage' );
1043 $page = WikiPage::factory( $title );
1044 $this->assertEquals( WikiPage::class, get_class( $page ) );
1053 public function testCommentMigrationOnDeletion( $writeStage, $readStage ) {
1054 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', $writeStage );
1055 $this->overrideMwServices();
1057 $dbr = wfGetDB( DB_REPLICA );
1059 $page = $this->createPage(
1062 CONTENT_MODEL_WIKITEXT
1064 $revid = $page->getLatest();
1065 if ( $writeStage > MIGRATION_OLD ) {
1066 $comment_id = $dbr->selectField(
1067 'revision_comment_temp',
1068 'revcomment_comment_id',
1069 [ 'revcomment_rev' => $revid ],
1074 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', $readStage );
1075 $this->overrideMwServices();
1077 $page->doDeleteArticle( "testing deletion
" );
1079 if ( $readStage > MIGRATION_OLD ) {
1080 // Didn't leave behind any 'revision_comment_temp' rows
1081 $n = $dbr->selectField(
1082 'revision_comment_temp', 'COUNT(*)', [ 'revcomment_rev' => $revid ], __METHOD__
1084 $this->assertEquals( 0, $n, 'no entry in revision_comment_temp after deletion' );
1086 // Copied or upgraded the comment_id, as applicable
1087 $ar_comment_id = $dbr->selectField(
1090 [ 'ar_rev_id' => $revid ],
1093 if ( $writeStage > MIGRATION_OLD ) {
1094 $this->assertSame( $comment_id, $ar_comment_id );
1096 $this->assertNotEquals( 0, $ar_comment_id );
1100 // Copied rev_comment, if applicable
1101 if ( $readStage <= MIGRATION_WRITE_BOTH && $writeStage <= MIGRATION_WRITE_BOTH ) {
1102 $ar_comment = $dbr->selectField(
1105 [ 'ar_rev_id' => $revid ],
1108 $this->assertSame( 'testing', $ar_comment );
1112 public function provideCommentMigrationOnDeletion() {
1114 [ MIGRATION_OLD, MIGRATION_OLD ],
1115 [ MIGRATION_OLD, MIGRATION_WRITE_BOTH ],
1116 [ MIGRATION_OLD, MIGRATION_WRITE_NEW ],
1117 [ MIGRATION_WRITE_BOTH, MIGRATION_OLD ],
1118 [ MIGRATION_WRITE_BOTH, MIGRATION_WRITE_BOTH ],
1119 [ MIGRATION_WRITE_BOTH, MIGRATION_WRITE_NEW ],
1120 [ MIGRATION_WRITE_BOTH, MIGRATION_NEW ],
1121 [ MIGRATION_WRITE_NEW, MIGRATION_WRITE_BOTH ],
1122 [ MIGRATION_WRITE_NEW, MIGRATION_WRITE_NEW ],
1123 [ MIGRATION_WRITE_NEW, MIGRATION_NEW ],
1124 [ MIGRATION_NEW, MIGRATION_WRITE_BOTH ],
1125 [ MIGRATION_NEW, MIGRATION_WRITE_NEW ],
1126 [ MIGRATION_NEW, MIGRATION_NEW ],
1133 public function testUpdateCategoryCounts() {
1134 $page = new WikiPage( Title::newFromText( __METHOD__ ) );
1136 // Add an initial category
1137 $page->updateCategoryCounts( [ 'A' ], [], 0 );
1139 $this->assertEquals( 1, Category::newFromName( 'A' )->getPageCount() );
1140 $this->assertEquals( 0, Category::newFromName( 'B' )->getPageCount() );
1141 $this->assertEquals( 0, Category::newFromName( 'C' )->getPageCount() );
1143 // Add a new category
1144 $page->updateCategoryCounts( [ 'B' ], [], 0 );
1146 $this->assertEquals( 1, Category::newFromName( 'A' )->getPageCount() );
1147 $this->assertEquals( 1, Category::newFromName( 'B' )->getPageCount() );
1148 $this->assertEquals( 0, Category::newFromName( 'C' )->getPageCount() );
1150 // Add and remove a category
1151 $page->updateCategoryCounts( [ 'C' ], [ 'A' ], 0 );
1153 $this->assertEquals( 0, Category::newFromName( 'A' )->getPageCount() );
1154 $this->assertEquals( 1, Category::newFromName( 'B' )->getPageCount() );
1155 $this->assertEquals( 1, Category::newFromName( 'C' )->getPageCount() );
1158 public function provideUpdateRedirectOn() {
1159 yield [ '#REDIRECT [[Foo]]', true, null, true, true, 0 ];
1160 yield [ '#REDIRECT [[Foo]]', true, 'Foo', true, false, 1 ];
1161 yield [ 'SomeText', false, null, false, true, 0 ];
1162 yield [ 'SomeText', false, 'Foo', false, false, 1 ];
1176 public function testUpdateRedirectOn(
1178 $initialRedirectState,
1184 static $pageCounter = 0;
1187 $page = $this->createPage( Title::newFromText( __METHOD__ . $pageCounter ), $initialText );
1188 $this->assertSame( $initialRedirectState, $page->isRedirect() );
1190 $redirectTitle = is_string( $redirectTitle )
1191 ? Title::newFromText( $redirectTitle )
1194 $success = $page->updateRedirectOn( $this->db, $redirectTitle, $lastRevIsRedirect );
1195 $this->assertSame( $expectedSuccess, $success, 'Success assertion' );
1201 $this->assertRedirectTableCountForPageId( $page->getId(), $expectedRowCount );
1204 private function assertRedirectTableCountForPageId( $pageId, $expected ) {
1205 $this->assertSelect(
1208 [ 'rd_from' => $pageId ],
1209 [ [ strval( $expected ) ] ]
1216 public function testInsertRedirectEntry_insertsRedirectEntry() {
1217 $page = $this->createPage( Title::newFromText( __METHOD__ ), 'A' );
1218 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1220 $targetTitle = Title::newFromText( 'SomeTarget#Frag' );
1221 $targetTitle->mInterwiki = 'eninter';
1222 $page->insertRedirectEntry( $targetTitle, null );
1224 $this->assertSelect(
1226 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1227 [ 'rd_from' => $page->getId() ],
1229 strval( $page->getId() ),
1230 strval( $targetTitle->getNamespace() ),
1231 strval( $targetTitle->getDBkey() ),
1232 strval( $targetTitle->getFragment() ),
1233 strval( $targetTitle->getInterwiki() ),
1241 public function testInsertRedirectEntry_insertsRedirectEntryWithPageLatest() {
1242 $page = $this->createPage( Title::newFromText( __METHOD__ ), 'A' );
1243 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1245 $targetTitle = Title::newFromText( 'SomeTarget#Frag' );
1246 $targetTitle->mInterwiki = 'eninter';
1247 $page->insertRedirectEntry( $targetTitle, $page->getLatest() );
1249 $this->assertSelect(
1251 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1252 [ 'rd_from' => $page->getId() ],
1254 strval( $page->getId() ),
1255 strval( $targetTitle->getNamespace() ),
1256 strval( $targetTitle->getDBkey() ),
1257 strval( $targetTitle->getFragment() ),
1258 strval( $targetTitle->getInterwiki() ),
1266 public function testInsertRedirectEntry_doesNotInsertIfPageLatestIncorrect() {
1267 $page = $this->createPage( Title::newFromText( __METHOD__ ), 'A' );
1268 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1270 $targetTitle = Title::newFromText( 'SomeTarget#Frag' );
1271 $targetTitle->mInterwiki = 'eninter';
1272 $page->insertRedirectEntry( $targetTitle, 215251 );
1274 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1277 private function getRow( array $overrides = [] ) {
1281 'page_is_redirect' => '1',
1282 'page_latest' => '99',
1283 'page_namespace' => '3',
1284 'page_title' => 'JaJaTitle',
1285 'page_restrictions' => 'edit=autoconfirmed,sysop:move=sysop',
1286 'page_touched' => '20120101020202',
1287 'page_links_updated' => '20140101020202',
1289 foreach ( $overrides as $key => $value ) {
1290 $row[$key] = $value;
1292 return (object)$row;
1295 public function provideNewFromRowSuccess() {
1296 yield 'basic row' => [
1298 function ( WikiPage $wikiPage, self $test ) {
1299 $test->assertSame( 44, $wikiPage->getId() );
1300 $test->assertSame( 76, $wikiPage->getTitle()->getLength() );
1301 $test->assertTrue( $wikiPage->isRedirect() );
1302 $test->assertSame( 99, $wikiPage->getLatest() );
1303 $test->assertSame( 3, $wikiPage->getTitle()->getNamespace() );
1304 $test->assertSame( 'JaJaTitle', $wikiPage->getTitle()->getDBkey() );
1307 'edit' => [ 'autoconfirmed', 'sysop' ],
1308 'move' => [ 'sysop' ],
1310 $wikiPage->getTitle()->getAllRestrictions()
1312 $test->assertSame( '20120101020202', $wikiPage->getTouched() );
1313 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() );
1316 yield 'different timestamp formats' => [
1318 'page_touched' => '2012-01-01 02:02:02',
1319 'page_links_updated' => '2014-01-01 02:02:02',
1321 function ( WikiPage $wikiPage, self $test ) {
1322 $test->assertSame( '20120101020202', $wikiPage->getTouched() );
1323 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() );
1326 yield 'no restrictions' => [
1328 'page_restrictions' => '',
1330 function ( WikiPage $wikiPage, self $test ) {
1336 $wikiPage->getTitle()->getAllRestrictions()
1340 yield 'not redirect' => [
1342 'page_is_redirect' => '0',
1344 function ( WikiPage $wikiPage, self $test ) {
1345 $test->assertFalse( $wikiPage->isRedirect() );
1358 public function testNewFromRow( $row, $assertions ) {
1359 $page = WikiPage::newFromRow( $row, 'fromdb' );
1360 $assertions( $page, $this );
1363 public function provideTestNewFromId_returnsNullOnBadPageId() {
1372 public function testNewFromId_returnsNullOnBadPageId( $pageId ) {
1373 $this->assertNull( WikiPage::newFromID( $pageId ) );
1379 public function testNewFromId_appearsToFetchCorrectRow() {
1380 $createdPage = $this->createPage( __METHOD__, 'Xsfaij09' );
1381 $fetchedPage = WikiPage::newFromID( $createdPage->getId() );
1382 $this->assertSame( $createdPage->getId(), $fetchedPage->getId() );
1383 $this->assertEquals(
1384 $createdPage->getContent()->getNativeData(),
1385 $fetchedPage->getContent()->getNativeData()
1392 public function testNewFromId_returnsNullOnNonExistingId() {
1393 $this->assertNull( WikiPage::newFromID( 2147483647 ) );
1396 public function provideTestInsertProtectNullRevision() {
1397 // phpcs:disable Generic.Files.LineLength
1400 [ 'edit' => 'sysop' ],
1401 [ 'edit' => '20200101040404' ],
1405 '(goat-message-key: WikiPageDbTestBase::testInsertProtectNullRevision, UTSysop)(colon-separator)Goat Reason(word-separator)(parentheses: (protect-summary-desc: (restriction-edit), (protect-level-sysop), (protect-expiring: 04:04, 1 (january) 2020, 1 (january) 2020, 04:04)))'
1409 [ 'edit' => 'sysop', 'move' => 'something' ],
1410 [ 'edit' => '20200101040404', 'move' => '20210101050505' ],
1414 '(goat-key: WikiPageDbTestBase::testInsertProtectNullRevision, UTSysop)(colon-separator)Goat Goat(word-separator)(parentheses: (protect-summary-desc: (restriction-edit), (protect-level-sysop), (protect-expiring: 04:04, 1 (january) 2020, 1 (january) 2020, 04:04))(word-separator)(protect-summary-desc: (restriction-move), (protect-level-something), (protect-expiring: 05:05, 1 (january) 2021, 1 (january) 2021, 05:05)))'
1432 public function testInsertProtectNullRevision(
1441 $this->setContentLang( 'qqx' );
1443 $page = $this->createPage( __METHOD__, 'Goat' );
1445 $user = $user === null ? $user : $this->getTestSysop()->getUser();
1447 $result = $page->insertProtectNullRevision(
1456 $this->assertTrue( $result instanceof Revision );
1457 $this->assertSame( $expectedComment, $result->getComment( Revision::RAW ) );
1463 public function testUpdateRevisionOn_existingPage() {
1464 $user = $this->getTestSysop()->getUser();
1465 $page = $this->createPage( __METHOD__, 'StartText' );
1467 $revision = new Revision(
1470 'page' => $page->getId(),
1471 'title' => $page->getTitle(),
1472 'comment' => __METHOD__,
1473 'minor_edit' => true,
1474 'text' => __METHOD__ . '-text',
1475 'len' => strlen( __METHOD__ . '-text' ),
1476 'user' => $user->getId(),
1477 'user_text' => $user->getName(),
1478 'timestamp' => '20170707040404',
1479 'content_model' => CONTENT_MODEL_WIKITEXT,
1480 'content_format' => CONTENT_FORMAT_WIKITEXT,
1484 $result = $page->updateRevisionOn( $this->db, $revision );
1485 $this->assertTrue( $result );
1486 $this->assertSame( 9989, $page->getLatest() );
1487 $this->assertEquals( $revision, $page->getRevision() );
1493 public function testUpdateRevisionOn_NonExistingPage() {
1494 $user = $this->getTestSysop()->getUser();
1495 $page = $this->createPage( __METHOD__, 'StartText' );
1496 $page->doDeleteArticle( 'reason' );
1498 $revision = new Revision(
1501 'page' => $page->getId(),
1502 'title' => $page->getTitle(),
1503 'comment' => __METHOD__,
1504 'minor_edit' => true,
1505 'text' => __METHOD__ . '-text',
1506 'len' => strlen( __METHOD__ . '-text' ),
1507 'user' => $user->getId(),
1508 'user_text' => $user->getName(),
1509 'timestamp' => '20170707040404',
1510 'content_model' => CONTENT_MODEL_WIKITEXT,
1511 'content_format' => CONTENT_FORMAT_WIKITEXT,
1515 $result = $page->updateRevisionOn( $this->db, $revision );
1516 $this->assertFalse( $result );
1522 public function testUpdateIfNewerOn_olderRevision() {
1523 $user = $this->getTestSysop()->getUser();
1524 $page = $this->createPage( __METHOD__, 'StartText' );
1525 $initialRevision = $page->getRevision();
1527 $olderTimeStamp = wfTimestamp(
1529 wfTimestamp( TS_UNIX, $initialRevision->getTimestamp() ) - 1
1532 $olderRevison = new Revision(
1535 'page' => $page->getId(),
1536 'title' => $page->getTitle(),
1537 'comment' => __METHOD__,
1538 'minor_edit' => true,
1539 'text' => __METHOD__ . '-text',
1540 'len' => strlen( __METHOD__ . '-text' ),
1541 'user' => $user->getId(),
1542 'user_text' => $user->getName(),
1543 'timestamp' => $olderTimeStamp,
1544 'content_model' => CONTENT_MODEL_WIKITEXT,
1545 'content_format' => CONTENT_FORMAT_WIKITEXT,
1549 $result = $page->updateIfNewerOn( $this->db, $olderRevison );
1550 $this->assertFalse( $result );
1556 public function testUpdateIfNewerOn_newerRevision() {
1557 $user = $this->getTestSysop()->getUser();
1558 $page = $this->createPage( __METHOD__, 'StartText' );
1559 $initialRevision = $page->getRevision();
1561 $newerTimeStamp = wfTimestamp(
1563 wfTimestamp( TS_UNIX, $initialRevision->getTimestamp() ) + 1
1566 $newerRevision = new Revision(
1569 'page' => $page->getId(),
1570 'title' => $page->getTitle(),
1571 'comment' => __METHOD__,
1572 'minor_edit' => true,
1573 'text' => __METHOD__ . '-text',
1574 'len' => strlen( __METHOD__ . '-text' ),
1575 'user' => $user->getId(),
1576 'user_text' => $user->getName(),
1577 'timestamp' => $newerTimeStamp,
1578 'content_model' => CONTENT_MODEL_WIKITEXT,
1579 'content_format' => CONTENT_FORMAT_WIKITEXT,
1582 $result = $page->updateIfNewerOn( $this->db, $newerRevision );
1583 $this->assertTrue( $result );
1589 public function testInsertOn() {
1590 $title = Title::newFromText( __METHOD__ );
1591 $page = new WikiPage( $title );
1593 $startTimeStamp = wfTimestampNow();
1594 $result = $page->insertOn( $this->db );
1595 $endTimeStamp = wfTimestampNow();
1597 $this->assertInternalType( 'int', $result );
1598 $this->assertTrue( $result > 0 );
1600 $condition = [ 'page_id' => $result ];
1602 // Check the default fields have been filled
1603 $this->assertSelect(
1608 'page_restrictions',
1626 // Check the page_random field has been filled
1627 $pageRandom = $this->db->selectField( 'page', 'page_random', $condition );
1628 $this->assertTrue( (float)$pageRandom < 1 && (float)$pageRandom > 0 );
1630 // Assert the touched timestamp in the DB is roughly when we inserted the page
1631 $pageTouched = $this->db->selectField( 'page', 'page_touched', $condition );
1633 wfTimestamp( TS_UNIX, $startTimeStamp )
1634 <= wfTimestamp( TS_UNIX, $pageTouched )
1637 wfTimestamp( TS_UNIX, $endTimeStamp )
1638 >= wfTimestamp( TS_UNIX, $pageTouched )
1641 // Try inserting the same page again and checking the result is false (no change)
1642 $result = $page->insertOn( $this->db );
1643 $this->assertFalse( $result );
1649 public function testInsertOn_idSpecified() {
1650 $title = Title::newFromText( __METHOD__ );
1651 $page = new WikiPage( $title );
1654 $result = $page->insertOn( $this->db, $id );
1656 $this->assertSame( $id, $result );
1658 $condition = [ 'page_id' => $result ];
1660 // Check there is actually a row in the db
1661 $this->assertSelect(
1669 public function provideTestDoUpdateRestrictions_setBasicRestrictions() {
1670 // Note: Once the current dates passes the date in these tests they will fail.
1671 yield 'move something' => [
1673 [ 'move' => 'something' ],
1675 [ 'edit' => [], 'move' => [ 'something' ] ],
1678 yield 'move something, edit blank' => [
1680 [ 'move' => 'something', 'edit' => '' ],
1682 [ 'edit' => [], 'move' => [ 'something' ] ],
1685 yield 'edit sysop, with expiry' => [
1687 [ 'edit' => 'sysop' ],
1688 [ 'edit' => '21330101020202' ],
1689 [ 'edit' => [ 'sysop' ], 'move' => [] ],
1690 [ 'edit' => '21330101020202' ],
1692 yield 'move and edit, move with expiry' => [
1694 [ 'move' => 'something', 'edit' => 'another' ],
1695 [ 'move' => '22220202010101' ],
1696 [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ],
1697 [ 'move' => '22220202010101' ],
1699 yield 'move and edit, edit with infinity expiry' => [
1701 [ 'move' => 'something', 'edit' => 'another' ],
1702 [ 'edit' => 'infinity' ],
1703 [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ],
1704 [ 'edit' => 'infinity' ],
1706 yield 'non existing, create something' => [
1708 [ 'create' => 'something' ],
1710 [ 'create' => [ 'something' ] ],
1713 yield 'non existing, create something with expiry' => [
1715 [ 'create' => 'something' ],
1716 [ 'create' => '23451212112233' ],
1717 [ 'create' => [ 'something' ] ],
1718 [ 'create' => '23451212112233' ],
1726 public function testDoUpdateRestrictions_setBasicRestrictions(
1730 array $expectedRestrictions,
1731 array $expectedRestrictionExpiries
1733 if ( $pageExists ) {
1734 $page = $this->createPage( __METHOD__, 'ABC' );
1736 $page = new WikiPage( Title::newFromText( __METHOD__ . '-nonexist' ) );
1738 $user = $this->getTestSysop()->getUser();
1741 $status = $page->doUpdateRestrictions( $limit, $expiry, $cascade, 'aReason', $user, [] );
1743 $logId = $status->getValue();
1744 $allRestrictions = $page->getTitle()->getAllRestrictions();
1746 $this->assertTrue( $status->isGood() );
1747 $this->assertInternalType( 'int', $logId );
1748 $this->assertSame( $expectedRestrictions, $allRestrictions );
1749 foreach ( $expectedRestrictionExpiries as $key => $value ) {
1750 $this->assertSame( $value, $page->getTitle()->getRestrictionExpiry( $key ) );
1753 // Make sure the log entry looks good
1754 // log_params is not checked here
1755 $actorQuery = ActorMigration::newMigration()->getJoin( 'log_user' );
1756 $this->assertSelect(
1757 [ 'logging' ] + $actorQuery['tables'],
1760 'log_user' => $actorQuery['fields']['log_user'],
1761 'log_user_text' => $actorQuery['fields']['log_user_text'],
1765 [ 'log_id' => $logId ],
1768 (string)$user->getId(),
1770 (string)$page->getTitle()->getNamespace(),
1771 $page->getTitle()->getDBkey(),
1774 $actorQuery['joins']
1781 public function testDoUpdateRestrictions_failsOnReadOnly() {
1782 $page = $this->createPage( __METHOD__, 'ABC' );
1783 $user = $this->getTestSysop()->getUser();
1787 $readOnly = $this->getMockBuilder( ReadOnlyMode::class )
1788 ->disableOriginalConstructor()
1789 ->setMethods( [ 'isReadOnly', 'getReason' ] )
1791 $readOnly->expects( $this->once() )
1792 ->method( 'isReadOnly' )
1793 ->will( $this->returnValue( true ) );
1794 $readOnly->expects( $this->once() )
1795 ->method( 'getReason' )
1796 ->will( $this->returnValue( 'Some Read Only Reason' ) );
1797 $this->setService( 'ReadOnlyMode', $readOnly );
1799 $status = $page->doUpdateRestrictions( [], [], $cascade, 'aReason', $user, [] );
1800 $this->assertFalse( $status->isOK() );
1801 $this->assertSame( 'readonlytext', $status->getMessage()->getKey() );
1807 public function testDoUpdateRestrictions_returnsGoodIfNothingChanged() {
1808 $page = $this->createPage( __METHOD__, 'ABC' );
1809 $user = $this->getTestSysop()->getUser();
1811 $limit = [ 'edit' => 'sysop' ];
1813 $status = $page->doUpdateRestrictions(
1822 // The first entry should have a logId as it did something
1823 $this->assertTrue( $status->isGood() );
1824 $this->assertInternalType( 'int', $status->getValue() );
1826 $status = $page->doUpdateRestrictions(
1835 // The second entry should not have a logId as nothing changed
1836 $this->assertTrue( $status->isGood() );
1837 $this->assertNull( $status->getValue() );
1843 public function testDoUpdateRestrictions_logEntryTypeAndAction() {
1844 $page = $this->createPage( __METHOD__, 'ABC' );
1845 $user = $this->getTestSysop()->getUser();
1849 $status = $page->doUpdateRestrictions(
1850 [ 'edit' => 'sysop' ],
1857 $this->assertTrue( $status->isGood() );
1858 $this->assertInternalType( 'int', $status->getValue() );
1859 $this->assertSelect(
1861 [ 'log_type', 'log_action' ],
1862 [ 'log_id' => $status->getValue() ],
1863 [ [ 'protect', 'protect' ] ]
1866 // Modify the protection
1867 $status = $page->doUpdateRestrictions(
1868 [ 'edit' => 'somethingElse' ],
1875 $this->assertTrue( $status->isGood() );
1876 $this->assertInternalType( 'int', $status->getValue() );
1877 $this->assertSelect(
1879 [ 'log_type', 'log_action' ],
1880 [ 'log_id' => $status->getValue() ],
1881 [ [ 'protect', 'modify' ] ]
1884 // Remove the protection
1885 $status = $page->doUpdateRestrictions(
1893 $this->assertTrue( $status->isGood() );
1894 $this->assertInternalType( 'int', $status->getValue() );
1895 $this->assertSelect(
1897 [ 'log_type', 'log_action' ],
1898 [ 'log_id' => $status->getValue() ],
1899 [ [ 'protect', 'unprotect' ] ]
could not be made into a sysop(Did you enter the name correctly?) <
$wgContentHandlerUseDB
Set to false to disable use of the database fields introduced by the ContentHandler facility.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
static destroySingletons()
Destroy the singleton instances.
loadParamsAndArgs( $self=null, $opts=null, $args=null)
Process command line arguments $mOptions becomes an array with keys set to the option names $mArgs be...
Maintenance script that runs pending jobs.
Represents a title within MediaWiki.
testGetRedirectTarget( $title, $model, $text, $target)
provideGetRedirectTarget WikiPage::getRedirectTarget
testGetContent()
WikiPage::getContent.
newPage( $title, $model=null)
testExists()
WikiPage::exists.
testHasViewableContent( $title, $viewable, $create=false)
provideHasViewableContent WikiPage::hasViewableContent
testDoEditContent()
WikiPage::doEditContent WikiPage::doModify WikiPage::doCreate WikiPage::doEditUpdates.
testGetRevision()
WikiPage::getRevision.
provideHasViewableContent()
provideGetRedirectTarget()
testIsCountable( $title, $model, $text, $mode, $expected)
provideIsCountable WikiPage::isCountable
testDoDeleteArticle()
WikiPage::doDeleteArticle WikiPage::doDeleteArticleReal.
createPage( $page, $text, $model=null)
testIsRedirect( $title, $model, $text, $target)
provideGetRedirectTarget WikiPage::isRedirect
testDoDeleteUpdates()
WikiPage::doDeleteUpdates.
__construct( $name=null, array $data=[], $dataName='')
testGetParserOutput( $model, $text, $expectedHtml)
provideGetParserOutput WikiPage::getParserOutput
Class representing a MediaWiki article and history.
I won t presume to tell you how to I m just describing the methods I chose to use for myself If you do choose to follow these it will probably be easier for you to collaborate with others on the but if you want to contribute without by all means do which work well I also use K &R brace matching style I know that s a religious issue for so if you want to use a style that puts opening braces on the next line
presenting them properly to the user as errors is done by the caller return true use this to change the list i e rollback
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
processing should stop and the error should be shown to the user * false
const CONTENT_MODEL_WIKITEXT