Daniel Kinzler has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/369424 )

Change subject: Introduce Title::getTalkPageIfDefined.
......................................................................

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
---
M includes/Title.php
M tests/phpunit/includes/TitleTest.php
2 files changed, 67 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/24/369424/1

diff --git a/includes/Title.php b/includes/Title.php
index edfdaca..0a2f868 100644
--- a/includes/Title.php
+++ b/includes/Title.php
@@ -1317,6 +1317,21 @@
        }
 
        /**
+        * 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 c06a2e4..7770cbc 100644
--- a/tests/phpunit/includes/TitleTest.php
+++ b/tests/phpunit/includes/TitleTest.php
@@ -716,6 +716,58 @@
                $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' ],

-- 
To view, visit https://gerrit.wikimedia.org/r/369424
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6d2613d8f7105048022f8093186dc57f1f8173ab
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to