Fix DatabaseSqlite IDEA warnings
[mediawiki.git] / tests / phpunit / skins / SideBarTest.php
blobaaa1febbd11e0d33c6cd58cf0cf063f5af95c3f4
1 <?php
3 /**
4 * @group Skin
5 */
6 class SideBarTest extends MediaWikiLangTestCase {
8 /**
9 * A skin template, reinitialized before each test
10 * @var SkinTemplate
12 private $skin;
13 /** Local cache for sidebar messages */
14 private $messages;
16 /** Build $this->messages array */
17 private function initMessagesHref() {
18 # List of default messages for the sidebar. The sidebar doesn't care at
19 # all whether they are full URLs, interwiki links or local titles.
20 $URL_messages = [
21 'mainpage',
22 'portal-url',
23 'currentevents-url',
24 'recentchanges-url',
25 'randompage-url',
26 'helppage',
29 # We're assuming that isValidURI works as advertised: it's also
30 # tested separately, in tests/phpunit/includes/HttpTest.php.
31 foreach ( $URL_messages as $m ) {
32 $titleName = MessageCache::singleton()->get( $m );
33 if ( Http::isValidURI( $titleName ) ) {
34 $this->messages[$m]['href'] = $titleName;
35 } else {
36 $title = Title::newFromText( $titleName );
37 $this->messages[$m]['href'] = $title->getLocalURL();
42 protected function setUp() {
43 parent::setUp();
44 $this->initMessagesHref();
45 $this->skin = new SkinTemplate();
46 $this->skin->getContext()->setLanguage( Language::factory( 'en' ) );
49 /**
50 * Internal helper to test the sidebar
51 * @param array $expected
52 * @param string $text
53 * @param string $message (Default: '')
54 * @todo this assert method to should be converted to a test using a dataprovider..
56 private function assertSideBar( $expected, $text, $message = '' ) {
57 $bar = [];
58 $this->skin->addToSidebarPlain( $bar, $text );
59 $this->assertEquals( $expected, $bar, $message );
62 /**
63 * @covers SkinTemplate::addToSidebarPlain
65 public function testSidebarWithOnlyTwoTitles() {
66 $this->assertSideBar(
68 'Title1' => [],
69 'Title2' => [],
71 '* Title1
72 * Title2
77 /**
78 * @covers SkinTemplate::addToSidebarPlain
80 public function testExpandMessages() {
81 $this->assertSideBar(
82 [ 'Title' => [
84 'text' => 'Help',
85 'href' => $this->messages['helppage']['href'],
86 'id' => 'n-help',
87 'active' => null
89 ] ],
90 '* Title
91 ** helppage|help
96 /**
97 * @covers SkinTemplate::addToSidebarPlain
99 public function testExternalUrlsRequireADescription() {
100 $this->setMwGlobals( [
101 'wgNoFollowLinks' => true,
102 'wgNoFollowDomainExceptions' => [],
103 'wgNoFollowNsExceptions' => [],
104 ] );
105 $this->assertSideBar(
106 [ 'Title' => [
107 # ** http://www.mediawiki.org/| Home
109 'text' => 'Home',
110 'href' => 'http://www.mediawiki.org/',
111 'id' => 'n-Home',
112 'active' => null,
113 'rel' => 'nofollow',
115 # ** http://valid.no.desc.org/
116 # ... skipped since it is missing a pipe with a description
117 ] ],
118 '* Title
119 ** http://www.mediawiki.org/| Home
120 ** http://valid.no.desc.org/
126 * bug 33321 - Make sure there's a | after transforming.
127 * @group Database
128 * @covers SkinTemplate::addToSidebarPlain
130 public function testTrickyPipe() {
131 $this->assertSideBar(
132 [ 'Title' => [
133 # The first 2 are skipped
134 # Doesn't really test the url properly
135 # because it will vary with $wgArticlePath et al.
136 # ** Baz|Fred
138 'text' => 'Fred',
139 'href' => Title::newFromText( 'Baz' )->getLocalURL(),
140 'id' => 'n-Fred',
141 'active' => null,
144 'text' => 'title-to-display',
145 'href' => Title::newFromText( 'page-to-go-to' )->getLocalURL(),
146 'id' => 'n-title-to-display',
147 'active' => null,
149 ] ],
150 '* Title
151 ** {{PAGENAME|Foo}}
152 ** Bar
153 ** Baz|Fred
154 ** {{PLURAL:1|page-to-go-to{{int:pipe-separator/en}}title-to-display|branch not taken}}
159 #### Attributes for external links ##########################
160 private function getAttribs() {
161 # Sidebar text we will use everytime
162 $text = '* Title
163 ** http://www.mediawiki.org/| Home';
165 $bar = [];
166 $this->skin->addToSidebarPlain( $bar, $text );
168 return $bar['Title'][0];
172 * Simple test to verify our helper assertAttribs() is functional
174 public function testTestAttributesAssertionHelper() {
175 $this->setMwGlobals( [
176 'wgNoFollowLinks' => true,
177 'wgNoFollowDomainExceptions' => [],
178 'wgNoFollowNsExceptions' => [],
179 'wgExternalLinkTarget' => false,
180 ] );
181 $attribs = $this->getAttribs();
183 $this->assertArrayHasKey( 'rel', $attribs );
184 $this->assertEquals( 'nofollow', $attribs['rel'] );
186 $this->assertArrayNotHasKey( 'target', $attribs );
190 * Test $wgNoFollowLinks in sidebar
192 public function testRespectWgnofollowlinks() {
193 $this->setMwGlobals( 'wgNoFollowLinks', false );
195 $attribs = $this->getAttribs();
196 $this->assertArrayNotHasKey( 'rel', $attribs,
197 'External URL in sidebar do not have rel=nofollow when $wgNoFollowLinks = false'
202 * Test $wgExternaLinkTarget in sidebar
203 * @dataProvider dataRespectExternallinktarget
205 public function testRespectExternallinktarget( $externalLinkTarget ) {
206 $this->setMwGlobals( 'wgExternalLinkTarget', $externalLinkTarget );
208 $attribs = $this->getAttribs();
209 $this->assertArrayHasKey( 'target', $attribs );
210 $this->assertEquals( $attribs['target'], $externalLinkTarget );
213 public static function dataRespectExternallinktarget() {
214 return [
215 [ '_blank' ],
216 [ '_self' ],