| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Test\Integration\Mapper; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Notifications\DbFactory; |
| 6 |
use MediaWiki\Extension\Notifications\Mapper\NotificationMapper; |
| 7 |
use MediaWiki\Extension\Notifications\Model\Notification; |
| 8 |
use MediaWiki\MainConfigNames; |
| 9 |
use MediaWiki\User\User; |
| 10 |
use MediaWikiIntegrationTestCase; |
| 11 |
use Wikimedia\Rdbms\DeleteQueryBuilder; |
| 12 |
use Wikimedia\Rdbms\FakeResultWrapper; |
| 13 |
use Wikimedia\Rdbms\IDatabase; |
| 14 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class NotificationMapperTest extends MediaWikiIntegrationTestCase { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function testInsert() { |
| 25 |
$this->assertTrue( true ); |
| 26 |
} |
| 27 |
|
| 28 |
public function fetchUnreadByUser( User $user, $limit, array $eventTypes = [] ) { |
| 29 |
$dbResult = [ |
| 30 |
(object)[ |
| 31 |
'event_id' => 1, |
| 32 |
'event_type' => 'test_event', |
| 33 |
'event_variant' => '', |
| 34 |
'event_extra' => '', |
| 35 |
'event_page_id' => '', |
| 36 |
'event_agent_id' => '', |
| 37 |
'event_agent_ip' => '', |
| 38 |
'notification_user' => 1, |
| 39 |
'notification_timestamp' => '20140615101010', |
| 40 |
'notification_read_timestamp' => null, |
| 41 |
'notification_bundle_hash' => 'testhash', |
| 42 |
] |
| 43 |
]; |
| 44 |
$notifMapper = new NotificationMapper( $this->mockDbFactory( [ 'select' => $dbResult ] ) ); |
| 45 |
$res = $notifMapper->fetchUnreadByUser( $this->mockUser(), 10, null, '', [] ); |
| 46 |
$this->assertSame( [], $res ); |
| 47 |
|
| 48 |
$notifMapper = new NotificationMapper( $this->mockDbFactory( [ 'select' => $dbResult ] ) ); |
| 49 |
$res = $notifMapper->fetchUnreadByUser( $this->mockUser(), 10, null, '', [ 'test_event' ] ); |
| 50 |
$this->assertIsArray( $res ); |
| 51 |
$this->assertNotEmpty( $res ); |
| 52 |
foreach ( $res as $row ) { |
| 53 |
$this->assertInstanceOf( Notification::class, $row ); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public function testFetchByUser() { |
| 58 |
$notifDbResult = [ |
| 59 |
(object)[ |
| 60 |
'event_id' => 1, |
| 61 |
'event_type' => 'test_event', |
| 62 |
'event_variant' => '', |
| 63 |
'event_extra' => '', |
| 64 |
'event_page_id' => '', |
| 65 |
'event_agent_id' => '', |
| 66 |
'event_agent_ip' => '', |
| 67 |
'event_deleted' => 0, |
| 68 |
'notification_user' => 1, |
| 69 |
'notification_timestamp' => '20140615101010', |
| 70 |
'notification_read_timestamp' => '20140616101010', |
| 71 |
'notification_bundle_hash' => 'testhash', |
| 72 |
] |
| 73 |
]; |
| 74 |
|
| 75 |
$notifMapper = new NotificationMapper( $this->mockDbFactory( [ 'select' => $notifDbResult ] ) ); |
| 76 |
$res = $notifMapper->fetchByUser( $this->mockUser(), 10, '', [] ); |
| 77 |
$this->assertSame( [], $res ); |
| 78 |
|
| 79 |
$notifMapper = new NotificationMapper( |
| 80 |
$this->mockDbFactory( [ 'select' => $notifDbResult ] ) |
| 81 |
); |
| 82 |
$res = $notifMapper->fetchByUser( $this->mockUser(), 10, '', [ 'test_event' ] ); |
| 83 |
$this->assertIsArray( $res ); |
| 84 |
$this->assertNotEmpty( $res ); |
| 85 |
foreach ( $res as $row ) { |
| 86 |
$this->assertInstanceOf( Notification::class, $row ); |
| 87 |
} |
| 88 |
|
| 89 |
$notifMapper = new NotificationMapper( $this->mockDbFactory( [] ) ); |
| 90 |
$res = $notifMapper->fetchByUser( $this->mockUser(), 10, '' ); |
| 91 |
$this->assertSame( [], $res ); |
| 92 |
} |
| 93 |
|
| 94 |
public function testFetchByUserOffset() { |
| 95 |
|
| 96 |
$notifMapper = new NotificationMapper( $this->mockDbFactory( [ 'selectRow' => false ] ) ); |
| 97 |
$res = $notifMapper->fetchByUserOffset( User::newFromId( 1 ), 500 ); |
| 98 |
$this->assertFalse( $res ); |
| 99 |
|
| 100 |
|
| 101 |
$dbResult = (object)[ |
| 102 |
'event_id' => 1, |
| 103 |
'event_type' => 'test', |
| 104 |
'event_variant' => '', |
| 105 |
'event_extra' => '', |
| 106 |
'event_page_id' => '', |
| 107 |
'event_agent_id' => '', |
| 108 |
'event_agent_ip' => '', |
| 109 |
'event_deleted' => 0, |
| 110 |
'notification_user' => 1, |
| 111 |
'notification_timestamp' => '20140615101010', |
| 112 |
'notification_read_timestamp' => '20140616101010', |
| 113 |
'notification_bundle_hash' => 'testhash', |
| 114 |
]; |
| 115 |
$notifMapper = new NotificationMapper( $this->mockDbFactory( [ 'selectRow' => $dbResult ] ) ); |
| 116 |
$row = $notifMapper->fetchByUserOffset( User::newFromId( 1 ), 500 ); |
| 117 |
$this->assertInstanceOf( Notification::class, $row ); |
| 118 |
} |
| 119 |
|
| 120 |
public function testDeleteByUserEventOffset() { |
| 121 |
$this->overrideConfigValue( MainConfigNames::UpdateRowsPerQuery, 4 ); |
| 122 |
$mockDb = $this->createMock( IDatabase::class ); |
| 123 |
$makeResultRows = static function ( $eventIds ) { |
| 124 |
return new FakeResultWrapper( array_map( static function ( $eventId ) { |
| 125 |
return (object)[ 'notification_event' => $eventId ]; |
| 126 |
}, $eventIds ) ); |
| 127 |
}; |
| 128 |
$mockDb->expects( $this->exactly( 4 ) ) |
| 129 |
->method( 'select' ) |
| 130 |
->willReturnOnConsecutiveCalls( |
| 131 |
$this->returnValue( $makeResultRows( [ 1, 2, 3, 5 ] ) ), |
| 132 |
$this->returnValue( $makeResultRows( [ 8, 13, 21, 34 ] ) ), |
| 133 |
$this->returnValue( $makeResultRows( [ 55, 89 ] ) ), |
| 134 |
$this->returnValue( $makeResultRows( [] ) ) |
| 135 |
); |
| 136 |
$mockDb->expects( $this->exactly( 3 ) ) |
| 137 |
->method( 'selectFieldValues' ) |
| 138 |
->willReturnOnConsecutiveCalls( |
| 139 |
$this->returnValue( [] ), |
| 140 |
$this->returnValue( [ 13, 21 ] ), |
| 141 |
$this->returnValue( [ 55 ] ) |
| 142 |
); |
| 143 |
$expectedArgs = [ |
| 144 |
[ |
| 145 |
'echo_notification', |
| 146 |
[ 'notification_user' => 1, 'notification_event' => [ 1, 2, 3, 5 ] ], |
| 147 |
], |
| 148 |
[ |
| 149 |
'echo_notification', |
| 150 |
[ 'notification_user' => 1, 'notification_event' => [ 8, 13, 21, 34 ] ], |
| 151 |
], |
| 152 |
[ |
| 153 |
'echo_event', |
| 154 |
[ 'event_id' => [ 13, 21 ] ], |
| 155 |
], |
| 156 |
[ |
| 157 |
'echo_target_page', |
| 158 |
[ 'etp_event' => [ 13, 21 ] ], |
| 159 |
], |
| 160 |
[ |
| 161 |
'echo_notification', |
| 162 |
[ 'notification_user' => 1, 'notification_event' => [ 55, 89 ] ], |
| 163 |
], |
| 164 |
[ |
| 165 |
'echo_event', |
| 166 |
[ 'event_id' => [ 55 ] ], |
| 167 |
], |
| 168 |
[ |
| 169 |
'echo_target_page', |
| 170 |
[ 'etp_event' => [ 55 ] ], |
| 171 |
] |
| 172 |
]; |
| 173 |
$mockDb->expects( $this->exactly( count( $expectedArgs ) ) ) |
| 174 |
->method( 'delete' ) |
| 175 |
->willReturnCallback( function ( $table, $conds ) use ( &$expectedArgs ): bool { |
| 176 |
$this->assertSame( array_shift( $expectedArgs ), [ $table, $conds ] ); |
| 177 |
return true; |
| 178 |
} ); |
| 179 |
$mockDb->method( 'newDeleteQueryBuilder' ) |
| 180 |
->willReturnCallback( static function () use ( $mockDb ) { |
| 181 |
return new DeleteQueryBuilder( $mockDb ); |
| 182 |
} ); |
| 183 |
$mockDb->method( 'newSelectQueryBuilder' ) |
| 184 |
->willReturnCallback( static function () use ( $mockDb ) { |
| 185 |
return new SelectQueryBuilder( $mockDb ); |
| 186 |
} ); |
| 187 |
|
| 188 |
$notifMapper = new NotificationMapper( $this->mockDbFactory( $mockDb ) ); |
| 189 |
$this->assertTrue( $notifMapper->deleteByUserEventOffset( User::newFromId( 1 ), 500 ) ); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
protected function mockUser() { |
| 197 |
$user = $this->createMock( User::class ); |
| 198 |
$user->method( 'getID' ) |
| 199 |
->willReturn( 1 ); |
| 200 |
|
| 201 |
return $user; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
protected function mockNotification() { |
| 209 |
$event = $this->createMock( Notification::class ); |
| 210 |
$event->method( 'toDbArray' ) |
| 211 |
->willReturn( [] ); |
| 212 |
|
| 213 |
return $event; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
protected function mockDbFactory( $dbResultOrMockDb ) { |
| 224 |
$mockDb = is_array( $dbResultOrMockDb ) ? $this->mockDb( $dbResultOrMockDb ) : $dbResultOrMockDb; |
| 225 |
$dbFactory = $this->createMock( DbFactory::class ); |
| 226 |
$dbFactory->method( 'getEchoDb' ) |
| 227 |
->willReturn( $mockDb ); |
| 228 |
|
| 229 |
return $dbFactory; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
protected function mockDb( array $dbResult ) { |
| 240 |
$dbResult += [ |
| 241 |
'insert' => '', |
| 242 |
'select' => [], |
| 243 |
'selectRow' => '', |
| 244 |
'delete' => '' |
| 245 |
]; |
| 246 |
|
| 247 |
$db = $this->createMock( IDatabase::class ); |
| 248 |
$db->method( 'insert' ) |
| 249 |
->willReturn( $dbResult['insert'] ); |
| 250 |
$db->method( 'select' ) |
| 251 |
->willReturn( new FakeResultWrapper( $dbResult['select'] ) ); |
| 252 |
$db->method( 'delete' ) |
| 253 |
->willReturn( $dbResult['delete'] ); |
| 254 |
$db->method( 'selectRow' ) |
| 255 |
->willReturn( $dbResult['selectRow'] ); |
| 256 |
$db->method( 'onTransactionCommitOrIdle' ) |
| 257 |
->will( new EchoExecuteFirstArgumentStub ); |
| 258 |
$db->method( 'newSelectQueryBuilder' ) |
| 259 |
->willReturnCallback( static function () use ( $db ) { |
| 260 |
return new SelectQueryBuilder( $db ); |
| 261 |
} ); |
| 262 |
|
| 263 |
return $db; |
| 264 |
} |
| 265 |
|
| 266 |
} |