2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-02 20:47:04 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Mapper\EventMapper;
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
2019-10-05 03:54:45 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
|
2018-01-24 00:31:53 +00:00
|
|
|
/**
|
2019-04-10 20:09:40 +00:00
|
|
|
* @group Database
|
2022-11-02 20:47:04 +00:00
|
|
|
* @covers \MediaWiki\Extension\Notifications\Mapper\EventMapper
|
2018-01-24 00:31:53 +00:00
|
|
|
*/
|
2022-11-02 20:47:04 +00:00
|
|
|
class EventMapperTest extends MediaWikiIntegrationTestCase {
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2021-07-23 21:23:16 +00:00
|
|
|
protected function setUp(): void {
|
2019-04-10 20:09:40 +00:00
|
|
|
parent::setUp();
|
|
|
|
$this->tablesUsed[] = 'echo_event';
|
|
|
|
$this->tablesUsed[] = 'echo_notification';
|
|
|
|
$this->tablesUsed[] = 'echo_target_page';
|
|
|
|
}
|
|
|
|
|
2023-05-20 18:35:53 +00:00
|
|
|
public static function provideDataTestInsert() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
'successful insert with next sequence = 1',
|
2017-09-06 16:47:22 +00:00
|
|
|
[ 'insert' => true, 'insertId' => 1 ],
|
2014-07-18 03:58:21 +00:00
|
|
|
1
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
'successful insert with insert id = 2',
|
2017-09-06 16:47:22 +00:00
|
|
|
[ 'insert' => true, 'insertId' => 2 ],
|
2014-07-18 03:58:21 +00:00
|
|
|
2
|
2018-10-26 21:36:39 +00:00
|
|
|
]
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideDataTestInsert
|
|
|
|
*/
|
|
|
|
public function testInsert( $message, $dbResult, $result ) {
|
2022-11-02 21:34:17 +00:00
|
|
|
$event = $this->mockEvent();
|
2022-11-02 20:47:04 +00:00
|
|
|
$eventMapper = new EventMapper( $this->mockMWEchoDbFactory( $dbResult ) );
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->assertEquals( $result, $eventMapper->insert( $event ), $message );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Successful fetchById()
|
|
|
|
*/
|
|
|
|
public function testSuccessfulFetchById() {
|
2022-11-02 20:47:04 +00:00
|
|
|
$eventMapper = new EventMapper(
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->mockMWEchoDbFactory(
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
|
|
|
'selectRow' => (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' => '',
|
2016-03-04 19:23:02 +00:00
|
|
|
'event_agent_ip' => '',
|
|
|
|
'event_deleted' => 0,
|
2016-12-05 18:51:07 +00:00
|
|
|
]
|
|
|
|
]
|
2014-07-18 03:58:21 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$res = $eventMapper->fetchById( 1 );
|
2022-11-02 21:34:17 +00:00
|
|
|
$this->assertInstanceOf( Event::class, $res );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnsuccessfulFetchById() {
|
2022-11-02 20:47:04 +00:00
|
|
|
$eventMapper = new EventMapper(
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->mockMWEchoDbFactory(
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
'selectRow' => false
|
2016-12-05 18:51:07 +00:00
|
|
|
]
|
2014-07-18 03:58:21 +00:00
|
|
|
)
|
|
|
|
);
|
2019-02-19 10:39:13 +00:00
|
|
|
$this->expectException( MWException::class );
|
|
|
|
$eventMapper->fetchById( 1 );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-02 21:34:17 +00:00
|
|
|
* @return Event
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
2022-11-02 21:34:17 +00:00
|
|
|
protected function mockEvent() {
|
|
|
|
$event = $this->createMock( Event::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-23 11:54:27 +00:00
|
|
|
* @param array $dbResult
|
2019-02-19 10:40:12 +00:00
|
|
|
* @return MWEchoDbFactory
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
|
|
|
protected function mockMWEchoDbFactory( $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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-23 11:54:27 +00:00
|
|
|
* @param array $dbResult
|
2019-10-05 03:54:45 +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' => '',
|
|
|
|
'insertId' => '',
|
|
|
|
'select' => '',
|
|
|
|
'selectRow' => ''
|
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( 'insert' )
|
|
|
|
->willReturn( $dbResult['insert'] );
|
|
|
|
$db->method( 'insertId' )
|
|
|
|
->willReturn( $dbResult['insertId'] );
|
|
|
|
$db->method( 'select' )
|
|
|
|
->willReturn( $dbResult['select'] );
|
|
|
|
$db->method( 'selectRow' )
|
|
|
|
->willReturn( $dbResult['selectRow'] );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $db;
|
|
|
|
}
|
|
|
|
|
2019-04-10 20:09:40 +00:00
|
|
|
public function testFetchByPage() {
|
|
|
|
$user = $this->getTestUser()->getUser();
|
|
|
|
$page = $this->getExistingTestPage();
|
|
|
|
|
|
|
|
// Create a notification that is not associated with any page
|
2022-11-02 21:34:17 +00:00
|
|
|
Event::create( [
|
2019-04-10 20:09:40 +00:00
|
|
|
'type' => 'welcome',
|
|
|
|
'agent' => $user,
|
|
|
|
] );
|
|
|
|
|
|
|
|
// Create a notification with a title
|
2022-11-02 21:34:17 +00:00
|
|
|
$eventWithTitle = Event::create( [
|
2019-04-10 20:09:40 +00:00
|
|
|
'type' => 'welcome',
|
|
|
|
'agent' => $user,
|
2019-04-17 15:46:06 +00:00
|
|
|
'title' => $page->getTitle()
|
2019-04-10 20:09:40 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
// Create a notification with a target-page
|
2022-11-02 21:34:17 +00:00
|
|
|
$eventWithTargetPage = Event::create( [
|
2019-04-10 20:09:40 +00:00
|
|
|
'type' => 'welcome',
|
|
|
|
'agent' => $user,
|
|
|
|
'extra' => [ 'target-page' => $page->getId() ]
|
|
|
|
] );
|
|
|
|
|
2022-11-02 20:47:04 +00:00
|
|
|
$eventMapper = new EventMapper();
|
2019-04-10 20:09:40 +00:00
|
|
|
|
|
|
|
$this->assertArrayEquals(
|
|
|
|
[ $eventWithTitle->getId(), $eventWithTargetPage->getId() ],
|
|
|
|
$eventMapper->fetchIdsByPage( $page->getId() )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|