summaryrefslogtreecommitdiff
blob: 84cf8364ba63b18d7d6733350368c468fe0fd273 (plain)
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
<?php

/**
 * Tests for the built in notification types
 *
 * @group Database
 */
class NotificationsTest extends MediaWikiIntegrationTestCase {

	/** @var User */
	private $sysop;

	protected function setUp(): void {
		parent::setUp();
		$this->sysop = $this->getTestSysop()->getUser();
	}

	/**
	 * Helper function to get a user's latest notification
	 * @param User $user
	 * @return EchoEvent
	 */
	public static function getLatestNotification( $user ) {
		$notifMapper = new EchoNotificationMapper();
		$notifs = $notifMapper->fetchUnreadByUser( $user, 1, '', [ 'user-rights' ] );
		$notif = array_pop( $notifs );

		return $notif->getEvent();
	}

	/**
	 * @covers \MediaWiki\Extension\Notifications\Hooks::onUserGroupsChanged
	 */
	public function testUserRightsNotif() {
		$user = new User();
		$user->setName( 'Dummy' );
		$user->addToDatabase();

		$context = new DerivativeContext( RequestContext::getMain() );
		$context->setUser( $this->sysop );
		$ur = $this->getServiceContainer()
			->getSpecialPageFactory()
			->getPage( 'Userrights' );
		$ur->setContext( $context );
		$ur->doSaveUserGroups( $user, [ 'sysop' ], [], 'reason' );
		$event = self::getLatestNotification( $user );
		$this->assertEquals( 'user-rights', $event->getType() );
		$this->assertEquals( $this->sysop->getName(), $event->getAgent()->getName() );
		$extra = $event->getExtra();
		$this->assertArrayHasKey( 'add', $extra );
		$this->assertArrayEquals( [ 'sysop' ], $extra['add'] );
		$this->assertArrayEquals( [], $extra['remove'] );
	}

}