2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-12 07:19:00 +00:00
|
|
|
use MediaWiki\Extension\Notifications\DbFactory;
|
2022-11-02 20:47:04 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Mapper\NotificationMapper;
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Notification;
|
2019-10-05 03:54:45 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
|
2018-01-24 00:31:53 +00:00
|
|
|
/**
|
2022-11-02 20:47:04 +00:00
|
|
|
* @covers \MediaWiki\Extension\Notifications\Mapper\NotificationMapper
|
2018-01-24 00:31:53 +00:00
|
|
|
*/
|
2022-11-02 20:47:04 +00:00
|
|
|
class NotificationMapperTest extends MediaWikiIntegrationTestCase {
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo write this test
|
|
|
|
*/
|
|
|
|
public function testInsert() {
|
|
|
|
$this->assertTrue( true );
|
|
|
|
}
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
public function fetchUnreadByUser( User $user, $limit, array $eventTypes = [] ) {
|
2014-08-14 18:46:26 +00:00
|
|
|
// Unsuccessful select
|
2022-11-12 07:19:00 +00:00
|
|
|
$notifMapper = new NotificationMapper( $this->mockDbFactory( [ 'select' => false ] ) );
|
2015-12-09 18:41:40 +00:00
|
|
|
$res = $notifMapper->fetchUnreadByUser( $this->mockUser(), 10, null, '' );
|
2022-11-24 22:03:18 +00:00
|
|
|
$this->assertSame( [], $res );
|
2014-08-12 23:28:15 +00:00
|
|
|
|
2014-08-14 18:46:26 +00:00
|
|
|
// Successful select
|
2016-12-05 18:51:07 +00:00
|
|
|
$dbResult = [
|
|
|
|
(object)[
|
2014-08-14 18:46:26 +00:00
|
|
|
'event_id' => 1,
|
|
|
|
'event_type' => 'test_event',
|
|
|
|
'event_variant' => '',
|
|
|
|
'event_extra' => '',
|
|
|
|
'event_page_id' => '',
|
|
|
|
'event_agent_id' => '',
|
|
|
|
'event_agent_ip' => '',
|
|
|
|
'notification_user' => 1,
|
|
|
|
'notification_timestamp' => '20140615101010',
|
2022-01-05 19:08:47 +00:00
|
|
|
'notification_read_timestamp' => null,
|
2014-08-14 18:46:26 +00:00
|
|
|
'notification_bundle_hash' => 'testhash',
|
2016-12-05 18:51:07 +00:00
|
|
|
]
|
|
|
|
];
|
2022-11-12 07:19:00 +00:00
|
|
|
$notifMapper = new NotificationMapper( $this->mockDbFactory( [ 'select' => $dbResult ] ) );
|
2016-12-05 18:51:07 +00:00
|
|
|
$res = $notifMapper->fetchUnreadByUser( $this->mockUser(), 10, null, '', [] );
|
2022-11-24 22:03:18 +00:00
|
|
|
$this->assertSame( [], $res );
|
2014-08-14 18:46:26 +00:00
|
|
|
|
2022-11-12 07:19:00 +00:00
|
|
|
$notifMapper = new NotificationMapper( $this->mockDbFactory( [ 'select' => $dbResult ] ) );
|
2016-12-05 18:51:07 +00:00
|
|
|
$res = $notifMapper->fetchUnreadByUser( $this->mockUser(), 10, null, '', [ 'test_event' ] );
|
2020-01-14 05:09:42 +00:00
|
|
|
$this->assertIsArray( $res );
|
2018-06-17 16:59:03 +00:00
|
|
|
$this->assertNotEmpty( $res );
|
2014-08-14 18:46:26 +00:00
|
|
|
foreach ( $res as $row ) {
|
2022-11-02 21:34:17 +00:00
|
|
|
$this->assertInstanceOf( Notification::class, $row );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
2014-08-14 18:46:26 +00:00
|
|
|
}
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2014-08-14 18:46:26 +00:00
|
|
|
public function testFetchByUser() {
|
2014-07-18 03:58:21 +00:00
|
|
|
// Unsuccessful select
|
2022-11-12 07:19:00 +00:00
|
|
|
$notifMapper = new NotificationMapper( $this->mockDbFactory( [ 'select' => false ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
$res = $notifMapper->fetchByUser( $this->mockUser(), 10, '' );
|
2022-11-24 22:03:18 +00:00
|
|
|
$this->assertSame( [], $res );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
// Successful select
|
2016-12-05 18:51:07 +00:00
|
|
|
$notifDbResult = [
|
|
|
|
(object)[
|
2014-07-18 03:58:21 +00:00
|
|
|
'event_id' => 1,
|
2014-08-14 18:46:26 +00:00
|
|
|
'event_type' => 'test_event',
|
2014-07-18 03:58:21 +00:00
|
|
|
'event_variant' => '',
|
|
|
|
'event_extra' => '',
|
|
|
|
'event_page_id' => '',
|
|
|
|
'event_agent_id' => '',
|
|
|
|
'event_agent_ip' => '',
|
2016-03-04 19:23:02 +00:00
|
|
|
'event_deleted' => 0,
|
2014-07-18 03:58:21 +00:00
|
|
|
'notification_user' => 1,
|
|
|
|
'notification_timestamp' => '20140615101010',
|
|
|
|
'notification_read_timestamp' => '20140616101010',
|
|
|
|
'notification_bundle_hash' => 'testhash',
|
2016-12-05 18:51:07 +00:00
|
|
|
]
|
|
|
|
];
|
2015-03-16 15:47:13 +00:00
|
|
|
|
2022-11-12 07:19:00 +00:00
|
|
|
$notifMapper = new NotificationMapper( $this->mockDbFactory( [ 'select' => $notifDbResult ] ) );
|
2016-12-05 18:51:07 +00:00
|
|
|
$res = $notifMapper->fetchByUser( $this->mockUser(), 10, '', [] );
|
2022-11-24 22:03:18 +00:00
|
|
|
$this->assertSame( [], $res );
|
2014-07-22 21:33:22 +00:00
|
|
|
|
2022-11-02 20:47:04 +00:00
|
|
|
$notifMapper = new NotificationMapper(
|
2022-11-12 07:19:00 +00:00
|
|
|
$this->mockDbFactory( [ 'select' => $notifDbResult ] )
|
2015-03-16 15:47:13 +00:00
|
|
|
);
|
2016-12-05 18:51:07 +00:00
|
|
|
$res = $notifMapper->fetchByUser( $this->mockUser(), 10, '', [ 'test_event' ] );
|
2020-01-14 05:09:42 +00:00
|
|
|
$this->assertIsArray( $res );
|
2018-06-17 16:59:03 +00:00
|
|
|
$this->assertNotEmpty( $res );
|
2014-07-18 03:58:21 +00:00
|
|
|
foreach ( $res as $row ) {
|
2022-11-02 21:34:17 +00:00
|
|
|
$this->assertInstanceOf( Notification::class, $row );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 07:19:00 +00:00
|
|
|
$notifMapper = new NotificationMapper( $this->mockDbFactory( [] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
$res = $notifMapper->fetchByUser( $this->mockUser(), 10, '' );
|
2022-11-24 22:03:18 +00:00
|
|
|
$this->assertSame( [], $res );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
2014-09-09 22:11:53 +00:00
|
|
|
public function testFetchByUserOffset() {
|
|
|
|
// Unsuccessful select
|
2022-11-12 07:19:00 +00:00
|
|
|
$notifMapper = new NotificationMapper( $this->mockDbFactory( [ 'selectRow' => false ] ) );
|
2014-09-09 22:11:53 +00:00
|
|
|
$res = $notifMapper->fetchByUserOffset( User::newFromId( 1 ), 500 );
|
|
|
|
$this->assertFalse( $res );
|
|
|
|
|
|
|
|
// Successful select
|
2016-12-05 18:51:07 +00:00
|
|
|
$dbResult = (object)[
|
2014-09-09 22:11:53 +00:00
|
|
|
'event_id' => 1,
|
|
|
|
'event_type' => 'test',
|
|
|
|
'event_variant' => '',
|
|
|
|
'event_extra' => '',
|
|
|
|
'event_page_id' => '',
|
|
|
|
'event_agent_id' => '',
|
|
|
|
'event_agent_ip' => '',
|
2016-03-04 19:23:02 +00:00
|
|
|
'event_deleted' => 0,
|
2014-09-09 22:11:53 +00:00
|
|
|
'notification_user' => 1,
|
|
|
|
'notification_timestamp' => '20140615101010',
|
|
|
|
'notification_read_timestamp' => '20140616101010',
|
|
|
|
'notification_bundle_hash' => 'testhash',
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2022-11-12 07:19:00 +00:00
|
|
|
$notifMapper = new NotificationMapper( $this->mockDbFactory( [ 'selectRow' => $dbResult ] ) );
|
2016-08-25 23:47:35 +00:00
|
|
|
$row = $notifMapper->fetchByUserOffset( User::newFromId( 1 ), 500 );
|
2022-11-02 21:34:17 +00:00
|
|
|
$this->assertInstanceOf( Notification::class, $row );
|
2014-09-09 22:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteByUserEventOffset() {
|
2018-06-28 18:15:04 +00:00
|
|
|
$this->setMwGlobals( [ 'wgUpdateRowsPerQuery' => 4 ] );
|
2019-10-05 03:54:45 +00:00
|
|
|
$mockDb = $this->createMock( IDatabase::class );
|
2021-05-04 16:06:42 +00:00
|
|
|
$makeResultRows = static function ( $eventIds ) {
|
|
|
|
return new ArrayIterator( array_map( static function ( $eventId ) {
|
2019-04-23 00:16:21 +00:00
|
|
|
return (object)[ 'notification_event' => $eventId ];
|
|
|
|
}, $eventIds ) );
|
|
|
|
};
|
|
|
|
$mockDb->expects( $this->exactly( 4 ) )
|
|
|
|
->method( 'select' )
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
$this->returnValue( $makeResultRows( [ 1, 2, 3, 5 ] ) ),
|
|
|
|
$this->returnValue( $makeResultRows( [ 8, 13, 21, 34 ] ) ),
|
|
|
|
$this->returnValue( $makeResultRows( [ 55, 89 ] ) ),
|
|
|
|
$this->returnValue( $makeResultRows( [] ) )
|
|
|
|
);
|
2018-06-28 18:15:04 +00:00
|
|
|
$mockDb->expects( $this->exactly( 3 ) )
|
2019-04-23 00:16:21 +00:00
|
|
|
->method( 'selectFieldValues' )
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
$this->returnValue( [] ),
|
|
|
|
$this->returnValue( [ 13, 21 ] ),
|
|
|
|
$this->returnValue( [ 55 ] )
|
|
|
|
);
|
|
|
|
$mockDb->expects( $this->exactly( 7 ) )
|
2018-06-28 18:15:04 +00:00
|
|
|
->method( 'delete' )
|
|
|
|
->withConsecutive(
|
|
|
|
[
|
2022-09-29 13:41:35 +00:00
|
|
|
'echo_notification',
|
|
|
|
[ 'notification_user' => 1, 'notification_event' => [ 1, 2, 3, 5 ] ],
|
2018-06-28 18:15:04 +00:00
|
|
|
$this->anything()
|
|
|
|
],
|
|
|
|
[
|
2022-09-29 13:41:35 +00:00
|
|
|
'echo_notification',
|
|
|
|
[ 'notification_user' => 1, 'notification_event' => [ 8, 13, 21, 34 ] ],
|
2018-06-28 18:15:04 +00:00
|
|
|
$this->anything()
|
|
|
|
],
|
2019-04-23 00:16:21 +00:00
|
|
|
[
|
2022-09-29 13:41:35 +00:00
|
|
|
'echo_event',
|
|
|
|
[ 'event_id' => [ 13, 21 ] ],
|
2019-04-23 00:16:21 +00:00
|
|
|
$this->anything()
|
|
|
|
],
|
|
|
|
[
|
2022-09-29 13:41:35 +00:00
|
|
|
'echo_target_page',
|
|
|
|
[ 'etp_event' => [ 13, 21 ] ],
|
2019-04-23 00:16:21 +00:00
|
|
|
$this->anything()
|
|
|
|
],
|
2018-06-28 18:15:04 +00:00
|
|
|
[
|
2022-09-29 13:41:35 +00:00
|
|
|
'echo_notification',
|
|
|
|
[ 'notification_user' => 1, 'notification_event' => [ 55, 89 ] ],
|
2018-06-28 18:15:04 +00:00
|
|
|
$this->anything()
|
2019-04-23 00:16:21 +00:00
|
|
|
],
|
|
|
|
[
|
2022-09-29 13:41:35 +00:00
|
|
|
'echo_event',
|
|
|
|
[ 'event_id' => [ 55 ] ],
|
2019-04-23 00:16:21 +00:00
|
|
|
$this->anything()
|
|
|
|
],
|
|
|
|
[
|
2022-09-29 13:41:35 +00:00
|
|
|
'echo_target_page',
|
|
|
|
[ 'etp_event' => [ 55 ] ],
|
2019-04-23 00:16:21 +00:00
|
|
|
$this->anything()
|
2018-06-28 18:15:04 +00:00
|
|
|
]
|
|
|
|
)
|
2019-04-07 21:44:48 +00:00
|
|
|
->willReturn( true );
|
2014-09-09 22:11:53 +00:00
|
|
|
|
2022-11-12 07:19:00 +00:00
|
|
|
$notifMapper = new NotificationMapper( $this->mockDbFactory( $mockDb ) );
|
2018-06-28 18:15:04 +00:00
|
|
|
$this->assertTrue( $notifMapper->deleteByUserEventOffset( User::newFromId( 1 ), 500 ) );
|
2014-09-09 22:11:53 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
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-02 21:34:17 +00:00
|
|
|
* Mock object of Notification
|
|
|
|
* @return Notification
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
2022-11-02 21:34:17 +00:00
|
|
|
protected function mockNotification() {
|
|
|
|
$event = $this->createMock( Notification::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$event->method( 'toDbArray' )
|
|
|
|
->willReturn( [] );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-12 07:19:00 +00:00
|
|
|
* Mock object of DbFactory
|
|
|
|
*
|
|
|
|
* @param array|IDatabase $dbResultOrMockDb
|
|
|
|
*
|
|
|
|
* @return DbFactory
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
2022-11-12 07:19:00 +00:00
|
|
|
protected function mockDbFactory( $dbResultOrMockDb ) {
|
2018-06-28 18:15:04 +00:00
|
|
|
$mockDb = is_array( $dbResultOrMockDb ) ? $this->mockDb( $dbResultOrMockDb ) : $dbResultOrMockDb;
|
2022-11-12 07:19:00 +00:00
|
|
|
$dbFactory = $this->createMock( DbFactory::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$dbFactory->method( 'getEchoDb' )
|
|
|
|
->willReturn( $mockDb );
|
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
|
2022-11-12 07:19:00 +00:00
|
|
|
*
|
2021-01-23 11:54:27 +00:00
|
|
|
* @param array $dbResult
|
2022-11-12 07:19:00 +00:00
|
|
|
*
|
|
|
|
* @return IDatabase
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
|
|
|
protected function mockDb( array $dbResult ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$dbResult += [
|
2014-07-18 03:58:21 +00:00
|
|
|
'insert' => '',
|
|
|
|
'select' => '',
|
2014-09-09 22:11:53 +00:00
|
|
|
'selectRow' => '',
|
|
|
|
'delete' => ''
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-09-09 22:11:53 +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( 'insert' )
|
|
|
|
->willReturn( $dbResult['insert'] );
|
|
|
|
$db->method( 'select' )
|
|
|
|
->willReturn( $dbResult['select'] );
|
|
|
|
$db->method( 'delete' )
|
|
|
|
->willReturn( $dbResult['delete'] );
|
|
|
|
$db->method( 'selectRow' )
|
|
|
|
->willReturn( $dbResult['selectRow'] );
|
|
|
|
$db->method( 'onTransactionCommitOrIdle' )
|
2014-07-18 03:58:21 +00:00
|
|
|
->will( new EchoExecuteFirstArgumentStub );
|
|
|
|
|
|
|
|
return $db;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|