mediawiki-extensions-Echo/tests/phpunit/NotificationsTest.php
Umherirrender 43820a6390 Move NotificationsTest to tests/phpunit
Under this folder the test is run automatically on CI or
phpunit.php --testsuite extensions

Change-Id: I5e466c04fc5c827883882f798ba0e41d6990e423
2018-06-06 19:30:35 +00:00

57 lines
1.5 KiB
PHP

<?php
/**
* Tests for the built in notification types
*
* @group Database
*/
class NotificationsTest extends MediaWikiTestCase {
/** @var User $sysop */
// @codingStandardsIgnoreStart
var $sysop;
// @codingStandardsIgnoreEnd
protected function setUp() {
parent::setUp();
$this->sysop = User::newFromName( 'UTSysop' );
$this->setMwGlobals( 'wgUser', $this->sysop );
}
/**
* 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 EchoHooks::onUserGroupsChanged
*/
public function testUserRightsNotif() {
$user = new User();
$user->setName( 'Dummy' );
$user->addToDatabase();
$context = new DerivativeContext( RequestContext::getMain() );
$context->setUser( $this->sysop );
$ur = new UserrightsPage();
$ur->setContext( $context );
$ur->doSaveUserGroups( $user, [ 'sysop' ], [], 'reason' );
$event = self::getLatestNotification( $user );
$this->assertEquals( $event->getType(), 'user-rights' );
$this->assertEquals( $this->sysop->getName(), $event->getAgent()->getName() );
$extra = $event->getExtra();
$this->assertArrayHasKey( 'add', $extra );
$this->assertArrayEquals( [ 'sysop' ], $extra['add'] );
$this->assertArrayEquals( [], $extra['remove'] );
}
}