2013-10-28 02:43:55 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-22 15:31:00 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Test;
|
|
|
|
|
2024-06-12 18:31:47 +00:00
|
|
|
use MediaWiki\Context\DerivativeContext;
|
|
|
|
use MediaWiki\Context\RequestContext;
|
2022-11-02 20:47:04 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Mapper\NotificationMapper;
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\User;
|
2024-03-15 08:27:36 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2024-02-22 15:31:00 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
2022-11-02 20:47:04 +00:00
|
|
|
|
2013-10-28 02:43:55 +00:00
|
|
|
/**
|
|
|
|
* Tests for the built in notification types
|
|
|
|
*
|
|
|
|
* @group Database
|
|
|
|
*/
|
2021-10-11 22:42:10 +00:00
|
|
|
class NotificationsTest extends MediaWikiIntegrationTestCase {
|
2013-10-28 02:43:55 +00:00
|
|
|
|
2020-10-29 11:07:16 +00:00
|
|
|
/** @var User */
|
2021-01-29 12:55:41 +00:00
|
|
|
private $sysop;
|
2013-10-28 02:43:55 +00:00
|
|
|
|
2021-07-23 21:23:16 +00:00
|
|
|
protected function setUp(): void {
|
2013-10-28 02:43:55 +00:00
|
|
|
parent::setUp();
|
2021-01-29 12:54:21 +00:00
|
|
|
$this->sysop = $this->getTestSysop()->getUser();
|
2013-10-28 02:43:55 +00:00
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2013-10-28 02:43:55 +00:00
|
|
|
/**
|
|
|
|
* Helper function to get a user's latest notification
|
2024-03-15 08:27:36 +00:00
|
|
|
* @param UserIdentity $user
|
2022-11-02 21:34:17 +00:00
|
|
|
* @return Event
|
2013-10-28 02:43:55 +00:00
|
|
|
*/
|
|
|
|
public static function getLatestNotification( $user ) {
|
2022-11-02 20:47:04 +00:00
|
|
|
$notifMapper = new NotificationMapper();
|
2018-04-24 19:46:31 +00:00
|
|
|
$notifs = $notifMapper->fetchUnreadByUser( $user, 1, '', [ 'user-rights' ] );
|
|
|
|
$notif = array_pop( $notifs );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2018-04-24 19:46:31 +00:00
|
|
|
return $notif->getEvent();
|
2013-10-28 02:43:55 +00:00
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2013-10-28 02:43:55 +00:00
|
|
|
/**
|
2022-04-08 00:28:15 +00:00
|
|
|
* @covers \MediaWiki\Extension\Notifications\Hooks::onUserGroupsChanged
|
2013-10-28 02:43:55 +00:00
|
|
|
*/
|
|
|
|
public function testUserRightsNotif() {
|
|
|
|
$user = new User();
|
|
|
|
$user->setName( 'Dummy' );
|
|
|
|
$user->addToDatabase();
|
|
|
|
|
|
|
|
$context = new DerivativeContext( RequestContext::getMain() );
|
|
|
|
$context->setUser( $this->sysop );
|
2021-03-04 21:49:16 +00:00
|
|
|
$ur = $this->getServiceContainer()
|
|
|
|
->getSpecialPageFactory()
|
|
|
|
->getPage( 'Userrights' );
|
2013-10-28 02:43:55 +00:00
|
|
|
$ur->setContext( $context );
|
2016-12-05 18:51:07 +00:00
|
|
|
$ur->doSaveUserGroups( $user, [ 'sysop' ], [], 'reason' );
|
2013-10-28 02:43:55 +00:00
|
|
|
$event = self::getLatestNotification( $user );
|
2021-10-23 21:59:04 +00:00
|
|
|
$this->assertEquals( 'user-rights', $event->getType() );
|
2013-10-28 02:43:55 +00:00
|
|
|
$this->assertEquals( $this->sysop->getName(), $event->getAgent()->getName() );
|
|
|
|
$extra = $event->getExtra();
|
|
|
|
$this->assertArrayHasKey( 'add', $extra );
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->assertArrayEquals( [ 'sysop' ], $extra['add'] );
|
|
|
|
$this->assertArrayEquals( [], $extra['remove'] );
|
2013-10-28 02:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|