2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\Config\HashConfig;
|
2022-11-12 07:19:00 +00:00
|
|
|
use MediaWiki\Extension\Notifications\DbFactory;
|
2022-11-02 03:37:20 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Gateway\UserNotificationGateway;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\User;
|
2024-04-19 19:14:11 +00:00
|
|
|
use Wikimedia\Rdbms\FakeResultWrapper;
|
2019-10-05 03:54:45 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2024-04-13 18:33:56 +00:00
|
|
|
use Wikimedia\Rdbms\UpdateQueryBuilder;
|
2019-10-05 03:54:45 +00:00
|
|
|
|
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(),
|
2022-11-12 07:19:00 +00:00
|
|
|
$this->mockDbFactory(),
|
2019-08-30 20:58:41 +00:00
|
|
|
$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(),
|
2022-11-12 07:19:00 +00:00
|
|
|
$this->mockDbFactory( [ 'update' => true ] ),
|
2019-08-30 20:58:41 +00:00
|
|
|
$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(),
|
2022-11-12 07:19:00 +00:00
|
|
|
$this->mockDbFactory( [ 'update' => false ] ),
|
2019-08-30 20:58:41 +00:00
|
|
|
$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(),
|
2022-11-12 07:19:00 +00:00
|
|
|
$this->mockDbFactory( [ 'update' => true ] ),
|
2019-08-30 20:58:41 +00:00
|
|
|
$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(),
|
2022-11-12 07:19:00 +00:00
|
|
|
$this->mockDbFactory( [ 'update' => false ] ),
|
2019-08-30 20:58:41 +00:00
|
|
|
$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(),
|
2022-11-12 07:19:00 +00:00
|
|
|
$this->mockDbFactory( [ 'selectRowCount' => 0 ] ),
|
2019-08-30 20:58:41 +00:00
|
|
|
$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(),
|
2022-11-12 07:19:00 +00:00
|
|
|
$this->mockDbFactory( [ 'selectRowCount' => 2 ] ),
|
2019-08-30 20:58:41 +00:00
|
|
|
$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(),
|
2022-11-12 07:19:00 +00:00
|
|
|
$this->mockDbFactory( [ 'selectRowCount' => 2 ] ),
|
2019-08-30 20:58:41 +00:00
|
|
|
$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(),
|
2022-11-12 07:19:00 +00:00
|
|
|
$this->mockDbFactory( [ 'selectRowCount' => 3 ] ),
|
2019-08-30 20:58:41 +00:00
|
|
|
$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() {
|
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(),
|
2022-11-12 07:19:00 +00:00
|
|
|
$this->mockDbFactory( [ 'select' => $dbResult ] ),
|
2019-08-30 20:58:41 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-12 07:19:00 +00:00
|
|
|
* Mock object of DbFactory
|
2021-01-23 11:54:27 +00:00
|
|
|
* @param array $dbResult
|
2022-11-12 07:19:00 +00:00
|
|
|
* @return DbFactory
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
2022-11-12 07:19:00 +00:00
|
|
|
protected function mockDbFactory( array $dbResult = [] ) {
|
|
|
|
$dbFactory = $this->createMock( DbFactory::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$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' => '',
|
2024-04-19 19:14:11 +00:00
|
|
|
'select' => [],
|
2014-07-18 03:58:21 +00:00
|
|
|
'selectRow' => '',
|
2024-04-19 19:14:11 +00:00
|
|
|
'selectRowCount' => 0,
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2019-10-05 03:54:45 +00:00
|
|
|
$db = $this->createMock( IDatabase::class );
|
2024-04-13 18:33:56 +00:00
|
|
|
$db->method( 'affectedRows' )
|
|
|
|
->willReturn( $dbResult['update'] ? 1 : 0 );
|
2022-09-29 13:41:35 +00:00
|
|
|
$db->method( 'update' )
|
|
|
|
->willReturn( $dbResult['update'] );
|
|
|
|
$db->method( 'select' )
|
2024-04-19 19:14:11 +00:00
|
|
|
->willReturn( new FakeResultWrapper( $dbResult['select'] ) );
|
2022-09-29 13:41:35 +00:00
|
|
|
$db->method( 'selectRow' )
|
|
|
|
->willReturn( $dbResult['selectRow'] );
|
|
|
|
$db->method( 'selectRowCount' )
|
|
|
|
->willReturn( $dbResult['selectRowCount'] );
|
2024-04-13 18:33:56 +00:00
|
|
|
$db->method( 'newUpdateQueryBuilder' )
|
|
|
|
->willReturnCallback( static function () use ( $db ) {
|
|
|
|
return new UpdateQueryBuilder( $db );
|
|
|
|
} );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $db;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|