| 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 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class NotificationsTest extends MediaWikiIntegrationTestCase { |
| 19 |
|
| 20 |
|
| 21 |
private $sysop; |
| 22 |
|
| 23 |
protected function setUp(): void { |
| 24 |
parent::setUp(); |
| 25 |
$this->sysop = $this->getTestSysop()->getUser(); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 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 |
|
| 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 |
} |