Debian Codegraph @

tests/phpunit/includes/recentchanges/RecentChangeTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\MainConfigNames;
4 use MediaWiki\Page\PageIdentity;
5 use MediaWiki\Page\PageIdentityValue;
6 use MediaWiki\Page\PageProps;
7 use MediaWiki\Page\PageReference;
8 use MediaWiki\Page\PageReferenceValue;
9 use MediaWiki\Permissions\PermissionStatus;
10 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
11 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
12 use MediaWiki\Title\Title;
13 use MediaWiki\User\UserIdentity;
14 use MediaWiki\User\UserIdentityValue;
15 use MediaWiki\Utils\MWTimestamp;
16
17 /**
18 * @group Database
19 */
20 class RecentChangeTest extends MediaWikiIntegrationTestCase {
21 use MockAuthorityTrait;
22 use MockTitleTrait;
23 use TempUserTestTrait;
24
25 /** @var PageIdentity */
26 protected $title;
27 /** @var PageIdentity */
28 protected $target;
29 /** @var UserIdentity */
30 protected $user;
31 private const USER_COMMENT = '<User comment about action>';
32
33 protected function setUp(): void {
34 parent::setUp();
35
36 $this->title = new PageIdentityValue( 17, NS_MAIN, 'SomeTitle', PageIdentity::LOCAL );
37 $this->target = new PageIdentityValue( 78, NS_MAIN, 'TestTarget', PageIdentity::LOCAL );
38
39 $user = $this->getTestUser()->getUser();
40 $this->user = new UserIdentityValue( $user->getId(), $user->getName() );
41
42 $this->overrideConfigValues( [
43 MainConfigNames::CanonicalServer => 'https://example.org',
44 MainConfigNames::ServerName => 'example.org',
45 MainConfigNames::ScriptPath => '/w',
46 MainConfigNames::Script => '/w/index.php',
47 MainConfigNames::UseRCPatrol => false,
48 MainConfigNames::UseNPPatrol => false,
49 MainConfigNames::RCFeeds => [],
50 MainConfigNames::RCEngines => [],
51 ] );
52 }
53
54 public static function provideAttribs() {
55 $attribs = [
56 'rc_timestamp' => wfTimestamp( TS_MW ),
57 'rc_namespace' => NS_USER,
58 'rc_title' => 'Tony',
59 'rc_type' => RC_EDIT,
60 'rc_source' => RecentChange::SRC_EDIT,
61 'rc_minor' => 0,
62 'rc_cur_id' => 77,
63 'rc_user' => 858173476,
64 'rc_user_text' => 'Tony',
65 'rc_comment' => '',
66 'rc_comment_text' => '',
67 'rc_comment_data' => null,
68 'rc_this_oldid' => 70,
69 'rc_last_oldid' => 71,
70 'rc_bot' => 0,
71 'rc_ip' => '',
72 'rc_patrolled' => 0,
73 'rc_new' => 0,
74 'rc_old_len' => 80,
75 'rc_new_len' => 88,
76 'rc_deleted' => 0,
77 'rc_logid' => 0,
78 'rc_log_type' => null,
79 'rc_log_action' => '',
80 'rc_params' => '',
81 ];
82
83 yield 'external user' => [
84 [
85 'rc_type' => RC_EXTERNAL,
86 'rc_source' => 'foo',
87 'rc_user' => 0,
88 'rc_user_text' => 'm>External User',
89 ] + $attribs
90 ];
91
92 yield 'anon user' => [
93 [
94 'rc_type' => RC_EXTERNAL,
95 'rc_source' => 'foo',
96 'rc_user' => 0,
97 'rc_user_text' => '192.168.0.1',
98 ] + $attribs
99 ];
100
101 yield 'special title' => [
102 [
103 'rc_namespace' => NS_SPECIAL,
104 'rc_title' => 'Log',
105 'rc_type' => RC_LOG,
106 'rc_source' => RecentChange::SRC_LOG,
107 'rc_log_type' => 'delete',
108 'rc_log_action' => 'delete',
109 ] + $attribs
110 ];
111
112 yield 'no title' => [
113 [
114 'rc_namespace' => NS_MAIN,
115 'rc_title' => '',
116 'rc_type' => RC_LOG,
117 'rc_source' => RecentChange::SRC_LOG,
118 'rc_log_type' => 'delete',
119 'rc_log_action' => 'delete',
120 ] + $attribs
121 ];
122 }
123
124 /**
125 * @covers \RecentChange::save
126 * @covers \RecentChange::newFromId
127 * @covers \RecentChange::getTitle
128 * @covers \RecentChange::getPerformerIdentity
129 * @dataProvider provideAttribs
130 */
131 public function testDatabaseRoundTrip( $attribs ) {
132 $rc_user = $attribs['rc_user'] ?? 0;
133 if ( !$rc_user ) {
134 $this->disableAutoCreateTempUser();
135 }
136 $rc = new RecentChange;
137 $rc->mAttribs = $attribs;
138 $rc->mExtra = [
139 'pageStatus' => 'changed'
140 ];
141 $rc->save();
142 $id = $rc->getAttribute( 'rc_id' );
143
144 $rc = RecentChange::newFromId( $id );
145
146 $actualAttribs = array_intersect_key( $rc->mAttribs, $attribs );
147 $this->assertArrayEquals( $attribs, $actualAttribs, false, true );
148
149 $user = new UserIdentityValue( $rc_user, $attribs['rc_user_text'] );
150 $this->assertTrue( $user->equals( $rc->getPerformerIdentity() ) );
151
152 if ( empty( $attribs['rc_title'] ) ) {
153 $this->assertNull( $rc->getPage() );
154 } else {
155 $title = Title::makeTitle( $attribs['rc_namespace'], $attribs['rc_title'] );
156 $this->assertTrue( $title->isSamePageAs( $rc->getTitle() ) );
157 $this->assertTrue( $title->isSamePageAs( $rc->getPage() ) );
158 }
159 }
160
161 /**
162 * @covers \RecentChange::newFromRow
163 * @covers \RecentChange::loadFromRow
164 * @covers \RecentChange::getAttributes
165 * @covers \RecentChange::getPerformerIdentity
166 */
167 public function testNewFromRow() {
168 $user = $this->getTestUser()->getUser();
169
170 $row = (object)[
171 'rc_foo' => 'AAA',
172 'rc_timestamp' => '20150921134808',
173 'rc_deleted' => 'bar',
174 'rc_comment_text' => 'comment',
175 'rc_comment_data' => null,
176 'rc_user' => $user->getId(), // lookup by id
177 ];
178
179 $rc = RecentChange::newFromRow( $row );
180
181 $expected = [
182 'rc_foo' => 'AAA',
183 'rc_timestamp' => '20150921134808',
184 'rc_deleted' => 'bar',
185 'rc_comment' => 'comment',
186 'rc_comment_text' => 'comment',
187 'rc_comment_data' => null,
188 'rc_user' => $user->getId(),
189 'rc_user_text' => $user->getName()
190 ];
191 $this->assertEquals( $expected, $rc->getAttributes() );
192 $this->assertTrue( $user->equals( $rc->getPerformerIdentity() ) );
193
194 $row = (object)[
195 'rc_foo' => 'AAA',
196 'rc_timestamp' => '20150921134808',
197 'rc_deleted' => 'bar',
198 'rc_comment' => 'comment',
199 'rc_user_text' => $user->getName(), // lookup by name
200 ];
201 $rc = @RecentChange::newFromRow( $row );
202
203 $expected = [
204 'rc_foo' => 'AAA',
205 'rc_timestamp' => '20150921134808',
206 'rc_deleted' => 'bar',
207 'rc_comment' => 'comment',
208 'rc_comment_text' => 'comment',
209 'rc_comment_data' => null,
210 'rc_user' => $user->getId(),
211 'rc_user_text' => $user->getName()
212 ];
213 $this->assertEquals( $expected, $rc->getAttributes() );
214 $this->assertEquals( $expected, $rc->getAttributes() );
215 $this->assertTrue( $user->equals( $rc->getPerformerIdentity() ) );
216 }
217
218 /**
219 * @covers \RecentChange::notifyNew
220 * @covers \RecentChange::newFromId
221 * @covers \RecentChange::getAttributes
222 * @covers \RecentChange::getPerformerIdentity
223 */
224 public function testNotifyNew() {
225 $now = MWTimestamp::now();
226 $rc = RecentChange::notifyNew(
227 $now,
228 $this->title,
229 false,
230 $this->user,
231 self::USER_COMMENT,
232 false
233 );
234
235 $expected = [
236 'rc_timestamp' => $now,
237 'rc_deleted' => 0,
238 'rc_comment_text' => self::USER_COMMENT,
239 'rc_user' => $this->user->getId(),
240 'rc_user_text' => $this->user->getName()
241 ];
242
243 $actual = array_intersect_key( $rc->getAttributes(), $expected );
244
245 $this->assertEquals( $expected, $actual );
246 $this->assertTrue( $this->user->equals( $rc->getPerformerIdentity() ) );
247
248 $rc = RecentChange::newFromId( $rc->getAttribute( 'rc_id' ) );
249
250 $actual = array_intersect_key( $rc->getAttributes(), $expected );
251
252 $this->assertEquals( $expected, $actual );
253 $this->assertTrue( $this->user->equals( $rc->getPerformerIdentity() ) );
254 }
255
256 /**
257 * @covers \RecentChange::notifyNew
258 * @covers \RecentChange::newFromId
259 * @covers \RecentChange::getAttributes
260 * @covers \RecentChange::getPerformerIdentity
261 */
262 public function testNotifyEdit() {
263 $now = MWTimestamp::now();
264 $rc = RecentChange::notifyEdit(
265 $now,
266 $this->title,
267 false,
268 $this->user,
269 self::USER_COMMENT,
270 0,
271 $now,
272 false
273 );
274
275 $expected = [
276 'rc_timestamp' => $now,
277 'rc_deleted' => 0,
278 'rc_comment_text' => self::USER_COMMENT,
279 'rc_user' => $this->user->getId(),
280 'rc_user_text' => $this->user->getName()
281 ];
282
283 $actual = array_intersect_key( $rc->getAttributes(), $expected );
284
285 $this->assertEquals( $expected, $actual );
286 $this->assertTrue( $this->user->equals( $rc->getPerformerIdentity() ) );
287
288 $rc = RecentChange::newFromId( $rc->getAttribute( 'rc_id' ) );
289
290 $actual = array_intersect_key( $rc->getAttributes(), $expected );
291
292 $this->assertEquals( $expected, $actual );
293 $this->assertTrue( $this->user->equals( $rc->getPerformerIdentity() ) );
294 }
295
296 /**
297 * @covers \RecentChange::notifyNew
298 * @covers \RecentChange::newFromId
299 * @covers \RecentChange::getAttributes
300 * @covers \RecentChange::getPerformerIdentity
301 */
302 public function testNewLogEntry() {
303 $now = MWTimestamp::now();
304 $logPage = new PageReferenceValue( NS_SPECIAL, 'Log/test', PageReference::LOCAL );
305
306 $rc = RecentChange::newLogEntry(
307 $now,
308 $logPage,
309 $this->user,
310 'action comment',
311 '192.168.0.2',
312 'test',
313 'testing',
314 $this->title,
315 self::USER_COMMENT,
316 'a|b|c',
317 7,
318 '',
319 42,
320 false,
321 true
322 );
323
324 $expected = [
325 'rc_timestamp' => $now,
326 'rc_comment_text' => self::USER_COMMENT,
327 'rc_user' => $this->user->getId(),
328 'rc_user_text' => $this->user->getName(),
329 'rc_title' => $this->title->getDBkey(),
330 'rc_logid' => 7,
331 'rc_log_type' => 'test',
332 'rc_log_action' => 'testing',
333 'rc_this_oldid' => 42,
334 'rc_patrolled' => RecentChange::PRC_AUTOPATROLLED,
335 'rc_bot' => 1,
336 ];
337
338 $actual = array_intersect_key( $rc->getAttributes(), $expected );
339
340 $this->assertEquals( $expected, $actual );
341 $this->assertTrue( $this->user->equals( $rc->getPerformerIdentity() ) );
342 $this->assertTrue( $this->title->isSamePageAs( $rc->getPage() ) );
343 $this->assertTrue( $this->title->isSamePageAs( $rc->getTitle() ) );
344 }
345
346 public static function provideParseParams() {
347 // $expected, $raw
348 yield 'extracting an array' => [
349 [
350 'root' => [
351 'A' => 1,
352 'B' => 'two'
353 ]
354 ],
355 'a:1:{s:4:"root";a:2:{s:1:"A";i:1;s:1:"B";s:3:"two";}}'
356 ];
357
358 yield 'null' => [ null, null ];
359 yield 'false' => [ null, serialize( false ) ];
360 yield 'non-array' => [ null, 'not-an-array' ];
361 }
362
363 /**
364 * @covers \RecentChange::parseParams
365 * @dataProvider provideParseParams
366 * @param array $expectedParseParams
367 * @param string|null $rawRcParams
368 */
369 public function testParseParams( $expectedParseParams, $rawRcParams ) {
370 $rc = new RecentChange;
371 $rc->setAttribs( [ 'rc_params' => $rawRcParams ] );
372
373 $actualParseParams = $rc->parseParams();
374
375 $this->assertEquals( $expectedParseParams, $actualParseParams );
376 }
377
378 /**
379 * @covers \RecentChange::getNotifyUrl
380 */
381 public function testGetNotifyUrlForEdit() {
382 $rc = new RecentChange;
383 $rc->mAttribs = [
384 'rc_id' => 60,
385 'rc_timestamp' => '20110401090000',
386 'rc_namespace' => NS_MAIN,
387 'rc_title' => 'Example',
388 'rc_type' => RC_EDIT,
389 'rc_cur_id' => 42,
390 'rc_this_oldid' => 50,
391 'rc_last_oldid' => 30,
392 'rc_patrolled' => 0,
393 ];
394 $this->assertSame(
395 'https://example.org/w/index.php?diff=50&oldid=30',
396 $rc->getNotifyUrl(), 'Notify url'
397 );
398
399 $this->overrideConfigValue( MainConfigNames::UseRCPatrol, true );
400 $this->assertSame(
401 'https://example.org/w/index.php?diff=50&oldid=30&rcid=60',
402 $rc->getNotifyUrl(), 'Notify url (RC Patrol)'
403 );
404 }
405
406 /**
407 * @covers \RecentChange::getNotifyUrl
408 */
409 public function testGetNotifyUrlForCreate() {
410 $rc = new RecentChange;
411 $rc->mAttribs = [
412 'rc_id' => 60,
413 'rc_timestamp' => '20110401090000',
414 'rc_namespace' => NS_MAIN,
415 'rc_title' => 'Example',
416 'rc_type' => RC_NEW,
417 'rc_cur_id' => 42,
418 'rc_this_oldid' => 50,
419 'rc_last_oldid' => 0,
420 'rc_patrolled' => 0,
421 ];
422 $this->assertSame(
423 'https://example.org/w/index.php?oldid=50',
424 $rc->getNotifyUrl(), 'Notify url'
425 );
426
427 $this->overrideConfigValue( MainConfigNames::UseNPPatrol, true );
428 $this->assertSame(
429 'https://example.org/w/index.php?oldid=50&rcid=60',
430 $rc->getNotifyUrl(), 'Notify url (NP Patrol)'
431 );
432 }
433
434 /**
435 * @covers \RecentChange::getNotifyUrl
436 */
437 public function testGetNotifyUrlForLog() {
438 $rc = new RecentChange;
439 $rc->mAttribs = [
440 'rc_id' => 60,
441 'rc_timestamp' => '20110401090000',
442 'rc_namespace' => NS_MAIN,
443 'rc_title' => 'Example',
444 'rc_type' => RC_LOG,
445 'rc_cur_id' => 42,
446 'rc_this_oldid' => 50,
447 'rc_last_oldid' => 0,
448 'rc_patrolled' => 2,
449 'rc_logid' => 160,
450 'rc_log_type' => 'delete',
451 'rc_log_action' => 'delete',
452 ];
453 $this->assertSame( null, $rc->getNotifyUrl(), 'Notify url' );
454 }
455
456 /**
457 * @return array
458 */
459 public static function provideIsInRCLifespan() {
460 return [
461 [ 6000, -3000, 0, true ],
462 [ 3000, -6000, 0, false ],
463 [ 6000, -3000, 6000, true ],
464 [ 3000, -6000, 6000, true ],
465 ];
466 }
467
468 /**
469 * @covers \RecentChange::isInRCLifespan
470 * @dataProvider provideIsInRCLifespan
471 */
472 public function testIsInRCLifespan( $maxAge, $offset, $tolerance, $expected ) {
473 $this->overrideConfigValue( MainConfigNames::RCMaxAge, $maxAge );
474 // Calculate this here instead of the data provider because the provider
475 // is expanded early on and the full test suite may take longer than 100 minutes
476 // when coverage is enabled.
477 $timestamp = time() + $offset;
478 $this->assertEquals( $expected, RecentChange::isInRCLifespan( $timestamp, $tolerance ) );
479 }
480
481 public static function provideRCTypes() {
482 return [
483 [ RC_EDIT, 'edit' ],
484 [ RC_NEW, 'new' ],
485 [ RC_LOG, 'log' ],
486 [ RC_EXTERNAL, 'external' ],
487 [ RC_CATEGORIZE, 'categorize' ],
488 ];
489 }
490
491 /**
492 * @dataProvider provideRCTypes
493 * @covers \RecentChange::parseFromRCType
494 */
495 public function testParseFromRCType( $rcType, $type ) {
496 $this->assertEquals( $type, RecentChange::parseFromRCType( $rcType ) );
497 }
498
499 /**
500 * @dataProvider provideRCTypes
501 * @covers \RecentChange::parseToRCType
502 */
503 public function testParseToRCType( $rcType, $type ) {
504 $this->assertEquals( $rcType, RecentChange::parseToRCType( $type ) );
505 }
506
507 public static function provideCategoryContent() {
508 return [
509 [ true ],
510 [ false ],
511 ];
512 }
513
514 /**
515 * @dataProvider provideCategoryContent
516 * @covers \RecentChange::newForCategorization
517 */
518 public function testHiddenCategoryChange( $isHidden ) {
519 $categoryTitle = Title::makeTitle( NS_CATEGORY, 'CategoryPage' );
520
521 $pageProps = $this->createMock( PageProps::class );
522 $pageProps->expects( $this->once() )
523 ->method( 'getProperties' )
524 ->with( $categoryTitle, 'hiddencat' )
525 ->willReturn( $isHidden ? [ $categoryTitle->getArticleID() => '' ] : [] );
526
527 $this->setService( 'PageProps', $pageProps );
528
529 $rc = RecentChange::newForCategorization(
530 '0',
531 $categoryTitle,
532 $this->user,
533 self::USER_COMMENT,
534 $this->title,
535 $categoryTitle->getLatestRevID(),
536 $categoryTitle->getLatestRevID(),
537 '0',
538 false
539 );
540
541 $this->assertEquals( $isHidden, $rc->getParam( 'hidden-cat' ) );
542 }
543
544 private function getDummyEditRecentChange(): RecentChange {
545 return RecentChange::notifyEdit(
546 MWTimestamp::now(),
547 $this->title,
548 false,
549 $this->user,
550 self::USER_COMMENT,
551 0,
552 MWTimestamp::now(),
553 false
554 );
555 }
556
557 /**
558 * @covers \RecentChange::markPatrolled
559 */
560 public function testMarkPatrolledPermissions() {
561 $rc = $this->getDummyEditRecentChange();
562 $performer = $this->mockRegisteredAuthority( static function (
563 string $permission,
564 PageIdentity $page,
565 PermissionStatus $status
566 ) {
567 if ( $permission === 'patrol' ) {
568 $status->fatal( 'missing-patrol' );
569 return false;
570 }
571 return true;
572 } );
573 $status = $rc->markPatrolled(
574 $performer
575 );
576 $this->assertStatusError( 'missing-patrol', $status );
577 }
578
579 /**
580 * @covers \RecentChange::markPatrolled
581 */
582 public function testMarkPatrolledPermissions_Hook() {
583 $rc = $this->getDummyEditRecentChange();
584 $this->setTemporaryHook( 'MarkPatrolled', static function () {
585 return false;
586 } );
587 $status = $rc->markPatrolled( $this->mockRegisteredUltimateAuthority() );
588 $this->assertStatusError( 'hookaborted', $status );
589 }
590
591 /**
592 * @covers \RecentChange::markPatrolled
593 */
594 public function testMarkPatrolledPermissions_Self() {
595 $rc = $this->getDummyEditRecentChange();
596 $status = $rc->markPatrolled(
597 $this->mockUserAuthorityWithoutPermissions( $this->user, [ 'autopatrol' ] )
598 );
599 $this->assertStatusError( 'markedaspatrollederror-noautopatrol', $status );
600 }
601
602 /**
603 * @covers \RecentChange::markPatrolled
604 */
605 public function testMarkPatrolledPermissions_NoRcPatrol() {
606 $rc = $this->getDummyEditRecentChange();
607 $status = $rc->markPatrolled( $this->mockRegisteredUltimateAuthority() );
608 $this->assertStatusError( 'rcpatroldisabled', $status );
609 }
610
611 /**
612 * @covers \RecentChange::markPatrolled
613 */
614 public function testMarkPatrolled() {
615 $this->overrideConfigValue( MainConfigNames::UseRCPatrol, true );
616 $rc = $this->getDummyEditRecentChange();
617 $status = $rc->markPatrolled(
618 $this->mockUserAuthorityWithPermissions( $this->user, [ 'patrol', 'autopatrol' ] )
619 );
620 $this->assertStatusGood( $status );
621
622 $reloadedRC = RecentChange::newFromId( $rc->getAttribute( 'rc_id' ) );
623 $this->assertSame( '1', $reloadedRC->getAttribute( 'rc_patrolled' ) );
624 }
625 }