File: WatchedItemUnitTest.php

package info (click to toggle)
mediawiki 1%3A1.43.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 420,592 kB
  • sloc: php: 1,064,309; javascript: 664,315; sql: 9,737; python: 5,743; xml: 3,489; sh: 1,131; makefile: 64
file content (113 lines) | stat: -rw-r--r-- 5,023 bytes parent folder | download | duplicates (2)
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
<?php

use MediaWiki\Title\TitleValue;
use MediaWiki\User\UserIdentityValue;
use MediaWiki\Watchlist\WatchedItem;
use Wikimedia\Timestamp\ConvertibleTimestamp;

/**
 * @covers \MediaWiki\Watchlist\WatchedItem
 */
class WatchedItemUnitTest extends MediaWikiUnitTestCase {

	public function testIsExpired() {
		$user = new UserIdentityValue( 7, 'MockUser' );
		$target = new TitleValue( 0, 'SomeDbKey' );

		$notExpired1 = new WatchedItem( $user, $target, null, '20500101000000' );
		$this->assertFalse( $notExpired1->isExpired() );

		$notExpired2 = new WatchedItem( $user, $target, null );
		$this->assertFalse( $notExpired2->isExpired() );

		$expired = new WatchedItem( $user, $target, null, '20010101000000' );
		$this->assertTrue( $expired->isExpired() );
	}

	public function testgetExpiryInDays() {
		$user = new UserIdentityValue( 7, 'MockUser' );
		$target = new TitleValue( 0, 'SomeDbKey' );

		// Fake current time to be 2020-05-27T00:00:00Z
		ConvertibleTimestamp::setFakeTime( '20200527000000' );

		// Adding a watched item with an expiry of a month from the frozen time
		$watchedItemMonth = new WatchedItem( $user, $target, null, '20200627000000' );
		$daysRemainingMonth = $watchedItemMonth->getExpiryInDays();
		$this->assertSame( 31, $daysRemainingMonth );

		// Adding a watched item with an expiry of 7 days from the frozen time
		$watchedItemWeek = new WatchedItem( $user, $target, null, '20200603000000' );
		$daysRemainingWeek = $watchedItemWeek->getExpiryInDays();
		$this->assertSame( 7, $daysRemainingWeek );

		// Adding a watched item with an expiry of 1 day from the frozen time
		$watchedItemDay = new WatchedItem( $user, $target, null, '20200528000000' );
		$daysRemainingDay = $watchedItemDay->getExpiryInDays();
		$this->assertSame( 1, $daysRemainingDay );

		// Adding a watched item with an expiry in less than 1 day from the frozen time
		$watchedItemSoon = new WatchedItem( $user, $target, null, '20200527010001' );
		$daysRemainingSoon = $watchedItemSoon->getExpiryInDays();
		$this->assertSame( 0, $daysRemainingSoon );

		// Adding a watched item with no expiry
		$permamentlyWatchedItem = new WatchedItem( $user, $target, null, null );
		$days = $permamentlyWatchedItem->getExpiryInDays();
		$this->assertSame( null, $days );
	}

	public function testgetExpiryInDaysText() {
		$user = new UserIdentityValue( 7, 'MockUser' );
		$target = new TitleValue( 0, 'SomeDbKey' );

		$messageLocalizer = $this->createMock( MessageLocalizer::class );
		$messageLocalizer->method( 'msg' )->willReturnCallback(
			function ( $key, ...$params ) {
				// Right now we aren't worrying about the message formatting,
				// just testing if the correct parameter is there by adding it
				// to the end. Use MediaWikiTestCaseTrait::getMockMessage
				return $this->getMockMessage(
					$params ? ( $key . '-' . $params[0][0] ) : $key
				);
			}
		);

		// Fake current time to be 2020-05-27T00:00:00Z
		ConvertibleTimestamp::setFakeTime( '20200527000000' );

		// Adding a watched item with an expiry of a month from the frozen time
		$watchedItemMonth = new WatchedItem( $user, $target, null, '20200627000000' );
		$daysRemainingMonthText = $watchedItemMonth->getExpiryInDaysText( $messageLocalizer );
		$this->assertSame( 'watchlist-expiring-days-full-text-31', $daysRemainingMonthText );

		// Adding a watched item with an expiry of 7 days from the frozen time
		$watchedItemWeek = new WatchedItem( $user, $target, null, '20200603000000' );
		$daysRemainingWeekText = $watchedItemWeek->getExpiryInDaysText( $messageLocalizer );
		$this->assertSame( 'watchlist-expiring-days-full-text-7', $daysRemainingWeekText );

		// Show a watched item with an expiry of 7 days from the frozen time in a dropdown option
		$daysRemainingWeekText = $watchedItemWeek->getExpiryInDaysText( $messageLocalizer, true );
		$this->assertSame( 'watchlist-expiry-days-left-7', $daysRemainingWeekText );

		// Adding a watched item with an expiry of 1 day from the frozen time
		$watchedItemDay = new WatchedItem( $user, $target, null, '20200528000000' );
		$daysRemainingDayText = $watchedItemDay->getExpiryInDaysText( $messageLocalizer );
		$this->assertSame( 'watchlist-expiring-days-full-text-1', $daysRemainingDayText );

		// Adding a watched item with an expiry in less than 1 day from the frozen time
		$watchedItemSoon = new WatchedItem( $user, $target, null, '20200527010001' );
		$daysRemainingSoonText = $watchedItemSoon->getExpiryInDaysText( $messageLocalizer );
		$this->assertSame( 'watchlist-expiring-hours-full-text', $daysRemainingSoonText );

		// Text is for a watchlist expiry dropdown
		$daysRemainingSoonText = $watchedItemSoon->getExpiryInDaysText( $messageLocalizer, true );
		$this->assertSame( 'watchlist-expiry-hours-left', $daysRemainingSoonText );

		// Adding a watched item with no expiry
		$permamentlyWatchedItem = new WatchedItem( $user, $target, null, null );
		$daysText = $permamentlyWatchedItem->getExpiryInDaysText( $messageLocalizer );
		$this->assertSame( '', $daysText );
	}

}