Debian Codegraph @

extensions/Echo/tests/phpunit/NotificationsTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Test;
4
5 use MediaWiki\Context\DerivativeContext;
6 use MediaWiki\Context\RequestContext;
7 use MediaWiki\Extension\Notifications\Mapper\NotificationMapper;
8 use MediaWiki\Extension\Notifications\Model\Event;
9 use MediaWiki\User\User;
10 use MediaWiki\User\UserIdentity;
11 use MediaWikiIntegrationTestCase;
12
13 /**
14 * Tests for the built in notification types
15 *
16 * @group Database
17 */
18 class NotificationsTest extends MediaWikiIntegrationTestCase {
19
20 /** @var User */
21 private $sysop;
22
23 protected function setUp(): void {
24 parent::setUp();
25 $this->sysop = $this->getTestSysop()->getUser();
26 }
27
28 /**
29 * Helper function to get a user's latest notification
30 * @param UserIdentity $user
31 * @return Event
32 */
33 public static function getLatestNotification( $user ) {
34 $notifMapper = new NotificationMapper();
35 $notifs = $notifMapper->fetchUnreadByUser( $user, 1, '', [ 'user-rights' ] );
36 $notif = array_pop( $notifs );
37
38 return $notif->getEvent();
39 }
40
41 /**
42 * @covers \MediaWiki\Extension\Notifications\Hooks::onUserGroupsChanged
43 */
44 public function testUserRightsNotif() {
45 $user = new User();
46 $user->setName( 'Dummy' );
47 $user->addToDatabase();
48
49 $context = new DerivativeContext( RequestContext::getMain() );
50 $context->setUser( $this->sysop );
51 $ur = $this->getServiceContainer()
52 ->getSpecialPageFactory()
53 ->getPage( 'Userrights' );
54 $ur->setContext( $context );
55 $ur->doSaveUserGroups( $user, [ 'sysop' ], [], 'reason' );
56 $event = self::getLatestNotification( $user );
57 $this->assertEquals( 'user-rights', $event->getType() );
58 $this->assertEquals( $this->sysop->getName(), $event->getAgent()->getName() );
59 $extra = $event->getExtra();
60 $this->assertArrayHasKey( 'add', $extra );
61 $this->assertArrayEquals( [ 'sysop' ], $extra['add'] );
62 $this->assertArrayEquals( [], $extra['remove'] );
63 }
64
65 }