| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Test\Controller; |
| 4 |
|
| 5 |
use MapCacheLRU; |
| 6 |
use MediaWiki\Extension\Notifications\AttributeManager; |
| 7 |
use MediaWiki\Extension\Notifications\Controller\NotificationController; |
| 8 |
use MediaWiki\Extension\Notifications\Model\Event; |
| 9 |
use MediaWiki\Extension\Notifications\UserLocator; |
| 10 |
use MediaWiki\Title\Title; |
| 11 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 12 |
use MediaWiki\User\User; |
| 13 |
use MediaWiki\Utils\MWTimestamp; |
| 14 |
use MediaWikiIntegrationTestCase; |
| 15 |
use Wikimedia\TestingAccessWrapper; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class NotificationControllerTest extends MediaWikiIntegrationTestCase { |
| 21 |
|
| 22 |
public static function evaluateUserLocatorsProvider() { |
| 23 |
return [ |
| 24 |
[ |
| 25 |
'With no options no users are notified', |
| 26 |
|
| 27 |
[], |
| 28 |
|
| 29 |
[], |
| 30 |
], |
| 31 |
|
| 32 |
[ |
| 33 |
'Does not error when given non-existant user-locator', |
| 34 |
|
| 35 |
[], |
| 36 |
|
| 37 |
[ 'not-callable' ], |
| 38 |
], |
| 39 |
|
| 40 |
[ |
| 41 |
'Calls selected locator and returns result', |
| 42 |
|
| 43 |
[ [ 123 ] ], |
| 44 |
|
| 45 |
static function () { |
| 46 |
return [ 123 => 123 ]; |
| 47 |
} |
| 48 |
], |
| 49 |
|
| 50 |
[ |
| 51 |
'evaluates multiple locators', |
| 52 |
|
| 53 |
[ [ 123 ], [ 456 ] ], |
| 54 |
|
| 55 |
[ |
| 56 |
static function () { |
| 57 |
return [ 123 => 123 ]; |
| 58 |
}, |
| 59 |
static function () { |
| 60 |
return [ 456 => 456 ]; |
| 61 |
}, |
| 62 |
], |
| 63 |
], |
| 64 |
|
| 65 |
[ |
| 66 |
'Passes parameters to locateFromEventExtra in expected manner', |
| 67 |
|
| 68 |
[ [ 123 ] ], |
| 69 |
|
| 70 |
[ |
| 71 |
[ [ UserLocator::class, 'locateFromEventExtra' ], [ 'other-user' ] ], |
| 72 |
], |
| 73 |
|
| 74 |
static function ( $test, $event ) { |
| 75 |
$event->expects( $test->any() ) |
| 76 |
->method( 'getExtraParam' ) |
| 77 |
->with( 'other-user' ) |
| 78 |
->willReturn( 123 ); |
| 79 |
} |
| 80 |
], |
| 81 |
]; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function testEvaluateUserLocators( $message, $expect, $locatorConfigForEventType, $setup = null ) { |
| 88 |
$this->overrideConfigValue( 'EchoNotifications', [ |
| 89 |
'unit-test' => [ |
| 90 |
AttributeManager::ATTR_LOCATORS => $locatorConfigForEventType |
| 91 |
], |
| 92 |
] ); |
| 93 |
|
| 94 |
$event = $this->createMock( Event::class ); |
| 95 |
$event->method( 'getType' ) |
| 96 |
->willReturn( 'unit-test' ); |
| 97 |
|
| 98 |
if ( $setup !== null ) { |
| 99 |
$setup( $this, $event ); |
| 100 |
} |
| 101 |
|
| 102 |
$result = NotificationController::evaluateUserCallable( $event, AttributeManager::ATTR_LOCATORS ); |
| 103 |
$this->assertEquals( $expect, array_map( 'array_keys', $result ), $message ); |
| 104 |
} |
| 105 |
|
| 106 |
public function testEvaluateUserLocatorPassesParameters() { |
| 107 |
$callback = function ( $event, $firstOption, $secondOption ) { |
| 108 |
$this->assertInstanceOf( Event::class, $event ); |
| 109 |
$this->assertEquals( 'first', $firstOption ); |
| 110 |
$this->assertEquals( 'second', $secondOption ); |
| 111 |
|
| 112 |
return []; |
| 113 |
}; |
| 114 |
|
| 115 |
$this->testEvaluateUserLocators( |
| 116 |
__FUNCTION__, |
| 117 |
[ [] ], |
| 118 |
[ [ $callback, 'first', 'second' ] ] |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
public static function getUsersToNotifyForEventProvider() { |
| 123 |
return [ |
| 124 |
[ |
| 125 |
'Filters anonymous users', |
| 126 |
|
| 127 |
[], |
| 128 |
|
| 129 |
[ User::newFromName( '4.5.6.7', false ) ], |
| 130 |
], |
| 131 |
|
| 132 |
[ |
| 133 |
'Filters duplicate users', |
| 134 |
|
| 135 |
[ 123 ], |
| 136 |
|
| 137 |
[ User::newFromId( 123 ), User::newFromId( 123 ) ], |
| 138 |
], |
| 139 |
|
| 140 |
[ |
| 141 |
'Filters non-user objects', |
| 142 |
|
| 143 |
[ 123 ], |
| 144 |
|
| 145 |
[ null, 'foo', User::newFromId( 123 ), (object)[], 456 ], |
| 146 |
], |
| 147 |
]; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function testGetUsersToNotifyForEvent( |
| 154 |
$message, |
| 155 |
$expect, |
| 156 |
$users |
| 157 |
) { |
| 158 |
$this->overrideConfigValue( 'EchoNotifications', [ |
| 159 |
'unit-test' => [ |
| 160 |
AttributeManager::ATTR_LOCATORS => static function () use ( $users ) { |
| 161 |
return $users; |
| 162 |
}, |
| 163 |
], |
| 164 |
] ); |
| 165 |
|
| 166 |
$event = $this->createMock( Event::class ); |
| 167 |
$event->method( 'getType' ) |
| 168 |
->willReturn( 'unit-test' ); |
| 169 |
|
| 170 |
$result = NotificationController::getUsersToNotifyForEvent( $event ); |
| 171 |
$ids = []; |
| 172 |
foreach ( $result as $user ) { |
| 173 |
$ids[] = $user->getId(); |
| 174 |
} |
| 175 |
$this->assertEquals( $expect, $ids, $message ); |
| 176 |
} |
| 177 |
|
| 178 |
public function testDoesNotDeliverDisabledEvent() { |
| 179 |
$event = $this->createMock( Event::class ); |
| 180 |
$event->method( 'isEnabledEvent' ) |
| 181 |
->willReturn( false ); |
| 182 |
|
| 183 |
|
| 184 |
$event->expects( $this->never() ) |
| 185 |
->method( 'getType' ); |
| 186 |
|
| 187 |
NotificationController::notify( $event, false ); |
| 188 |
} |
| 189 |
|
| 190 |
public static function getEventNotifyTypesProvider() { |
| 191 |
return [ |
| 192 |
[ |
| 193 |
'Selects the `all` configuration by default', |
| 194 |
|
| 195 |
[ 'web' ], |
| 196 |
|
| 197 |
'bar', |
| 198 |
|
| 199 |
[ 'web' => true ], |
| 200 |
|
| 201 |
[ |
| 202 |
'f' => [ 'email' => true ] |
| 203 |
], |
| 204 |
|
| 205 |
[ |
| 206 |
'foo' => [ |
| 207 |
'category' => 'f', |
| 208 |
], |
| 209 |
'bar' => [ |
| 210 |
'category' => 'b', |
| 211 |
] |
| 212 |
], |
| 213 |
], |
| 214 |
|
| 215 |
[ |
| 216 |
'Overrides `all` configuration with event category configuration', |
| 217 |
|
| 218 |
[ 'web' ], |
| 219 |
|
| 220 |
'foo', |
| 221 |
|
| 222 |
[ 'web' => true, 'email' => true ], |
| 223 |
|
| 224 |
[ |
| 225 |
'f' => [ 'email' => false ], |
| 226 |
'b' => [ 'sms' => true ], |
| 227 |
], |
| 228 |
|
| 229 |
[ |
| 230 |
'foo' => [ |
| 231 |
'category' => 'f', |
| 232 |
], |
| 233 |
'bar' => [ |
| 234 |
'category' => 'b', |
| 235 |
], |
| 236 |
], |
| 237 |
] |
| 238 |
]; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public function testGetEventNotifyTypes( |
| 245 |
$message, |
| 246 |
$expect, |
| 247 |
$type, |
| 248 |
array $defaultNotifyTypeAvailability, |
| 249 |
array $notifyTypeAvailabilityByCategory, |
| 250 |
array $notifications |
| 251 |
) { |
| 252 |
$this->overrideConfigValues( [ |
| 253 |
'DefaultNotifyTypeAvailability' => $defaultNotifyTypeAvailability, |
| 254 |
'NotifyTypeAvailabilityByCategory' => $notifyTypeAvailabilityByCategory, |
| 255 |
'EchoNotifications' => $notifications, |
| 256 |
'EchoNotificationCategories' => array_fill_keys( |
| 257 |
array_keys( $notifyTypeAvailabilityByCategory ), |
| 258 |
[ 'priority' => 4 ] |
| 259 |
), |
| 260 |
] ); |
| 261 |
$result = NotificationController::getEventNotifyTypes( $type ); |
| 262 |
$this->assertEquals( $expect, $result, $message ); |
| 263 |
} |
| 264 |
|
| 265 |
public function testEnqueueEvent() { |
| 266 |
$event = $this->createMock( Event::class ); |
| 267 |
$event->method( 'getExtraParam' ) |
| 268 |
->willReturn( null ); |
| 269 |
$event->expects( $this->once() ) |
| 270 |
->method( 'getTitle' ) |
| 271 |
->willReturn( Title::newFromText( 'test-title' ) ); |
| 272 |
$event->expects( $this->once() ) |
| 273 |
->method( 'getId' ) |
| 274 |
->willReturn( 42 ); |
| 275 |
NotificationController::enqueueEvent( $event ); |
| 276 |
$jobQueueGroup = $this->getServiceContainer()->getJobQueueGroup(); |
| 277 |
$queues = $jobQueueGroup->getQueuesWithJobs(); |
| 278 |
$this->assertCount( 1, $queues ); |
| 279 |
$this->assertEquals( 'EchoNotificationJob', $queues[0] ); |
| 280 |
$job = $jobQueueGroup->pop( 'EchoNotificationJob' ); |
| 281 |
$this->assertEquals( 'Test-title', $job->params[ 'title' ] ); |
| 282 |
$this->assertEquals( 42, $job->params[ 'eventId' ] ); |
| 283 |
} |
| 284 |
|
| 285 |
public function testNotSupportedDelay() { |
| 286 |
$queueGroup = $this->getServiceContainer()->getJobQueueGroup(); |
| 287 |
$this->assertCount( 0, $queueGroup->getQueuesWithJobs() ); |
| 288 |
|
| 289 |
$event = $this->createMock( Event::class ); |
| 290 |
$event->method( 'getExtraParam' ) |
| 291 |
->willReturnMap( [ |
| 292 |
[ 'delay', null, 120 ], |
| 293 |
[ 'rootJobSignature', null, 'test-signature' ], |
| 294 |
[ 'rootJobTimestamp', null, wfTimestamp() ] |
| 295 |
] ); |
| 296 |
$event->expects( $this->once() ) |
| 297 |
->method( 'getTitle' ) |
| 298 |
->willReturn( Title::newFromText( 'test-title' ) ); |
| 299 |
$event->method( 'getId' ) |
| 300 |
->willReturn( 42 ); |
| 301 |
NotificationController::enqueueEvent( $event ); |
| 302 |
|
| 303 |
$this->assertCount( 0, $queueGroup->getQueuesWithJobs() ); |
| 304 |
} |
| 305 |
|
| 306 |
public function testEventParams() { |
| 307 |
$rootJobTimestamp = wfTimestamp(); |
| 308 |
MWTimestamp::setFakeTime( 0 ); |
| 309 |
|
| 310 |
$event = $this->createMock( Event::class ); |
| 311 |
$event->method( 'getExtraParam' ) |
| 312 |
->willReturnMap( [ |
| 313 |
[ 'delay', null, 10 ], |
| 314 |
[ 'rootJobSignature', null, 'test-signature' ], |
| 315 |
[ 'rootJobTimestamp', null, $rootJobTimestamp ] |
| 316 |
] ); |
| 317 |
$event->expects( $this->once() ) |
| 318 |
->method( 'getId' ) |
| 319 |
->willReturn( 42 ); |
| 320 |
|
| 321 |
$params = NotificationController::getEventParams( $event ); |
| 322 |
$expectedParams = [ |
| 323 |
'eventId' => 42, |
| 324 |
'rootJobSignature' => 'test-signature', |
| 325 |
'rootJobTimestamp' => $rootJobTimestamp, |
| 326 |
'jobReleaseTimestamp' => 10 |
| 327 |
]; |
| 328 |
$this->assertArrayEquals( $expectedParams, $params ); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public function testIsPageLinkedTitleMutedByUser( |
| 338 |
int $mockArticleID, array $mockMutedTitlePreferences, $expected ): void { |
| 339 |
$title = $this->getMockTitle( $mockArticleID ); |
| 340 |
$user = $this->getMockUser(); |
| 341 |
$userOptionsLookup = $this->getUserOptionsLookupMock( $mockMutedTitlePreferences ); |
| 342 |
$wrapper = TestingAccessWrapper::newFromClass( NotificationController::class ); |
| 343 |
$wrapper->mutedPageLinkedTitlesCache = $this->createMock( MapCacheLRU::class ); |
| 344 |
$this->setService( 'UserOptionsLookup', $userOptionsLookup ); |
| 345 |
$this->assertSame( |
| 346 |
$expected, |
| 347 |
$wrapper->isPageLinkedTitleMutedByUser( $title, $user ) |
| 348 |
); |
| 349 |
} |
| 350 |
|
| 351 |
public static function PageLinkedTitleMutedByUserDataProvider(): array { |
| 352 |
return [ |
| 353 |
[ |
| 354 |
123, |
| 355 |
[], |
| 356 |
false |
| 357 |
], |
| 358 |
[ |
| 359 |
123, |
| 360 |
[ 123, 456, 789 ], |
| 361 |
true |
| 362 |
], |
| 363 |
[ |
| 364 |
456, |
| 365 |
[ 489 ], |
| 366 |
false |
| 367 |
] |
| 368 |
|
| 369 |
]; |
| 370 |
} |
| 371 |
|
| 372 |
private function getMockTitle( int $articleID ) { |
| 373 |
$title = $this->createMock( Title::class ); |
| 374 |
$title->method( 'getArticleID' ) |
| 375 |
->willReturn( $articleID ); |
| 376 |
return $title; |
| 377 |
} |
| 378 |
|
| 379 |
private function getMockUser() { |
| 380 |
$user = $this->createMock( User::class ); |
| 381 |
$user->method( 'getId' ) |
| 382 |
->willReturn( 456 ); |
| 383 |
return $user; |
| 384 |
} |
| 385 |
|
| 386 |
private function getUserOptionsLookupMock( $mutedTitlePreferences = [] ) { |
| 387 |
$userOptionsLookupMock = $this->createMock( UserOptionsLookup::class ); |
| 388 |
$userOptionsLookupMock->method( 'getOption' ) |
| 389 |
->willReturn( implode( "\n", $mutedTitlePreferences ) ); |
| 390 |
return $userOptionsLookupMock; |
| 391 |
} |
| 392 |
} |