2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
2019-10-05 03:54:45 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
|
2018-01-24 00:31:53 +00:00
|
|
|
/**
|
2019-10-23 10:23:09 +00:00
|
|
|
* @covers \EchoUserNotificationGateway
|
2018-01-24 00:31:53 +00:00
|
|
|
*/
|
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
|
2019-08-30 20:58:41 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway(
|
2020-10-02 03:40:58 +00:00
|
|
|
$this->mockUser(),
|
2019-08-30 20:58:41 +00:00
|
|
|
$this->mockMWEchoDbFactory(),
|
|
|
|
$this->mockConfig()
|
|
|
|
);
|
2017-10-02 23:07:31 +00:00
|
|
|
$this->assertFalse( $gateway->markRead( [] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
// successful update
|
2019-08-30 20:58:41 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway(
|
2020-10-02 03:40:58 +00:00
|
|
|
$this->mockUser(),
|
2019-08-30 20:58:41 +00:00
|
|
|
$this->mockMWEchoDbFactory( [ 'update' => true ] ),
|
|
|
|
$this->mockConfig()
|
|
|
|
);
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->assertTrue( $gateway->markRead( [ 2 ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
// unsuccessful update
|
2019-08-30 20:58:41 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway(
|
2020-10-02 03:40:58 +00:00
|
|
|
$this->mockUser(),
|
2019-08-30 20:58:41 +00:00
|
|
|
$this->mockMWEchoDbFactory( [ 'update' => false ] ),
|
|
|
|
$this->mockConfig()
|
|
|
|
);
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->assertFalse( $gateway->markRead( [ 2 ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMarkAllRead() {
|
|
|
|
// successful update
|
2019-08-30 20:58:41 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway(
|
2020-10-02 03:40:58 +00:00
|
|
|
$this->mockUser(),
|
2019-08-30 20:58:41 +00:00
|
|
|
$this->mockMWEchoDbFactory( [ 'update' => true ] ),
|
|
|
|
$this->mockConfig()
|
|
|
|
);
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->assertTrue( $gateway->markAllRead( [ 2 ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2018-10-26 19:40:33 +00:00
|
|
|
// null update
|
2019-08-30 20:58:41 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway(
|
2020-10-02 03:40:58 +00:00
|
|
|
$this->mockUser(),
|
2019-08-30 20:58:41 +00:00
|
|
|
$this->mockMWEchoDbFactory( [ 'update' => false ] ),
|
|
|
|
$this->mockConfig()
|
|
|
|
);
|
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
|
2019-08-30 20:58:41 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway(
|
|
|
|
$this->mockUser(),
|
|
|
|
$this->mockMWEchoDbFactory( [ 'selectRowCount' => 0 ] ),
|
|
|
|
$this->mockConfig()
|
|
|
|
);
|
2019-10-23 10:28:30 +00:00
|
|
|
$this->assertSame( 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
|
2019-08-30 20:58:41 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway(
|
|
|
|
$this->mockUser(),
|
|
|
|
$this->mockMWEchoDbFactory( [ 'selectRowCount' => 2 ] ),
|
|
|
|
$this->mockConfig()
|
|
|
|
);
|
2019-10-23 10:28:30 +00:00
|
|
|
$this->assertSame( 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
|
2019-08-30 20:58:41 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway(
|
|
|
|
$this->mockUser(),
|
|
|
|
$this->mockMWEchoDbFactory( [ 'selectRowCount' => 2 ] ),
|
|
|
|
$this->mockConfig()
|
|
|
|
);
|
2019-10-23 10:28:30 +00:00
|
|
|
$this->assertSame( 0, $gateway->getCappedNotificationCount( DB_REPLICA, [] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2014-07-22 21:33:22 +00:00
|
|
|
// successful select
|
2019-08-30 20:58:41 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway(
|
|
|
|
$this->mockUser(),
|
|
|
|
$this->mockMWEchoDbFactory( [ 'selectRowCount' => 3 ] ),
|
|
|
|
$this->mockConfig()
|
|
|
|
);
|
2019-10-23 10:28:30 +00:00
|
|
|
$this->assertSame( 3, $gateway->getCappedNotificationCount( DB_REPLICA, [ 'event_one' ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetUnreadNotifications() {
|
2019-08-30 20:58:41 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway(
|
|
|
|
$this->mockUser(),
|
|
|
|
$this->mockMWEchoDbFactory( [ 'select' => false ] ),
|
|
|
|
$this->mockConfig()
|
|
|
|
);
|
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 ],
|
|
|
|
];
|
2019-08-30 20:58:41 +00:00
|
|
|
$gateway = new EchoUserNotificationGateway(
|
|
|
|
$this->mockUser(),
|
|
|
|
$this->mockMWEchoDbFactory( [ 'select' => $dbResult ] ),
|
|
|
|
$this->mockConfig()
|
|
|
|
);
|
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
|
2021-01-23 11:54:27 +00:00
|
|
|
* @return User
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
2021-02-25 02:46:03 +00:00
|
|
|
protected function mockUser() {
|
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 ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock object of MWEchoDbFactory
|
2021-01-23 11:54:27 +00:00
|
|
|
* @param array $dbResult
|
|
|
|
* @return MWEchoDbFactory
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-30 20:58:41 +00:00
|
|
|
protected function mockConfig() {
|
|
|
|
return new HashConfig( [
|
|
|
|
'UpdateRowsPerQuery' => 500,
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
2018-01-16 18:58:52 +00:00
|
|
|
* Returns a mock database object
|
2021-01-23 11:54:27 +00:00
|
|
|
* @param array $dbResult
|
2018-01-16 18:58:52 +00:00
|
|
|
* @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-10-05 03:54:45 +00:00
|
|
|
$db = $this->createMock( 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|