2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-24 00:31:53 +00:00
|
|
|
/**
|
|
|
|
* @covers EchoUserNotificationGateway
|
|
|
|
*/
|
2019-07-15 08:49:05 +00:00
|
|
|
class EchoUserNotificationGatewayTest extends MediaWikiUnitTestCase {
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
public function testMarkRead() {
|
|
|
|
// no event ids to mark
|
|
|
|
$gateway = new EchoUserNotificationGateway( User::newFromId( 1 ), $this->mockMWEchoDbFactory() );
|
2017-10-02 23:07:31 +00:00
|
|
|
$this->assertFalse( $gateway->markRead( [] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
// successful update
|
2016-12-05 18:51:07 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway( User::newFromId( 1 ), $this->mockMWEchoDbFactory( [ 'update' => true ] ) );
|
|
|
|
$this->assertTrue( $gateway->markRead( [ 2 ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
// unsuccessful update
|
2016-12-05 18:51:07 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway( User::newFromId( 1 ), $this->mockMWEchoDbFactory( [ 'update' => false ] ) );
|
|
|
|
$this->assertFalse( $gateway->markRead( [ 2 ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMarkAllRead() {
|
|
|
|
// successful update
|
2016-12-05 18:51:07 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway( User::newFromId( 1 ), $this->mockMWEchoDbFactory( [ 'update' => true ] ) );
|
|
|
|
$this->assertTrue( $gateway->markAllRead( [ 2 ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2018-10-26 19:40:33 +00:00
|
|
|
// null update
|
2016-12-05 18:51:07 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway( User::newFromId( 1 ), $this->mockMWEchoDbFactory( [ 'update' => false ] ) );
|
2018-10-26 19:40:33 +00:00
|
|
|
$this->assertTrue( $gateway->markAllRead( [ 2 ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetNotificationCount() {
|
2014-07-22 21:33:22 +00:00
|
|
|
// unsuccessful select
|
2016-12-05 18:51:07 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway( $this->mockUser(), $this->mockMWEchoDbFactory( [ 'selectRowCount' => 0 ] ) );
|
2017-09-24 05:23:47 +00:00
|
|
|
$this->assertEquals( 0, $gateway->getCappedNotificationCount( DB_REPLICA, [ 'event_one' ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2014-07-22 21:33:22 +00:00
|
|
|
// successful select of alert
|
2016-12-05 18:51:07 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway( $this->mockUser(), $this->mockMWEchoDbFactory( [ 'selectRowCount' => 2 ] ) );
|
2017-09-24 05:23:47 +00:00
|
|
|
$this->assertEquals( 2, $gateway->getCappedNotificationCount( DB_REPLICA, [ 'event_one', 'event_two' ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2014-07-22 21:33:22 +00:00
|
|
|
// there is event, should return 0
|
2016-12-05 18:51:07 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway( $this->mockUser(), $this->mockMWEchoDbFactory( [ 'selectRowCount' => 2 ] ) );
|
2017-09-24 05:23:47 +00:00
|
|
|
$this->assertEquals( 0, $gateway->getCappedNotificationCount( DB_REPLICA, [] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2014-07-22 21:33:22 +00:00
|
|
|
// successful select
|
2016-12-05 18:51:07 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway( $this->mockUser(), $this->mockMWEchoDbFactory( [ 'selectRowCount' => 3 ] ) );
|
2017-09-24 05:23:47 +00:00
|
|
|
$this->assertEquals( 3, $gateway->getCappedNotificationCount( DB_REPLICA, [ 'event_one' ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetUnreadNotifications() {
|
2016-12-05 18:51:07 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway( $this->mockUser(), $this->mockMWEchoDbFactory( [ 'select' => false ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->assertEmpty( $gateway->getUnreadNotifications( 'user_talk' ) );
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$dbResult = [
|
|
|
|
(object)[ 'notification_event' => 1 ],
|
|
|
|
(object)[ 'notification_event' => 2 ],
|
|
|
|
(object)[ 'notification_event' => 3 ],
|
|
|
|
];
|
|
|
|
$gateway = new EchoUserNotificationGateway( $this->mockUser(), $this->mockMWEchoDbFactory( [ 'select' => $dbResult ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
$res = $gateway->getUnreadNotifications( 'user_talk' );
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->assertEquals( $res, [ 1 => 1, 2 => 2, 3 => 3 ] );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock object of User
|
|
|
|
*/
|
2014-07-22 21:33:22 +00:00
|
|
|
protected function mockUser( $group = 'echo_group' ) {
|
2019-02-19 10:35:54 +00:00
|
|
|
$user = $this->getMockBuilder( User::class )
|
2014-07-18 03:58:21 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$user->expects( $this->any() )
|
|
|
|
->method( 'getID' )
|
|
|
|
->will( $this->returnValue( 1 ) );
|
|
|
|
$user->expects( $this->any() )
|
|
|
|
->method( 'getOption' )
|
|
|
|
->will( $this->returnValue( true ) );
|
|
|
|
$user->expects( $this->any() )
|
|
|
|
->method( 'getGroups' )
|
2016-12-05 18:51:07 +00:00
|
|
|
->will( $this->returnValue( [ $group ] ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock object of MWEchoDbFactory
|
|
|
|
*/
|
2016-12-05 18:51:07 +00:00
|
|
|
protected function mockMWEchoDbFactory( array $dbResult = [] ) {
|
2019-02-19 10:35:54 +00:00
|
|
|
$dbFactory = $this->getMockBuilder( MWEchoDbFactory::class )
|
2014-07-18 03:58:21 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$dbFactory->expects( $this->any() )
|
|
|
|
->method( 'getEchoDb' )
|
|
|
|
->will( $this->returnValue( $this->mockDb( $dbResult ) ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $dbFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-16 18:58:52 +00:00
|
|
|
* Returns a mock database object
|
|
|
|
* @return \Wikimedia\Rdbms\IDatabase
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
2016-12-05 18:51:07 +00:00
|
|
|
protected function mockDb( array $dbResult = [] ) {
|
|
|
|
$dbResult += [
|
2014-07-18 03:58:21 +00:00
|
|
|
'update' => '',
|
|
|
|
'select' => '',
|
|
|
|
'selectRow' => '',
|
2016-03-14 13:37:20 +00:00
|
|
|
'selectRowCount' => '',
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2019-04-07 21:44:48 +00:00
|
|
|
$db = $this->getMock( IDatabase::class );
|
2014-07-18 03:58:21 +00:00
|
|
|
$db->expects( $this->any() )
|
|
|
|
->method( 'update' )
|
|
|
|
->will( $this->returnValue( $dbResult['update'] ) );
|
|
|
|
$db->expects( $this->any() )
|
|
|
|
->method( 'select' )
|
|
|
|
->will( $this->returnValue( $dbResult['select'] ) );
|
|
|
|
$db->expects( $this->any() )
|
|
|
|
->method( 'selectRow' )
|
|
|
|
->will( $this->returnValue( $dbResult['selectRow'] ) );
|
2016-03-14 13:37:20 +00:00
|
|
|
$db->expects( $this->any() )
|
|
|
|
->method( 'selectRowCount' )
|
|
|
|
->will( $this->returnValue( $dbResult['selectRowCount'] ) );
|
2018-10-13 03:49:42 +00:00
|
|
|
$numRows = is_array( $dbResult['select'] ) ? count( $dbResult['select'] ) : 0;
|
2014-07-18 03:58:21 +00:00
|
|
|
$db->expects( $this->any() )
|
|
|
|
->method( 'numRows' )
|
2018-10-13 03:49:42 +00:00
|
|
|
->will( $this->returnValue( $numRows ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $db;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|