Remove unused exception in SpecialPage::getTitleFor and add tests
authoraude <aude.wiki@gmail.com>
Sat, 16 Nov 2013 14:24:02 +0000 (15:24 +0100)
committeraude <aude.wiki@gmail.com>
Sat, 16 Nov 2013 19:48:47 +0000 (20:48 +0100)
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
tests/phpunit/includes/SpecialPageTest.php [new file with mode: 0644]

index a6195fc..20571d7 100644 (file)
@@ -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 (file)
index 0000000..fb8f7ad
--- /dev/null
@@ -0,0 +1,60 @@
+<?php
+
+/**
+ * @covers SpecialPage
+ *
+ * @group Database
+ *
+ * @licence GNU GPL v2+
+ * @author Katie Filbert < aude.wiki@gmail.com >
+ */
+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' )
+               );
+       }
+
+}