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