| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Test\Unit; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Notifications\AttributeManager; |
| 6 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 7 |
use MediaWiki\User\UserGroupManager; |
| 8 |
use MediaWiki\User\UserIdentity; |
| 9 |
use MediaWiki\User\UserIdentityValue; |
| 10 |
use MediaWikiUnitTestCase; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class AttributeManagerTest extends MediaWikiUnitTestCase { |
| 16 |
|
| 17 |
private function getAttributeManager( |
| 18 |
array $notifications, |
| 19 |
array $categories = [], |
| 20 |
array $defaultNotifyTypeAvailability = [], |
| 21 |
array $notifyTypeAvailabilityByCategory = [] |
| 22 |
): AttributeManager { |
| 23 |
$userGroupManager = $this->createNoOpMock( UserGroupManager::class, [ 'getUserGroups' ] ); |
| 24 |
$userGroupManager->method( 'getUserGroups' )->willReturn( [ 'echo_group' ] ); |
| 25 |
|
| 26 |
$userOptionsLookup = $this->createNoOpMock( UserOptionsLookup::class, [ 'getOption' ] ); |
| 27 |
$userOptionsLookup->method( 'getOption' )->willReturn( true ); |
| 28 |
|
| 29 |
return new AttributeManager( |
| 30 |
$notifications, |
| 31 |
$categories, |
| 32 |
$defaultNotifyTypeAvailability, |
| 33 |
$notifyTypeAvailabilityByCategory, |
| 34 |
$userGroupManager, |
| 35 |
$userOptionsLookup |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
protected function getUser(): UserIdentity { |
| 43 |
return new UserIdentityValue( 1, 'ExampleUserName' ); |
| 44 |
} |
| 45 |
|
| 46 |
public static function getUserLocatorsProvider() { |
| 47 |
return [ |
| 48 |
[ |
| 49 |
'No errors when requesting unknown type', |
| 50 |
|
| 51 |
[], |
| 52 |
|
| 53 |
'foo', |
| 54 |
|
| 55 |
[], |
| 56 |
], |
| 57 |
|
| 58 |
[ |
| 59 |
'Returns selected notification configuration', |
| 60 |
|
| 61 |
[ 'woot!' ], |
| 62 |
|
| 63 |
'magic', |
| 64 |
|
| 65 |
[ |
| 66 |
'foo' => [ |
| 67 |
AttributeManager::ATTR_LOCATORS => [ 'frown' ], |
| 68 |
], |
| 69 |
'magic' => [ |
| 70 |
AttributeManager::ATTR_LOCATORS => [ 'woot!' ], |
| 71 |
], |
| 72 |
], |
| 73 |
], |
| 74 |
|
| 75 |
[ |
| 76 |
'Accepts user-locators as string and returns array', |
| 77 |
|
| 78 |
[ 'sagen' ], |
| 79 |
|
| 80 |
'challah', |
| 81 |
|
| 82 |
[ |
| 83 |
'challah' => [ |
| 84 |
AttributeManager::ATTR_LOCATORS => 'sagen', |
| 85 |
], |
| 86 |
], |
| 87 |
], |
| 88 |
]; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function testGetUserLocators( $message, $expect, $type, $notifications ) { |
| 95 |
$manager = $this->getAttributeManager( $notifications ); |
| 96 |
|
| 97 |
$result = $manager->getUserCallable( $type, AttributeManager::ATTR_LOCATORS ); |
| 98 |
$this->assertEquals( $expect, $result, $message ); |
| 99 |
} |
| 100 |
|
| 101 |
public function testGetCategoryEligibility() { |
| 102 |
$notif = [ |
| 103 |
'event_one' => [ |
| 104 |
'category' => 'category_one' |
| 105 |
], |
| 106 |
]; |
| 107 |
$category = [ |
| 108 |
'category_one' => [ |
| 109 |
'priority' => 10 |
| 110 |
] |
| 111 |
]; |
| 112 |
$manager = $this->getAttributeManager( $notif, $category ); |
| 113 |
$this->assertTrue( $manager->getCategoryEligibility( $this->getUser(), 'category_one' ) ); |
| 114 |
$category = [ |
| 115 |
'category_one' => [ |
| 116 |
'priority' => 10, |
| 117 |
'usergroups' => [ |
| 118 |
'sysop' |
| 119 |
] |
| 120 |
] |
| 121 |
]; |
| 122 |
$manager = $this->getAttributeManager( $notif, $category ); |
| 123 |
$this->assertFalse( $manager->getCategoryEligibility( $this->getUser(), 'category_one' ) ); |
| 124 |
} |
| 125 |
|
| 126 |
public function testGetNotificationCategory() { |
| 127 |
$notif = [ |
| 128 |
'event_one' => [ |
| 129 |
'category' => 'category_one' |
| 130 |
], |
| 131 |
]; |
| 132 |
$category = [ |
| 133 |
'category_one' => [ |
| 134 |
'priority' => 10 |
| 135 |
] |
| 136 |
]; |
| 137 |
$manager = $this->getAttributeManager( $notif, $category ); |
| 138 |
$this->assertEquals( 'category_one', $manager->getNotificationCategory( 'event_one' ) ); |
| 139 |
|
| 140 |
$manager = $this->getAttributeManager( $notif ); |
| 141 |
$this->assertEquals( 'other', $manager->getNotificationCategory( 'event_one' ) ); |
| 142 |
|
| 143 |
$notif = [ |
| 144 |
'event_one' => [ |
| 145 |
'category' => 'category_two' |
| 146 |
], |
| 147 |
]; |
| 148 |
$category = [ |
| 149 |
'category_one' => [ |
| 150 |
'priority' => 10 |
| 151 |
] |
| 152 |
]; |
| 153 |
$manager = $this->getAttributeManager( $notif, $category ); |
| 154 |
$this->assertEquals( 'other', $manager->getNotificationCategory( 'event_one' ) ); |
| 155 |
} |
| 156 |
|
| 157 |
public function testGetCategoryPriority() { |
| 158 |
$notif = [ |
| 159 |
'event_one' => [ |
| 160 |
'category' => 'category_two' |
| 161 |
], |
| 162 |
]; |
| 163 |
$category = [ |
| 164 |
'category_one' => [ |
| 165 |
'priority' => 6 |
| 166 |
], |
| 167 |
'category_two' => [ |
| 168 |
'priority' => 100 |
| 169 |
], |
| 170 |
'category_three' => [ |
| 171 |
'priority' => -10 |
| 172 |
], |
| 173 |
'category_four' => [] |
| 174 |
]; |
| 175 |
$manager = $this->getAttributeManager( $notif, $category ); |
| 176 |
$this->assertSame( 6, $manager->getCategoryPriority( 'category_one' ) ); |
| 177 |
$this->assertSame( 10, $manager->getCategoryPriority( 'category_two' ) ); |
| 178 |
$this->assertSame( 10, $manager->getCategoryPriority( 'category_three' ) ); |
| 179 |
$this->assertSame( 10, $manager->getCategoryPriority( 'category_four' ) ); |
| 180 |
} |
| 181 |
|
| 182 |
public function testGetNotificationPriority() { |
| 183 |
$notif = [ |
| 184 |
'event_one' => [ |
| 185 |
'category' => 'category_one' |
| 186 |
], |
| 187 |
'event_two' => [ |
| 188 |
'category' => 'category_two' |
| 189 |
], |
| 190 |
'event_three' => [ |
| 191 |
'category' => 'category_three' |
| 192 |
], |
| 193 |
'event_four' => [ |
| 194 |
'category' => 'category_four' |
| 195 |
] |
| 196 |
]; |
| 197 |
$category = [ |
| 198 |
'category_one' => [ |
| 199 |
'priority' => 6 |
| 200 |
], |
| 201 |
'category_two' => [ |
| 202 |
'priority' => 100 |
| 203 |
], |
| 204 |
'category_three' => [ |
| 205 |
'priority' => -10 |
| 206 |
], |
| 207 |
'category_four' => [] |
| 208 |
]; |
| 209 |
$manager = $this->getAttributeManager( $notif, $category ); |
| 210 |
$this->assertSame( 6, $manager->getNotificationPriority( 'event_one' ) ); |
| 211 |
$this->assertSame( 10, $manager->getNotificationPriority( 'event_two' ) ); |
| 212 |
$this->assertSame( 10, $manager->getNotificationPriority( 'event_three' ) ); |
| 213 |
$this->assertSame( 10, $manager->getNotificationPriority( 'event_four' ) ); |
| 214 |
} |
| 215 |
|
| 216 |
public static function getEventsForSectionProvider() { |
| 217 |
$notifications = [ |
| 218 |
'event_one' => [ |
| 219 |
'category' => 'category_one', |
| 220 |
'section' => 'message', |
| 221 |
], |
| 222 |
'event_two' => [ |
| 223 |
'category' => 'category_two', |
| 224 |
'section' => 'invalid', |
| 225 |
], |
| 226 |
'event_three' => [ |
| 227 |
'category' => 'category_three', |
| 228 |
'section' => 'message', |
| 229 |
], |
| 230 |
'event_four' => [ |
| 231 |
'category' => 'category_four', |
| 232 |
|
| 233 |
], |
| 234 |
'event_five' => [ |
| 235 |
'category' => 'category_two', |
| 236 |
'section' => 'alert', |
| 237 |
], |
| 238 |
]; |
| 239 |
|
| 240 |
return [ |
| 241 |
[ |
| 242 |
[ 'event_one', 'event_three' ], |
| 243 |
$notifications, |
| 244 |
'message', |
| 245 |
'Messages', |
| 246 |
], |
| 247 |
|
| 248 |
[ |
| 249 |
[ 'event_two', 'event_four', 'event_five' ], |
| 250 |
$notifications, |
| 251 |
'alert', |
| 252 |
'Alerts', |
| 253 |
], |
| 254 |
]; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public function testGetEventsForSection( $expected, $notificationTypes, $section, $message ) { |
| 261 |
$am = $this->getAttributeManager( $notificationTypes ); |
| 262 |
$actual = $am->getEventsForSection( $section ); |
| 263 |
$this->assertEquals( $expected, $actual, $message ); |
| 264 |
} |
| 265 |
|
| 266 |
public function testGetUserEnabledEvents() { |
| 267 |
$notif = [ |
| 268 |
'event_one' => [ |
| 269 |
'category' => 'category_one' |
| 270 |
], |
| 271 |
'event_two' => [ |
| 272 |
'category' => 'category_two' |
| 273 |
], |
| 274 |
'event_three' => [ |
| 275 |
'category' => 'category_three' |
| 276 |
], |
| 277 |
'event_four' => [ |
| 278 |
'category' => 'category_four' |
| 279 |
] |
| 280 |
]; |
| 281 |
$category = [ |
| 282 |
'category_one' => [ |
| 283 |
'priority' => 10, |
| 284 |
'usergroups' => [ |
| 285 |
'sysop' |
| 286 |
] |
| 287 |
], |
| 288 |
'category_two' => [ |
| 289 |
'priority' => 10, |
| 290 |
'usergroups' => [ |
| 291 |
'echo_group' |
| 292 |
] |
| 293 |
], |
| 294 |
'category_three' => [ |
| 295 |
'priority' => 10, |
| 296 |
], |
| 297 |
'category_four' => [ |
| 298 |
'priority' => 10, |
| 299 |
] |
| 300 |
]; |
| 301 |
$defaultNotifyTypeAvailability = [ |
| 302 |
'web' => true, |
| 303 |
'email' => true, |
| 304 |
]; |
| 305 |
$notifyTypeAvailabilityByCategory = [ |
| 306 |
'category_three' => [ |
| 307 |
'web' => false, |
| 308 |
'email' => true, |
| 309 |
] |
| 310 |
]; |
| 311 |
$manager = $this->getAttributeManager( |
| 312 |
$notif, |
| 313 |
$category, |
| 314 |
$defaultNotifyTypeAvailability, |
| 315 |
$notifyTypeAvailabilityByCategory |
| 316 |
); |
| 317 |
$this->assertEquals( |
| 318 |
[ 'event_two', 'event_four' ], |
| 319 |
$manager->getUserEnabledEvents( $this->getUser(), 'web' ) |
| 320 |
); |
| 321 |
$this->assertEquals( |
| 322 |
[ 'event_two', 'event_three', 'event_four' ], |
| 323 |
$manager->getUserEnabledEvents( $this->getUser(), [ 'web', 'email' ] ) |
| 324 |
); |
| 325 |
} |
| 326 |
|
| 327 |
public function testGetUserEnabledEventsBySections() { |
| 328 |
$notif = [ |
| 329 |
'event_one' => [ |
| 330 |
'category' => 'category_one' |
| 331 |
], |
| 332 |
'event_two' => [ |
| 333 |
'category' => 'category_two', |
| 334 |
'section' => 'message' |
| 335 |
], |
| 336 |
'event_three' => [ |
| 337 |
'category' => 'category_three', |
| 338 |
'section' => 'alert' |
| 339 |
], |
| 340 |
'event_four' => [ |
| 341 |
'category' => 'category_three', |
| 342 |
], |
| 343 |
'event_five' => [ |
| 344 |
'category' => 'category_five' |
| 345 |
] |
| 346 |
]; |
| 347 |
$category = [ |
| 348 |
'category_one' => [ |
| 349 |
'priority' => 10, |
| 350 |
], |
| 351 |
'category_two' => [ |
| 352 |
'priority' => 10, |
| 353 |
], |
| 354 |
'category_three' => [ |
| 355 |
'priority' => 10 |
| 356 |
], |
| 357 |
'category_five' => [ |
| 358 |
'priority' => 10 |
| 359 |
] |
| 360 |
]; |
| 361 |
$defaultNotifyTypeAvailability = [ |
| 362 |
'web' => true, |
| 363 |
'email' => true, |
| 364 |
]; |
| 365 |
$notifyTypeAvailabilityByCategory = [ |
| 366 |
'category_five' => [ |
| 367 |
'web' => false, |
| 368 |
'email' => true, |
| 369 |
] |
| 370 |
]; |
| 371 |
$manager = $this->getAttributeManager( |
| 372 |
$notif, |
| 373 |
$category, |
| 374 |
$defaultNotifyTypeAvailability, |
| 375 |
$notifyTypeAvailabilityByCategory |
| 376 |
); |
| 377 |
$expected = [ 'event_one', 'event_three', 'event_four' ]; |
| 378 |
$actual = $manager->getUserEnabledEventsBySections( $this->getUser(), 'web', [ 'alert' ] ); |
| 379 |
sort( $expected ); |
| 380 |
sort( $actual ); |
| 381 |
$this->assertEquals( $expected, $actual ); |
| 382 |
|
| 383 |
$expected = [ 'event_one', 'event_three', 'event_four', 'event_five' ]; |
| 384 |
$actual = $manager->getUserEnabledEventsBySections( $this->getUser(), [ 'web', 'email' ], [ 'alert' ] ); |
| 385 |
sort( $expected ); |
| 386 |
sort( $actual ); |
| 387 |
$this->assertEquals( $expected, $actual ); |
| 388 |
|
| 389 |
$expected = [ 'event_two' ]; |
| 390 |
$actual = $manager->getUserEnabledEventsBySections( $this->getUser(), 'web', [ 'message' ] ); |
| 391 |
sort( $expected ); |
| 392 |
sort( $actual ); |
| 393 |
$this->assertEquals( $expected, $actual ); |
| 394 |
|
| 395 |
$expected = [ 'event_one', 'event_two', 'event_three', 'event_four' ]; |
| 396 |
$actual = $manager->getUserEnabledEventsBySections( $this->getUser(), 'web', |
| 397 |
[ 'message', 'alert' ] ); |
| 398 |
sort( $expected ); |
| 399 |
sort( $actual ); |
| 400 |
$this->assertEquals( $expected, $actual ); |
| 401 |
} |
| 402 |
|
| 403 |
public static function getEventsByCategoryProvider() { |
| 404 |
return [ |
| 405 |
[ |
| 406 |
'Mix of populated and empty categories handled appropriately', |
| 407 |
[ |
| 408 |
'category_one' => [ |
| 409 |
'event_two', |
| 410 |
'event_five', |
| 411 |
], |
| 412 |
'category_two' => [ |
| 413 |
'event_one', |
| 414 |
'event_three', |
| 415 |
'event_four', |
| 416 |
], |
| 417 |
'category_three' => [], |
| 418 |
], |
| 419 |
[ |
| 420 |
'category_one' => [], |
| 421 |
'category_two' => [], |
| 422 |
'category_three' => [], |
| 423 |
], |
| 424 |
[ |
| 425 |
'event_one' => [ |
| 426 |
'category' => 'category_two', |
| 427 |
], |
| 428 |
'event_two' => [ |
| 429 |
'category' => 'category_one', |
| 430 |
], |
| 431 |
'event_three' => [ |
| 432 |
'category' => 'category_two', |
| 433 |
], |
| 434 |
'event_four' => [ |
| 435 |
'category' => 'category_two', |
| 436 |
], |
| 437 |
'event_five' => [ |
| 438 |
'category' => 'category_one', |
| 439 |
], |
| 440 |
] |
| 441 |
] |
| 442 |
]; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
public function testGetEventsByCategory( |
| 449 |
$message, |
| 450 |
$expectedMapping, |
| 451 |
$categories, |
| 452 |
$notifications |
| 453 |
) { |
| 454 |
$am = $this->getAttributeManager( $notifications, $categories ); |
| 455 |
$actualMapping = $am->getEventsByCategory(); |
| 456 |
$this->assertEquals( $expectedMapping, $actualMapping, $message ); |
| 457 |
} |
| 458 |
|
| 459 |
public static function isNotifyTypeAvailableForCategoryProvider() { |
| 460 |
return [ |
| 461 |
[ |
| 462 |
'Fallback to default entirely', |
| 463 |
true, |
| 464 |
'category_one', |
| 465 |
'web', |
| 466 |
[ 'web' => true, 'email' => true ], |
| 467 |
[] |
| 468 |
], |
| 469 |
[ |
| 470 |
'Fallback to default for single type', |
| 471 |
false, |
| 472 |
'category_two', |
| 473 |
'email', |
| 474 |
[ 'web' => true, 'email' => false ], |
| 475 |
[ |
| 476 |
'category_two' => [ |
| 477 |
'web' => true, |
| 478 |
], |
| 479 |
] |
| 480 |
], |
| 481 |
[ |
| 482 |
'Use override', |
| 483 |
false, |
| 484 |
'category_three', |
| 485 |
'web', |
| 486 |
[ 'web' => true, 'email' => true ], |
| 487 |
[ |
| 488 |
'category_three' => [ |
| 489 |
'web' => false, |
| 490 |
], |
| 491 |
], |
| 492 |
], |
| 493 |
]; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
public function testIsNotifyTypeAvailableForCategory( |
| 500 |
$message, |
| 501 |
$expected, |
| 502 |
$categoryName, |
| 503 |
$notifyType, |
| 504 |
$defaultNotifyTypeAvailability, |
| 505 |
$notifyTypeAvailabilityByCategory |
| 506 |
) { |
| 507 |
$am = $this->getAttributeManager( |
| 508 |
[], |
| 509 |
[], |
| 510 |
$defaultNotifyTypeAvailability, |
| 511 |
$notifyTypeAvailabilityByCategory |
| 512 |
); |
| 513 |
$actual = $am->isNotifyTypeAvailableForCategory( $categoryName, $notifyType ); |
| 514 |
$this->assertEquals( $expected, $actual, $message ); |
| 515 |
} |
| 516 |
|
| 517 |
public static function isNotifyTypeDismissableForCategoryProvider() { |
| 518 |
return [ |
| 519 |
[ |
| 520 |
'Not dismissable because of all', |
| 521 |
false, |
| 522 |
[ |
| 523 |
'category_one' => [ |
| 524 |
'no-dismiss' => [ 'all' ], |
| 525 |
] |
| 526 |
], |
| 527 |
'category_one', |
| 528 |
'web', |
| 529 |
], |
| 530 |
[ |
| 531 |
'Not dismissable because of specific notify type', |
| 532 |
false, |
| 533 |
[ |
| 534 |
'category_two' => [ |
| 535 |
'no-dismiss' => [ 'email' ], |
| 536 |
] |
| 537 |
], |
| 538 |
'category_two', |
| 539 |
'email', |
| 540 |
], |
| 541 |
[ |
| 542 |
'Dismissable because of different affected notify type', |
| 543 |
true, |
| 544 |
[ |
| 545 |
'category_three' => [ |
| 546 |
'no-dismiss' => [ 'web' ], |
| 547 |
] |
| 548 |
], |
| 549 |
'category_three', |
| 550 |
'email', |
| 551 |
], |
| 552 |
]; |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
public function testIsNotifyTypeDismissableForCategory( |
| 559 |
$message, |
| 560 |
$expected, |
| 561 |
$categories, |
| 562 |
$categoryName, |
| 563 |
$notifyType |
| 564 |
) { |
| 565 |
$am = $this->getAttributeManager( [], $categories ); |
| 566 |
$actual = $am->isNotifyTypeDismissableForCategory( $categoryName, $notifyType ); |
| 567 |
$this->assertEquals( $expected, $actual, $message ); |
| 568 |
} |
| 569 |
|
| 570 |
public static function getNotificationSectionProvider() { |
| 571 |
yield [ 'event_one', 'alert' ]; |
| 572 |
yield [ 'event_two', 'message' ]; |
| 573 |
yield [ 'event_three', 'alert' ]; |
| 574 |
yield [ 'event_undefined', 'alert' ]; |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
public function testGetNotificationSection( $type, $expected ) { |
| 581 |
$am = $this->getAttributeManager( [ |
| 582 |
'event_one' => [ |
| 583 |
'section' => 'alert', |
| 584 |
], |
| 585 |
'event_two' => [ |
| 586 |
'section' => 'message', |
| 587 |
], |
| 588 |
'event_three' => [], |
| 589 |
] ); |
| 590 |
$actual = $am->getNotificationSection( $type ); |
| 591 |
$this->assertSame( $expected, $actual ); |
| 592 |
} |
| 593 |
|
| 594 |
public static function isBundleExpandableProvider() { |
| 595 |
yield [ 'event_one', false ]; |
| 596 |
yield [ 'event_two', false ]; |
| 597 |
yield [ 'event_three', false ]; |
| 598 |
yield [ 'event_four', true ]; |
| 599 |
yield [ 'event_undefined', false ]; |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
public function testIsBundleExpandable( $type, $expected ) { |
| 606 |
$am = $this->getAttributeManager( [ |
| 607 |
'event_one' => [], |
| 608 |
'event_two' => [ |
| 609 |
'bundle' => [ |
| 610 |
'web' => true |
| 611 |
] |
| 612 |
], |
| 613 |
'event_three' => [ |
| 614 |
'bundle' => [ |
| 615 |
'web' => true, |
| 616 |
'email' => false, |
| 617 |
'expandable' => false |
| 618 |
] |
| 619 |
], |
| 620 |
'event_four' => [ |
| 621 |
'bundle' => [ |
| 622 |
'web' => true, |
| 623 |
'email' => true, |
| 624 |
'expandable' => true |
| 625 |
] |
| 626 |
], |
| 627 |
] ); |
| 628 |
$actual = $am->isBundleExpandable( $type ); |
| 629 |
$this->assertSame( $expected, $actual ); |
| 630 |
} |
| 631 |
|
| 632 |
} |