2013-05-24 22:51:47 +00:00
|
|
|
<?php
|
2018-08-13 07:29:32 +00:00
|
|
|
|
2022-11-13 06:43:40 +00:00
|
|
|
use MediaWiki\Extension\Notifications\EmailFormat;
|
2022-11-02 03:37:20 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Gateway\UserNotificationGateway;
|
2022-11-02 20:47:04 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Mapper\NotificationMapper;
|
|
|
|
use MediaWiki\Extension\Notifications\Mapper\TargetPageMapper;
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
|
|
|
use MediaWiki\Extension\Notifications\Model\Notification;
|
2022-11-13 07:48:43 +00:00
|
|
|
use MediaWiki\Extension\Notifications\NotifUser;
|
2023-11-29 12:38:49 +00:00
|
|
|
use MediaWiki\User\Options\UserOptionsLookup;
|
2023-08-04 00:31:24 +00:00
|
|
|
use MediaWiki\User\TalkPageNotificationManager;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\User;
|
2023-08-04 00:31:24 +00:00
|
|
|
use MediaWiki\User\UserFactory;
|
|
|
|
use MediaWiki\User\UserGroupManager;
|
2023-08-19 07:48:53 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2013-05-24 22:51:47 +00:00
|
|
|
|
2014-07-25 20:19:15 +00:00
|
|
|
/**
|
2022-11-13 07:48:43 +00:00
|
|
|
* @covers \MediaWiki\Extension\Notifications\NotifUser
|
2014-07-25 20:19:15 +00:00
|
|
|
* @group Echo
|
|
|
|
*/
|
2022-11-13 07:48:43 +00:00
|
|
|
class NotifUserTest extends MediaWikiIntegrationTestCase {
|
2013-05-24 22:51:47 +00:00
|
|
|
|
2015-11-01 09:59:16 +00:00
|
|
|
/**
|
2016-12-08 20:50:03 +00:00
|
|
|
* @var WANObjectCache
|
2015-11-01 09:59:16 +00:00
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
2021-07-23 21:23:16 +00:00
|
|
|
protected function setUp(): void {
|
2013-05-24 22:51:47 +00:00
|
|
|
parent::setUp();
|
2016-12-08 20:50:03 +00:00
|
|
|
$this->cache = new WANObjectCache( [
|
2023-11-09 20:16:38 +00:00
|
|
|
'cache' => $this->getServiceContainer()->getMainObjectStash(),
|
2016-12-08 20:50:03 +00:00
|
|
|
] );
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNewFromUser() {
|
|
|
|
$exception = false;
|
|
|
|
try {
|
2022-11-13 07:48:43 +00:00
|
|
|
NotifUser::newFromUser( User::newFromId( 0 ) );
|
2013-05-24 22:51:47 +00:00
|
|
|
} catch ( Exception $e ) {
|
|
|
|
$exception = true;
|
|
|
|
$this->assertEquals( "User must be logged in to view notification!",
|
|
|
|
$e->getMessage() );
|
|
|
|
}
|
|
|
|
$this->assertTrue( $exception, "Got exception" );
|
|
|
|
|
2022-11-13 07:48:43 +00:00
|
|
|
$notifUser = NotifUser::newFromUser( User::newFromId( 2 ) );
|
|
|
|
$this->assertInstanceOf( NotifUser::class, $notifUser );
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
2013-06-24 00:22:08 +00:00
|
|
|
public function testGetEmailFormat() {
|
|
|
|
$user = User::newFromId( 2 );
|
2023-08-04 00:31:24 +00:00
|
|
|
$pref = 'foo';
|
|
|
|
$userOptionsLookup = $this->createMock( UserOptionsLookup::class );
|
|
|
|
$userOptionsLookup->expects( $this->atLeastOnce() )
|
|
|
|
->method( 'getOption' )
|
|
|
|
->with( $user, 'echo-email-format' )
|
|
|
|
->willReturn( $pref );
|
|
|
|
$this->setService( 'UserOptionsLookup', $userOptionsLookup );
|
2022-11-13 07:48:43 +00:00
|
|
|
$notifUser = NotifUser::newFromUser( $user );
|
2013-06-24 00:22:08 +00:00
|
|
|
|
|
|
|
$this->setMwGlobals( 'wgAllowHTMLEmail', true );
|
2023-08-04 00:31:24 +00:00
|
|
|
$this->assertEquals( $notifUser->getEmailFormat(), $pref );
|
2013-06-24 00:22:08 +00:00
|
|
|
$this->setMwGlobals( 'wgAllowHTMLEmail', false );
|
2022-11-13 06:43:40 +00:00
|
|
|
$this->assertEquals( EmailFormat::PLAIN_TEXT, $notifUser->getEmailFormat() );
|
2013-06-24 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 22:00:25 +00:00
|
|
|
public function testMarkRead() {
|
2023-08-04 00:31:24 +00:00
|
|
|
$this->setService( 'UserFactory', $this->createMock( UserFactory::class ) );
|
|
|
|
$this->setService( 'TalkPageNotificationManager', $this->createMock( TalkPageNotificationManager::class ) );
|
2022-11-13 07:48:43 +00:00
|
|
|
$notifUser = new NotifUser(
|
2014-08-13 22:00:25 +00:00
|
|
|
User::newFromId( 2 ),
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->cache,
|
2022-11-02 03:37:20 +00:00
|
|
|
$this->mockUserNotificationGateway( [ 'markRead' => true ] ),
|
2022-11-02 20:47:04 +00:00
|
|
|
$this->mockNotificationMapper(),
|
|
|
|
$this->createMock( TargetPageMapper::class ),
|
2021-12-19 01:16:52 +00:00
|
|
|
$this->createNoOpMock( UserOptionsLookup::class ),
|
2022-02-03 21:14:54 +00:00
|
|
|
$this->getServiceContainer()->getUserFactory(),
|
|
|
|
$this->getServiceContainer()->getReadOnlyMode()
|
2014-08-13 22:00:25 +00:00
|
|
|
);
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->assertFalse( $notifUser->markRead( [] ) );
|
|
|
|
$this->assertTrue( $notifUser->markRead( [ 1 ] ) );
|
2014-08-13 22:00:25 +00:00
|
|
|
|
2022-11-13 07:48:43 +00:00
|
|
|
$notifUser = new NotifUser(
|
2014-08-13 22:00:25 +00:00
|
|
|
User::newFromId( 2 ),
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->cache,
|
2022-11-02 03:37:20 +00:00
|
|
|
$this->mockUserNotificationGateway( [ 'markRead' => false ] ),
|
2022-11-02 20:47:04 +00:00
|
|
|
$this->mockNotificationMapper(),
|
|
|
|
$this->createMock( TargetPageMapper::class ),
|
2021-12-19 01:16:52 +00:00
|
|
|
$this->createNoOpMock( UserOptionsLookup::class ),
|
2022-02-03 21:14:54 +00:00
|
|
|
$this->getServiceContainer()->getUserFactory(),
|
|
|
|
$this->getServiceContainer()->getReadOnlyMode()
|
2014-08-13 22:00:25 +00:00
|
|
|
);
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->assertFalse( $notifUser->markRead( [] ) );
|
|
|
|
$this->assertFalse( $notifUser->markRead( [ 1 ] ) );
|
2014-08-13 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMarkAllRead() {
|
2023-08-04 00:31:24 +00:00
|
|
|
$this->setService( 'UserFactory', $this->createMock( UserFactory::class ) );
|
|
|
|
$this->setService( 'TalkPageNotificationManager', $this->createMock( TalkPageNotificationManager::class ) );
|
|
|
|
$this->setService( 'UserGroupManager', $this->createMock( UserGroupManager::class ) );
|
|
|
|
$this->setService( 'UserOptionsLookup', $this->createMock( UserOptionsLookup::class ) );
|
2014-08-13 22:00:25 +00:00
|
|
|
// Successful mark as read & non empty fetch
|
2022-11-13 07:48:43 +00:00
|
|
|
$notifUser = new NotifUser(
|
2014-08-13 22:00:25 +00:00
|
|
|
User::newFromId( 2 ),
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->cache,
|
2022-11-02 03:37:20 +00:00
|
|
|
$this->mockUserNotificationGateway( [ 'markRead' => true ] ),
|
2022-11-02 21:34:17 +00:00
|
|
|
$this->mockNotificationMapper( [ $this->mockNotification() ] ),
|
2022-11-02 20:47:04 +00:00
|
|
|
$this->createMock( TargetPageMapper::class ),
|
2021-12-19 01:16:52 +00:00
|
|
|
$this->createNoOpMock( UserOptionsLookup::class ),
|
2022-02-03 21:14:54 +00:00
|
|
|
$this->getServiceContainer()->getUserFactory(),
|
|
|
|
$this->getServiceContainer()->getReadOnlyMode()
|
2014-08-13 22:00:25 +00:00
|
|
|
);
|
|
|
|
$this->assertTrue( $notifUser->markAllRead() );
|
|
|
|
|
|
|
|
// Unsuccessful mark as read & non empty fetch
|
2022-11-13 07:48:43 +00:00
|
|
|
$notifUser = new NotifUser(
|
2014-08-13 22:00:25 +00:00
|
|
|
User::newFromId( 2 ),
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->cache,
|
2022-11-02 03:37:20 +00:00
|
|
|
$this->mockUserNotificationGateway( [ 'markRead' => false ] ),
|
2022-11-02 21:34:17 +00:00
|
|
|
$this->mockNotificationMapper( [ $this->mockNotification() ] ),
|
2022-11-02 20:47:04 +00:00
|
|
|
$this->createMock( TargetPageMapper::class ),
|
2021-12-19 01:16:52 +00:00
|
|
|
$this->createNoOpMock( UserOptionsLookup::class ),
|
2022-02-03 21:14:54 +00:00
|
|
|
$this->getServiceContainer()->getUserFactory(),
|
|
|
|
$this->getServiceContainer()->getReadOnlyMode()
|
2014-08-13 22:00:25 +00:00
|
|
|
);
|
|
|
|
$this->assertFalse( $notifUser->markAllRead() );
|
|
|
|
|
|
|
|
// Successful mark as read & empty fetch
|
2022-11-13 07:48:43 +00:00
|
|
|
$notifUser = new NotifUser(
|
2014-08-13 22:00:25 +00:00
|
|
|
User::newFromId( 2 ),
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->cache,
|
2022-11-02 03:37:20 +00:00
|
|
|
$this->mockUserNotificationGateway( [ 'markRead' => true ] ),
|
2022-11-02 20:47:04 +00:00
|
|
|
$this->mockNotificationMapper(),
|
|
|
|
$this->createMock( TargetPageMapper::class ),
|
2021-12-19 01:16:52 +00:00
|
|
|
$this->createNoOpMock( UserOptionsLookup::class ),
|
2022-02-03 21:14:54 +00:00
|
|
|
$this->getServiceContainer()->getUserFactory(),
|
|
|
|
$this->getServiceContainer()->getReadOnlyMode()
|
2014-08-13 22:00:25 +00:00
|
|
|
);
|
|
|
|
$this->assertFalse( $notifUser->markAllRead() );
|
|
|
|
|
|
|
|
// Unsuccessful mark as read & empty fetch
|
2022-11-13 07:48:43 +00:00
|
|
|
$notifUser = new NotifUser(
|
2014-08-13 22:00:25 +00:00
|
|
|
User::newFromId( 2 ),
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->cache,
|
2022-11-02 03:37:20 +00:00
|
|
|
$this->mockUserNotificationGateway( [ 'markRead' => false ] ),
|
2022-11-02 20:47:04 +00:00
|
|
|
$this->mockNotificationMapper(),
|
|
|
|
$this->createMock( TargetPageMapper::class ),
|
2021-12-19 01:16:52 +00:00
|
|
|
$this->createNoOpMock( UserOptionsLookup::class ),
|
2022-02-03 21:14:54 +00:00
|
|
|
$this->getServiceContainer()->getUserFactory(),
|
|
|
|
$this->getServiceContainer()->getReadOnlyMode()
|
2014-08-13 22:00:25 +00:00
|
|
|
);
|
|
|
|
$this->assertFalse( $notifUser->markAllRead() );
|
|
|
|
}
|
|
|
|
|
2022-11-02 03:37:20 +00:00
|
|
|
public function mockUserNotificationGateway( array $dbResult = [] ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$dbResult += [
|
2014-08-13 22:00:25 +00:00
|
|
|
'markRead' => true
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2022-11-02 03:37:20 +00:00
|
|
|
$gateway = $this->createMock( UserNotificationGateway::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$gateway->method( 'markRead' )
|
|
|
|
->willReturn( $dbResult['markRead'] );
|
|
|
|
$gateway->method( 'getDB' )
|
|
|
|
->willReturn( $this->createMock( IDatabase::class ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-13 22:00:25 +00:00
|
|
|
return $gateway;
|
|
|
|
}
|
|
|
|
|
2022-11-02 20:47:04 +00:00
|
|
|
public function mockNotificationMapper( array $result = [] ) {
|
|
|
|
$mapper = $this->createMock( NotificationMapper::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$mapper->method( 'fetchUnreadByUser' )
|
|
|
|
->willReturn( $result );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-13 22:00:25 +00:00
|
|
|
return $mapper;
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
protected function mockNotification() {
|
|
|
|
$notification = $this->createMock( Notification::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$notification->method( 'getEvent' )
|
2022-11-02 21:34:17 +00:00
|
|
|
->willReturn( $this->mockEvent() );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-13 22:00:25 +00:00
|
|
|
return $notification;
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
protected function mockEvent() {
|
|
|
|
$event = $this->createMock( Event::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$event->method( 'getId' )
|
|
|
|
->willReturn( 1 );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-13 22:00:25 +00:00
|
|
|
return $event;
|
|
|
|
}
|
2016-12-08 20:50:03 +00:00
|
|
|
|
|
|
|
protected function newNotifUser() {
|
2022-11-13 07:48:43 +00:00
|
|
|
return new NotifUser(
|
2016-12-08 20:50:03 +00:00
|
|
|
User::newFromId( 2 ),
|
|
|
|
$this->cache,
|
2022-11-02 03:37:20 +00:00
|
|
|
$this->mockUserNotificationGateway(),
|
2022-11-02 20:47:04 +00:00
|
|
|
$this->mockNotificationMapper(),
|
|
|
|
$this->createMock( TargetPageMapper::class ),
|
2021-12-19 01:16:52 +00:00
|
|
|
$this->createNoOpMock( UserOptionsLookup::class ),
|
2022-02-03 21:14:54 +00:00
|
|
|
$this->getServiceContainer()->getUserFactory(),
|
|
|
|
$this->getServiceContainer()->getReadOnlyMode()
|
2016-12-08 20:50:03 +00:00
|
|
|
);
|
|
|
|
}
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|