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

namespace MediaWiki\Tests\recentchanges;

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
	) {
		// 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 );
	}

}