3 use MediaWiki\MainConfigNames
;
4 use MediaWiki\MediaWikiServices
;
5 use MediaWiki\Title\Title
;
9 * @covers \SkinTemplate
13 class SideBarTest
extends MediaWikiLangTestCase
{
14 /** @var SkinTemplate */
16 /** @var string[][] Local cache for sidebar messages */
19 private function initMessagesHref() {
20 # List of default messages for the sidebar. The sidebar doesn't care at
21 # all whether they are full URLs, interwiki links or local titles.
31 $messageCache = MediaWikiServices
::getInstance()->getMessageCache();
32 # We're assuming that isValidURI works as advertised: it's also
33 # tested separately, in tests/phpunit/includes/HttpTest.php.
34 foreach ( $URL_messages as $m ) {
35 $titleName = $messageCache->get( $m );
36 if ( MWHttpRequest
::isValidURI( $titleName ) ) {
37 $this->messages
[$m]['href'] = $titleName;
39 $title = Title
::newFromText( $titleName );
40 $this->messages
[$m]['href'] = $title->getLocalURL();
45 protected function setUp(): void
{
47 $this->skin
= new SkinTemplate();
48 $this->skin
->getContext()->setLanguage( 'en' );
52 public function provideSidebars() {
53 $this->initMessagesHref();
55 // sidebar with only two titles
70 'href' => $this->messages
['helppage']['href'],
80 // test tricky pipe - T35321 - Make sure there's a | after transforming.
83 # The first 2 are skipped
84 # Doesn't really test the url properly
85 # because it will vary with $wgArticlePath et al.
89 'href' => Title
::makeTitle( NS_MAIN
, 'Baz' )->getLocalURL(),
95 'text' => 'title-to-display',
96 'href' => Title
::makeTitle( NS_MAIN
, 'Page-to-go-to' )->getLocalURL(),
97 'id' => 'n-title-to-display',
106 ** {{PLURAL:1|page-to-go-to{{int:pipe-separator/en}}title-to-display|branch not taken}}
114 * @dataProvider provideSidebars
116 public function testAddToSidebarPlain( $expected, $text, $message = '' ) {
118 $this->skin
->addToSidebarPlain( $bar, $text );
119 $this->assertEquals( $expected, $bar, $message );
122 public function testExternalUrlsRequireADescription() {
123 $this->overrideConfigValues( [
124 MainConfigNames
::NoFollowLinks
=> true,
125 MainConfigNames
::NoFollowDomainExceptions
=> [],
126 MainConfigNames
::NoFollowNsExceptions
=> [],
131 ** https://www.mediawiki.org/| Home
132 ** http://valid.no.desc.org/
134 $this->skin
->addToSidebarPlain( $bar, $text );
137 # ** https://www.mediawiki.org/| Home
140 'href' => 'https://www.mediawiki.org/',
146 # ** http://valid.no.desc.org/
147 # ... skipped since it is missing a pipe with a description
153 public function testProtocolRelativeExternalUrl() {
154 $this->overrideConfigValues( [
155 MainConfigNames
::NoFollowLinks
=> true,
156 MainConfigNames
::NoFollowDomainExceptions
=> [],
157 MainConfigNames
::NoFollowNsExceptions
=> [],
162 ** //www.mediawiki.org/| Home
164 $this->skin
->addToSidebarPlain( $bar, $text );
167 # ** //www.mediawiki.org/| Home
170 'href' => '//www.mediawiki.org/', // not /wiki///www.mediawiki.org/ (T364539)
181 private function getAttribs() {
182 # Sidebar text we will use everytime
184 ** https://www.mediawiki.org/| Home';
187 $this->skin
->addToSidebarPlain( $bar, $text );
189 return $bar['Title'][0];
193 * Test our assertAttribs() helper function
196 public function testTestAttributesAssertionHelper() {
197 $this->overrideConfigValues( [
198 MainConfigNames
::NoFollowLinks
=> true,
199 MainConfigNames
::NoFollowDomainExceptions
=> [],
200 MainConfigNames
::NoFollowNsExceptions
=> [],
201 MainConfigNames
::ExternalLinkTarget
=> false,
203 $attribs = $this->getAttribs();
205 $this->assertArrayHasKey( 'rel', $attribs );
206 $this->assertEquals( 'nofollow', $attribs['rel'] );
208 $this->assertArrayNotHasKey( 'target', $attribs );
212 * Test $wgNoFollowLinks in sidebar
214 public function testRespectWgnofollowlinks() {
215 $this->overrideConfigValue( MainConfigNames
::NoFollowLinks
, false );
217 $attribs = $this->getAttribs();
218 $this->assertArrayNotHasKey( 'rel', $attribs,
219 'External URL in sidebar do not have rel=nofollow when $wgNoFollowLinks = false'
224 * Test $wgExternaLinkTarget in sidebar
225 * @dataProvider dataRespectExternallinktarget
227 public function testRespectExternallinktarget( $externalLinkTarget ) {
228 $this->overrideConfigValue( MainConfigNames
::ExternalLinkTarget
, $externalLinkTarget );
230 $attribs = $this->getAttribs();
231 $this->assertArrayHasKey( 'target', $attribs );
232 $this->assertEquals( $attribs['target'], $externalLinkTarget );
235 public static function dataRespectExternallinktarget() {