| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Page\PageIdentity; |
| 4 |
use MediaWiki\Page\PageIdentityValue; |
| 5 |
use MediaWiki\Page\PageReference; |
| 6 |
use MediaWiki\Page\PageReferenceValue; |
| 7 |
use MediaWiki\Page\WikiPageFactory; |
| 8 |
use MediaWiki\Revision\RevisionLookup; |
| 9 |
use MediaWiki\Tests\Unit\DummyServicesTrait; |
| 10 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 11 |
use MediaWiki\Title\Title; |
| 12 |
use MediaWiki\Title\TitleValue; |
| 13 |
use MediaWiki\User\TalkPageNotificationManager; |
| 14 |
use MediaWiki\User\User; |
| 15 |
use MediaWiki\User\UserFactory; |
| 16 |
use MediaWiki\User\UserIdentity; |
| 17 |
use MediaWiki\User\UserIdentityValue; |
| 18 |
use MediaWiki\Watchlist\WatchedItem; |
| 19 |
use MediaWiki\Watchlist\WatchedItemStoreInterface; |
| 20 |
use MediaWiki\Watchlist\WatchlistManager; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class WatchlistManagerUnitTest extends MediaWikiUnitTestCase { |
| 31 |
use DummyServicesTrait; |
| 32 |
use MockTitleTrait; |
| 33 |
use MockAuthorityTrait; |
| 34 |
|
| 35 |
private function getWikiPageFactory() { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
$wikiPageFactory = $this->createMock( WikiPageFactory::class ); |
| 41 |
$wikiPageFactory->method( 'newFromTitle' )->willReturnCallback( |
| 42 |
function ( PageIdentity $pageIdentity ) { |
| 43 |
$title = Title::newFromPageReference( $pageIdentity ); |
| 44 |
$wikiPage = $this->createMock( WikiPage::class ); |
| 45 |
$wikiPage->method( 'getTitle' )->willReturn( $title ); |
| 46 |
return $wikiPage; |
| 47 |
} |
| 48 |
); |
| 49 |
return $wikiPageFactory; |
| 50 |
} |
| 51 |
|
| 52 |
private function getManager( array $params = [] ) { |
| 53 |
$options = $params['options'] ?? [ |
| 54 |
WatchlistManager::OPTION_ENOTIF => false, |
| 55 |
]; |
| 56 |
|
| 57 |
$talkPageNotificationManager = $params['talkPageNotificationManager'] ?? |
| 58 |
$this->createNoOpMock( TalkPageNotificationManager::class ); |
| 59 |
|
| 60 |
$watchedItemStore = $params['watchedItemStore'] ?? |
| 61 |
$this->createNoOpAbstractMock( WatchedItemStoreInterface::class ); |
| 62 |
|
| 63 |
$userFactory = $params['userFactory'] ?? |
| 64 |
$this->createNoOpMock( UserFactory::class ); |
| 65 |
|
| 66 |
$hookContainer = $params['hookContainer'] ?? $this->createHookContainer(); |
| 67 |
|
| 68 |
$nsInfo = $this->getDummyNamespaceInfo( [ |
| 69 |
'hookContainer' => $hookContainer, |
| 70 |
] ); |
| 71 |
|
| 72 |
return new WatchlistManager( |
| 73 |
$options, |
| 74 |
$hookContainer, |
| 75 |
$this->getDummyReadOnlyMode( $params['readOnly'] ?? false ), |
| 76 |
$this->createMock( RevisionLookup::class ), |
| 77 |
$talkPageNotificationManager, |
| 78 |
$watchedItemStore, |
| 79 |
$userFactory, |
| 80 |
$nsInfo, |
| 81 |
$this->getWikiPageFactory() |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
private function getAuthorityAndUserFactory( UserIdentity $userIdentity, array $permissions = [] ) { |
| 86 |
$authority = $this->mockUserAuthorityWithPermissions( |
| 87 |
$userIdentity, |
| 88 |
$permissions |
| 89 |
); |
| 90 |
|
| 91 |
$userFactory = $this->createMock( UserFactory::class ); |
| 92 |
$userFactory->method( 'newFromUserIdentity' ) |
| 93 |
->willReturnCallback( function ( $userIdentity ) use ( $permissions ) { |
| 94 |
$user = $this->createMock( User::class ); |
| 95 |
$user->method( 'isAllowed' ) |
| 96 |
->willReturnCallback( static function ( $permission ) use ( $permissions ) { |
| 97 |
return in_array( $permission, $permissions ); |
| 98 |
} ); |
| 99 |
$user->method( 'getUser' ) |
| 100 |
->willReturn( $userIdentity ); |
| 101 |
return $user; |
| 102 |
} ); |
| 103 |
|
| 104 |
return [ $authority, $userFactory ]; |
| 105 |
} |
| 106 |
|
| 107 |
public function testClearAllUserNotifications_readOnly() { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 112 |
[ $authority, ] = $this->getAuthorityAndUserFactory( $userIdentity ); |
| 113 |
|
| 114 |
$watchedItemStore = $this->createMock( WatchedItemStoreInterface::class ); |
| 115 |
$watchedItemStore->expects( $this->never() ) |
| 116 |
->method( 'resetAllNotificationTimestampsForUser' ); |
| 117 |
|
| 118 |
$manager = $this->getManager( [ |
| 119 |
'readOnly' => true, |
| 120 |
'watchedItemStore' => $watchedItemStore |
| 121 |
] ); |
| 122 |
|
| 123 |
$manager->clearAllUserNotifications( $userIdentity ); |
| 124 |
$manager->clearAllUserNotifications( $authority ); |
| 125 |
} |
| 126 |
|
| 127 |
public function testClearAllUserNotifications_noPerms() { |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 132 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( $userIdentity ); |
| 133 |
|
| 134 |
$watchedItemStore = $this->createMock( WatchedItemStoreInterface::class ); |
| 135 |
$watchedItemStore->expects( $this->never() ) |
| 136 |
->method( 'resetAllNotificationTimestampsForUser' ); |
| 137 |
|
| 138 |
$talkPageNotificationManager = $this->createMock( TalkPageNotificationManager::class ); |
| 139 |
$talkPageNotificationManager->expects( $this->exactly( 2 ) ) |
| 140 |
->method( 'removeUserHasNewMessages' ) |
| 141 |
->with( $userIdentity ); |
| 142 |
|
| 143 |
$manager = $this->getManager( [ |
| 144 |
'watchedItemStore' => $watchedItemStore, |
| 145 |
'talkPageNotificationManager' => $talkPageNotificationManager, |
| 146 |
'userFactory' => $userFactory |
| 147 |
] ); |
| 148 |
|
| 149 |
$manager->clearAllUserNotifications( $userIdentity ); |
| 150 |
$manager->clearAllUserNotifications( $authority ); |
| 151 |
} |
| 152 |
|
| 153 |
public function testClearAllUserNotifications_configDisabled() { |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 158 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( |
| 159 |
$userIdentity, |
| 160 |
[ 'editmywatchlist' ] |
| 161 |
); |
| 162 |
|
| 163 |
$talkPageNotificationManager = $this->createMock( TalkPageNotificationManager::class ); |
| 164 |
$talkPageNotificationManager->expects( $this->exactly( 2 ) ) |
| 165 |
->method( 'removeUserHasNewMessages' ); |
| 166 |
|
| 167 |
$watchedItemStore = $this->createMock( WatchedItemStoreInterface::class ); |
| 168 |
$watchedItemStore->expects( $this->never() ) |
| 169 |
->method( 'resetAllNotificationTimestampsForUser' ); |
| 170 |
|
| 171 |
$manager = $this->getManager( [ |
| 172 |
'watchedItemStore' => $watchedItemStore, |
| 173 |
'talkPageNotificationManager' => $talkPageNotificationManager, |
| 174 |
'userFactory' => $userFactory |
| 175 |
] ); |
| 176 |
|
| 177 |
$manager->clearAllUserNotifications( $userIdentity ); |
| 178 |
$manager->clearAllUserNotifications( $authority ); |
| 179 |
} |
| 180 |
|
| 181 |
public function testClearAllUserNotifications_falseyId() { |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
$options = [ |
| 186 |
WatchlistManager::OPTION_ENOTIF => true |
| 187 |
]; |
| 188 |
|
| 189 |
$userIdentity = new UserIdentityValue( 0, 'User Name' ); |
| 190 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( |
| 191 |
$userIdentity, |
| 192 |
[ 'editmywatchlist' ] |
| 193 |
); |
| 194 |
|
| 195 |
$watchedItemStore = $this->createMock( WatchedItemStoreInterface::class ); |
| 196 |
$watchedItemStore->expects( $this->never() ) |
| 197 |
->method( 'resetAllNotificationTimestampsForUser' ); |
| 198 |
|
| 199 |
$manager = $this->getManager( [ |
| 200 |
'options' => $options, |
| 201 |
'watchedItemStore' => $watchedItemStore, |
| 202 |
'userFactory' => $userFactory |
| 203 |
] ); |
| 204 |
|
| 205 |
$manager->clearAllUserNotifications( $userIdentity ); |
| 206 |
$manager->clearAllUserNotifications( $authority ); |
| 207 |
} |
| 208 |
|
| 209 |
public function testClearAllUserNotifications() { |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
$options = [ |
| 214 |
WatchlistManager::OPTION_ENOTIF => true |
| 215 |
]; |
| 216 |
|
| 217 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 218 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( |
| 219 |
$userIdentity, |
| 220 |
[ 'editmywatchlist' ] |
| 221 |
); |
| 222 |
|
| 223 |
$watchedItemStore = $this->createMock( WatchedItemStoreInterface::class ); |
| 224 |
$watchedItemStore->expects( $this->exactly( 2 ) ) |
| 225 |
->method( 'resetAllNotificationTimestampsForUser' ); |
| 226 |
|
| 227 |
$manager = $this->getManager( [ |
| 228 |
'options' => $options, |
| 229 |
'watchedItemStore' => $watchedItemStore, |
| 230 |
'userFactory' => $userFactory |
| 231 |
] ); |
| 232 |
|
| 233 |
$manager->clearAllUserNotifications( $userIdentity ); |
| 234 |
$manager->clearAllUserNotifications( $authority ); |
| 235 |
} |
| 236 |
|
| 237 |
public function provideTestPageFactory() { |
| 238 |
yield [ static function ( $pageId, $namespace, $dbKey ) { |
| 239 |
return new TitleValue( $namespace, $dbKey ); |
| 240 |
} ]; |
| 241 |
yield [ static function ( $pageId, $namespace, $dbKey ) { |
| 242 |
return new PageIdentityValue( $pageId, $namespace, $dbKey, PageIdentityValue::LOCAL ); |
| 243 |
} ]; |
| 244 |
yield [ function ( $pageId, $namespace, $dbKey ) { |
| 245 |
return $this->makeMockTitle( $dbKey, [ |
| 246 |
'id' => $pageId, |
| 247 |
'namespace' => $namespace |
| 248 |
] ); |
| 249 |
} ]; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public function testClearTitleUserNotifications_readOnly( $testPageFactory ) { |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
$title = $testPageFactory( 100, 0, 'SomeDbKey' ); |
| 260 |
|
| 261 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 262 |
[ $authority, ] = $this->getAuthorityAndUserFactory( $userIdentity ); |
| 263 |
|
| 264 |
$watchedItemStore = $this->createMock( WatchedItemStoreInterface::class ); |
| 265 |
$watchedItemStore->expects( $this->never() ) |
| 266 |
->method( 'resetNotificationTimestamp' ); |
| 267 |
|
| 268 |
$manager = $this->getManager( [ |
| 269 |
'readOnly' => true, |
| 270 |
'watchedItemStore' => $watchedItemStore |
| 271 |
] ); |
| 272 |
|
| 273 |
$manager->clearTitleUserNotifications( $userIdentity, $title ); |
| 274 |
$manager->clearTitleUserNotifications( $authority, $title ); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
public function testClearTitleUserNotifications_noPerms( $testPageFactory ) { |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
$title = $testPageFactory( 100, 0, 'SomeDbKey' ); |
| 285 |
|
| 286 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 287 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( $userIdentity ); |
| 288 |
|
| 289 |
$watchedItemStore = $this->createMock( WatchedItemStoreInterface::class ); |
| 290 |
$watchedItemStore->expects( $this->never() ) |
| 291 |
->method( 'resetNotificationTimestamp' ); |
| 292 |
|
| 293 |
$manager = $this->getManager( [ |
| 294 |
'watchedItemStore' => $watchedItemStore, |
| 295 |
'userFactory' => $userFactory |
| 296 |
] ); |
| 297 |
|
| 298 |
$manager->clearTitleUserNotifications( $userIdentity, $title ); |
| 299 |
$manager->clearTitleUserNotifications( $authority, $title ); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public function testClearTitleUserNotifications_configDisabled( $testPageFactory ) { |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
$title = $testPageFactory( 100, NS_USER_TALK, 'PageTitleGoesHere' ); |
| 310 |
|
| 311 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 312 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( |
| 313 |
$userIdentity, |
| 314 |
[ 'editmywatchlist' ] |
| 315 |
); |
| 316 |
|
| 317 |
$watchedItemStore = $this->createMock( WatchedItemStoreInterface::class ); |
| 318 |
$watchedItemStore->expects( $this->never() ) |
| 319 |
->method( 'resetNotificationTimestamp' ); |
| 320 |
|
| 321 |
$manager = $this->getManager( [ |
| 322 |
'watchedItemStore' => $watchedItemStore, |
| 323 |
'userFactory' => $userFactory |
| 324 |
] ); |
| 325 |
|
| 326 |
$manager->clearTitleUserNotifications( $userIdentity, $title ); |
| 327 |
$manager->clearTitleUserNotifications( $authority, $title ); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public function testClearTitleUserNotifications_notRegistered( $testPageFactory ) { |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
$title = $testPageFactory( 100, NS_USER_TALK, 'PageTitleGoesHere' ); |
| 338 |
|
| 339 |
$options = [ |
| 340 |
WatchlistManager::OPTION_ENOTIF => true, |
| 341 |
]; |
| 342 |
|
| 343 |
$userIdentity = new UserIdentityValue( 0, 'User Name' ); |
| 344 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( |
| 345 |
$userIdentity, |
| 346 |
[ 'editmywatchlist' ] |
| 347 |
); |
| 348 |
|
| 349 |
$watchedItemStore = $this->createMock( WatchedItemStoreInterface::class ); |
| 350 |
$watchedItemStore->expects( $this->never() ) |
| 351 |
->method( 'resetNotificationTimestamp' ); |
| 352 |
|
| 353 |
$manager = $this->getManager( [ |
| 354 |
'options' => $options, |
| 355 |
'userFactory' => $userFactory, |
| 356 |
'watchedItemStore' => $watchedItemStore |
| 357 |
] ); |
| 358 |
|
| 359 |
$manager->clearTitleUserNotifications( $userIdentity, $title ); |
| 360 |
$manager->clearTitleUserNotifications( $authority, $title ); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
public function testClearTitleUserNotifications( $testPageFactory ) { |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
$title = $testPageFactory( 100, NS_USER_TALK, 'PageTitleGoesHere' ); |
| 371 |
|
| 372 |
$options = [ |
| 373 |
WatchlistManager::OPTION_ENOTIF => true, |
| 374 |
]; |
| 375 |
|
| 376 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 377 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( |
| 378 |
$userIdentity, |
| 379 |
[ 'editmywatchlist' ] |
| 380 |
); |
| 381 |
|
| 382 |
$watchedItemStore = $this->createMock( WatchedItemStoreInterface::class ); |
| 383 |
$watchedItemStore->expects( $this->exactly( 2 ) ) |
| 384 |
->method( 'resetNotificationTimestamp' ); |
| 385 |
|
| 386 |
$manager = $this->getManager( [ |
| 387 |
'options' => $options, |
| 388 |
'userFactory' => $userFactory, |
| 389 |
'watchedItemStore' => $watchedItemStore |
| 390 |
] ); |
| 391 |
|
| 392 |
$manager->clearTitleUserNotifications( $userIdentity, $title ); |
| 393 |
$manager->clearTitleUserNotifications( $authority, $title ); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
public function testGetTitleNotificationTimestamp_falseyId( $testPageFactory ) { |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
$userIdentity = new UserIdentityValue( 0, 'User Name' ); |
| 404 |
|
| 405 |
$title = $testPageFactory( 100, 0, 'SomeDbKey' ); |
| 406 |
|
| 407 |
$manager = $this->getManager(); |
| 408 |
|
| 409 |
$res = $manager->getTitleNotificationTimestamp( $userIdentity, $title ); |
| 410 |
|
| 411 |
$this->assertFalse( $res, 'Early return for anonymous users is false' ); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
public function testGetTitleNotificationTimestamp_timestamp( $testPageFactory ) { |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 427 |
|
| 428 |
$title = $testPageFactory( 100, NS_MAIN, 'Page_db_Key_goesHere' ); |
| 429 |
|
| 430 |
$watchedItem = $this->createMock( WatchedItem::class ); |
| 431 |
$watchedItem->expects( $this->once() ) |
| 432 |
->method( 'getNotificationTimestamp' ) |
| 433 |
->willReturn( 'stringTimestamp' ); |
| 434 |
|
| 435 |
$watchedItemStore = $this->createMock( WatchedItemStoreInterface::class ); |
| 436 |
$watchedItemStore->expects( $this->once() ) |
| 437 |
->method( 'getWatchedItem' ) |
| 438 |
->with( $userIdentity, $title ) |
| 439 |
->willReturn( $watchedItem ); |
| 440 |
|
| 441 |
$manager = $this->getManager( [ |
| 442 |
'watchedItemStore' => $watchedItemStore |
| 443 |
] ); |
| 444 |
|
| 445 |
$res = $manager->getTitleNotificationTimestamp( $userIdentity, $title ); |
| 446 |
|
| 447 |
$this->assertSame( |
| 448 |
'stringTimestamp', |
| 449 |
$res, |
| 450 |
'if getWatchedItem returns a WatchedItem, that object\'s timestamp is returned' |
| 451 |
); |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
$res = $manager->getTitleNotificationTimestamp( $userIdentity, $title ); |
| 459 |
$this->assertSame( |
| 460 |
'stringTimestamp', |
| 461 |
$res, |
| 462 |
'if the timestamp is cached getWatchedItem is not called again' |
| 463 |
); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
public function testGetTitleNotificationTimestamp_null( $testPageFactory ) { |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 474 |
|
| 475 |
$title = $testPageFactory( 100, NS_MAIN, 'Page_db_Key_goesHere' ); |
| 476 |
|
| 477 |
$watchedItem = $this->createMock( WatchedItem::class ); |
| 478 |
$watchedItem->expects( $this->once() ) |
| 479 |
->method( 'getNotificationTimestamp' ) |
| 480 |
->willReturn( null ); |
| 481 |
|
| 482 |
$watchedItemStore = $this->createMock( WatchedItemStoreInterface::class ); |
| 483 |
$watchedItemStore->expects( $this->once() ) |
| 484 |
->method( 'getWatchedItem' ) |
| 485 |
->with( $userIdentity, $title ) |
| 486 |
->willReturn( $watchedItem ); |
| 487 |
|
| 488 |
$manager = $this->getManager( [ |
| 489 |
'watchedItemStore' => $watchedItemStore |
| 490 |
] ); |
| 491 |
|
| 492 |
$res = $manager->getTitleNotificationTimestamp( $userIdentity, $title ); |
| 493 |
|
| 494 |
$this->assertNull( $res, 'WatchedItem can return null instead of a timestamp' ); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
public function testGetTitleNotificationTimestamp_false( $testPageFactory ) { |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 505 |
|
| 506 |
$title = $testPageFactory( 100, NS_MAIN, 'Page_db_Key_goesHere' ); |
| 507 |
|
| 508 |
$watchedItemStore = $this->createMock( WatchedItemStoreInterface::class ); |
| 509 |
$watchedItemStore->expects( $this->once() ) |
| 510 |
->method( 'getWatchedItem' ) |
| 511 |
->with( $userIdentity, $title ) |
| 512 |
->willReturn( false ); |
| 513 |
|
| 514 |
$manager = $this->getManager( [ |
| 515 |
'watchedItemStore' => $watchedItemStore |
| 516 |
] ); |
| 517 |
|
| 518 |
$res = $manager->getTitleNotificationTimestamp( $userIdentity, $title ); |
| 519 |
|
| 520 |
$this->assertFalse( |
| 521 |
$res, |
| 522 |
'getWatchedItem can return false if the item is not watched' |
| 523 |
); |
| 524 |
} |
| 525 |
|
| 526 |
public function testIsWatchable() { |
| 527 |
$manager = $this->getManager( [ 'readOnly' => 'never' ] ); |
| 528 |
|
| 529 |
$target = new PageReferenceValue( NS_USER, __METHOD__, PageReference::LOCAL ); |
| 530 |
$this->assertTrue( $manager->isWatchable( $target ) ); |
| 531 |
} |
| 532 |
|
| 533 |
public static function provideNotIsWatchable() { |
| 534 |
yield [ new PageReferenceValue( NS_SPECIAL, 'Contributions', PageReference::LOCAL ) ]; |
| 535 |
yield [ Title::makeTitle( NS_MAIN, '', 'References' ) ]; |
| 536 |
yield [ Title::makeTitle( NS_MAIN, 'Foo', '', 'acme' ) ]; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
public function testNotIsWatchable( $target ) { |
| 543 |
$manager = $this->getManager( [ 'readOnly' => 'never' ] ); |
| 544 |
|
| 545 |
$this->assertFalse( $manager->isWatchable( $target ) ); |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
public function testSetWatchWithExpiry() { |
| 552 |
|
| 553 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 554 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( |
| 555 |
$userIdentity, |
| 556 |
[ 'editmywatchlist' ] |
| 557 |
); |
| 558 |
$title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL ); |
| 559 |
|
| 560 |
$watchedItemStore = $this->getDummyWatchedItemStore(); |
| 561 |
$watchedItemStore->expects( $this->exactly( 4 ) )->method( 'addWatch' ); |
| 562 |
$watchedItemStore->expects( $this->never() )->method( 'removeWatch' ); |
| 563 |
|
| 564 |
$watchlistManager = $this->getManager( [ |
| 565 |
'userFactory' => $userFactory, |
| 566 |
'watchedItemStore' => $watchedItemStore, |
| 567 |
] ); |
| 568 |
|
| 569 |
$watchlistManager->addWatchIgnoringRights( $userIdentity, $title ); |
| 570 |
|
| 571 |
$status = $watchlistManager->setWatch( true, $authority, $title, '1 week' ); |
| 572 |
|
| 573 |
$this->assertStatusGood( $status ); |
| 574 |
$this->assertTrue( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) ); |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
public function testSetWatchUserNotLoggedIn() { |
| 581 |
$userIdentity = new UserIdentityValue( 0, 'User Name' ); |
| 582 |
$performer = $this->mockUserAuthorityWithPermissions( $userIdentity, [ 'editmywatchlist' ] ); |
| 583 |
$title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL ); |
| 584 |
|
| 585 |
$watchedItemStore = $this->getDummyWatchedItemStore(); |
| 586 |
$watchedItemStore->expects( $this->never() )->method( 'addWatch' ); |
| 587 |
$watchedItemStore->expects( $this->never() )->method( 'removeWatch' ); |
| 588 |
|
| 589 |
$watchlistManager = $this->getManager( [ |
| 590 |
'watchedItemStore' => $watchedItemStore, |
| 591 |
] ); |
| 592 |
|
| 593 |
$status = $watchlistManager->setWatch( true, $performer, $title ); |
| 594 |
|
| 595 |
|
| 596 |
$this->assertStatusGood( $status ); |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
public function testSetWatchSkipsIfAlreadyWatched() { |
| 603 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 604 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( |
| 605 |
$userIdentity, |
| 606 |
[ 'editmywatchlist' ] |
| 607 |
); |
| 608 |
$title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL ); |
| 609 |
|
| 610 |
$watchedItemStore = $this->getDummyWatchedItemStore(); |
| 611 |
$watchedItemStore->expects( $this->exactly( 2 ) )->method( 'addWatch' ); |
| 612 |
$watchedItemStore->expects( $this->never() )->method( 'removeWatch' ); |
| 613 |
|
| 614 |
$watchlistManager = $this->getManager( [ |
| 615 |
'userFactory' => $userFactory, |
| 616 |
'watchedItemStore' => $watchedItemStore, |
| 617 |
] ); |
| 618 |
|
| 619 |
$expiry = '99990123000000'; |
| 620 |
$watchlistManager->addWatchIgnoringRights( $userIdentity, $title, $expiry ); |
| 621 |
|
| 622 |
|
| 623 |
$status = $watchlistManager->setWatch( true, $authority, $title, $expiry ); |
| 624 |
|
| 625 |
$this->assertStatusGood( $status ); |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
public function testSetWatchSkipsIfAlreadyUnWatched() { |
| 632 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 633 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( |
| 634 |
$userIdentity, |
| 635 |
[ 'editmywatchlist' ] |
| 636 |
); |
| 637 |
$title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL ); |
| 638 |
|
| 639 |
$watchedItemStore = $this->getDummyWatchedItemStore(); |
| 640 |
$watchedItemStore->expects( $this->never() )->method( 'addWatch' ); |
| 641 |
$watchedItemStore->expects( $this->never() )->method( 'removeWatch' ); |
| 642 |
|
| 643 |
$watchlistManager = $this->getManager( [ |
| 644 |
'userFactory' => $userFactory, |
| 645 |
'watchedItemStore' => $watchedItemStore, |
| 646 |
] ); |
| 647 |
|
| 648 |
$status = $watchlistManager->setWatch( false, $authority, $title ); |
| 649 |
|
| 650 |
$this->assertStatusGood( $status ); |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
public function testSetWatchWatchesIfWatch() { |
| 657 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 658 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( |
| 659 |
$userIdentity, |
| 660 |
[ 'editmywatchlist' ] |
| 661 |
); |
| 662 |
$title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL ); |
| 663 |
|
| 664 |
$watchedItemStore = $this->getDummyWatchedItemStore(); |
| 665 |
$watchlistManager = $this->getManager( [ |
| 666 |
'userFactory' => $userFactory, |
| 667 |
'watchedItemStore' => $watchedItemStore, |
| 668 |
] ); |
| 669 |
|
| 670 |
$watchlistManager->addWatchIgnoringRights( $userIdentity, $title ); |
| 671 |
|
| 672 |
$status = $watchlistManager->setWatch( true, $authority, $title ); |
| 673 |
|
| 674 |
$this->assertStatusGood( $status ); |
| 675 |
$this->assertTrue( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) ); |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
public function testSetWatchUnwatchesIfUnwatch() { |
| 682 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 683 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( |
| 684 |
$userIdentity, |
| 685 |
[ 'editmywatchlist' ] |
| 686 |
); |
| 687 |
$title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL ); |
| 688 |
|
| 689 |
$watchedItemStore = $this->getDummyWatchedItemStore(); |
| 690 |
$watchlistManager = $this->getManager( [ |
| 691 |
'userFactory' => $userFactory, |
| 692 |
'watchedItemStore' => $watchedItemStore, |
| 693 |
] ); |
| 694 |
|
| 695 |
$status = $watchlistManager->setWatch( false, $authority, $title ); |
| 696 |
|
| 697 |
$this->assertStatusGood( $status ); |
| 698 |
$this->assertFalse( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) ); |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
public function testAddWatchNoCheckRights() { |
| 705 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 706 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( |
| 707 |
$userIdentity, |
| 708 |
[] |
| 709 |
); |
| 710 |
$title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL ); |
| 711 |
|
| 712 |
$watchedItemStore = $this->getDummyWatchedItemStore(); |
| 713 |
$watchlistManager = $this->getManager( [ |
| 714 |
'userFactory' => $userFactory, |
| 715 |
'watchedItemStore' => $watchedItemStore, |
| 716 |
] ); |
| 717 |
|
| 718 |
$actual = $watchlistManager->addWatchIgnoringRights( $userIdentity, $title ); |
| 719 |
|
| 720 |
$this->assertStatusGood( $actual ); |
| 721 |
$this->assertTrue( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) ); |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
public function testAddWatchSuccess() { |
| 728 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 729 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( |
| 730 |
$userIdentity, |
| 731 |
[ 'editmywatchlist' ] |
| 732 |
); |
| 733 |
$title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL ); |
| 734 |
|
| 735 |
$watchedItemStore = $this->getDummyWatchedItemStore(); |
| 736 |
$watchlistManager = $this->getManager( [ |
| 737 |
'userFactory' => $userFactory, |
| 738 |
'watchedItemStore' => $watchedItemStore, |
| 739 |
] ); |
| 740 |
|
| 741 |
$actual = $watchlistManager->addWatch( $authority, $title ); |
| 742 |
|
| 743 |
$this->assertStatusGood( $actual ); |
| 744 |
$this->assertTrue( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) ); |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
public function testRemoveWatchUserHookAborted() { |
| 751 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 752 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( |
| 753 |
$userIdentity, |
| 754 |
[ 'editmywatchlist' ] |
| 755 |
); |
| 756 |
$title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL ); |
| 757 |
|
| 758 |
$hookContainer = $this->createHookContainer( [ |
| 759 |
'UnwatchArticle' => static function () { |
| 760 |
return false; |
| 761 |
}, |
| 762 |
] ); |
| 763 |
$watchedItemStore = $this->getDummyWatchedItemStore(); |
| 764 |
$watchlistManager = $this->getManager( [ |
| 765 |
'hookContainer' => $hookContainer, |
| 766 |
'userFactory' => $userFactory, |
| 767 |
'watchedItemStore' => $watchedItemStore, |
| 768 |
] ); |
| 769 |
|
| 770 |
$watchlistManager->addWatchIgnoringRights( $userIdentity, $title ); |
| 771 |
|
| 772 |
$status = $watchlistManager->removeWatch( $authority, $title ); |
| 773 |
|
| 774 |
$this->assertStatusError( 'hookaborted', $status ); |
| 775 |
$this->assertTrue( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) ); |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
public function testRemoveWatchSuccess() { |
| 782 |
$userIdentity = new UserIdentityValue( 100, 'User Name' ); |
| 783 |
[ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( |
| 784 |
$userIdentity, |
| 785 |
[ 'editmywatchlist' ] |
| 786 |
); |
| 787 |
$title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL ); |
| 788 |
|
| 789 |
$watchedItemStore = $this->getDummyWatchedItemStore(); |
| 790 |
$watchlistManager = $this->getManager( [ |
| 791 |
'userFactory' => $userFactory, |
| 792 |
'watchedItemStore' => $watchedItemStore, |
| 793 |
] ); |
| 794 |
|
| 795 |
$watchlistManager->addWatchIgnoringRights( $userIdentity, $title ); |
| 796 |
|
| 797 |
$status = $watchlistManager->removeWatch( $authority, $title ); |
| 798 |
|
| 799 |
$this->assertStatusGood( $status ); |
| 800 |
$this->assertFalse( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) ); |
| 801 |
} |
| 802 |
} |