From f284dffc212bbf6628ccbd386cc1d2b16ace891c Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 1 Aug 2017 19:09:13 +0200 Subject: [PATCH] Introduce Title::getTalkPageIfDefined. This is part of the effort to remove the assumption that every page can have a talk page. Before we can merge Icee208dc4 which makes Title::getTalkPage() throw an exception of no corresponding talk namespace is defined, all extensions that call getTalkPage() must be changed to either check canHaveTalkPage() first, or to use the conveniance function getTalkPageIfDefined() instead. Bug: T165149 Bug: T172146 Change-Id: I6d2613d8f7105048022f8093186dc57f1f8173ab --- includes/Title.php | 15 ++++++++ tests/phpunit/includes/TitleTest.php | 52 ++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/includes/Title.php b/includes/Title.php index edfdacacc0..0a2f86810b 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -1316,6 +1316,21 @@ class Title implements LinkTarget { return self::makeTitle( MWNamespace::getTalk( $this->getNamespace() ), $this->getDBkey() ); } + /** + * Get a Title object associated with the talk page of this article, + * if such a talk page can exist. + * + * @return Title The object for the talk page, + * or null if no associated talk page can exist, according to canHaveTalkPage(). + */ + public function getTalkPageIfDefined() { + if ( !$this->canHaveTalkPage() ) { + return null; + } + + return $this->getTalkPage(); + } + /** * Get a title object associated with the subject page of this * talk page diff --git a/tests/phpunit/includes/TitleTest.php b/tests/phpunit/includes/TitleTest.php index c06a2e4f1c..7770cbc2c3 100644 --- a/tests/phpunit/includes/TitleTest.php +++ b/tests/phpunit/includes/TitleTest.php @@ -716,6 +716,58 @@ class TitleTest extends MediaWikiTestCase { $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() ); } + public static function provideGetTalkPage_good() { + return [ + [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ], + [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ], + ]; + } + + /** + * @dataProvider provideGetTalkPage_good + * @covers Title::getTalkPage + */ + public function testGetTalkPage_good( Title $title, Title $expected ) { + $talk = $title->getTalkPage(); + $this->assertSame( + $expected->getPrefixedDBKey(), + $talk->getPrefixedDBKey(), + $title->getPrefixedDBKey() + ); + } + + /** + * @dataProvider provideGetTalkPage_good + * @covers Title::getTalkPageIfDefined + */ + public function testGetTalkPageIfDefined_good( Title $title ) { + $talk = $title->getTalkPageIfDefined(); + $this->assertInstanceOf( + Title::class, + $talk, + $title->getPrefixedDBKey() + ); + } + + public static function provideGetTalkPage_bad() { + return [ + [ Title::makeTitle( NS_SPECIAL, 'Test' ) ], + [ Title::makeTitle( NS_MEDIA, 'Test' ) ], + ]; + } + + /** + * @dataProvider provideGetTalkPage_bad + * @covers Title::getTalkPageIfDefined + */ + public function testGetTalkPageIfDefined_bad( Title $title ) { + $talk = $title->getTalkPageIfDefined(); + $this->assertNull( + $talk, + $title->getPrefixedDBKey() + ); + } + public function provideCreateFragmentTitle() { return [ [ Title::makeTitle( NS_MAIN, 'Test' ), 'foo' ], -- 2.20.1