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