summaryrefslogtreecommitdiff
path: root/tests/phpunit/skins/SideBarTest.php
blob: 47182a711a642019c4eb49f16558bab2d6eacf7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php

/**
 * @group Skin
 */
class SideBarTest extends MediaWikiLangTestCase {

	/** A skin template, reinitialized before each test */
	private $skin;
	/** Local cache for sidebar messages */
	private $messages;

	function __construct() {
		parent::__construct();
	}

	/** Build $this->messages array */
	private function initMessagesHref() {
		# List of default messages for the sidebar:
		$URL_messages = array(
			'mainpage',
			'portal-url',
			'currentevents-url',
			'recentchanges-url',
			'randompage-url',
			'helppage',
		);

		foreach( $URL_messages as $m ) {
			$titleName = MessageCache::singleton()->get($m);
			$title = Title::newFromText( $titleName );
			$this->messages[$m]['href'] = $title->getLocalURL();
		}
	}

	function setUp() {
		parent::setUp();
		$this->initMessagesHref();
		$this->skin = new SkinTemplate();
	}
	function tearDown() {
		parent::tearDown();
		$this->skin = null;
	}

	/**
	 * Internal helper to test the sidebar
	 * @param $expected
	 * @param $text
	 * @param $message (Default: '')
	 */
	private function assertSideBar( $expected, $text, $message = '' ) {
		$bar = array();
		$this->skin->addToSidebarPlain( $bar, $text );
		$this->assertEquals( $expected, $bar, $message );
	}

	function testSidebarWithOnlyTwoTitles() {
		$this->assertSideBar(
		array(
			'Title1' => array(),
			'Title2' => array(),
		),
'* Title1
* Title2
'
		);
	}

	function testExpandMessages() {
		$this->assertSidebar(
		array( 'Title' => array(
			array(
				'text' => 'Help',
				'href' => $this->messages['helppage']['href'],
				'id' => 'n-help',
				'active' => null
			)
		)),
'* Title
** helppage|help
'
		);
	}

	function testExternalUrlsRequireADescription() {
		$this->assertSidebar(
		array( 'Title' => array(
			# ** http://www.mediawiki.org/| Home
			array(
				'text'   => 'Home',
				'href'   => 'http://www.mediawiki.org/',
				'id'     => 'n-Home',
				'active' => null,
				'rel'    => 'nofollow',
			),
			# ** http://valid.no.desc.org/
			# ... skipped since it is missing a pipe with a description
		)),
'* Title
** http://www.mediawiki.org/| Home
** http://valid.no.desc.org/
'

		);

	}


	#### Attributes for external links ##########################
	private function getAttribs( ) {
		# Sidebar text we will use everytime
		$text = '* Title
** http://www.mediawiki.org/| Home';

		$bar = array();
		$this->skin->addToSideBarPlain( $bar, $text );

		return $bar['Title'][0];
	}

	/**
	 * Simple test to verify our helper assertAttribs() is functional
	 * Please note this assume MediaWiki default settings:
	 *   $wgNoFollowLinks = true
	 *   $wgExternalLinkTarget = false
	 */
	function testTestAttributesAssertionHelper() {
		$attribs = $this->getAttribs();

		$this->assertArrayHasKey( 'rel', $attribs );
		$this->assertEquals( 'nofollow', $attribs['rel'] );

		$this->assertArrayNotHasKey( 'target', $attribs );
	}

	/**
	 * Test wgNoFollowLinks in sidebar
	 */
	function testRespectWgnofollowlinks() {
		global $wgNoFollowLinks;
		$saved = $wgNoFollowLinks;
		$wgNoFollowLinks = false;

		$attribs = $this->getAttribs();
		$this->assertArrayNotHasKey( 'rel', $attribs,
			'External URL in sidebar do not have rel=nofollow when wgNoFollowLinks = false'
		);

		// Restore global
		$wgNoFollowLinks = $saved;
	}

	/**
	 * Test wgExternaLinkTarget in sidebar
	 */
	function testRespectExternallinktarget() {
		global $wgExternalLinkTarget;
		$saved = $wgExternalLinkTarget;

		$wgExternalLinkTarget = '_blank';
		$attribs = $this->getAttribs();
		$this->assertArrayHasKey( 'target', $attribs );
		$this->assertEquals( $attribs['target'], '_blank' );

		$wgExternalLinkTarget = '_self';
		$attribs = $this->getAttribs();
		$this->assertArrayHasKey( 'target', $attribs );
		$this->assertEquals( $attribs['target'], '_self' );

		// Restore global
		$wgExternalLinkTarget = $saved;
	}

}