Add LinkTarget::hasFragment() helper function
authorKunal Mehta <legoktm@member.fsf.org>
Wed, 20 Apr 2016 08:07:58 +0000 (01:07 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Wed, 20 Apr 2016 22:51:39 +0000 (15:51 -0700)
LinkTarget::hasFragment() is a helper function which returns a boolean
of whether the target has a fragment. Title already had such a function,
and one was added to TitleValue.

Co-Authored-By: addshore <addshorewiki@gmail.com>
Change-Id: I49e607ae5a58c3aef96d0246297740e7d88ac816

includes/LinkTarget.php
includes/title/TitleValue.php
tests/phpunit/includes/title/TitleValueTest.php

index 175a839..8246f7d 100644 (file)
@@ -21,6 +21,13 @@ interface LinkTarget {
         */
        public function getFragment();
 
+       /**
+        * Whether the link target has a fragment
+        *
+        * @return bool
+        */
+       public function hasFragment();
+
        /**
         * Get the main part with underscores.
         *
index c8ebc2a..18a06ea 100644 (file)
@@ -95,6 +95,14 @@ class TitleValue implements LinkTarget {
                return $this->fragment;
        }
 
+       /**
+        * @since 1.27
+        * @return bool
+        */
+       public function hasFragment() {
+               return $this->fragment !== '';
+       }
+
        /**
         * Returns the title's DB key, as supplied to the constructor,
         * without namespace prefix or fragment.
index af7b758..013bbc1 100644 (file)
  */
 class TitleValueTest extends MediaWikiTestCase {
 
-       public function testConstruction() {
-               $title = new TitleValue( NS_USER, 'TestThis', 'stuff' );
+       public function goodConstructorProvider() {
+               return [
+                       [ NS_USER, 'TestThis', 'stuff', true ],
+                       [ NS_USER, 'TestThis', '', false ],
+               ];
+       }
 
-               $this->assertEquals( NS_USER, $title->getNamespace() );
-               $this->assertEquals( 'TestThis', $title->getText() );
-               $this->assertEquals( 'stuff', $title->getFragment() );
+       /**
+        * @dataProvider goodConstructorProvider
+        */
+       public function testConstruction( $ns, $text, $fragment, $hasFragment ) {
+               $title = new TitleValue( $ns, $text, $fragment );
+
+               $this->assertEquals( $ns, $title->getNamespace() );
+               $this->assertEquals( $text, $title->getText() );
+               $this->assertEquals( $fragment, $title->getFragment() );
+               $this->assertEquals( $hasFragment, $title->hasFragment() );
        }
 
        public function badConstructorProvider() {