Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / skins / SideBarTest.php
blobde927fb343de19574bedb8c89550db57041dc3f9
1 <?php
3 use MediaWiki\MainConfigNames;
4 use MediaWiki\MediaWikiServices;
5 use MediaWiki\Title\Title;
7 /**
8 * @covers \Skin
9 * @covers \SkinTemplate
10 * @group Skin
11 * @group Database
13 class SideBarTest extends MediaWikiLangTestCase {
14 /** @var SkinTemplate */
15 private $skin;
16 /** @var string[][] Local cache for sidebar messages */
17 private $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.
22 $URL_messages = [
23 'mainpage',
24 'portal-url',
25 'currentevents-url',
26 'recentchanges-url',
27 'randompage-url',
28 'helppage',
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;
38 } else {
39 $title = Title::newFromText( $titleName );
40 $this->messages[$m]['href'] = $title->getLocalURL();
45 protected function setUp(): void {
46 parent::setUp();
47 $this->skin = new SkinTemplate();
48 $this->skin->getContext()->setLanguage( 'en' );
51 /** @return array */
52 public function provideSidebars() {
53 $this->initMessagesHref();
54 return [
55 // sidebar with only two titles
58 'Title1' => [],
59 'Title2' => [],
61 '* Title1
62 * Title2
65 // expand messages
67 [ 'Title' => [
69 'text' => 'Help',
70 'href' => $this->messages['helppage']['href'],
71 'id' => 'n-help',
72 'icon' => 'help',
73 'active' => null
75 ] ],
76 '* Title
77 ** helppage|help
80 // test tricky pipe - T35321 - Make sure there's a | after transforming.
82 [ 'Title' => [
83 # The first 2 are skipped
84 # Doesn't really test the url properly
85 # because it will vary with $wgArticlePath et al.
86 # ** Baz|Fred
88 'text' => 'Fred',
89 'href' => Title::makeTitle( NS_MAIN, 'Baz' )->getLocalURL(),
90 'id' => 'n-Fred',
91 'active' => null,
92 'icon' => null,
95 'text' => 'title-to-display',
96 'href' => Title::makeTitle( NS_MAIN, 'Page-to-go-to' )->getLocalURL(),
97 'id' => 'n-title-to-display',
98 'active' => null,
99 'icon' => null,
101 ] ],
102 '* Title
103 ** {{PAGENAME|Foo}}
104 ** Bar
105 ** Baz|Fred
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 = '' ) {
117 $bar = [];
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 => [],
127 ] );
129 $bar = [];
130 $text = '* Title
131 ** https://www.mediawiki.org/| Home
132 ** http://valid.no.desc.org/
134 $this->skin->addToSidebarPlain( $bar, $text );
135 $this->assertEquals(
136 [ 'Title' => [
137 # ** https://www.mediawiki.org/| Home
139 'text' => 'Home',
140 'href' => 'https://www.mediawiki.org/',
141 'id' => 'n-Home',
142 'active' => null,
143 'icon' => null,
144 'rel' => 'nofollow',
146 # ** http://valid.no.desc.org/
147 # ... skipped since it is missing a pipe with a description
148 ] ],
149 $bar
153 public function testProtocolRelativeExternalUrl() {
154 $this->overrideConfigValues( [
155 MainConfigNames::NoFollowLinks => true,
156 MainConfigNames::NoFollowDomainExceptions => [],
157 MainConfigNames::NoFollowNsExceptions => [],
158 ] );
160 $bar = [];
161 $text = '* Title
162 ** //www.mediawiki.org/| Home
164 $this->skin->addToSidebarPlain( $bar, $text );
165 $this->assertEquals(
166 [ 'Title' => [
167 # ** //www.mediawiki.org/| Home
169 'text' => 'Home',
170 'href' => '//www.mediawiki.org/', // not /wiki///www.mediawiki.org/ (T364539)
171 'id' => 'n-Home',
172 'active' => null,
173 'icon' => null,
174 'rel' => 'nofollow',
176 ] ],
177 $bar
181 private function getAttribs() {
182 # Sidebar text we will use everytime
183 $text = '* Title
184 ** https://www.mediawiki.org/| Home';
186 $bar = [];
187 $this->skin->addToSidebarPlain( $bar, $text );
189 return $bar['Title'][0];
193 * Test our assertAttribs() helper function
194 * @coversNothing
196 public function testTestAttributesAssertionHelper() {
197 $this->overrideConfigValues( [
198 MainConfigNames::NoFollowLinks => true,
199 MainConfigNames::NoFollowDomainExceptions => [],
200 MainConfigNames::NoFollowNsExceptions => [],
201 MainConfigNames::ExternalLinkTarget => false,
202 ] );
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() {
236 return [
237 [ '_blank' ],
238 [ '_self' ],