Improve return types in class MagicWordArray
[lhc/web/wiklou.git] / tests / phpunit / includes / specialpage / SpecialPageTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
6 * @covers SpecialPage
7 *
8 * @group Database
9 *
10 * @author Katie Filbert < aude.wiki@gmail.com >
11 */
12 class SpecialPageTest extends MediaWikiTestCase {
13
14 protected function setUp() {
15 parent::setUp();
16
17 $this->setContentLang( 'en' );
18 $this->setMwGlobals( [
19 'wgScript' => '/index.php',
20 ] );
21 }
22
23 /**
24 * @dataProvider getTitleForProvider
25 */
26 public function testGetTitleFor( $expectedName, $name ) {
27 $title = SpecialPage::getTitleFor( $name );
28 $expected = Title::makeTitle( NS_SPECIAL, $expectedName );
29 $this->assertEquals( $expected, $title );
30 }
31
32 public function getTitleForProvider() {
33 return [
34 [ 'UserLogin', 'Userlogin' ]
35 ];
36 }
37
38 /**
39 * @expectedException PHPUnit_Framework_Error_Notice
40 */
41 public function testInvalidGetTitleFor() {
42 $title = SpecialPage::getTitleFor( 'cat' );
43 $expected = Title::makeTitle( NS_SPECIAL, 'Cat' );
44 $this->assertEquals( $expected, $title );
45 }
46
47 /**
48 * @expectedException PHPUnit_Framework_Error_Notice
49 * @dataProvider getTitleForWithWarningProvider
50 */
51 public function testGetTitleForWithWarning( $expected, $name ) {
52 $title = SpecialPage::getTitleFor( $name );
53 $this->assertEquals( $expected, $title );
54 }
55
56 public function getTitleForWithWarningProvider() {
57 return [
58 [ Title::makeTitle( NS_SPECIAL, 'UserLogin' ), 'UserLogin' ]
59 ];
60 }
61
62 /**
63 * @dataProvider requireLoginAnonProvider
64 */
65 public function testRequireLoginAnon( $expected, $reason, $title ) {
66 $specialPage = new SpecialPage( 'Watchlist', 'viewmywatchlist' );
67
68 $user = User::newFromId( 0 );
69 $specialPage->getContext()->setUser( $user );
70 $specialPage->getContext()->setLanguage( Language::factory( 'en' ) );
71
72 $this->setExpectedException( UserNotLoggedIn::class, $expected );
73
74 // $specialPage->requireLogin( [ $reason [, $title ] ] )
75 call_user_func_array(
76 [ $specialPage, 'requireLogin' ],
77 array_filter( [ $reason, $title ] )
78 );
79 }
80
81 public function requireLoginAnonProvider() {
82 $lang = 'en';
83
84 $expected1 = wfMessage( 'exception-nologin-text' )->inLanguage( $lang )->text();
85 $expected2 = wfMessage( 'about' )->inLanguage( $lang )->text();
86
87 return [
88 [ $expected1, null, null ],
89 [ $expected2, 'about', null ],
90 [ $expected2, 'about', 'about' ],
91 ];
92 }
93
94 public function testRequireLoginNotAnon() {
95 $specialPage = new SpecialPage( 'Watchlist', 'viewmywatchlist' );
96
97 $user = User::newFromName( "UTSysop" );
98 $specialPage->getContext()->setUser( $user );
99
100 $specialPage->requireLogin();
101
102 // no exception thrown, logged in use can access special page
103 $this->assertTrue( true );
104 }
105
106 public function provideBuildPrevNextNavigation() {
107 yield [ 0, 20, false, false ];
108 yield [ 17, 20, false, false ];
109 yield [ 0, 17, false, false ];
110 yield [ 0, 20, true, 'Foo' ];
111 yield [ 17, 20, true, 'Föö_Bär' ];
112 }
113
114 /**
115 * @dataProvider provideBuildPrevNextNavigation
116 */
117 public function testBuildPrevNextNavigation( $offset, $limit, $atEnd, $subPage ) {
118 $this->setUserLang( Language::factory( 'qqx' ) ); // disable i18n
119
120 $specialPage = new SpecialPage( 'Watchlist' );
121 $specialPage = TestingAccessWrapper::newFromObject( $specialPage );
122
123 $html = $specialPage->buildPrevNextNavigation(
124 $offset,
125 $limit,
126 [ 'x' => 25 ],
127 $atEnd,
128 $subPage
129 );
130
131 $this->assertStringStartsWith( '(viewprevnext:', $html );
132
133 preg_match_all( '!<a.*?</a>!', $html, $m, PREG_PATTERN_ORDER );
134 $links = $m[0];
135
136 foreach ( $links as $a ) {
137 if ( $subPage ) {
138 $this->assertContains( 'Special:Watchlist/' . wfUrlencode( $subPage ), $a );
139 } else {
140 $this->assertContains( 'Special:Watchlist', $a );
141 $this->assertNotContains( 'Special:Watchlist/', $a );
142 }
143 $this->assertContains( 'x=25', $a );
144 }
145
146 $i = 0;
147
148 if ( $offset > 0 ) {
149 $this->assertContains(
150 'limit=' . $limit . '&amp;offset=' . max( 0, $offset - $limit ) . '&amp;',
151 $links[ $i ]
152 );
153 $this->assertContains( 'title="(prevn-title: ' . $limit . ')"', $links[$i] );
154 $this->assertContains( 'class="mw-prevlink"', $links[$i] );
155 $this->assertContains( '>(prevn: ' . $limit . ')<', $links[$i] );
156 $i += 1;
157 }
158
159 if ( !$atEnd ) {
160 $this->assertContains(
161 'limit=' . $limit . '&amp;offset=' . ( $offset + $limit ) . '&amp;',
162 $links[ $i ]
163 );
164 $this->assertContains( 'title="(nextn-title: ' . $limit . ')"', $links[$i] );
165 $this->assertContains( 'class="mw-nextlink"', $links[$i] );
166 $this->assertContains( '>(nextn: ' . $limit . ')<', $links[$i] );
167 $i += 1;
168 }
169
170 $this->assertCount( 5 + $i, $links );
171
172 $this->assertContains( 'limit=20&amp;offset=' . $offset, $links[$i] );
173 $this->assertContains( 'title="(shown-title: 20)"', $links[$i] );
174 $this->assertContains( 'class="mw-numlink"', $links[$i] );
175 $this->assertContains( '>20<', $links[$i] );
176 $i += 4;
177
178 $this->assertContains( 'limit=500&amp;offset=' . $offset, $links[$i] );
179 $this->assertContains( 'title="(shown-title: 500)"', $links[$i] );
180 $this->assertContains( 'class="mw-numlink"', $links[$i] );
181 $this->assertContains( '>500<', $links[$i] );
182 }
183
184 }