2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
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
|
2018-01-24 00:31:53 +00:00
|
|
|
* @covers EchoEventMapper
|
|
|
|
*/
|
2014-07-18 03:58:21 +00:00
|
|
|
class EchoEventMapperTest extends MediaWikiTestCase {
|
|
|
|
|
2019-10-09 22:57:35 +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';
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
public 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 ) {
|
|
|
|
$event = $this->mockEchoEvent();
|
|
|
|
$eventMapper = new EchoEventMapper( $this->mockMWEchoDbFactory( $dbResult ) );
|
|
|
|
$this->assertEquals( $result, $eventMapper->insert( $event ), $message );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Successful fetchById()
|
|
|
|
*/
|
|
|
|
public function testSuccessfulFetchById() {
|
|
|
|
$eventMapper = new EchoEventMapper(
|
|
|
|
$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 );
|
2019-02-19 10:35:54 +00:00
|
|
|
$this->assertInstanceOf( EchoEvent::class, $res );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnsuccessfulFetchById() {
|
|
|
|
$eventMapper = new EchoEventMapper(
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-19 10:40:12 +00:00
|
|
|
* @return EchoEvent
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
|
|
|
protected function mockEchoEvent() {
|
2019-02-19 10:35:54 +00:00
|
|
|
$event = $this->getMockBuilder( EchoEvent::class )
|
2014-07-18 03:58:21 +00:00
|
|
|
->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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-19 10:40:12 +00:00
|
|
|
* @return MWEchoDbFactory
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
|
|
|
protected function mockMWEchoDbFactory( $dbResult ) {
|
2019-02-19 10:35:54 +00:00
|
|
|
$dbFactory = $this->getMockBuilder( MWEchoDbFactory::class )
|
2014-07-18 03:58:21 +00:00
|
|
|
->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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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 );
|
2014-07-18 03:58:21 +00:00
|
|
|
$db->expects( $this->any() )
|
|
|
|
->method( 'insert' )
|
|
|
|
->will( $this->returnValue( $dbResult['insert'] ) );
|
|
|
|
$db->expects( $this->any() )
|
|
|
|
->method( 'insertId' )
|
|
|
|
->will( $this->returnValue( $dbResult['insertId'] ) );
|
|
|
|
$db->expects( $this->any() )
|
|
|
|
->method( 'select' )
|
|
|
|
->will( $this->returnValue( $dbResult['select'] ) );
|
|
|
|
$db->expects( $this->any() )
|
|
|
|
->method( 'selectRow' )
|
|
|
|
->will( $this->returnValue( $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
|
|
|
/**
|
|
|
|
* @covers EchoEventMapper::fetchIdsByPage
|
|
|
|
*/
|
|
|
|
public function testFetchByPage() {
|
|
|
|
$user = $this->getTestUser()->getUser();
|
|
|
|
$page = $this->getExistingTestPage();
|
|
|
|
|
|
|
|
// Create a notification that is not associated with any page
|
|
|
|
EchoEvent::create( [
|
|
|
|
'type' => 'welcome',
|
|
|
|
'agent' => $user,
|
|
|
|
] );
|
|
|
|
|
|
|
|
// Create a notification with a title
|
|
|
|
$eventWithTitle = EchoEvent::create( [
|
|
|
|
'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
|
|
|
|
$eventWithTargetPage = EchoEvent::create( [
|
|
|
|
'type' => 'welcome',
|
|
|
|
'agent' => $user,
|
|
|
|
'extra' => [ 'target-page' => $page->getId() ]
|
|
|
|
] );
|
|
|
|
|
|
|
|
$eventMapper = new EchoEventMapper();
|
|
|
|
|
|
|
|
$this->assertArrayEquals(
|
|
|
|
[ $eventWithTitle->getId(), $eventWithTargetPage->getId() ],
|
|
|
|
$eventMapper->fetchIdsByPage( $page->getId() )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|