X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Ftitle%2FNamespaceInfoTest.php;h=c1e258dac078851b0872605492dcd7509609d495;hb=5e7829e08f69f02bc466a0e7013968b6496f0abc;hp=5e6763677f944dca2e1f9b459f370c99063e332f;hpb=8d9d8c8bb319482919d3d62732b9a0f689ede052;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/title/NamespaceInfoTest.php b/tests/phpunit/includes/title/NamespaceInfoTest.php index 5e6763677f..c1e258dac0 100644 --- a/tests/phpunit/includes/title/NamespaceInfoTest.php +++ b/tests/phpunit/includes/title/NamespaceInfoTest.php @@ -6,6 +6,7 @@ */ use MediaWiki\Config\ServiceOptions; +use MediaWiki\Linker\LinkTarget; class NamespaceInfoTest extends MediaWikiTestCase { /********************************************************************************************** @@ -166,85 +167,6 @@ class NamespaceInfoTest extends MediaWikiTestCase { ]; } - /** - * @covers NamespaceInfo::getSubject - */ - public function testGetSubject() { - // Special namespaces are their own subjects - $obj = $this->newObj(); - $this->assertEquals( NS_MEDIA, $obj->getSubject( NS_MEDIA ) ); - $this->assertEquals( NS_SPECIAL, $obj->getSubject( NS_SPECIAL ) ); - - $this->assertEquals( NS_MAIN, $obj->getSubject( NS_TALK ) ); - $this->assertEquals( NS_USER, $obj->getSubject( NS_USER_TALK ) ); - } - - /** - * Regular getTalk() calls - * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in - * the function testGetTalkExceptions() - * @covers NamespaceInfo::getTalk - * @covers NamespaceInfo::isMethodValidFor - */ - public function testGetTalk() { - $obj = $this->newObj(); - $this->assertEquals( NS_TALK, $obj->getTalk( NS_MAIN ) ); - $this->assertEquals( NS_TALK, $obj->getTalk( NS_TALK ) ); - $this->assertEquals( NS_USER_TALK, $obj->getTalk( NS_USER ) ); - $this->assertEquals( NS_USER_TALK, $obj->getTalk( NS_USER_TALK ) ); - } - - /** - * Exceptions with getTalk() - * NS_MEDIA does not have talk pages. MediaWiki raise an exception for them. - * @expectedException MWException - * @covers NamespaceInfo::getTalk - * @covers NamespaceInfo::isMethodValidFor - */ - public function testGetTalkExceptionsForNsMedia() { - $this->assertNull( $this->newObj()->getTalk( NS_MEDIA ) ); - } - - /** - * Exceptions with getTalk() - * NS_SPECIAL does not have talk pages. MediaWiki raise an exception for them. - * @expectedException MWException - * @covers NamespaceInfo::getTalk - */ - public function testGetTalkExceptionsForNsSpecial() { - $this->assertNull( $this->newObj()->getTalk( NS_SPECIAL ) ); - } - - /** - * Regular getAssociated() calls - * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in - * the function testGetAssociatedExceptions() - * @covers NamespaceInfo::getAssociated - */ - public function testGetAssociated() { - $this->assertEquals( NS_TALK, $this->newObj()->getAssociated( NS_MAIN ) ); - $this->assertEquals( NS_MAIN, $this->newObj()->getAssociated( NS_TALK ) ); - } - - # ## Exceptions with getAssociated() - # ## NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises - # ## an exception for them. - /** - * @expectedException MWException - * @covers NamespaceInfo::getAssociated - */ - public function testGetAssociatedExceptionsForNsMedia() { - $this->assertNull( $this->newObj()->getAssociated( NS_MEDIA ) ); - } - - /** - * @expectedException MWException - * @covers NamespaceInfo::getAssociated - */ - public function testGetAssociatedExceptionsForNsSpecial() { - $this->assertNull( $this->newObj()->getAssociated( NS_SPECIAL ) ); - } - /** * @covers NamespaceInfo::exists * @dataProvider provideExists @@ -481,7 +403,7 @@ class NamespaceInfoTest extends MediaWikiTestCase { } /** - * @param $contentNamespaces To pass to constructor + * @param mixed $contentNamespaces To pass to constructor * @param array $expected * @dataProvider provideGetContentNamespaces * @covers NamespaceInfo::getContentNamespaces @@ -676,6 +598,226 @@ class NamespaceInfoTest extends MediaWikiTestCase { // %} End basic methods + /********************************************************************************************** + * getSubject/Talk/Associated + * %{ + */ + + /** + * @dataProvider provideSubjectTalk + * @covers NamespaceInfo::getSubject + * @covers NamespaceInfo::getSubjectPage + * @covers NamespaceInfo::isMethodValidFor + * @covers Title::getSubjectPage + * + * @param int $subject + * @param int $talk + */ + public function testGetSubject( $subject, $talk ) { + $obj = $this->newObj(); + $this->assertSame( $subject, $obj->getSubject( $subject ) ); + $this->assertSame( $subject, $obj->getSubject( $talk ) ); + + $subjectTitleVal = new TitleValue( $subject, 'A' ); + $talkTitleVal = new TitleValue( $talk, 'A' ); + // Object will be the same one passed in if it's a subject, different but equal object if + // it's talk + $this->assertSame( $subjectTitleVal, $obj->getSubjectPage( $subjectTitleVal ) ); + $this->assertEquals( $subjectTitleVal, $obj->getSubjectPage( $talkTitleVal ) ); + + $subjectTitle = Title::makeTitle( $subject, 'A' ); + $talkTitle = Title::makeTitle( $talk, 'A' ); + $this->assertSame( $subjectTitle, $subjectTitle->getSubjectPage() ); + $this->assertEquals( $subjectTitle, $talkTitle->getSubjectPage() ); + } + + /** + * @dataProvider provideSpecialNamespaces + * @covers NamespaceInfo::getSubject + * @covers NamespaceInfo::getSubjectPage + * + * @param int $ns + */ + public function testGetSubject_special( $ns ) { + $obj = $this->newObj(); + $this->assertSame( $ns, $obj->getSubject( $ns ) ); + + $title = new TitleValue( $ns, 'A' ); + $this->assertSame( $title, $obj->getSubjectPage( $title ) ); + } + + /** + * @dataProvider provideSubjectTalk + * @covers NamespaceInfo::getTalk + * @covers NamespaceInfo::getTalkPage + * @covers NamespaceInfo::isMethodValidFor + * @covers Title::getTalkPage + * + * @param int $subject + * @param int $talk + */ + public function testGetTalk( $subject, $talk ) { + $obj = $this->newObj(); + $this->assertSame( $talk, $obj->getTalk( $subject ) ); + $this->assertSame( $talk, $obj->getTalk( $talk ) ); + + $subjectTitleVal = new TitleValue( $subject, 'A' ); + $talkTitleVal = new TitleValue( $talk, 'A' ); + // Object will be the same one passed in if it's a talk, different but equal object if it's + // subject + $this->assertEquals( $talkTitleVal, $obj->getTalkPage( $subjectTitleVal ) ); + $this->assertSame( $talkTitleVal, $obj->getTalkPage( $talkTitleVal ) ); + + $subjectTitle = Title::makeTitle( $subject, 'A' ); + $talkTitle = Title::makeTitle( $talk, 'A' ); + $this->assertEquals( $talkTitle, $subjectTitle->getTalkPage() ); + $this->assertSame( $talkTitle, $talkTitle->getTalkPage() ); + } + + /** + * @dataProvider provideSpecialNamespaces + * @covers NamespaceInfo::getTalk + * @covers NamespaceInfo::isMethodValidFor + * + * @param int $ns + */ + public function testGetTalk_special( $ns ) { + $this->setExpectedException( MWException::class, + "NamespaceInfo::getTalk does not make any sense for given namespace $ns" ); + $this->newObj()->getTalk( $ns ); + } + + /** + * @dataProvider provideSpecialNamespaces + * @covers NamespaceInfo::getAssociated + * @covers NamespaceInfo::isMethodValidFor + * + * @param int $ns + */ + public function testGetAssociated_special( $ns ) { + $this->setExpectedException( + MWException::class, + "NamespaceInfo::getAssociated does not make any sense for given namespace $ns" + ); + $this->newObj()->getAssociated( $ns ); + } + + public static function provideCanHaveTalkPage() { + return [ + [ new TitleValue( NS_MAIN, 'Test' ), true ], + [ new TitleValue( NS_TALK, 'Test' ), true ], + [ new TitleValue( NS_USER, 'Test' ), true ], + [ new TitleValue( NS_SPECIAL, 'Test' ), false ], + [ new TitleValue( NS_MEDIA, 'Test' ), false ], + [ new TitleValue( NS_MAIN, '', 'Kittens' ), false ], + [ new TitleValue( NS_MAIN, 'Kittens', '', 'acme' ), false ], + ]; + } + + /** + * @dataProvider provideCanHaveTalkPage + * @covers NamespaceInfo::canHaveTalkPage + */ + public function testCanHaveTalkPage( LinkTarget $t, $expected ) { + $actual = $this->newObj()->canHaveTalkPage( $t ); + $this->assertEquals( $expected, $actual, $t->getDBkey() ); + } + + public static function provideGetTalkPage_good() { + return [ + [ new TitleValue( NS_MAIN, 'Test' ), new TitleValue( NS_TALK, 'Test' ) ], + [ new TitleValue( NS_TALK, 'Test' ), new TitleValue( NS_TALK, 'Test' ) ], + [ new TitleValue( NS_USER, 'Test' ), new TitleValue( NS_USER_TALK, 'Test' ) ], + ]; + } + + /** + * @dataProvider provideGetTalkPage_good + * @covers NamespaceInfo::getTalk + * @covers NamespaceInfo::getTalkPage + * @covers NamespaceInfo::isMethodValidFor + */ + public function testGetTalkPage_good( LinkTarget $t, LinkTarget $expected ) { + $actual = $this->newObj()->getTalkPage( $t ); + $this->assertEquals( $expected, $actual, $t->getDBkey() ); + } + + public static function provideGetTalkPage_bad() { + return [ + [ new TitleValue( NS_SPECIAL, 'Test' ) ], + [ new TitleValue( NS_MEDIA, 'Test' ) ], + [ new TitleValue( NS_MAIN, '', 'Kittens' ) ], + [ new TitleValue( NS_MAIN, 'Kittens', '', 'acme' ) ], + ]; + } + + /** + * @dataProvider provideGetTalkPage_bad + * @covers NamespaceInfo::getTalk + * @covers NamespaceInfo::getTalkPage + * @covers NamespaceInfo::isMethodValidFor + */ + public function testGetTalkPage_bad( LinkTarget $t ) { + $this->setExpectedException( MWException::class ); + $this->newObj()->getTalkPage( $t ); + } + + /** + * @dataProvider provideGetTalkPage_bad + * @covers NamespaceInfo::getAssociated + * @covers NamespaceInfo::getAssociatedPage + * @covers NamespaceInfo::isMethodValidFor + */ + public function testGetAssociatedPage_bad( LinkTarget $t ) { + $this->setExpectedException( MWException::class ); + $this->newObj()->getAssociatedPage( $t ); + } + + /** + * @dataProvider provideSubjectTalk + * @covers NamespaceInfo::getAssociated + * @covers NamespaceInfo::getAssociatedPage + * @covers Title::getOtherPage + * + * @param int $subject + * @param int $talk + */ + public function testGetAssociated( $subject, $talk ) { + $obj = $this->newObj(); + $this->assertSame( $talk, $obj->getAssociated( $subject ) ); + $this->assertSame( $subject, $obj->getAssociated( $talk ) ); + + $subjectTitle = new TitleValue( $subject, 'A' ); + $talkTitle = new TitleValue( $talk, 'A' ); + // Object will not be the same + $this->assertEquals( $talkTitle, $obj->getAssociatedPage( $subjectTitle ) ); + $this->assertEquals( $subjectTitle, $obj->getAssociatedPage( $talkTitle ) ); + + $subjectTitle = Title::makeTitle( $subject, 'A' ); + $talkTitle = Title::makeTitle( $talk, 'A' ); + $this->assertEquals( $talkTitle, $subjectTitle->getOtherPage() ); + $this->assertEquals( $subjectTitle, $talkTitle->getOtherPage() ); + } + + public static function provideSubjectTalk() { + return [ + // Format: [ subject, talk ] + 'Main/talk' => [ NS_MAIN, NS_TALK ], + 'User/user talk' => [ NS_USER, NS_USER_TALK ], + 'Unknown namespaces also supported' => [ 106, 107 ], + ]; + } + + public static function provideSpecialNamespaces() { + return [ + 'Special' => [ NS_SPECIAL ], + 'Media' => [ NS_MEDIA ], + 'Unknown negative index' => [ -613 ], + ]; + } + + // %} End getSubject/Talk/Associated + /********************************************************************************************** * Canonical namespaces * %{ @@ -755,6 +897,7 @@ class NamespaceInfoTest extends MediaWikiTestCase { // No canonical namespace names // %{ + /** * @covers NamespaceInfo::getCanonicalNamespaces */ @@ -857,6 +1000,7 @@ class NamespaceInfoTest extends MediaWikiTestCase { // Hook namespaces // %{ + /** * @return array Expected canonical namespaces */ @@ -922,6 +1066,7 @@ class NamespaceInfoTest extends MediaWikiTestCase { // Extra namespaces // %{ + /** * @return NamespaceInfo */ @@ -977,6 +1122,7 @@ class NamespaceInfoTest extends MediaWikiTestCase { // Canonical namespace caching // %{ + /** * @covers NamespaceInfo::getCanonicalNamespaces */ @@ -1170,11 +1316,7 @@ class NamespaceInfoTest extends MediaWikiTestCase { 'No namespace restriction' => [ [ '', 'autoconfirmed', 'sysop' ], NS_TALK ], 'Restricted to autoconfirmed' => [ [ '', 'sysop' ], NS_MAIN ], 'Restricted to sysop' => [ [ '' ], NS_USER ], - // @todo Bug -- 'sysop' protection should be allowed in this case. Someone who's - // autoconfirmed and also privileged can edit this namespace, and would be blocked by - // the sysop protection. - 'Restricted to someone in two groups' => [ [ '' ], 101 ], - + 'Restricted to someone in two groups' => [ [ '', 'sysop' ], 101 ], 'No special permissions' => [ [ '' ], NS_TALK, $this->getMockUser() ], 'autoconfirmed' => [ [ '', 'autoconfirmed' ],