Debian Codegraph @

tests/phpunit/unit/includes/watchlist/WatchedItemUnitTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Title\TitleValue;
4 use MediaWiki\User\UserIdentityValue;
5 use MediaWiki\Watchlist\WatchedItem;
6 use Wikimedia\Timestamp\ConvertibleTimestamp;
7
8 /**
9 * @covers \MediaWiki\Watchlist\WatchedItem
10 */
11 class WatchedItemUnitTest extends MediaWikiUnitTestCase {
12
13 public function testIsExpired() {
14 $user = new UserIdentityValue( 7, 'MockUser' );
15 $target = new TitleValue( 0, 'SomeDbKey' );
16
17 $notExpired1 = new WatchedItem( $user, $target, null, '20500101000000' );
18 $this->assertFalse( $notExpired1->isExpired() );
19
20 $notExpired2 = new WatchedItem( $user, $target, null );
21 $this->assertFalse( $notExpired2->isExpired() );
22
23 $expired = new WatchedItem( $user, $target, null, '20010101000000' );
24 $this->assertTrue( $expired->isExpired() );
25 }
26
27 public function testgetExpiryInDays() {
28 $user = new UserIdentityValue( 7, 'MockUser' );
29 $target = new TitleValue( 0, 'SomeDbKey' );
30
31 // Fake current time to be 2020-05-27T00:00:00Z
32 ConvertibleTimestamp::setFakeTime( '20200527000000' );
33
34 // Adding a watched item with an expiry of a month from the frozen time
35 $watchedItemMonth = new WatchedItem( $user, $target, null, '20200627000000' );
36 $daysRemainingMonth = $watchedItemMonth->getExpiryInDays();
37 $this->assertSame( 31, $daysRemainingMonth );
38
39 // Adding a watched item with an expiry of 7 days from the frozen time
40 $watchedItemWeek = new WatchedItem( $user, $target, null, '20200603000000' );
41 $daysRemainingWeek = $watchedItemWeek->getExpiryInDays();
42 $this->assertSame( 7, $daysRemainingWeek );
43
44 // Adding a watched item with an expiry of 1 day from the frozen time
45 $watchedItemDay = new WatchedItem( $user, $target, null, '20200528000000' );
46 $daysRemainingDay = $watchedItemDay->getExpiryInDays();
47 $this->assertSame( 1, $daysRemainingDay );
48
49 // Adding a watched item with an expiry in less than 1 day from the frozen time
50 $watchedItemSoon = new WatchedItem( $user, $target, null, '20200527010001' );
51 $daysRemainingSoon = $watchedItemSoon->getExpiryInDays();
52 $this->assertSame( 0, $daysRemainingSoon );
53
54 // Adding a watched item with no expiry
55 $permamentlyWatchedItem = new WatchedItem( $user, $target, null, null );
56 $days = $permamentlyWatchedItem->getExpiryInDays();
57 $this->assertSame( null, $days );
58 }
59
60 public function testgetExpiryInDaysText() {
61 $user = new UserIdentityValue( 7, 'MockUser' );
62 $target = new TitleValue( 0, 'SomeDbKey' );
63
64 $messageLocalizer = $this->createMock( MessageLocalizer::class );
65 $messageLocalizer->method( 'msg' )->willReturnCallback(
66 function ( $key, ...$params ) {
67 // Right now we aren't worrying about the message formatting,
68 // just testing if the correct parameter is there by adding it
69 // to the end. Use MediaWikiTestCaseTrait::getMockMessage
70 return $this->getMockMessage(
71 $params ? ( $key . '-' . $params[0][0] ) : $key
72 );
73 }
74 );
75
76 // Fake current time to be 2020-05-27T00:00:00Z
77 ConvertibleTimestamp::setFakeTime( '20200527000000' );
78
79 // Adding a watched item with an expiry of a month from the frozen time
80 $watchedItemMonth = new WatchedItem( $user, $target, null, '20200627000000' );
81 $daysRemainingMonthText = $watchedItemMonth->getExpiryInDaysText( $messageLocalizer );
82 $this->assertSame( 'watchlist-expiring-days-full-text-31', $daysRemainingMonthText );
83
84 // Adding a watched item with an expiry of 7 days from the frozen time
85 $watchedItemWeek = new WatchedItem( $user, $target, null, '20200603000000' );
86 $daysRemainingWeekText = $watchedItemWeek->getExpiryInDaysText( $messageLocalizer );
87 $this->assertSame( 'watchlist-expiring-days-full-text-7', $daysRemainingWeekText );
88
89 // Show a watched item with an expiry of 7 days from the frozen time in a dropdown option
90 $daysRemainingWeekText = $watchedItemWeek->getExpiryInDaysText( $messageLocalizer, true );
91 $this->assertSame( 'watchlist-expiry-days-left-7', $daysRemainingWeekText );
92
93 // Adding a watched item with an expiry of 1 day from the frozen time
94 $watchedItemDay = new WatchedItem( $user, $target, null, '20200528000000' );
95 $daysRemainingDayText = $watchedItemDay->getExpiryInDaysText( $messageLocalizer );
96 $this->assertSame( 'watchlist-expiring-days-full-text-1', $daysRemainingDayText );
97
98 // Adding a watched item with an expiry in less than 1 day from the frozen time
99 $watchedItemSoon = new WatchedItem( $user, $target, null, '20200527010001' );
100 $daysRemainingSoonText = $watchedItemSoon->getExpiryInDaysText( $messageLocalizer );
101 $this->assertSame( 'watchlist-expiring-hours-full-text', $daysRemainingSoonText );
102
103 // Text is for a watchlist expiry dropdown
104 $daysRemainingSoonText = $watchedItemSoon->getExpiryInDaysText( $messageLocalizer, true );
105 $this->assertSame( 'watchlist-expiry-hours-left', $daysRemainingSoonText );
106
107 // Adding a watched item with no expiry
108 $permamentlyWatchedItem = new WatchedItem( $user, $target, null, null );
109 $daysText = $permamentlyWatchedItem->getExpiryInDaysText( $messageLocalizer );
110 $this->assertSame( '', $daysText );
111 }
112
113 }