aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/recentchanges/ChangeTrackingUpdateSpyTrait.php
blob: dd31a56f75d52206b8ad54775660cc6a47ac3aee (plain) (blame)
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
<?php

namespace MediaWiki\Tests\recentchanges;

use MediaWiki\JobQueue\JobQueueGroup;
use MediaWiki\MainConfigNames;
use MediaWiki\RecentChanges\RecentChange;
use MediaWiki\User\TalkPageNotificationManager;
use MediaWiki\User\UserEditTracker;
use StatusValue;

/**
 * Trait for asserting that the change tracking component is getting notified
 * about changes as expected.
 */
trait ChangeTrackingUpdateSpyTrait {

	/**
	 * Register expectations about updates that should get triggered.
	 * The parameters of this method represent known kinds of updates.
	 * If a parameter is added, tests calling this method should be forced
	 * to specify their expectations with respect to that kind of update.
	 * For this reason, this method should not be split, and all parameters
	 * should be required.
	 */
	private function expectChangeTrackingUpdates(
		int $rcEdit,
		int $rcOther,
		int $userEditCount,
		int $talkPageNotifications,
		int $categoryMembershipChangeJobs
	) {
		// Force enable RC entry creation for category changes
		// to verify CategoryMembershipChangeJobs get enqueued irrespective of local configuration.
		$this->overrideConfigValue( MainConfigNames::RCWatchCategoryMembership, true );

		// Hack for spying on RC insertions
		$rcEditStatus = $this->createMock( StatusValue::class );
		$rcEditStatus->expects( $this->exactly( $rcEdit ) )
			->method( 'setOK' );

		$rcOtherStatus = $this->createMock( StatusValue::class );
		$rcOtherStatus->expects( $this->exactly( $rcOther ) )
			->method( 'setOK' );

		$this->setTemporaryHook(
			'RecentChange_save',
			static function ( RecentChange $rc ) use ( $rcEditStatus, $rcOtherStatus ) {
				// Only count types recorded by ChangeTrackingEventIngress
				$type = (int)$rc->getAttributes()['rc_type'];
				if ( $type === RC_EDIT || $type === RC_NEW ) {
					$rcEditStatus->setOK( true );
				} else {
					$rcOtherStatus->setOK( true );
				}
			}
		);

		// Spy for UserEditTracker
		$userEditTracker = $this->createNoOpMock(
			UserEditTracker::class,
			[ 'incrementUserEditCount', 'setCachedUserEditCount', 'clearUserEditCache' ]
		);

		$userEditTracker->expects( $this->exactly( $userEditCount ) )
			->method( 'incrementUserEditCount' );

		$this->setService( 'UserEditTracker', $userEditTracker );

		// Spy for TalkPageNotificationManager
		$talkPageNotificationManager = $this->createNoOpMock(
			TalkPageNotificationManager::class,
			[ 'setUserHasNewMessages', 'removeUserHasNewMessages', 'clearInstanceCache' ]
		);

		$talkPageNotificationManager
			->expects( $this->exactly( max( +$talkPageNotifications, 0 ) ) )
			->method( 'setUserHasNewMessages' );

		$talkPageNotificationManager
			->expects( $this->exactly( max( -$talkPageNotifications, 0 ) ) )
			->method( 'removeUserHasNewMessages' );

		$this->setService( 'TalkPageNotificationManager', $talkPageNotificationManager );

		$categoryMembershipChangeJobStatus = $this->createMock( StatusValue::class );
		$categoryMembershipChangeJobStatus->expects( $this->exactly( $categoryMembershipChangeJobs ) )
			->method( 'setOK' );

		$jobQueueGroup = $this->createMock( JobQueueGroup::class );
		$jobQueueGroup->method( $this->logicalOr( 'push', 'lazyPush' ) )
			->willReturnCallback(
				static function ( $specs ) use ( $categoryMembershipChangeJobStatus ): void {
					$specs = is_array( $specs ) ? $specs : [ $specs ];
					foreach ( $specs as $spec ) {
						if ( $spec->getType() === 'categoryMembershipChange' ) {
							$categoryMembershipChangeJobStatus->setOK( true );
						}
					}
				}
			);

		$this->setService( 'JobQueueGroup', $jobQueueGroup );
	}

}