2013-05-24 22:51:47 +00:00
|
|
|
<?php
|
2018-08-13 07:29:32 +00:00
|
|
|
|
2022-11-02 03:37:20 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Gateway\UserNotificationGateway;
|
2016-12-08 20:50:03 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-12-01 18:29:00 +00:00
|
|
|
use MediaWiki\User\UserOptionsLookup;
|
2013-05-24 22:51:47 +00:00
|
|
|
|
2014-07-25 20:19:15 +00:00
|
|
|
/**
|
2019-10-23 10:23:09 +00:00
|
|
|
* @covers \MWEchoNotifUser
|
2014-07-25 20:19:15 +00:00
|
|
|
* @group Echo
|
|
|
|
*/
|
2021-10-11 22:42:10 +00:00
|
|
|
class MWEchoNotifUserTest 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( [
|
|
|
|
'cache' => MediaWikiServices::getInstance()->getMainObjectStash(),
|
|
|
|
] );
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNewFromUser() {
|
|
|
|
$exception = false;
|
|
|
|
try {
|
|
|
|
MWEchoNotifUser::newFromUser( User::newFromId( 0 ) );
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
$exception = true;
|
|
|
|
$this->assertEquals( "User must be logged in to view notification!",
|
|
|
|
$e->getMessage() );
|
|
|
|
}
|
|
|
|
$this->assertTrue( $exception, "Got exception" );
|
|
|
|
|
|
|
|
$notifUser = MWEchoNotifUser::newFromUser( User::newFromId( 2 ) );
|
2019-02-19 10:35:54 +00:00
|
|
|
$this->assertInstanceOf( MWEchoNotifUser::class, $notifUser );
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
2013-06-24 00:22:08 +00:00
|
|
|
public function testGetEmailFormat() {
|
2021-12-01 18:29:00 +00:00
|
|
|
$userOptionsLookup = $this->getServiceContainer()->getUserOptionsLookup();
|
2013-06-24 00:22:08 +00:00
|
|
|
$user = User::newFromId( 2 );
|
|
|
|
$notifUser = MWEchoNotifUser::newFromUser( $user );
|
|
|
|
|
|
|
|
$this->setMwGlobals( 'wgAllowHTMLEmail', true );
|
2021-12-01 18:29:00 +00:00
|
|
|
$this->assertEquals( $notifUser->getEmailFormat(),
|
|
|
|
$userOptionsLookup->getOption( $user, 'echo-email-format' ) );
|
2013-06-24 00:22:08 +00:00
|
|
|
$this->setMwGlobals( 'wgAllowHTMLEmail', false );
|
2016-05-05 13:05:03 +00:00
|
|
|
$this->assertEquals( $notifUser->getEmailFormat(), EchoEmailFormat::PLAIN_TEXT );
|
2013-06-24 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 22:00:25 +00:00
|
|
|
public function testMarkRead() {
|
|
|
|
$notifUser = new MWEchoNotifUser(
|
|
|
|
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 ] ),
|
2014-08-07 00:07:34 +00:00
|
|
|
$this->mockEchoNotificationMapper(),
|
2022-09-29 13:41:35 +00:00
|
|
|
$this->createMock( EchoTargetPageMapper::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
|
|
|
|
|
|
|
$notifUser = new MWEchoNotifUser(
|
|
|
|
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 ] ),
|
2014-08-07 00:07:34 +00:00
|
|
|
$this->mockEchoNotificationMapper(),
|
2022-09-29 13:41:35 +00:00
|
|
|
$this->createMock( EchoTargetPageMapper::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() {
|
|
|
|
// Successful mark as read & non empty fetch
|
|
|
|
$notifUser = new MWEchoNotifUser(
|
|
|
|
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 ] ),
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->mockEchoNotificationMapper( [ $this->mockEchoNotification() ] ),
|
2022-09-29 13:41:35 +00:00
|
|
|
$this->createMock( EchoTargetPageMapper::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
|
|
|
|
$notifUser = new MWEchoNotifUser(
|
|
|
|
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 ] ),
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->mockEchoNotificationMapper( [ $this->mockEchoNotification() ] ),
|
2022-09-29 13:41:35 +00:00
|
|
|
$this->createMock( EchoTargetPageMapper::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
|
|
|
|
$notifUser = new MWEchoNotifUser(
|
|
|
|
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 ] ),
|
2014-08-07 00:07:34 +00:00
|
|
|
$this->mockEchoNotificationMapper(),
|
2022-09-29 13:41:35 +00:00
|
|
|
$this->createMock( EchoTargetPageMapper::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
|
|
|
|
$notifUser = new MWEchoNotifUser(
|
|
|
|
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 ] ),
|
2014-08-07 00:07:34 +00:00
|
|
|
$this->mockEchoNotificationMapper(),
|
2022-09-29 13:41:35 +00:00
|
|
|
$this->createMock( EchoTargetPageMapper::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;
|
|
|
|
}
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
public function mockEchoNotificationMapper( array $result = [] ) {
|
2022-09-29 13:41:35 +00:00
|
|
|
$mapper = $this->createMock( EchoNotificationMapper::class );
|
|
|
|
$mapper->method( 'fetchUnreadByUser' )
|
|
|
|
->willReturn( $result );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-13 22:00:25 +00:00
|
|
|
return $mapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function mockEchoNotification() {
|
2022-09-29 13:41:35 +00:00
|
|
|
$notification = $this->createMock( EchoNotification::class );
|
|
|
|
$notification->method( 'getEvent' )
|
|
|
|
->willReturn( $this->mockEchoEvent() );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-13 22:00:25 +00:00
|
|
|
return $notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function mockEchoEvent() {
|
2022-09-29 13:41:35 +00:00
|
|
|
$event = $this->createMock( EchoEvent::class );
|
|
|
|
$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() {
|
|
|
|
return new MWEchoNotifUser(
|
|
|
|
User::newFromId( 2 ),
|
|
|
|
$this->cache,
|
2022-11-02 03:37:20 +00:00
|
|
|
$this->mockUserNotificationGateway(),
|
2016-12-08 20:50:03 +00:00
|
|
|
$this->mockEchoNotificationMapper(),
|
2022-09-29 13:41:35 +00:00
|
|
|
$this->createMock( EchoTargetPageMapper::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
|
|
|
}
|