From 759518bdde8825dc414c1ff0cf99a9dc84088ae6 Mon Sep 17 00:00:00 2001 From: daniel Date: Fri, 12 Oct 2012 17:34:29 +0200 Subject: [PATCH] Fixing dump tests for non-wikitext in NS_MAIN. Change-Id: I7945a1b950d3b9b394a336813abc8cd1e288766f --- tests/phpunit/MediaWikiTestCase.php | 55 +++++++++++++++ .../maintenance/backupTextPassTest.php | 15 +++- tests/phpunit/maintenance/backup_PageTest.php | 69 +++++++++++-------- tests/phpunit/maintenance/fetchTextTest.php | 6 +- 4 files changed, 111 insertions(+), 34 deletions(-) diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index 9c2fb69e06..5bc36edea6 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -684,4 +684,59 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { return true; } + + /** + * Returns the ID of a namespace that defaults to Wikitext. + * Throws an MWException if there is none. + * + * @return int the ID of the wikitext Namespace + * @since 1.21 + */ + protected function getDefaultWikitextNS() { + global $wgNamespaceContentModels; + + static $wikitextNS = null; // this is not going to change + if ( $wikitextNS !== null ) { + return $wikitextNS; + } + + // quickly short out on most common case: + if ( !isset( $wgNamespaceContentModels[NS_MAIN] ) ) { + return NS_MAIN; + } + + // NOTE: prefer content namespaces + $namespaces = array_unique( array_merge( + MWNamespace::getContentNamespaces(), + array( NS_MAIN, NS_HELP, NS_PROJECT ), // prefer these + MWNamespace::getValidNamespaces() + ) ); + + $namespaces = array_diff( $namespaces, array( + NS_FILE, NS_CATEGORY, NS_MEDIAWIKI, NS_USER // don't mess with magic namespaces + )); + + $talk = array_filter( $namespaces, function ( $ns ) { + return MWNamespace::isTalk( $ns ); + } ); + + // prefer non-talk pages + $namespaces = array_diff( $namespaces, $talk ); + $namespaces = array_merge( $namespaces, $talk ); + + // check default content model of each namespace + foreach ( $namespaces as $ns ) { + if ( !isset( $wgNamespaceContentModels[$ns] ) || + $wgNamespaceContentModels[$ns] === CONTENT_MODEL_WIKITEXT ) { + + $wikitextNS = $ns; + return $wikitextNS; + } + } + + // give up + // @todo: Inside a test, we could skip the test as incomplete. + // But frequently, this is used in fixture setup. + throw new MWException( "No namespace defaults to wikitext!" ); + } } diff --git a/tests/phpunit/maintenance/backupTextPassTest.php b/tests/phpunit/maintenance/backupTextPassTest.php index 7072299144..2ebb351037 100644 --- a/tests/phpunit/maintenance/backupTextPassTest.php +++ b/tests/phpunit/maintenance/backupTextPassTest.php @@ -26,16 +26,18 @@ class TextPassDumperTest extends DumpTestCase { $this->tablesUsed[] = 'revision'; $this->tablesUsed[] = 'text'; + $ns = $this->getDefaultWikitextNS(); + try { // Simple page - $title = Title::newFromText( 'BackupDumperTestP1' ); + $title = Title::newFromText( 'BackupDumperTestP1', $ns ); $page = WikiPage::factory( $title ); list( $this->revId1_1, $this->textId1_1 ) = $this->addRevision( $page, "BackupDumperTestP1Text1", "BackupDumperTestP1Summary1" ); $this->pageId1 = $page->getId(); // Page with more than one revision - $title = Title::newFromText( 'BackupDumperTestP2' ); + $title = Title::newFromText( 'BackupDumperTestP2', $ns ); $page = WikiPage::factory( $title ); list( $this->revId2_1, $this->textId2_1 ) = $this->addRevision( $page, "BackupDumperTestP2Text1", "BackupDumperTestP2Summary1" ); @@ -49,7 +51,7 @@ class TextPassDumperTest extends DumpTestCase { $this->pageId2 = $page->getId(); // Deleted page. - $title = Title::newFromText( 'BackupDumperTestP3' ); + $title = Title::newFromText( 'BackupDumperTestP3', $ns ); $page = WikiPage::factory( $title ); list( $this->revId3_1, $this->textId3_1 ) = $this->addRevision( $page, "BackupDumperTestP3Text1", "BackupDumperTestP2Summary1" ); @@ -59,6 +61,13 @@ class TextPassDumperTest extends DumpTestCase { $page->doDeleteArticle( "Testing ;)" ); // Page from non-default namespace + + if ( $ns === NS_TALK ) { + //@todo: work around this. + throw new MWException( "The default wikitext namespace is the talk namespace. " + . " We can't currently deal with that."); + } + $title = Title::newFromText( 'BackupDumperTestP1', NS_TALK ); $page = WikiPage::factory( $title ); list( $this->revId4_1, $this->textId4_1 ) = $this->addRevision( $page, diff --git a/tests/phpunit/maintenance/backup_PageTest.php b/tests/phpunit/maintenance/backup_PageTest.php index 64374f86a3..149845e3c7 100644 --- a/tests/phpunit/maintenance/backup_PageTest.php +++ b/tests/phpunit/maintenance/backup_PageTest.php @@ -10,11 +10,13 @@ class BackupDumperPageTest extends DumpTestCase { // We'll add several pages, revision and texts. The following variables hold the // corresponding ids. private $pageId1, $pageId2, $pageId3, $pageId4, $pageId5; + private $pageTitle1, $pageTitle2, $pageTitle3, $pageTitle4, $pageTitle5; private $revId1_1, $textId1_1; private $revId2_1, $textId2_1, $revId2_2, $textId2_2; private $revId2_3, $textId2_3, $revId2_4, $textId2_4; private $revId3_1, $textId3_1, $revId3_2, $textId3_2; private $revId4_1, $textId4_1; + private $namespace, $talk_namespace; function addDBData() { $this->tablesUsed[] = 'page'; @@ -22,14 +24,23 @@ class BackupDumperPageTest extends DumpTestCase { $this->tablesUsed[] = 'text'; try { - $title = Title::newFromText( 'BackupDumperTestP1' ); - $page = WikiPage::factory( $title ); + $this->namespace = $this->getDefaultWikitextNS(); + $this->talk_namespace = NS_TALK; + + if ( $this->namespace === $this->talk_namespace ) { + //@todo: work around this. + throw new MWException( "The default wikitext namespace is the talk namespace. " + . " We can't currently deal with that."); + } + + $this->pageTitle1 = Title::newFromText( 'BackupDumperTestP1', $this->namespace ); + $page = WikiPage::factory( $this->pageTitle1 ); list( $this->revId1_1, $this->textId1_1 ) = $this->addRevision( $page, "BackupDumperTestP1Text1", "BackupDumperTestP1Summary1" ); $this->pageId1 = $page->getId(); - $title = Title::newFromText( 'BackupDumperTestP2' ); - $page = WikiPage::factory( $title ); + $this->pageTitle2 = Title::newFromText( 'BackupDumperTestP2', $this->namespace ); + $page = WikiPage::factory( $this->pageTitle2 ); list( $this->revId2_1, $this->textId2_1 ) = $this->addRevision( $page, "BackupDumperTestP2Text1", "BackupDumperTestP2Summary1" ); list( $this->revId2_2, $this->textId2_2 ) = $this->addRevision( $page, @@ -41,8 +52,8 @@ class BackupDumperPageTest extends DumpTestCase { "BackupDumperTestP2Summary4 extra " ); $this->pageId2 = $page->getId(); - $title = Title::newFromText( 'BackupDumperTestP3' ); - $page = WikiPage::factory( $title ); + $this->pageTitle3 = Title::newFromText( 'BackupDumperTestP3', $this->namespace ); + $page = WikiPage::factory( $this->pageTitle3 ); list( $this->revId3_1, $this->textId3_1 ) = $this->addRevision( $page, "BackupDumperTestP3Text1", "BackupDumperTestP2Summary1" ); list( $this->revId3_2, $this->textId3_2 ) = $this->addRevision( $page, @@ -50,8 +61,8 @@ class BackupDumperPageTest extends DumpTestCase { $this->pageId3 = $page->getId(); $page->doDeleteArticle( "Testing ;)" ); - $title = Title::newFromText( 'BackupDumperTestP1', NS_TALK ); - $page = WikiPage::factory( $title ); + $this->pageTitle4 = Title::newFromText( 'BackupDumperTestP1', $this->talk_namespace ); + $page = WikiPage::factory( $this->pageTitle4 ); list( $this->revId4_1, $this->textId4_1 ) = $this->addRevision( $page, "Talk about BackupDumperTestP1 Text1", "Talk BackupDumperTestP1 Summary1" ); @@ -95,14 +106,14 @@ class BackupDumperPageTest extends DumpTestCase { $this->assertDumpStart( $fname ); // Page 1 - $this->assertPageStart( $this->pageId1, NS_MAIN, "BackupDumperTestP1" ); + $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() ); $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1", $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87", "BackupDumperTestP1Text1" ); $this->assertPageEnd(); // Page 2 - $this->assertPageStart( $this->pageId2, NS_MAIN, "BackupDumperTestP2" ); + $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() ); $this->assertRevision( $this->revId2_1, "BackupDumperTestP2Summary1", $this->textId2_1, 23, "jprywrymfhysqllua29tj3sc7z39dl2", "BackupDumperTestP2Text1" ); @@ -121,7 +132,7 @@ class BackupDumperPageTest extends DumpTestCase { // -> Page is marked deleted. Hence not visible // Page 4 - $this->assertPageStart( $this->pageId4, NS_TALK, "Talk:BackupDumperTestP1" ); + $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() ); $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1", $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe", "Talk about BackupDumperTestP1 Text1" ); @@ -146,13 +157,13 @@ class BackupDumperPageTest extends DumpTestCase { $this->assertDumpStart( $fname ); // Page 1 - $this->assertPageStart( $this->pageId1, NS_MAIN, "BackupDumperTestP1" ); + $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() ); $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1", $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" ); $this->assertPageEnd(); // Page 2 - $this->assertPageStart( $this->pageId2, NS_MAIN, "BackupDumperTestP2" ); + $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() ); $this->assertRevision( $this->revId2_1, "BackupDumperTestP2Summary1", $this->textId2_1, 23, "jprywrymfhysqllua29tj3sc7z39dl2" ); $this->assertRevision( $this->revId2_2, "BackupDumperTestP2Summary2", @@ -167,7 +178,7 @@ class BackupDumperPageTest extends DumpTestCase { // -> Page is marked deleted. Hence not visible // Page 4 - $this->assertPageStart( $this->pageId4, NS_TALK, "Talk:BackupDumperTestP1" ); + $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() ); $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1", $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" ); $this->assertPageEnd(); @@ -191,13 +202,13 @@ class BackupDumperPageTest extends DumpTestCase { $this->assertDumpStart( $fname ); // Page 1 - $this->assertPageStart( $this->pageId1, NS_MAIN, "BackupDumperTestP1" ); + $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() ); $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1", $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" ); $this->assertPageEnd(); // Page 2 - $this->assertPageStart( $this->pageId2, NS_MAIN, "BackupDumperTestP2" ); + $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() ); $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra", $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 ); $this->assertPageEnd(); @@ -206,7 +217,7 @@ class BackupDumperPageTest extends DumpTestCase { // -> Page is marked deleted. Hence not visible // Page 4 - $this->assertPageStart( $this->pageId4, NS_TALK, "Talk:BackupDumperTestP1" ); + $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() ); $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1", $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" ); $this->assertPageEnd(); @@ -231,13 +242,13 @@ class BackupDumperPageTest extends DumpTestCase { $this->assertDumpStart( $fname ); // Page 1 - $this->assertPageStart( $this->pageId1, NS_MAIN, "BackupDumperTestP1" ); + $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() ); $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1", $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" ); $this->assertPageEnd(); // Page 2 - $this->assertPageStart( $this->pageId2, NS_MAIN, "BackupDumperTestP2" ); + $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() ); $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra", $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 ); $this->assertPageEnd(); @@ -246,7 +257,7 @@ class BackupDumperPageTest extends DumpTestCase { // -> Page is marked deleted. Hence not visible // Page 4 - $this->assertPageStart( $this->pageId4, NS_TALK, "Talk:BackupDumperTestP1" ); + $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() ); $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1", $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" ); $this->assertPageEnd(); @@ -300,13 +311,13 @@ class BackupDumperPageTest extends DumpTestCase { $this->assertDumpStart( $fnameMetaHistory ); // Page 1 - $this->assertPageStart( $this->pageId1, NS_MAIN, "BackupDumperTestP1" ); + $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() ); $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1", $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" ); $this->assertPageEnd(); // Page 2 - $this->assertPageStart( $this->pageId2, NS_MAIN, "BackupDumperTestP2" ); + $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() ); $this->assertRevision( $this->revId2_1, "BackupDumperTestP2Summary1", $this->textId2_1, 23, "jprywrymfhysqllua29tj3sc7z39dl2" ); $this->assertRevision( $this->revId2_2, "BackupDumperTestP2Summary2", @@ -321,7 +332,7 @@ class BackupDumperPageTest extends DumpTestCase { // -> Page is marked deleted. Hence not visible // Page 4 - $this->assertPageStart( $this->pageId4, NS_TALK, "Talk:BackupDumperTestP1" ); + $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() ); $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1", $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" ); $this->assertPageEnd(); @@ -334,13 +345,13 @@ class BackupDumperPageTest extends DumpTestCase { $this->assertDumpStart( $fnameMetaCurrent ); // Page 1 - $this->assertPageStart( $this->pageId1, NS_MAIN, "BackupDumperTestP1" ); + $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() ); $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1", $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" ); $this->assertPageEnd(); // Page 2 - $this->assertPageStart( $this->pageId2, NS_MAIN, "BackupDumperTestP2" ); + $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() ); $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra", $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 ); $this->assertPageEnd(); @@ -349,7 +360,7 @@ class BackupDumperPageTest extends DumpTestCase { // -> Page is marked deleted. Hence not visible // Page 4 - $this->assertPageStart( $this->pageId4, NS_TALK, "Talk:BackupDumperTestP1" ); + $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() ); $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1", $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" ); $this->assertPageEnd(); @@ -362,13 +373,13 @@ class BackupDumperPageTest extends DumpTestCase { $this->assertDumpStart( $fnameArticles ); // Page 1 - $this->assertPageStart( $this->pageId1, NS_MAIN, "BackupDumperTestP1" ); + $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() ); $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1", $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" ); $this->assertPageEnd(); // Page 2 - $this->assertPageStart( $this->pageId2, NS_MAIN, "BackupDumperTestP2" ); + $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() ); $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra", $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 ); $this->assertPageEnd(); @@ -377,7 +388,7 @@ class BackupDumperPageTest extends DumpTestCase { // -> Page is marked deleted. Hence not visible // Page 4 - // -> Page is not in NS_MAIN. Hence not visible + // -> Page is not in $this->namespace. Hence not visible $this->assertDumpEnd(); diff --git a/tests/phpunit/maintenance/fetchTextTest.php b/tests/phpunit/maintenance/fetchTextTest.php index fd6db0abd4..8041e3504a 100644 --- a/tests/phpunit/maintenance/fetchTextTest.php +++ b/tests/phpunit/maintenance/fetchTextTest.php @@ -129,12 +129,14 @@ class FetchTextTest extends MediaWikiTestCase { $this->tablesUsed[] = 'revision'; $this->tablesUsed[] = 'text'; + $wikitextNamespace = $this->getDefaultWikitextNS(); + try { - $title = Title::newFromText( 'FetchTextTestPage1' ); + $title = Title::newFromText( 'FetchTextTestPage1', $wikitextNamespace ); $page = WikiPage::factory( $title ); $this->textId1 = $this->addRevision( $page, "FetchTextTestPage1Text1", "FetchTextTestPage1Summary1" ); - $title = Title::newFromText( 'FetchTextTestPage2' ); + $title = Title::newFromText( 'FetchTextTestPage2', $wikitextNamespace ); $page = WikiPage::factory( $title ); $this->textId2 = $this->addRevision( $page, "FetchTextTestPage2Text1", "FetchTextTestPage2Summary1" ); $this->textId3 = $this->addRevision( $page, "FetchTextTestPage2Text2", "FetchTextTestPage2Summary2" ); -- 2.20.1