Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / skins / SideBarTest.php
bloba3122b94468f4bfbcac7be93a376715ef8da32b3
1 <?php
3 /**
4 * @group Skin
5 */
6 class SideBarTest extends MediaWikiLangTestCase {
8 /**
9 * A skin template, reinitialized before each test
10 * @var SkinTemplate
12 private $skin;
13 /** Local cache for sidebar messages */
14 private $messages;
16 /** Build $this->messages array */
17 private function initMessagesHref() {
18 # List of default messages for the sidebar. The sidebar doesn't care at
19 # all whether they are full URLs, interwiki links or local titles.
20 $URL_messages = array(
21 'mainpage',
22 'portal-url',
23 'currentevents-url',
24 'recentchanges-url',
25 'randompage-url',
26 'helppage',
29 # We're assuming that isValidURI works as advertised: it's also
30 # tested separately, in tests/phpunit/includes/HttpTest.php.
31 foreach ( $URL_messages as $m ) {
32 $titleName = MessageCache::singleton()->get( $m );
33 if ( Http::isValidURI( $titleName ) ) {
34 $this->messages[$m]['href'] = $titleName;
35 } else {
36 $title = Title::newFromText( $titleName );
37 $this->messages[$m]['href'] = $title->getLocalURL();
42 protected function setUp() {
43 parent::setUp();
44 $this->initMessagesHref();
45 $this->skin = new SkinTemplate();
46 $this->skin->getContext()->setLanguage( Language::factory( 'en' ) );
49 /**
50 * Internal helper to test the sidebar
51 * @param array $expected
52 * @param string $text
53 * @param string $message (Default: '')
54 * @todo this assert method to should be converted to a test using a dataprovider..
56 private function assertSideBar( $expected, $text, $message = '' ) {
57 $bar = array();
58 $this->skin->addToSidebarPlain( $bar, $text );
59 $this->assertEquals( $expected, $bar, $message );
62 /**
63 * @covers SkinTemplate::addToSidebarPlain
65 public function testSidebarWithOnlyTwoTitles() {
66 $this->assertSideBar(
67 array(
68 'Title1' => array(),
69 'Title2' => array(),
71 '* Title1
72 * Title2
77 /**
78 * @covers SkinTemplate::addToSidebarPlain
80 public function testExpandMessages() {
81 $this->assertSidebar(
82 array( 'Title' => array(
83 array(
84 'text' => 'Help',
85 'href' => $this->messages['helppage']['href'],
86 'id' => 'n-help',
87 'active' => null
89 ) ),
90 '* Title
91 ** helppage|help
96 /**
97 * @covers SkinTemplate::addToSidebarPlain
99 public function testExternalUrlsRequireADescription() {
100 $this->setMwGlobals( array(
101 'wgNoFollowLinks' => true,
102 'wgNoFollowDomainExceptions' => array(),
103 'wgNoFollowNsExceptions' => array(),
104 ) );
105 $this->assertSidebar(
106 array( 'Title' => array(
107 # ** http://www.mediawiki.org/| Home
108 array(
109 'text' => 'Home',
110 'href' => 'http://www.mediawiki.org/',
111 'id' => 'n-Home',
112 'active' => null,
113 'rel' => 'nofollow',
115 # ** http://valid.no.desc.org/
116 # ... skipped since it is missing a pipe with a description
117 ) ),
118 '* Title
119 ** http://www.mediawiki.org/| Home
120 ** http://valid.no.desc.org/
126 * bug 33321 - Make sure there's a | after transforming.
127 * @group Database
128 * @covers SkinTemplate::addToSidebarPlain
130 public function testTrickyPipe() {
131 $this->assertSidebar(
132 array( 'Title' => array(
133 # The first 2 are skipped
134 # Doesn't really test the url properly
135 # because it will vary with $wgArticlePath et al.
136 # ** Baz|Fred
137 array(
138 'text' => 'Fred',
139 'href' => Title::newFromText( 'Baz' )->getLocalURL(),
140 'id' => 'n-Fred',
141 'active' => null,
143 array(
144 'text' => 'title-to-display',
145 'href' => Title::newFromText( 'page-to-go-to' )->getLocalURL(),
146 'id' => 'n-title-to-display',
147 'active' => null,
149 ) ),
150 '* Title
151 ** {{PAGENAME|Foo}}
152 ** Bar
153 ** Baz|Fred
154 ** {{PLURAL:1|page-to-go-to{{int:pipe-separator/en}}title-to-display|branch not taken}}
159 #### Attributes for external links ##########################
160 private function getAttribs() {
161 # Sidebar text we will use everytime
162 $text = '* Title
163 ** http://www.mediawiki.org/| Home';
165 $bar = array();
166 $this->skin->addToSideBarPlain( $bar, $text );
168 return $bar['Title'][0];
172 * Simple test to verify our helper assertAttribs() is functional
174 public function testTestAttributesAssertionHelper() {
175 $this->setMwGlobals( array(
176 'wgNoFollowLinks' => true,
177 'wgNoFollowDomainExceptions' => array(),
178 'wgNoFollowNsExceptions' => array(),
179 'wgExternalLinkTarget' => false,
180 ) );
181 $attribs = $this->getAttribs();
183 $this->assertArrayHasKey( 'rel', $attribs );
184 $this->assertEquals( 'nofollow', $attribs['rel'] );
186 $this->assertArrayNotHasKey( 'target', $attribs );
190 * Test $wgNoFollowLinks in sidebar
192 public function testRespectWgnofollowlinks() {
193 $this->setMwGlobals( 'wgNoFollowLinks', false );
195 $attribs = $this->getAttribs();
196 $this->assertArrayNotHasKey( 'rel', $attribs,
197 'External URL in sidebar do not have rel=nofollow when $wgNoFollowLinks = false'
202 * Test $wgExternaLinkTarget in sidebar
203 * @dataProvider dataRespectExternallinktarget
205 public function testRespectExternallinktarget( $externalLinkTarget ) {
206 $this->setMwGlobals( 'wgExternalLinkTarget', $externalLinkTarget );
208 $attribs = $this->getAttribs();
209 $this->assertArrayHasKey( 'target', $attribs );
210 $this->assertEquals( $attribs['target'], $externalLinkTarget );
213 public static function dataRespectExternallinktarget() {
214 return array(
215 array( '_blank' ),
216 array( '_self' ),