File: WatchlistNotificationManagerTest.php

package info (click to toggle)
mediawiki 1%3A1.35.13-1%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 274,932 kB
  • sloc: php: 677,563; javascript: 572,709; sql: 11,565; python: 4,447; xml: 3,145; sh: 892; perl: 788; ruby: 496; pascal: 365; makefile: 128
file content (518 lines) | stat: -rw-r--r-- 17,222 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<?php

use MediaWiki\Config\ServiceOptions;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\Permissions\PermissionManager;
use MediaWiki\Revision\RevisionLookup;
use MediaWiki\User\TalkPageNotificationManager;
use MediaWiki\User\UserIdentity;
use MediaWiki\User\WatchlistNotificationManager;

/**
 * @covers \MediaWiki\User\WatchlistNotificationManager
 *
 * Cannot use the name `WatchlistNotificationManagerTest`, already used by the integration test
 * @phpcs:disable MediaWiki.Files.ClassMatchesFilename
 *
 * @author DannyS712
 */
class WatchlistNotificationManagerUnitTest extends MediaWikiUnitTestCase {

	private function getManager( array $params ) {
		$config = $params['config'] ?? [
			'UseEnotif' => false,
			'ShowUpdatedMarker' => false
		];
		$options = new ServiceOptions(
			WatchlistNotificationManager::CONSTRUCTOR_OPTIONS,
			$config
		);

		$hookContainer = $params['hookContainer'] ??
			$this->createNoOpMock( HookContainer::class );

		$permissionManager = $params['permissionManager'] ??
			$this->createNoOpMock( PermissionManager::class );

		$readOnly = $params['readOnly'] ?? false;
		$readOnlyMode = $this->createMock( ReadOnlyMode::class );
		if ( $readOnly !== 'never' ) {
			// Set 'never' for testing getTitleNotificationTimestamp, which doesn't call
			$readOnlyMode->expects( $this->once() )
				->method( 'isReadOnly' )
				->willReturn( $readOnly );
		}

		$revisionLookup = $params['revisionLookup'] ??
			$this->createNoOpAbstractMock( RevisionLookup::class );

		$talkPageNotificationManager = $params['talkPageNotificationManager'] ??
			$this->createNoOpMock( TalkPageNotificationManager::class );

		$watchedItemStore = $params['watchedItemStore'] ??
			$this->createNoOpAbstractMock( WatchedItemStoreInterface::class );

		return new WatchlistNotificationManager(
			$options,
			$hookContainer,
			$permissionManager,
			$readOnlyMode,
			$revisionLookup,
			$talkPageNotificationManager,
			$watchedItemStore
		);
	}

	public function testClearAllUserNotifications_readOnly() {
		$user = $this->createNoOpAbstractMock( UserIdentity::class );

		// ********** Code path #1 **********
		// Early return: read only mode
		$manager = $this->getManager( [
			'readOnly' => true
		] );
		$manager->clearAllUserNotifications( $user );
	}

	public function testClearAllUserNotifications_noPerms() {
		$user = $this->createNoOpAbstractMock( UserIdentity::class );

		// ********** Code path #2 **********
		// Early return: User lacks `editmywatchlist`
		$permissionManager = $this->createMock( PermissionManager::class );
		$permissionManager->expects( $this->once() )
			->method( 'userHasRight' )
			->with(
				$this->equalTo( $user ),
				$this->equalTo( 'editmywatchlist' )
			)
			->willReturn( false );

		$manager = $this->getManager( [
			'permissionManager' => $permissionManager
		] );
		$manager->clearAllUserNotifications( $user );
	}

	public function testClearAllUserNotifications_configDisabled() {
		$user = $this->createNoOpAbstractMock( UserIdentity::class );

		// ********** Code path #3 **********
		// Early return: config with `UseEnotif` and `ShowUpdatedMarker` both false
		$permissionManager = $this->createMock( PermissionManager::class );
		$permissionManager->expects( $this->once() )
			->method( 'userHasRight' )
			->with(
				$this->equalTo( $user ),
				$this->equalTo( 'editmywatchlist' )
			)
			->willReturn( true );
		$talkPageNotificationManager = $this->createMock( TalkPageNotificationManager::class );
		$talkPageNotificationManager->expects( $this->once() )
			->method( 'removeUserHasNewMessages' )
			->with( $this->equalTo( $user ) );

		$manager = $this->getManager( [
			'permissionManager' => $permissionManager,
			'talkPageNotificationManager' => $talkPageNotificationManager
		] );
		$manager->clearAllUserNotifications( $user );
	}

	public function testClearAllUserNotifications_falseyId() {
		// ********** Code path #4 **********
		// Early return: user's id is falsey
		$config = [
			'UseEnotif' => true,
			'ShowUpdatedMarker' => true
		];
		$user = $this->getMockBuilder( UserIdentity::class )
			->setMethods( [ 'getId' ] )
			->getMockForAbstractClass();
		$user->expects( $this->once() )
			->method( 'getId' )
			->willReturn( 0 );
		$permissionManager = $this->createMock( PermissionManager::class );
		$permissionManager->expects( $this->once() )
			->method( 'userHasRight' )
			->with(
				$this->equalTo( $user ),
				$this->equalTo( 'editmywatchlist' )
			)
			->willReturn( true );

		$manager = $this->getManager( [
			'config' => $config,
			'permissionManager' => $permissionManager
		] );
		$manager->clearAllUserNotifications( $user );
	}

	public function testClearAllUserNotifications() {
		// ********** Code path #5 **********
		// No early returns
		$config = [
			'UseEnotif' => true,
			'ShowUpdatedMarker' => true
		];
		$user = $this->getMockBuilder( UserIdentity::class )
			->setMethods( [ 'getId' ] )
			->getMockForAbstractClass();
		$user->expects( $this->once() )
			->method( 'getId' )
			->willReturn( 1 );
		$permissionManager = $this->createMock( PermissionManager::class );
		$permissionManager->expects( $this->once() )
			->method( 'userHasRight' )
			->with(
				$this->equalTo( $user ),
				$this->equalTo( 'editmywatchlist' )
			)
			->willReturn( true );
		$watchedItemStore = $this->getMockBuilder( WatchedItemStoreInterface::class )
			->setMethods( [ 'resetAllNotificationTimestampsForUser' ] )
			->getMockForAbstractClass();
		$watchedItemStore->expects( $this->once() )
			->method( 'resetAllNotificationTimestampsForUser' )
			->with( $this->equalTo( $user ) );

		$manager = $this->getManager( [
			'config' => $config,
			'permissionManager' => $permissionManager,
			'watchedItemStore' => $watchedItemStore
		] );
		$manager->clearAllUserNotifications( $user );
	}

	public function testClearTitleUserNotifications_readOnly() {
		$user = $this->createNoOpAbstractMock( UserIdentity::class );
		$title = $this->createNoOpAbstractMock( LinkTarget::class );

		// ********** Code path #1 **********
		// Early return: read only mode
		$manager = $this->getManager( [
			'readOnly' => true
		] );
		$manager->clearTitleUserNotifications( $user, $title );
	}

	public function testClearTitleUserNotifications_noPerms() {
		$user = $this->createNoOpAbstractMock( UserIdentity::class );
		$title = $this->createNoOpAbstractMock( LinkTarget::class );

		// ********** Code path #2 **********
		// Early return: User lacks `editmywatchlist`
		// readOnlyMode returned false, and since we'll use the same mock again don't only
		// expect it once
		$permissionManager = $this->createMock( PermissionManager::class );
		$permissionManager->expects( $this->once() )
			->method( 'userHasRight' )
			->with(
				$this->equalTo( $user ),
				$this->equalTo( 'editmywatchlist' )
			)
			->willReturn( false );

		$manager = $this->getManager( [
			'permissionManager' => $permissionManager
		] );

		$manager->clearTitleUserNotifications( $user, $title );
	}

	public function testClearTitleUserNotifications_configDisabled() {
		// ********** Code path #3 **********
		// Early return: config with `UseEnotif` and `ShowUpdatedMarker` both false
		// $user and $title are now checked for if the page is the user's talk page
		// Currently only tests for when the title isn't the user's talk page, since
		// DeferredUpdates::addCallableUpdate doesn't work in unit tests
		$user = $this->getMockBuilder( UserIdentity::class )
			->setMethods( [ 'getName' ] )
			->getMockForAbstractClass();
		$user->expects( $this->once() )
			->method( 'getName' )
			->willReturn( 'UserNameGoesHere' );
		$title = $this->getMockBuilder( LinkTarget::class )
			->setMethods( [ 'getNamespace', 'getText' ] )
			->getMockForAbstractClass();
		$title->expects( $this->once() )
			->method( 'getNamespace' )
			->willReturn( NS_USER_TALK );
		$title->expects( $this->once() )
			->method( 'getText' )
			->willReturn( 'PageTitleGoesHere' );
		$permissionManager = $this->createMock( PermissionManager::class );
		$permissionManager->expects( $this->once() )
			->method( 'userHasRight' )
			->with(
				$this->equalTo( $user ),
				$this->equalTo( 'editmywatchlist' )
			)
			->willReturn( true );

		$manager = $this->getManager( [
			'permissionManager' => $permissionManager
		] );
		$manager->clearTitleUserNotifications( $user, $title );
	}

	public function testClearTitleUserNotifications_notRegistered() {
		// ********** Code path #4 **********
		// Early return: user is not registered
		$config = [
			'UseEnotif' => true,
			'ShowUpdatedMarker' => true
		];
		$user = $this->getMockBuilder( UserIdentity::class )
			->setMethods( [ 'getName', 'isRegistered' ] )
			->getMockForAbstractClass();
		$user->expects( $this->once() )
			->method( 'getName' )
			->willReturn( 'UserNameGoesHere' );
		$user->expects( $this->once() )
			->method( 'isRegistered' )
			->willReturn( false );
		$title = $this->getMockBuilder( LinkTarget::class )
			->setMethods( [ 'getNamespace', 'getText' ] )
			->getMockForAbstractClass();
		$title->expects( $this->once() )
			->method( 'getNamespace' )
			->willReturn( NS_USER_TALK );
		$title->expects( $this->once() )
			->method( 'getText' )
			->willReturn( 'PageTitleGoesHere' );
		$permissionManager = $this->createMock( PermissionManager::class );
		$permissionManager->expects( $this->once() )
			->method( 'userHasRight' )
			->with(
				$this->equalTo( $user ),
				$this->equalTo( 'editmywatchlist' )
			)
			->willReturn( true );

		$manager = $this->getManager( [
			'config' => $config,
			'permissionManager' => $permissionManager
		] );
		$manager->clearTitleUserNotifications( $user, $title );
	}

	public function testClearTitleUserNotifications() {
		// ********** Code path #5 **********
		// No early returns; resetNotificationTimestamp is called
		// PermissionManager now expects a different user object
		$config = [
			'UseEnotif' => true,
			'ShowUpdatedMarker' => true
		];
		$user = $this->getMockBuilder( UserIdentity::class )
			->setMethods( [ 'getName', 'isRegistered' ] )
			->getMockForAbstractClass();
		$user->expects( $this->once() )
			->method( 'getName' )
			->willReturn( 'UserNameGoesHere' );
		$user->expects( $this->once() )
			->method( 'isRegistered' )
			->willReturn( true );
		$title = $this->getMockBuilder( LinkTarget::class )
			->setMethods( [ 'getNamespace', 'getText' ] )
			->getMockForAbstractClass();
		$title->expects( $this->once() )
			->method( 'getNamespace' )
			->willReturn( NS_USER_TALK );
		$title->expects( $this->once() )
			->method( 'getText' )
			->willReturn( 'PageTitleGoesHere' );
		$permissionManager = $this->createMock( PermissionManager::class );
		$permissionManager->expects( $this->once() )
			->method( 'userHasRight' )
			->with(
				$this->equalTo( $user ),
				$this->equalTo( 'editmywatchlist' )
			)
			->willReturn( true );
		$watchedItemStore = $this->getMockBuilder( WatchedItemStoreInterface::class )
			->setMethods( [ 'resetNotificationTimestamp' ] )
			->getMockForAbstractClass();
		$watchedItemStore->expects( $this->once() )
			->method( 'resetNotificationTimestamp' )
			->with(
				$this->equalTo( $user ),
				$this->equalTo( $title ),
				$this->equalTo( '' ),
				$this->equalTo( 0 )
			);

		$manager = $this->getManager( [
			'config' => $config,
			'permissionManager' => $permissionManager,
			'watchedItemStore' => $watchedItemStore
		] );
		$manager->clearTitleUserNotifications( $user, $title );
	}

	public function testGetTitleNotificationTimestamp_falseyId() {
		// ********** Code path #1 **********
		// Early return: user id is falsey
		$user = $this->getMockBuilder( UserIdentity::class )
			->setMethods( [ 'getId' ] )
			->getMockForAbstractClass();
		$user->expects( $this->once() )
			->method( 'getId' )
			->willReturn( 0 );
		$title = $this->createNoOpAbstractMock( LinkTarget::class );

		$manager = $this->getManager( [
			'readOnly' => 'never'
		] );
		$res = $manager->getTitleNotificationTimestamp( $user, $title );
		$this->assertFalse( $res, 'Early return for anonymous users is false' );
	}

	public function testGetTitleNotificationTimestamp_timestamp() {
		// ********** Code path #2 **********
		// Early return: value is already cached - will be tested after #3 because
		// an entry in the cache is needed (duh)

		// ********** Code path #3 **********
		// Actually check watchedItemStore, v.1-a - returns a WatchedItem with a timestamp
		// From here on a cache key will be generated each time
		$user = $this->getMockBuilder( UserIdentity::class )
			->setMethods( [ 'getId' ] )
			->getMockForAbstractClass();
		$user->expects( $this->exactly( 2 ) )
			->method( 'getId' )
			->willReturn( 1 );
		$title = $this->getMockBuilder( LinkTarget::class )
			->setMethods( [ 'getNamespace', 'getDBkey' ] )
			->getMockForAbstractClass();
		$title->expects( $this->exactly( 2 ) )
			->method( 'getNamespace' )
			->willReturn( NS_MAIN );
		$title->expects( $this->exactly( 2 ) )
			->method( 'getDBkey' )
			->willReturn( 'Page_db_Key_goesHere' );
		$watchedItem = $this->createMock( WatchedItem::class );
		$watchedItem->expects( $this->once() )
			->method( 'getNotificationTimestamp' )
			->willReturn( 'stringTimestamp' );
		$watchedItemStore = $this->getMockBuilder( WatchedItemStoreInterface::class )
			->setMethods( [ 'getWatchedItem' ] )
			->getMockForAbstractClass();
		$watchedItemStore->expects( $this->once() )
			->method( 'getWatchedItem' )
			->with(
				$this->equalTo( $user ),
				$this->equalTo( $title )
			)
			->willReturn( $watchedItem );

		$manager = $this->getManager( [
			'readOnly' => 'never',
			'watchedItemStore' => $watchedItemStore
		] );
		$res = $manager->getTitleNotificationTimestamp( $user, $title );
		$this->assertSame(
			'stringTimestamp',
			$res,
			'if getWatchedItem returns a WatchedItem, that object\'s timestamp is returned'
		);

		// ********** Code path #2 **********
		// Actually test code path #2 now that there is something in the cache
		// use the same $manager instance (so the value is in the cache, duh)
		// all of the same expectations apply - getWatchedItem shouldn't be called again, and
		// so was only expecting to be called ->once() above
		$res = $manager->getTitleNotificationTimestamp( $user, $title );
		$this->assertSame(
			'stringTimestamp',
			$res,
			'if the timestamp is cached getWatchedItem is not called again'
		);
	}

	public function testGetTitleNotificationTimestamp_null() {
		$user = $this->getMockBuilder( UserIdentity::class )
			->setMethods( [ 'getId' ] )
			->getMockForAbstractClass();
		$user->expects( $this->once() )
			->method( 'getId' )
			->willReturn( 1 );
		$title = $this->getMockBuilder( LinkTarget::class )
			->setMethods( [ 'getNamespace', 'getDBkey' ] )
			->getMockForAbstractClass();
		$title->expects( $this->once() )
			->method( 'getNamespace' )
			->willReturn( NS_MAIN );
		$title->expects( $this->once() )
			->method( 'getDBkey' )
			->willReturn( 'Page_db_Key_goesHere' );

		// ********** Code path #4 **********
		// Actually check watchedItemStore, v.1-b - returns a WatchedItem with null
		$watchedItem = $this->createMock( WatchedItem::class );
		$watchedItem->expects( $this->once() )
			->method( 'getNotificationTimestamp' )
			->willReturn( null );
		$watchedItemStore = $this->getMockBuilder( WatchedItemStoreInterface::class )
			->setMethods( [ 'getWatchedItem' ] )
			->getMockForAbstractClass();
		$watchedItemStore->expects( $this->once() )
			->method( 'getWatchedItem' )
			->with(
				$this->equalTo( $user ),
				$this->equalTo( $title )
			)
			->willReturn( $watchedItem );

		$manager = $this->getManager( [
			'readOnly' => 'never',
			'watchedItemStore' => $watchedItemStore
		] );
		$res = $manager->getTitleNotificationTimestamp( $user, $title );
		$this->assertNull( $res, 'WatchedItem can return null instead of a timestamp' );
	}

	public function testGetTitleNotificationTimestamp_false() {
		$user = $this->getMockBuilder( UserIdentity::class )
			->setMethods( [ 'getId' ] )
			->getMockForAbstractClass();
		$user->expects( $this->once() )
			->method( 'getId' )
			->willReturn( 1 );
		$title = $this->getMockBuilder( LinkTarget::class )
			->setMethods( [ 'getNamespace', 'getDBkey' ] )
			->getMockForAbstractClass();
		$title->expects( $this->once() )
			->method( 'getNamespace' )
			->willReturn( NS_MAIN );
		$title->expects( $this->once() )
			->method( 'getDBkey' )
			->willReturn( 'Page_db_Key_goesHere' );

		// ********** Code path #5 **********
		// Actually check watchedItemStore, v.2 - returns false
		$watchedItemStore = $this->getMockBuilder( WatchedItemStoreInterface::class )
			->setMethods( [ 'getWatchedItem' ] )
			->getMockForAbstractClass();
		$watchedItemStore->expects( $this->once() )
			->method( 'getWatchedItem' )
			->with(
				$this->equalTo( $user ),
				$this->equalTo( $title )
			)
			->willReturn( false );

		$manager = $this->getManager( [
			'readOnly' => 'never',
			'watchedItemStore' => $watchedItemStore
		] );
		$res = $manager->getTitleNotificationTimestamp( $user, $title );
		$this->assertFalse(
			$res,
			'getWatchedItem can return false if the item is not watched'
		);
	}

}