* MWNamespace::equals to test equivalence of two namespaces (forward compatible with any changes we may make like introducing namespace keys like 'USER') * MWNamespace::subjectEquals to test equivalence of the subject of two namespaces e.g.: MWNamespace::subjectEquals( NS_USER, $ns ); instead of testing for equivalence to both NS_USER and NS_USER_TALK * Title::inNamespace to use instead of $title->getNamespace() == NS_??? * Title::inNamespaces for use like $title->inNamespaces( NS_USER, NS_PROJECT ) when you only care if it's in one of a number of namespaces (also accepts an array) * Title::hasSubjectNamespace for use instead of testing for equivalence to both the subject and talk such as NS_USER and NS_USER_TALK. Include phpunit tests for all this new code, and also add some tests for some existing code.
78 lines
2.5 KiB
PHP
78 lines
2.5 KiB
PHP
<?php
|
|
|
|
class TitleMethodsTest extends MediaWikiTestCase {
|
|
|
|
public function dataEquals() {
|
|
return array(
|
|
array( 'Main Page', 'Main Page', true ),
|
|
array( 'Main Page', 'Not The Main Page', false ),
|
|
array( 'Main Page', 'Project:Main Page', false ),
|
|
array( 'File:Example.png', 'Image:Example.png', true ),
|
|
array( 'Special:Version', 'Special:Version', true ),
|
|
array( 'Special:Version', 'Special:Recentchanges', false ),
|
|
array( 'Special:Version', 'Main Page', false ),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider dataEquals
|
|
*/
|
|
public function testEquals( $titleA, $titleB, $expectedBool ) {
|
|
$titleA = Title::newFromText( $titleA );
|
|
$titleB = Title::newFromText( $titleB );
|
|
|
|
$this->assertEquals( $titleA->equals( $titleB ), $expectedBool );
|
|
$this->assertEquals( $titleB->equals( $titleA ), $expectedBool );
|
|
}
|
|
|
|
public function dataInNamespace() {
|
|
return array(
|
|
array( 'Main Page', NS_MAIN, true ),
|
|
array( 'Main Page', NS_TALK, false ),
|
|
array( 'Main Page', NS_USER, false ),
|
|
array( 'User:Foo', NS_USER, true ),
|
|
array( 'User:Foo', NS_USER_TALK, false ),
|
|
array( 'User:Foo', NS_TEMPLATE, false ),
|
|
array( 'User_talk:Foo', NS_USER_TALK, true ),
|
|
array( 'User_talk:Foo', NS_USER, false ),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider dataInNamespace
|
|
*/
|
|
public function testInNamespace( $title, $ns, $expectedBool ) {
|
|
$title = Title::newFromText( $title );
|
|
$this->assertEquals( $title->inNamespace( $ns ), $expectedBool );
|
|
}
|
|
|
|
public function testInNamespaces() {
|
|
$mainpage = Title::newFromText( 'Main Page' );
|
|
$this->assertTrue( $mainpage->inNamespaces( NS_MAIN, NS_USER ) );
|
|
$this->assertTrue( $mainpage->inNamespaces( array( NS_MAIN, NS_USER ) ) );
|
|
$this->assertTrue( $mainpage->inNamespaces( array( NS_USER, NS_MAIN ) ) );
|
|
$this->assertFalse( $mainpage->inNamespaces( array( NS_PROJECT, NS_TEMPLATE ) ) );
|
|
}
|
|
|
|
public function dataHasSubjectNamespace() {
|
|
return array(
|
|
array( 'Main Page', NS_MAIN, true ),
|
|
array( 'Main Page', NS_TALK, true ),
|
|
array( 'Main Page', NS_USER, false ),
|
|
array( 'User:Foo', NS_USER, true ),
|
|
array( 'User:Foo', NS_USER_TALK, true ),
|
|
array( 'User:Foo', NS_TEMPLATE, false ),
|
|
array( 'User_talk:Foo', NS_USER_TALK, true ),
|
|
array( 'User_talk:Foo', NS_USER, true ),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider dataHasSubjectNamespace
|
|
*/
|
|
public function testHasSubjectNamespace( $title, $ns, $expectedBool ) {
|
|
$title = Title::newFromText( $title );
|
|
$this->assertEquals( $title->hasSubjectNamespace( $ns ), $expectedBool );
|
|
}
|
|
|
|
}
|