2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-22 15:31:00 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Test\Integration\Mapper;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
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\EventMapper;
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
2024-02-22 15:31:00 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
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\InsertQueryBuilder;
|
2024-04-27 21:37:18 +00:00
|
|
|
use Wikimedia\Rdbms\SelectQueryBuilder;
|
2019-10-05 03:54:45 +00:00
|
|
|
|
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
|
|
|
|
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-12 07:19:00 +00:00
|
|
|
$eventMapper = new EventMapper( $this->mockDbFactory( $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(
|
2022-11-12 07:19:00 +00:00
|
|
|
$this->mockDbFactory(
|
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(
|
2022-11-12 07:19:00 +00:00
|
|
|
$this->mockDbFactory(
|
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
|
|
|
)
|
|
|
|
);
|
2023-06-09 00:21:09 +00:00
|
|
|
$this->expectException( InvalidArgumentException::class );
|
2019-02-19 10:39:13 +00:00
|
|
|
$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
|
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( $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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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' => '',
|
2024-04-19 19:14:11 +00:00
|
|
|
'select' => [],
|
2014-07-18 03:58:21 +00:00
|
|
|
'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' )
|
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'] );
|
2024-04-13 18:33:56 +00:00
|
|
|
$db->method( 'newInsertQueryBuilder' )
|
|
|
|
->willReturnCallback( static function () use ( $db ) {
|
|
|
|
return new InsertQueryBuilder( $db );
|
|
|
|
} );
|
2024-04-27 21:37:18 +00:00
|
|
|
$db->method( 'newSelectQueryBuilder' )
|
|
|
|
->willReturnCallback( static function () use ( $db ) {
|
|
|
|
return new SelectQueryBuilder( $db );
|
|
|
|
} );
|
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();
|
2023-08-18 13:49:23 +00:00
|
|
|
// Do not create a notification for the edit made by getExistingTestPage.
|
|
|
|
$this->clearHook( 'PageSaveComplete' );
|
2019-04-10 20:09:40 +00:00
|
|
|
$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
|
|
|
}
|