~ubuntu-branches/ubuntu/trusty/mediawiki/trusty

« back to all changes in this revision

Viewing changes to tests/phpunit/skins/SideBarTest.php

  • Committer: Package Import Robot
  • Author(s): Thorsten Glaser
  • Date: 2014-03-28 09:56:29 UTC
  • mfrom: (1.3.14)
  • Revision ID: package-import@ubuntu.com-20140328095629-1526y9tchdd507id
Tags: 1:1.19.14+dfsg-1
* New upstream security fix release (Closes: #742857):
  - (bug 62497) SECURITY: Add CSRF token on Special:ChangePassword
  - (bug 62467) Set a title for the context during import on the cli
* Use upstream-provided signing key bundle

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
                $this->skin->getContext()->setLanguage( Language::factory( 'en' ) );
 
41
        }
 
42
        function tearDown() {
 
43
                parent::tearDown();
 
44
                $this->skin = null;
 
45
        }
 
46
 
 
47
        /**
 
48
         * Internal helper to test the sidebar
 
49
         * @param $expected
 
50
         * @param $text
 
51
         * @param $message (Default: '')
 
52
         */
 
53
        private function assertSideBar( $expected, $text, $message = '' ) {
 
54
                $bar = array();
 
55
                $this->skin->addToSidebarPlain( $bar, $text );
 
56
                $this->assertEquals( $expected, $bar, $message );
 
57
        }
 
58
 
 
59
        function testSidebarWithOnlyTwoTitles() {
 
60
                $this->assertSideBar(
 
61
                array(
 
62
                        'Title1' => array(),
 
63
                        'Title2' => array(),
 
64
                ),
 
65
'* Title1
 
66
* Title2
 
67
'
 
68
                );
 
69
        }
 
70
 
 
71
        function testExpandMessages() {
 
72
                $this->assertSidebar(
 
73
                array( 'Title' => array(
 
74
                        array(
 
75
                                'text' => 'Help',
 
76
                                'href' => $this->messages['helppage']['href'],
 
77
                                'id' => 'n-help',
 
78
                                'active' => null
 
79
                        )
 
80
                )),
 
81
'* Title
 
82
** helppage|help
 
83
'
 
84
                );
 
85
        }
 
86
 
 
87
        function testExternalUrlsRequireADescription() {
 
88
                $this->assertSidebar(
 
89
                array( 'Title' => array(
 
90
                        # ** http://www.mediawiki.org/| Home
 
91
                        array(
 
92
                                'text'   => 'Home',
 
93
                                'href'   => 'http://www.mediawiki.org/',
 
94
                                'id'     => 'n-Home',
 
95
                                'active' => null,
 
96
                                'rel'    => 'nofollow',
 
97
                        ),
 
98
                        # ** http://valid.no.desc.org/
 
99
                        # ... skipped since it is missing a pipe with a description
 
100
                )),
 
101
'* Title
 
102
** http://www.mediawiki.org/| Home
 
103
** http://valid.no.desc.org/
 
104
'
 
105
 
 
106
                );
 
107
 
 
108
        }
 
109
        /**
 
110
         * bug 33321 - Make sure there's a | after transforming.
 
111
         * @group Database
 
112
         */
 
113
        function testTrickyPipe() {
 
114
                $this->assertSidebar(
 
115
                array( 'Title' => array(
 
116
                        # The first 2 are skipped
 
117
                        # Doesn't really test the url properly
 
118
                        # because it will vary with $wgArticlePath et al.
 
119
                        # ** Baz|Fred
 
120
                        array(
 
121
                                'text'   => 'Fred',
 
122
                                'href'   => Title::newFromText( 'Baz' )->getLocalUrl(),
 
123
                                'id'     => 'n-Fred',
 
124
                                'active' => null,
 
125
                        ),
 
126
                        array(
 
127
                                'text'   => 'title-to-display',
 
128
                                'href'   => Title::newFromText( 'page-to-go-to' )->getLocalUrl(),
 
129
                                'id'     => 'n-title-to-display',
 
130
                                'active' => null,
 
131
                        ),
 
132
                )),
 
133
'* Title
 
134
** {{PAGENAME|Foo}}
 
135
** Bar
 
136
** Baz|Fred
 
137
** {{PLURAL:1|page-to-go-to{{int:pipe-separator/en}}title-to-display|branch not taken}}
 
138
'
 
139
                );
 
140
 
 
141
        }
 
142
 
 
143
 
 
144
        #### Attributes for external links ##########################
 
145
        private function getAttribs( ) {
 
146
                # Sidebar text we will use everytime
 
147
                $text = '* Title
 
148
** http://www.mediawiki.org/| Home';
 
149
 
 
150
                $bar = array();
 
151
                $this->skin->addToSideBarPlain( $bar, $text );
 
152
 
 
153
                return $bar['Title'][0];
 
154
        }
 
155
 
 
156
        /**
 
157
         * Simple test to verify our helper assertAttribs() is functional
 
158
         * Please note this assume MediaWiki default settings:
 
159
         *   $wgNoFollowLinks = true
 
160
         *   $wgExternalLinkTarget = false
 
161
         */
 
162
        function testTestAttributesAssertionHelper() {
 
163
                $attribs = $this->getAttribs();
 
164
 
 
165
                $this->assertArrayHasKey( 'rel', $attribs );
 
166
                $this->assertEquals( 'nofollow', $attribs['rel'] );
 
167
 
 
168
                $this->assertArrayNotHasKey( 'target', $attribs );
 
169
        }
 
170
 
 
171
        /**
 
172
         * Test $wgNoFollowLinks in sidebar
 
173
         */
 
174
        function testRespectWgnofollowlinks() {
 
175
                global $wgNoFollowLinks;
 
176
                $saved = $wgNoFollowLinks;
 
177
                $wgNoFollowLinks = false;
 
178
 
 
179
                $attribs = $this->getAttribs();
 
180
                $this->assertArrayNotHasKey( 'rel', $attribs,
 
181
                        'External URL in sidebar do not have rel=nofollow when $wgNoFollowLinks = false'
 
182
                );
 
183
 
 
184
                // Restore global
 
185
                $wgNoFollowLinks = $saved;
 
186
        }
 
187
 
 
188
        /**
 
189
         * Test $wgExternaLinkTarget in sidebar
 
190
         */
 
191
        function testRespectExternallinktarget() {
 
192
                global $wgExternalLinkTarget;
 
193
                $saved = $wgExternalLinkTarget;
 
194
 
 
195
                $wgExternalLinkTarget = '_blank';
 
196
                $attribs = $this->getAttribs();
 
197
                $this->assertArrayHasKey( 'target', $attribs );
 
198
                $this->assertEquals( $attribs['target'], '_blank' );
 
199
 
 
200
                $wgExternalLinkTarget = '_self';
 
201
                $attribs = $this->getAttribs();
 
202
                $this->assertArrayHasKey( 'target', $attribs );
 
203
                $this->assertEquals( $attribs['target'], '_self' );
 
204
 
 
205
                // Restore global
 
206
                $wgExternalLinkTarget = $saved;
 
207
        }
 
208
 
 
209
}