Debian Codegraph @

tests/phpunit/includes/specials/SpecialContributionsTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\MainConfigNames;
4 use MediaWiki\Permissions\Authority;
5 use MediaWiki\Permissions\UltimateAuthority;
6 use MediaWiki\Request\FauxRequest;
7 use MediaWiki\Specials\SpecialContributions;
8 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
9 use MediaWiki\User\User;
10 use MediaWiki\User\UserFactory;
11 use Wikimedia\Parsoid\Utils\DOMCompat;
12 use Wikimedia\Parsoid\Utils\DOMUtils;
13
14 /**
15 * @author Ammarpad
16 * @group Database
17 * @covers \MediaWiki\Specials\SpecialContributions
18 * @covers \MediaWiki\SpecialPage\ContributionsSpecialPage
19 */
20 class SpecialContributionsTest extends SpecialPageTestBase {
21 use TempUserTestTrait;
22
23 private const PAGE_NAME = __CLASS__ . 'BlaBlaTest';
24 /** @var Authority */
25 private static $admin;
26 /** @var User */
27 private static $user;
28 /** @var User */
29 private static $zeroUser;
30 private static int $useModWikiIPRevId;
31
32 public function addDBDataOnce() {
33 $this->overrideConfigValue(
34 MainConfigNames::RangeContributionsCIDRLimit,
35 [
36 'IPv4' => 16,
37 'IPv6' => 32,
38 ]
39 );
40 $this->setTemporaryHook(
41 'SpecialContributionsBeforeMainOutput',
42 static function () {
43 }
44 );
45 self::$admin = new UltimateAuthority( $this->getTestSysop()->getUser() );
46 $this->assertTrue(
47 $this->editPage(
48 self::PAGE_NAME, 'Test Content', 'test', NS_MAIN, self::$admin
49 )->isOK(),
50 'Edit failed for admin'
51 );
52
53 self::$user = $this->getTestUser()->getUser();
54 $this->assertTrue(
55 $this->editPage(
56 'Test', 'Test Content', 'test', NS_MAIN, self::$user
57 )->isOK(),
58 'Edit failed for user'
59 );
60
61 // The name of this user is '0' which is a valid name.
62 self::$zeroUser = ( new TestUser( '0' ) )->getUser();
63 $this->assertTrue(
64 $this->editPage(
65 'TestPage', 'Test Content', 'test', NS_MAIN, self::$zeroUser
66 )->isOK(),
67 'Edit failed for user'
68 );
69
70 $this->disableAutoCreateTempUser();
71 $useModWikiIP = $this->getServiceContainer()->getUserFactory()
72 ->newFromName( '1.2.3.xxx', UserFactory::RIGOR_NONE );
73 $useModWikiIPEditStatus = $this->editPage( 'Test1234', 'Test Content', 'test', NS_MAIN, $useModWikiIP );
74 $this->assertStatusGood( $useModWikiIPEditStatus, 'Edit failed for IP in usemod format' );
75 static::$useModWikiIPRevId = $useModWikiIPEditStatus->getNewRevision()->getId();
76
77 $blockStatus = $this->getServiceContainer()->getBlockUserFactory()
78 ->newBlockUser(
79 self::$user->getName(),
80 self::$admin,
81 'infinity',
82 '',
83 [ 'isHideUser' => true ],
84 )
85 ->placeBlock();
86 $this->assertStatusGood( $blockStatus, 'Block was not placed' );
87 }
88
89 public function testExecuteForZeroUser() {
90 [ $html ] = $this->executeSpecialPage(
91 self::$zeroUser->getName()
92 );
93
94 $this->assertStringContainsString( 'mw-pager-body', $html );
95 }
96
97 public function testExecuteEmptyTarget() {
98 [ $html ] = $this->executeSpecialPage();
99 // This 'topOnly' filter should always be added to Special:Contributions
100 $this->assertStringContainsString( 'topOnly', $html );
101 $this->assertStringNotContainsString( 'mw-pager-body', $html );
102 }
103
104 public function testExecuteHiddenTarget() {
105 [ $html ] = $this->executeSpecialPage(
106 self::$user->getName()
107 );
108 $this->assertStringNotContainsString( 'mw-contributions-list', $html );
109 }
110
111 public function testExecuteHiddenTargetWithPermissions() {
112 [ $html ] = $this->executeSpecialPage(
113 self::$user->getName(),
114 null,
115 'qqx',
116 // This is necessary because permission checks aren't actually
117 // done on the UlitmateAuthority that is self::$admin. Instead,
118 // they are done on a UserAuthority. See the TODO comment in
119 // User::getThisAsAuthority for more details.
120 $this->getTestUser( [
121 'sysop',
122 'bureaucrat',
123 'suppress'
124 ] )->getUser()
125 );
126 $this->assertStringContainsString( 'mw-pager-body', $html );
127 }
128
129 public function testExecuteInvalidNamespace() {
130 [ $html ] = $this->executeSpecialPage(
131 '::1',
132 new FauxRequest( [
133 'namespace' => -1,
134 ] )
135 );
136 $this->assertStringNotContainsString( 'mw-pager-body', $html );
137 }
138
139 /** @dataProvider provideExecuteNoResultsForIPTarget */
140 public function testExecuteNoResultsForIPTarget( $temporaryAccountsEnabled, $expectedPageTitleMessageKey ) {
141 if ( $temporaryAccountsEnabled ) {
142 $this->enableAutoCreateTempUser();
143 } else {
144 $this->disableAutoCreateTempUser();
145 }
146 [ $html ] = $this->executeSpecialPage( '4.3.2.1', null, null, null, true );
147 $specialPageDocument = DOMUtils::parseHTML( $html );
148 $contentHtml = DOMCompat::getInnerHTML(
149 DOMCompat::querySelector( $specialPageDocument, '#content' )
150 );
151 $pagerContents = trim( DOMCompat::querySelector(
152 $specialPageDocument, '#content .mw-pager-body'
153 )->textContent );
154 $this->assertEquals( '(nocontribs)', $pagerContents );
155 $this->assertStringContainsString( "($expectedPageTitleMessageKey: <bdi>4.3.2.1</bdi>", $contentHtml );
156 }
157
158 public static function provideExecuteNoResultsForIPTarget() {
159 return [
160 'Temporary accounts not enabled' => [ false, 'contributions-title' ],
161 'Temporary accounts enabled' => [
162 true, 'contributions-title-for-ip-when-temporary-accounts-enabled',
163 ],
164 ];
165 }
166
167 public function testExecuteForUseModWikiIP() {
168 // Regression test for T370413
169 [ $html ] = $this->executeSpecialPage( '1.2.3.xxx' );
170 $contributionsList = DOMCompat::querySelectorAll( DOMUtils::parseHTML( $html ), '.mw-contributions-list' );
171 $this->assertCount(
172 1, $contributionsList, 'Should have the contributions list as 1.2.3.xxx has made one edit'
173 );
174 $matchingLines = DOMCompat::querySelectorAll(
175 $contributionsList[0], '[data-mw-revid="' . static::$useModWikiIPRevId . '"]'
176 );
177 $this->assertCount( 1, $matchingLines, "The edit made by the usemod IP is missing" );
178 }
179
180 /**
181 * @dataProvider provideTestExecuteRange
182 */
183 public function testExecuteRange( $username, $shouldShowLinks ) {
184 [ $html ] = $this->executeSpecialPage( $username, null, 'qqx', self::$admin, true );
185
186 if ( $shouldShowLinks ) {
187 $this->assertStringContainsString( 'blocklink', $html );
188 } else {
189 $this->assertStringNotContainsString( 'blocklink', $html );
190 $this->assertStringContainsString( 'sp-contributions-outofrange', $html );
191 }
192 }
193
194 /**
195 * @dataProvider provideTestExecuteNonRange
196 */
197 public function testExecuteNonRange( $username, $shouldShowLinks ) {
198 [ $html ] = $this->executeSpecialPage( $username, null, 'qqx', self::$admin, true );
199
200 if ( $shouldShowLinks ) {
201 $this->assertStringContainsString( 'blocklink', $html );
202 } else {
203 $this->assertStringNotContainsString( 'blocklink', $html );
204 }
205 }
206
207 public static function provideTestExecuteRange() {
208 yield 'Queryable IPv4 range should have blocklink for admin'
209 => [ '24.237.208.166/30', true ];
210 yield 'Queryable IPv6 range should have blocklink for admin'
211 => [ '2001:DB8:0:0:0:0:0:01/43', true ];
212 yield 'Unqueryable IPv4 range should not have blocklink for admin'
213 => [ '212.35.31.121/14', false ];
214 yield 'Unqueryable IPv6 range should not have blocklink for admin'
215 => [ '2000::/24', false ];
216 }
217
218 public static function provideTestExecuteNonRange() {
219 yield 'Valid IPv4 should have blocklink for admin' => [ '124.24.52.13', true ];
220 yield 'Valid IPv6 should have blocklink for admin' => [ '2001:db8::', true ];
221 yield 'Local user should have blocklink for admin' => [ 'UTSysop', true ];
222 yield 'Invalid IP should not have blocklink for admin' => [ '24.237.222208.166', false ];
223 yield 'External user should not have blocklink for admin' => [ 'imported>UTSysop', false ];
224 yield 'Nonexistent user should not have blocklink for admin' => [ __CLASS__, false ];
225 }
226
227 public static function provideYearMonthParams() {
228 yield 'Current year/month' => [
229 'year' => date( 'Y' ),
230 'month' => date( 'm' ),
231 'expect' => true,
232 ];
233 yield 'Old year/moth' => [
234 'year' => '2007',
235 'month' => '01',
236 'expect' => false,
237 ];
238 yield 'Garbage' => [
239 'year' => '123garbage123',
240 'month' => date( 'm' ),
241 'expect' => true,
242 ];
243 }
244
245 /**
246 * @dataProvider provideYearMonthParams
247 */
248 public function testYearMonthParams( string $year, string $month, bool $expect ) {
249 [ $html ] = $this->executeSpecialPage(
250 self::$admin->getUser()->getName(),
251 new FauxRequest( [
252 'year' => $year,
253 'month' => $month,
254 ] ) );
255 if ( $expect ) {
256 $this->assertStringContainsString( self::PAGE_NAME, $html );
257 } else {
258 $this->assertStringNotContainsString( self::PAGE_NAME, $html );
259 }
260 }
261
262 public function testBotParam() {
263 [ $html ] = $this->executeSpecialPage(
264 '::1',
265 new FauxRequest( [
266 'bot' => 1,
267 ] ),
268 null,
269 self::$admin
270 );
271 $this->assertStringContainsString( 'bot', $html );
272 }
273
274 public function testFeedFormat() {
275 $specialPage = $this->newSpecialPage();
276 [ $html ] = ( new SpecialPageExecutor() )->executeSpecialPage(
277 $specialPage,
278 '::1',
279 new FauxRequest( [
280 'feed' => 'atom',
281 'namespace' => 2,
282 'topOnly' => true,
283 'newOnly' => true,
284 'hideMinor' => true,
285 'deletedOnly' => true,
286 'tagfilter' => 'mw-reverted',
287 'year' => '2000',
288 'month' => '01',
289 ] )
290 );
291 $url = $specialPage->getOutput()->getRedirect();
292 $this->assertStringContainsString( 'namespace', $url );
293 $this->assertStringContainsString( 'toponly', $url );
294 $this->assertStringContainsString( 'newonly', $url );
295 $this->assertStringContainsString( 'hideminor', $url );
296 $this->assertStringContainsString( 'deletedonly', $url );
297 $this->assertStringContainsString( 'tagfilter', $url );
298 $this->assertStringContainsString( 'year', $url );
299 $this->assertStringContainsString( 'month', $url );
300 }
301
302 /**
303 * @dataProvider providePrefixSearchSubpages
304 */
305 public function testPrefixSearchSubpages( $search, $expected ) {
306 $specialPage = $this->newSpecialPage();
307 $this->assertCount(
308 $expected,
309 $specialPage->prefixSearchSubpages( $search, 10, 0 )
310 );
311 }
312
313 public static function providePrefixSearchSubpages() {
314 return [
315 'Invalid prefix' => [ '/', 0 ],
316 'Valid prefix' => [ 'U', 1 ],
317 ];
318 }
319
320 protected function newSpecialPage(): SpecialContributions {
321 $services = $this->getServiceContainer();
322
323 return new SpecialContributions(
324 $services->getLinkBatchFactory(),
325 $services->getPermissionManager(),
326 $services->getConnectionProvider(),
327 $services->getRevisionStore(),
328 $services->getNamespaceInfo(),
329 $services->getUserNameUtils(),
330 $services->getUserNamePrefixSearch(),
331 $services->getUserOptionsLookup(),
332 $services->getCommentFormatter(),
333 $services->getUserFactory(),
334 $services->getUserIdentityLookup(),
335 $services->getDatabaseBlockStore(),
336 $services->getTempUserConfig()
337 );
338 }
339
340 }