mediawiki-extensions-Echo/tests/phpunit/NotificationsTest.php
Reedy 5611662f06 Namespace Model
Depends-On: Id28792658de950b99a8786f881563476def59eba
Change-Id: Ib57ea2db947285946f31fa9912b37181044df9d3
2022-11-10 14:25:42 -07:00

59 lines
1.6 KiB
PHP

<?php
use MediaWiki\Extension\Notifications\Mapper\NotificationMapper;
use MediaWiki\Extension\Notifications\Model\Event;
/**
* 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 Event
*/
public static function getLatestNotification( $user ) {
$notifMapper = new NotificationMapper();
$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'] );
}
}