2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class EchoNotificationMapperTest extends MediaWikiTestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
2016-12-05 18:51:07 +00:00
|
|
|
$notifMapper = new EchoNotificationMapper( $this->mockMWEchoDbFactory( [ 'select' => false ] ) );
|
2015-12-09 18:41:40 +00:00
|
|
|
$res = $notifMapper->fetchUnreadByUser( $this->mockUser(), 10, null, '' );
|
2014-08-14 18:46:26 +00:00
|
|
|
$this->assertEmpty( $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',
|
|
|
|
'notification_read_timestamp' => '',
|
|
|
|
'notification_bundle_base' => 1,
|
|
|
|
'notification_bundle_hash' => 'testhash',
|
|
|
|
'notification_bundle_display_hash' => 'testdisplayhash'
|
2016-12-05 18:51:07 +00:00
|
|
|
]
|
|
|
|
];
|
|
|
|
$notifMapper = new EchoNotificationMapper( $this->mockMWEchoDbFactory( [ 'select' => $dbResult ] ) );
|
|
|
|
$res = $notifMapper->fetchUnreadByUser( $this->mockUser(), 10, null, '', [] );
|
2015-10-01 13:48:52 +00:00
|
|
|
$this->assertEmpty( $res );
|
2014-08-14 18:46:26 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$notifMapper = new EchoNotificationMapper( $this->mockMWEchoDbFactory( [ 'select' => $dbResult ] ) );
|
|
|
|
$res = $notifMapper->fetchUnreadByUser( $this->mockUser(), 10, null, '', [ 'test_event' ] );
|
2014-08-14 18:46:26 +00:00
|
|
|
$this->assertTrue( is_array( $res ) );
|
|
|
|
$this->assertGreaterThan( 0, count( $res ) );
|
|
|
|
foreach ( $res as $row ) {
|
2015-10-01 13:48:52 +00:00
|
|
|
$this->assertInstanceOf( 'EchoNotification', $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
|
2016-12-05 18:51:07 +00:00
|
|
|
$notifMapper = new EchoNotificationMapper( $this->mockMWEchoDbFactory( [ 'select' => false ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
$res = $notifMapper->fetchByUser( $this->mockUser(), 10, '' );
|
|
|
|
$this->assertEmpty( $res );
|
|
|
|
|
|
|
|
// 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_base' => 1,
|
|
|
|
'notification_bundle_hash' => 'testhash',
|
|
|
|
'notification_bundle_display_hash' => 'testdisplayhash'
|
2016-12-05 18:51:07 +00:00
|
|
|
]
|
|
|
|
];
|
2015-03-16 15:47:13 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$tpDbResult = [
|
|
|
|
(object)[
|
2015-03-16 15:47:13 +00:00
|
|
|
'etp_page' => 7, // pageid
|
|
|
|
'etp_event' => 1, // eventid
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
];
|
2015-03-16 15:47:13 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$notifMapper = new EchoNotificationMapper( $this->mockMWEchoDbFactory( [ 'select' => $notifDbResult ] ) );
|
|
|
|
$res = $notifMapper->fetchByUser( $this->mockUser(), 10, '', [] );
|
2015-10-01 13:48:52 +00:00
|
|
|
$this->assertEmpty( $res );
|
2014-07-22 21:33:22 +00:00
|
|
|
|
2015-03-16 15:47:13 +00:00
|
|
|
$notifMapper = new EchoNotificationMapper(
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->mockMWEchoDbFactory( [ 'select' => $notifDbResult ] ),
|
2015-03-16 15:47:13 +00:00
|
|
|
new EchoTargetPageMapper(
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->mockMWEchoDbFactory( [ 'select' => $tpDbResult ] )
|
2015-03-16 15:47:13 +00:00
|
|
|
)
|
|
|
|
);
|
2016-12-05 18:51:07 +00:00
|
|
|
$res = $notifMapper->fetchByUser( $this->mockUser(), 10, '', [ 'test_event' ] );
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->assertTrue( is_array( $res ) );
|
2014-07-22 21:33:22 +00:00
|
|
|
$this->assertGreaterThan( 0, count( $res ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
foreach ( $res as $row ) {
|
2015-10-01 13:48:52 +00:00
|
|
|
$this->assertInstanceOf( 'EchoNotification', $row );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$notifMapper = new EchoNotificationMapper( $this->mockMWEchoDbFactory( [] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
$res = $notifMapper->fetchByUser( $this->mockUser(), 10, '' );
|
|
|
|
$this->assertEmpty( $res );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFetchNewestByUserBundleHash() {
|
|
|
|
// Unsuccessful select
|
2016-12-05 18:51:07 +00:00
|
|
|
$notifMapper = new EchoNotificationMapper( $this->mockMWEchoDbFactory( [ 'selectRow' => false ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
$res = $notifMapper->fetchNewestByUserBundleHash( User::newFromId( 1 ), 'testhash' );
|
|
|
|
$this->assertFalse( $res );
|
|
|
|
|
|
|
|
// Successful select
|
2016-12-05 18:51:07 +00:00
|
|
|
$dbResult = (object)[
|
2014-07-18 03:58:21 +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-07-18 03:58:21 +00:00
|
|
|
'notification_user' => 1,
|
|
|
|
'notification_timestamp' => '20140615101010',
|
|
|
|
'notification_read_timestamp' => '20140616101010',
|
|
|
|
'notification_bundle_base' => 1,
|
|
|
|
'notification_bundle_hash' => 'testhash',
|
|
|
|
'notification_bundle_display_hash' => 'testdisplayhash'
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
|
|
|
$notifMapper = new EchoNotificationMapper( $this->mockMWEchoDbFactory( [ 'selectRow' => $dbResult ] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
$row = $notifMapper->fetchNewestByUserBundleHash( User::newFromId( 1 ), 'testdisplayhash' );
|
|
|
|
$this->assertInstanceOf( 'EchoNotification', $row );
|
|
|
|
}
|
|
|
|
|
2014-09-09 22:11:53 +00:00
|
|
|
public function testFetchByUserOffset() {
|
|
|
|
// Unsuccessful select
|
2016-12-05 18:51:07 +00:00
|
|
|
$notifMapper = new EchoNotificationMapper( $this->mockMWEchoDbFactory( [ '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_base' => 1,
|
|
|
|
'notification_bundle_hash' => 'testhash',
|
|
|
|
'notification_bundle_display_hash' => 'testdisplayhash'
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
|
|
|
$notifMapper = new EchoNotificationMapper( $this->mockMWEchoDbFactory( [ 'selectRow' => $dbResult ] ) );
|
2016-08-25 23:47:35 +00:00
|
|
|
$row = $notifMapper->fetchByUserOffset( User::newFromId( 1 ), 500 );
|
2014-09-09 22:11:53 +00:00
|
|
|
$this->assertInstanceOf( 'EchoNotification', $row );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteByUserEventOffset() {
|
2016-12-05 18:51:07 +00:00
|
|
|
$dbResult = [ 'delete' => true ];
|
2014-09-09 22:11:53 +00:00
|
|
|
$notifMapper = new EchoNotificationMapper( $this->mockMWEchoDbFactory( $dbResult ) );
|
|
|
|
$this->assertTrue( $notifMapper->deleteByUserEventOffset( User::newFromId( 1 ), 500 ) );
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$dbResult = [ 'delete' => false ];
|
2014-09-09 22:11:53 +00:00
|
|
|
$notifMapper = new EchoNotificationMapper( $this->mockMWEchoDbFactory( $dbResult ) );
|
|
|
|
$this->assertFalse( $notifMapper->deleteByUserEventOffset( User::newFromId( 1 ), 500 ) );
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Mock object of User
|
|
|
|
*/
|
|
|
|
protected function mockUser() {
|
|
|
|
$user = $this->getMockBuilder( 'User' )
|
|
|
|
->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( [ 'echo_group' ] ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock object of EchoNotification
|
|
|
|
*/
|
|
|
|
protected function mockEchoNotification() {
|
|
|
|
$event = $this->getMockBuilder( 'EchoNotification' )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'toDbArray' )
|
2016-12-05 18:51:07 +00:00
|
|
|
->will( $this->returnValue( [] ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock object of MWEchoDbFactory
|
|
|
|
*/
|
|
|
|
protected function mockMWEchoDbFactory( $dbResult ) {
|
|
|
|
$dbFactory = $this->getMockBuilder( 'MWEchoDbFactory' )
|
|
|
|
->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
|
|
|
*/
|
|
|
|
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
|
|
|
|
2018-01-16 18:58:52 +00:00
|
|
|
$db = $this->getMockBuilder( 'DatabaseMysqli' )
|
2014-07-18 03:58:21 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$db->expects( $this->any() )
|
|
|
|
->method( 'insert' )
|
|
|
|
->will( $this->returnValue( $dbResult['insert'] ) );
|
|
|
|
$db->expects( $this->any() )
|
|
|
|
->method( 'select' )
|
|
|
|
->will( $this->returnValue( $dbResult['select'] ) );
|
2014-09-09 22:11:53 +00:00
|
|
|
$db->expects( $this->any() )
|
|
|
|
->method( 'delete' )
|
|
|
|
->will( $this->returnValue( $dbResult['delete'] ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
$db->expects( $this->any() )
|
|
|
|
->method( 'selectRow' )
|
|
|
|
->will( $this->returnValue( $dbResult['selectRow'] ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
$db->expects( $this->any() )
|
2014-07-18 03:58:21 +00:00
|
|
|
->method( 'onTransactionIdle' )
|
|
|
|
->will( new EchoExecuteFirstArgumentStub );
|
|
|
|
|
|
|
|
return $db;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|