2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-02 03:37:20 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Gateway\UserNotificationGateway;
|
2019-10-05 03:54:45 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
|
2018-01-24 00:31:53 +00:00
|
|
|
/**
|
2022-11-02 03:37:20 +00:00
|
|
|
* @covers \MediaWiki\Extension\Notifications\Gateway\UserNotificationGateway
|
2018-01-24 00:31:53 +00:00
|
|
|
*/
|
2022-11-02 03:37:20 +00:00
|
|
|
class UserNotificationGatewayTest extends MediaWikiUnitTestCase {
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
public function testMarkRead() {
|
|
|
|
// no event ids to mark
|
2022-11-02 03:37:20 +00:00
|
|
|
$gateway = new UserNotificationGateway(
|
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
|
2022-11-02 03:37:20 +00:00
|
|
|
$gateway = new UserNotificationGateway(
|
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
|
2022-11-02 03:37:20 +00:00
|
|
|
$gateway = new UserNotificationGateway(
|
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
|
2022-11-02 03:37:20 +00:00
|
|
|
$gateway = new UserNotificationGateway(
|
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
|
2022-11-02 03:37:20 +00:00
|
|
|
$gateway = new UserNotificationGateway(
|
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
|
2022-11-02 03:37:20 +00:00
|
|
|
$gateway = new UserNotificationGateway(
|
2019-08-30 20:58:41 +00:00
|
|
|
$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
|
2022-11-02 03:37:20 +00:00
|
|
|
$gateway = new UserNotificationGateway(
|
2019-08-30 20:58:41 +00:00
|
|
|
$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
|
2022-11-02 03:37:20 +00:00
|
|
|
$gateway = new UserNotificationGateway(
|
2019-08-30 20:58:41 +00:00
|
|
|
$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
|
2022-11-02 03:37:20 +00:00
|
|
|
$gateway = new UserNotificationGateway(
|
2019-08-30 20:58:41 +00:00
|
|
|
$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() {
|
2022-11-02 03:37:20 +00:00
|
|
|
$gateway = new UserNotificationGateway(
|
2019-08-30 20:58:41 +00:00
|
|
|
$this->mockUser(),
|
|
|
|
$this->mockMWEchoDbFactory( [ 'select' => false ] ),
|
|
|
|
$this->mockConfig()
|
|
|
|
);
|
2022-11-24 22:03:18 +00:00
|
|
|
$this->assertSame( [], $gateway->getUnreadNotifications( 'user_talk' ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$dbResult = [
|
|
|
|
(object)[ 'notification_event' => 1 ],
|
|
|
|
(object)[ 'notification_event' => 2 ],
|
|
|
|
(object)[ 'notification_event' => 3 ],
|
|
|
|
];
|
2022-11-02 03:37:20 +00:00
|
|
|
$gateway = new UserNotificationGateway(
|
2019-08-30 20:58:41 +00:00
|
|
|
$this->mockUser(),
|
|
|
|
$this->mockMWEchoDbFactory( [ 'select' => $dbResult ] ),
|
|
|
|
$this->mockConfig()
|
|
|
|
);
|
2014-07-18 03:58:21 +00:00
|
|
|
$res = $gateway->getUnreadNotifications( 'user_talk' );
|
2023-03-11 20:11:11 +00:00
|
|
|
$this->assertEquals( [ 1 => 1, 2 => 2, 3 => 3 ], $res );
|
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() {
|
2022-09-29 13:41:35 +00:00
|
|
|
$user = $this->createMock( User::class );
|
|
|
|
$user->method( 'getID' )
|
|
|
|
->willReturn( 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 = [] ) {
|
2022-09-29 13:41:35 +00:00
|
|
|
$dbFactory = $this->createMock( MWEchoDbFactory::class );
|
|
|
|
$dbFactory->method( 'getEchoDb' )
|
|
|
|
->willReturn( $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 );
|
2022-09-29 13:41:35 +00:00
|
|
|
$db->method( 'update' )
|
|
|
|
->willReturn( $dbResult['update'] );
|
|
|
|
$db->method( 'select' )
|
|
|
|
->willReturn( $dbResult['select'] );
|
|
|
|
$db->method( 'selectRow' )
|
|
|
|
->willReturn( $dbResult['selectRow'] );
|
|
|
|
$db->method( 'selectRowCount' )
|
|
|
|
->willReturn( $dbResult['selectRowCount'] );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $db;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|