Merge "Add DROP INDEX support to DatabaseSqlite::replaceVars method"
[lhc/web/wiklou.git] / tests / phpunit / skins / SideBarTest.php
1 <?php
2
3 /**
4 * @group Skin
5 */
6 class SideBarTest extends MediaWikiLangTestCase {
7
8 /**
9 * A skin template, reinitialized before each test
10 * @var SkinTemplate
11 */
12 private $skin;
13 /** Local cache for sidebar messages */
14 private $messages;
15
16 /** Build $this->messages array */
17 private function initMessagesHref() {
18 # List of default messages for the sidebar:
19 $URL_messages = array(
20 'mainpage',
21 'portal-url',
22 'currentevents-url',
23 'recentchanges-url',
24 'randompage-url',
25 'helppage',
26 );
27
28 foreach ( $URL_messages as $m ) {
29 $titleName = MessageCache::singleton()->get( $m );
30 $title = Title::newFromText( $titleName );
31 $this->messages[$m]['href'] = $title->getLocalURL();
32 }
33 }
34
35 protected function setUp() {
36 parent::setUp();
37 $this->initMessagesHref();
38 $this->skin = new SkinTemplate();
39 $this->skin->getContext()->setLanguage( Language::factory( 'en' ) );
40 }
41
42 /**
43 * Internal helper to test the sidebar
44 * @param $expected
45 * @param $text
46 * @param $message (Default: '')
47 * @todo this assert method to should be converted to a test using a dataprovider..
48 */
49 private function assertSideBar( $expected, $text, $message = '' ) {
50 $bar = array();
51 $this->skin->addToSidebarPlain( $bar, $text );
52 $this->assertEquals( $expected, $bar, $message );
53 }
54
55 /**
56 * @covers SkinTemplate::addToSidebarPlain
57 */
58 public function testSidebarWithOnlyTwoTitles() {
59 $this->assertSideBar(
60 array(
61 'Title1' => array(),
62 'Title2' => array(),
63 ),
64 '* Title1
65 * Title2
66 '
67 );
68 }
69
70 /**
71 * @covers SkinTemplate::addToSidebarPlain
72 */
73 public function testExpandMessages() {
74 $this->assertSidebar(
75 array( 'Title' => array(
76 array(
77 'text' => 'Help',
78 'href' => $this->messages['helppage']['href'],
79 'id' => 'n-help',
80 'active' => null
81 )
82 ) ),
83 '* Title
84 ** helppage|help
85 '
86 );
87 }
88
89 /**
90 * @covers SkinTemplate::addToSidebarPlain
91 */
92 public function testExternalUrlsRequireADescription() {
93 $this->assertSidebar(
94 array( 'Title' => array(
95 # ** http://www.mediawiki.org/| Home
96 array(
97 'text' => 'Home',
98 'href' => 'http://www.mediawiki.org/',
99 'id' => 'n-Home',
100 'active' => null,
101 'rel' => 'nofollow',
102 ),
103 # ** http://valid.no.desc.org/
104 # ... skipped since it is missing a pipe with a description
105 ) ),
106 '* Title
107 ** http://www.mediawiki.org/| Home
108 ** http://valid.no.desc.org/
109 '
110 );
111 }
112
113 /**
114 * bug 33321 - Make sure there's a | after transforming.
115 * @group Database
116 * @covers SkinTemplate::addToSidebarPlain
117 */
118 public function testTrickyPipe() {
119 $this->assertSidebar(
120 array( 'Title' => array(
121 # The first 2 are skipped
122 # Doesn't really test the url properly
123 # because it will vary with $wgArticlePath et al.
124 # ** Baz|Fred
125 array(
126 'text' => 'Fred',
127 'href' => Title::newFromText( 'Baz' )->getLocalURL(),
128 'id' => 'n-Fred',
129 'active' => null,
130 ),
131 array(
132 'text' => 'title-to-display',
133 'href' => Title::newFromText( 'page-to-go-to' )->getLocalURL(),
134 'id' => 'n-title-to-display',
135 'active' => null,
136 ),
137 ) ),
138 '* Title
139 ** {{PAGENAME|Foo}}
140 ** Bar
141 ** Baz|Fred
142 ** {{PLURAL:1|page-to-go-to{{int:pipe-separator/en}}title-to-display|branch not taken}}
143 '
144 );
145 }
146
147
148 #### Attributes for external links ##########################
149 private function getAttribs() {
150 # Sidebar text we will use everytime
151 $text = '* Title
152 ** http://www.mediawiki.org/| Home';
153
154 $bar = array();
155 $this->skin->addToSideBarPlain( $bar, $text );
156
157 return $bar['Title'][0];
158 }
159
160 /**
161 * Simple test to verify our helper assertAttribs() is functional
162 */
163 public function testTestAttributesAssertionHelper() {
164 $this->setMwGlobals( array(
165 'wgNoFollowLinks' => true,
166 'wgExternalLinkTarget' => false,
167 ) );
168 $attribs = $this->getAttribs();
169
170 $this->assertArrayHasKey( 'rel', $attribs );
171 $this->assertEquals( 'nofollow', $attribs['rel'] );
172
173 $this->assertArrayNotHasKey( 'target', $attribs );
174 }
175
176 /**
177 * Test $wgNoFollowLinks in sidebar
178 */
179 public function testRespectWgnofollowlinks() {
180 $this->setMwGlobals( 'wgNoFollowLinks', false );
181
182 $attribs = $this->getAttribs();
183 $this->assertArrayNotHasKey( 'rel', $attribs,
184 'External URL in sidebar do not have rel=nofollow when $wgNoFollowLinks = false'
185 );
186 }
187
188 /**
189 * Test $wgExternaLinkTarget in sidebar
190 * @dataProvider dataRespectExternallinktarget
191 */
192 public function testRespectExternallinktarget( $externalLinkTarget ) {
193 $this->setMwGlobals( 'wgExternalLinkTarget', $externalLinkTarget );
194
195 $attribs = $this->getAttribs();
196 $this->assertArrayHasKey( 'target', $attribs );
197 $this->assertEquals( $attribs['target'], $externalLinkTarget );
198 }
199
200 public static function dataRespectExternallinktarget() {
201 return array(
202 array( '_blank' ),
203 array( '_self' ),
204 );
205 }
206 }