47182a711a642019c4eb49f16558bab2d6eacf7a
[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 function __construct() {
14 parent::__construct();
15 }
16
17 /** Build $this->messages array */
18 private function initMessagesHref() {
19 # List of default messages for the sidebar:
20 $URL_messages = array(
21 'mainpage',
22 'portal-url',
23 'currentevents-url',
24 'recentchanges-url',
25 'randompage-url',
26 'helppage',
27 );
28
29 foreach( $URL_messages as $m ) {
30 $titleName = MessageCache::singleton()->get($m);
31 $title = Title::newFromText( $titleName );
32 $this->messages[$m]['href'] = $title->getLocalURL();
33 }
34 }
35
36 function setUp() {
37 parent::setUp();
38 $this->initMessagesHref();
39 $this->skin = new SkinTemplate();
40 }
41 function tearDown() {
42 parent::tearDown();
43 $this->skin = null;
44 }
45
46 /**
47 * Internal helper to test the sidebar
48 * @param $expected
49 * @param $text
50 * @param $message (Default: '')
51 */
52 private function assertSideBar( $expected, $text, $message = '' ) {
53 $bar = array();
54 $this->skin->addToSidebarPlain( $bar, $text );
55 $this->assertEquals( $expected, $bar, $message );
56 }
57
58 function testSidebarWithOnlyTwoTitles() {
59 $this->assertSideBar(
60 array(
61 'Title1' => array(),
62 'Title2' => array(),
63 ),
64 '* Title1
65 * Title2
66 '
67 );
68 }
69
70 function testExpandMessages() {
71 $this->assertSidebar(
72 array( 'Title' => array(
73 array(
74 'text' => 'Help',
75 'href' => $this->messages['helppage']['href'],
76 'id' => 'n-help',
77 'active' => null
78 )
79 )),
80 '* Title
81 ** helppage|help
82 '
83 );
84 }
85
86 function testExternalUrlsRequireADescription() {
87 $this->assertSidebar(
88 array( 'Title' => array(
89 # ** http://www.mediawiki.org/| Home
90 array(
91 'text' => 'Home',
92 'href' => 'http://www.mediawiki.org/',
93 'id' => 'n-Home',
94 'active' => null,
95 'rel' => 'nofollow',
96 ),
97 # ** http://valid.no.desc.org/
98 # ... skipped since it is missing a pipe with a description
99 )),
100 '* Title
101 ** http://www.mediawiki.org/| Home
102 ** http://valid.no.desc.org/
103 '
104
105 );
106
107 }
108
109
110 #### Attributes for external links ##########################
111 private function getAttribs( ) {
112 # Sidebar text we will use everytime
113 $text = '* Title
114 ** http://www.mediawiki.org/| Home';
115
116 $bar = array();
117 $this->skin->addToSideBarPlain( $bar, $text );
118
119 return $bar['Title'][0];
120 }
121
122 /**
123 * Simple test to verify our helper assertAttribs() is functional
124 * Please note this assume MediaWiki default settings:
125 * $wgNoFollowLinks = true
126 * $wgExternalLinkTarget = false
127 */
128 function testTestAttributesAssertionHelper() {
129 $attribs = $this->getAttribs();
130
131 $this->assertArrayHasKey( 'rel', $attribs );
132 $this->assertEquals( 'nofollow', $attribs['rel'] );
133
134 $this->assertArrayNotHasKey( 'target', $attribs );
135 }
136
137 /**
138 * Test wgNoFollowLinks in sidebar
139 */
140 function testRespectWgnofollowlinks() {
141 global $wgNoFollowLinks;
142 $saved = $wgNoFollowLinks;
143 $wgNoFollowLinks = false;
144
145 $attribs = $this->getAttribs();
146 $this->assertArrayNotHasKey( 'rel', $attribs,
147 'External URL in sidebar do not have rel=nofollow when wgNoFollowLinks = false'
148 );
149
150 // Restore global
151 $wgNoFollowLinks = $saved;
152 }
153
154 /**
155 * Test wgExternaLinkTarget in sidebar
156 */
157 function testRespectExternallinktarget() {
158 global $wgExternalLinkTarget;
159 $saved = $wgExternalLinkTarget;
160
161 $wgExternalLinkTarget = '_blank';
162 $attribs = $this->getAttribs();
163 $this->assertArrayHasKey( 'target', $attribs );
164 $this->assertEquals( $attribs['target'], '_blank' );
165
166 $wgExternalLinkTarget = '_self';
167 $attribs = $this->getAttribs();
168 $this->assertArrayHasKey( 'target', $attribs );
169 $this->assertEquals( $attribs['target'], '_self' );
170
171 // Restore global
172 $wgExternalLinkTarget = $saved;
173 }
174
175 }