Merge "Add validation of the content model edited by EditPage"
[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->setMwGlobals( array(
94 'wgNoFollowLinks' => true,
95 'wgNoFollowDomainExceptions' => array(),
96 'wgNoFollowNsExceptions' => array(),
97 ) );
98 $this->assertSidebar(
99 array( 'Title' => array(
100 # ** http://www.mediawiki.org/| Home
101 array(
102 'text' => 'Home',
103 'href' => 'http://www.mediawiki.org/',
104 'id' => 'n-Home',
105 'active' => null,
106 'rel' => 'nofollow',
107 ),
108 # ** http://valid.no.desc.org/
109 # ... skipped since it is missing a pipe with a description
110 ) ),
111 '* Title
112 ** http://www.mediawiki.org/| Home
113 ** http://valid.no.desc.org/
114 '
115 );
116 }
117
118 /**
119 * bug 33321 - Make sure there's a | after transforming.
120 * @group Database
121 * @covers SkinTemplate::addToSidebarPlain
122 */
123 public function testTrickyPipe() {
124 $this->assertSidebar(
125 array( 'Title' => array(
126 # The first 2 are skipped
127 # Doesn't really test the url properly
128 # because it will vary with $wgArticlePath et al.
129 # ** Baz|Fred
130 array(
131 'text' => 'Fred',
132 'href' => Title::newFromText( 'Baz' )->getLocalURL(),
133 'id' => 'n-Fred',
134 'active' => null,
135 ),
136 array(
137 'text' => 'title-to-display',
138 'href' => Title::newFromText( 'page-to-go-to' )->getLocalURL(),
139 'id' => 'n-title-to-display',
140 'active' => null,
141 ),
142 ) ),
143 '* Title
144 ** {{PAGENAME|Foo}}
145 ** Bar
146 ** Baz|Fred
147 ** {{PLURAL:1|page-to-go-to{{int:pipe-separator/en}}title-to-display|branch not taken}}
148 '
149 );
150 }
151
152 #### Attributes for external links ##########################
153 private function getAttribs() {
154 # Sidebar text we will use everytime
155 $text = '* Title
156 ** http://www.mediawiki.org/| Home';
157
158 $bar = array();
159 $this->skin->addToSideBarPlain( $bar, $text );
160
161 return $bar['Title'][0];
162 }
163
164 /**
165 * Simple test to verify our helper assertAttribs() is functional
166 */
167 public function testTestAttributesAssertionHelper() {
168 $this->setMwGlobals( array(
169 'wgNoFollowLinks' => true,
170 'wgNoFollowDomainExceptions' => array(),
171 'wgNoFollowNsExceptions' => array(),
172 'wgExternalLinkTarget' => false,
173 ) );
174 $attribs = $this->getAttribs();
175
176 $this->assertArrayHasKey( 'rel', $attribs );
177 $this->assertEquals( 'nofollow', $attribs['rel'] );
178
179 $this->assertArrayNotHasKey( 'target', $attribs );
180 }
181
182 /**
183 * Test $wgNoFollowLinks in sidebar
184 */
185 public function testRespectWgnofollowlinks() {
186 $this->setMwGlobals( 'wgNoFollowLinks', false );
187
188 $attribs = $this->getAttribs();
189 $this->assertArrayNotHasKey( 'rel', $attribs,
190 'External URL in sidebar do not have rel=nofollow when $wgNoFollowLinks = false'
191 );
192 }
193
194 /**
195 * Test $wgExternaLinkTarget in sidebar
196 * @dataProvider dataRespectExternallinktarget
197 */
198 public function testRespectExternallinktarget( $externalLinkTarget ) {
199 $this->setMwGlobals( 'wgExternalLinkTarget', $externalLinkTarget );
200
201 $attribs = $this->getAttribs();
202 $this->assertArrayHasKey( 'target', $attribs );
203 $this->assertEquals( $attribs['target'], $externalLinkTarget );
204 }
205
206 public static function dataRespectExternallinktarget() {
207 return array(
208 array( '_blank' ),
209 array( '_self' ),
210 );
211 }
212 }