From d093453e7eb3117babbe704169b0b4feae157457 Mon Sep 17 00:00:00 2001 From: aude Date: Sat, 16 Nov 2013 15:24:02 +0100 Subject: [PATCH] Remove unused exception in SpecialPage::getTitleFor and add tests The way the existing code was, it seems the exception condition was never hit, as one could enter a bogus special page title in SpecialPage::getTitleFor and it returns a Title object anyway. Some tests are also added for SpecialPage::getTitleFor(), although more refactoring my be required to truly unit test this, vs. indirectly testing other stuff via static methods. Change-Id: I6e427d047a2f8e58677eb65b50866f767078c255 --- includes/SpecialPage.php | 6 +-- tests/phpunit/includes/SpecialPageTest.php | 60 ++++++++++++++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 tests/phpunit/includes/SpecialPageTest.php diff --git a/includes/SpecialPage.php b/includes/SpecialPage.php index a6195fc41e..20571d7135 100644 --- a/includes/SpecialPage.php +++ b/includes/SpecialPage.php @@ -259,11 +259,7 @@ class SpecialPage { */ public static function getTitleFor( $name, $subpage = false, $fragment = '' ) { $name = SpecialPageFactory::getLocalNameFor( $name, $subpage ); - if ( $name ) { - return Title::makeTitle( NS_SPECIAL, $name, $fragment ); - } else { - throw new MWException( "Invalid special page name \"$name\"" ); - } + return Title::makeTitle( NS_SPECIAL, $name, $fragment ); } /** diff --git a/tests/phpunit/includes/SpecialPageTest.php b/tests/phpunit/includes/SpecialPageTest.php new file mode 100644 index 0000000000..fb8f7ad90b --- /dev/null +++ b/tests/phpunit/includes/SpecialPageTest.php @@ -0,0 +1,60 @@ + + */ +class SpecialPageTest extends MediaWikiTestCase { + + public function setUp() { + parent::setUp(); + + $this->setMwGlobals( array( + 'wgContLang' => Language::factory( 'en' ) + ) ); + } + + /** + * @dataProvider getTitleForProvider + */ + public function testGetTitleFor( $expectedName, $name ) { + $title = SpecialPage::getTitleFor( $name ); + $expected = Title::makeTitle( NS_SPECIAL, $expectedName ); + $this->assertEquals( $expected, $title ); + } + + public function getTitleForProvider() { + return array( + array( 'UserLogin', 'Userlogin' ) + ); + } + + /** + * @expectedException PHPUnit_Framework_Error_Notice + */ + public function testInvalidGetTitleFor() { + $title = SpecialPage::getTitleFor( 'cat' ); + $expected = Title::makeTitle( NS_SPECIAL, 'Cat' ); + $this->assertEquals( $expected, $title ); + } + + /** + * @expectedException PHPUnit_Framework_Error_Notice + * @dataProvider getTitleForWithWarningProvider + */ + public function testGetTitleForWithWarning( $expected, $name ) { + $title = SpecialPage::getTitleFor( $name ); + $this->assertEquals( $expected, $title ); + } + + public function getTitleForWithWarningProvider() { + return array( + array( Title::makeTitle( NS_SPECIAL, 'UserLogin' ), 'UserLogin' ) + ); + } + +} -- 2.20.1