From 55ec9f2e4d9a9186879765596013cdcdb775ea4d Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Wed, 20 Apr 2016 01:07:58 -0700 Subject: [PATCH] Add LinkTarget::hasFragment() helper function 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 Change-Id: I49e607ae5a58c3aef96d0246297740e7d88ac816 --- includes/LinkTarget.php | 7 +++++++ includes/title/TitleValue.php | 8 +++++++ .../phpunit/includes/title/TitleValueTest.php | 21 ++++++++++++++----- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/includes/LinkTarget.php b/includes/LinkTarget.php index 175a8392a8..8246f7dfa4 100644 --- a/includes/LinkTarget.php +++ b/includes/LinkTarget.php @@ -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. * diff --git a/includes/title/TitleValue.php b/includes/title/TitleValue.php index c8ebc2ada1..18a06ea01d 100644 --- a/includes/title/TitleValue.php +++ b/includes/title/TitleValue.php @@ -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. diff --git a/tests/phpunit/includes/title/TitleValueTest.php b/tests/phpunit/includes/title/TitleValueTest.php index af7b758efb..013bbc16fc 100644 --- a/tests/phpunit/includes/title/TitleValueTest.php +++ b/tests/phpunit/includes/title/TitleValueTest.php @@ -26,12 +26,23 @@ */ 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() { -- 2.20.1