| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Test\Integration\Mapper; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Extension\Notifications\DbFactory; |
| 7 |
use MediaWiki\Extension\Notifications\Mapper\EventMapper; |
| 8 |
use MediaWiki\Extension\Notifications\Model\Event; |
| 9 |
use MediaWikiIntegrationTestCase; |
| 10 |
use Wikimedia\Rdbms\FakeResultWrapper; |
| 11 |
use Wikimedia\Rdbms\IDatabase; |
| 12 |
use Wikimedia\Rdbms\InsertQueryBuilder; |
| 13 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class EventMapperTest extends MediaWikiIntegrationTestCase { |
| 20 |
|
| 21 |
public static function provideDataTestInsert() { |
| 22 |
return [ |
| 23 |
[ |
| 24 |
'successful insert with next sequence = 1', |
| 25 |
[ 'insert' => true, 'insertId' => 1 ], |
| 26 |
1 |
| 27 |
], |
| 28 |
[ |
| 29 |
'successful insert with insert id = 2', |
| 30 |
[ 'insert' => true, 'insertId' => 2 ], |
| 31 |
2 |
| 32 |
] |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function testInsert( $message, $dbResult, $result ) { |
| 40 |
$event = $this->mockEvent(); |
| 41 |
$eventMapper = new EventMapper( $this->mockDbFactory( $dbResult ) ); |
| 42 |
$this->assertEquals( $result, $eventMapper->insert( $event ), $message ); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function testSuccessfulFetchById() { |
| 49 |
$eventMapper = new EventMapper( |
| 50 |
$this->mockDbFactory( |
| 51 |
[ |
| 52 |
'selectRow' => (object)[ |
| 53 |
'event_id' => 1, |
| 54 |
'event_type' => 'test', |
| 55 |
'event_variant' => '', |
| 56 |
'event_extra' => '', |
| 57 |
'event_page_id' => '', |
| 58 |
'event_agent_id' => '', |
| 59 |
'event_agent_ip' => '', |
| 60 |
'event_deleted' => 0, |
| 61 |
] |
| 62 |
] |
| 63 |
) |
| 64 |
); |
| 65 |
$res = $eventMapper->fetchById( 1 ); |
| 66 |
$this->assertInstanceOf( Event::class, $res ); |
| 67 |
} |
| 68 |
|
| 69 |
public function testUnsuccessfulFetchById() { |
| 70 |
$eventMapper = new EventMapper( |
| 71 |
$this->mockDbFactory( |
| 72 |
[ |
| 73 |
'selectRow' => false |
| 74 |
] |
| 75 |
) |
| 76 |
); |
| 77 |
$this->expectException( InvalidArgumentException::class ); |
| 78 |
$eventMapper->fetchById( 1 ); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
protected function mockEvent() { |
| 85 |
$event = $this->createMock( Event::class ); |
| 86 |
$event->method( 'toDbArray' ) |
| 87 |
->willReturn( [] ); |
| 88 |
|
| 89 |
return $event; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
protected function mockDbFactory( $dbResult ) { |
| 97 |
$dbFactory = $this->createMock( DbFactory::class ); |
| 98 |
$dbFactory->method( 'getEchoDb' ) |
| 99 |
->willReturn( $this->mockDb( $dbResult ) ); |
| 100 |
|
| 101 |
return $dbFactory; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
protected function mockDb( array $dbResult ) { |
| 109 |
$dbResult += [ |
| 110 |
'insert' => '', |
| 111 |
'insertId' => '', |
| 112 |
'select' => [], |
| 113 |
'selectRow' => '' |
| 114 |
]; |
| 115 |
$db = $this->createMock( IDatabase::class ); |
| 116 |
$db->method( 'insert' ) |
| 117 |
->willReturn( $dbResult['insert'] ); |
| 118 |
$db->method( 'insertId' ) |
| 119 |
->willReturn( $dbResult['insertId'] ); |
| 120 |
$db->method( 'select' ) |
| 121 |
->willReturn( new FakeResultWrapper( $dbResult['select'] ) ); |
| 122 |
$db->method( 'selectRow' ) |
| 123 |
->willReturn( $dbResult['selectRow'] ); |
| 124 |
$db->method( 'newInsertQueryBuilder' ) |
| 125 |
->willReturnCallback( static function () use ( $db ) { |
| 126 |
return new InsertQueryBuilder( $db ); |
| 127 |
} ); |
| 128 |
$db->method( 'newSelectQueryBuilder' ) |
| 129 |
->willReturnCallback( static function () use ( $db ) { |
| 130 |
return new SelectQueryBuilder( $db ); |
| 131 |
} ); |
| 132 |
|
| 133 |
return $db; |
| 134 |
} |
| 135 |
|
| 136 |
public function testFetchByPage() { |
| 137 |
$user = $this->getTestUser()->getUser(); |
| 138 |
|
| 139 |
$this->clearHook( 'PageSaveComplete' ); |
| 140 |
$page = $this->getExistingTestPage(); |
| 141 |
|
| 142 |
|
| 143 |
Event::create( [ |
| 144 |
'type' => 'welcome', |
| 145 |
'agent' => $user, |
| 146 |
] ); |
| 147 |
|
| 148 |
|
| 149 |
$eventWithTitle = Event::create( [ |
| 150 |
'type' => 'welcome', |
| 151 |
'agent' => $user, |
| 152 |
'title' => $page->getTitle() |
| 153 |
] ); |
| 154 |
|
| 155 |
|
| 156 |
$eventWithTargetPage = Event::create( [ |
| 157 |
'type' => 'welcome', |
| 158 |
'agent' => $user, |
| 159 |
'extra' => [ 'target-page' => $page->getId() ] |
| 160 |
] ); |
| 161 |
|
| 162 |
$eventMapper = new EventMapper(); |
| 163 |
|
| 164 |
$this->assertArrayEquals( |
| 165 |
[ $eventWithTitle->getId(), $eventWithTargetPage->getId() ], |
| 166 |
$eventMapper->fetchIdsByPage( $page->getId() ) |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
} |