| 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 |
|
| 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 |
|
| 32 |
ConvertibleTimestamp::setFakeTime( '20200527000000' ); |
| 33 |
|
| 34 |
|
| 35 |
$watchedItemMonth = new WatchedItem( $user, $target, null, '20200627000000' ); |
| 36 |
$daysRemainingMonth = $watchedItemMonth->getExpiryInDays(); |
| 37 |
$this->assertSame( 31, $daysRemainingMonth ); |
| 38 |
|
| 39 |
|
| 40 |
$watchedItemWeek = new WatchedItem( $user, $target, null, '20200603000000' ); |
| 41 |
$daysRemainingWeek = $watchedItemWeek->getExpiryInDays(); |
| 42 |
$this->assertSame( 7, $daysRemainingWeek ); |
| 43 |
|
| 44 |
|
| 45 |
$watchedItemDay = new WatchedItem( $user, $target, null, '20200528000000' ); |
| 46 |
$daysRemainingDay = $watchedItemDay->getExpiryInDays(); |
| 47 |
$this->assertSame( 1, $daysRemainingDay ); |
| 48 |
|
| 49 |
|
| 50 |
$watchedItemSoon = new WatchedItem( $user, $target, null, '20200527010001' ); |
| 51 |
$daysRemainingSoon = $watchedItemSoon->getExpiryInDays(); |
| 52 |
$this->assertSame( 0, $daysRemainingSoon ); |
| 53 |
|
| 54 |
|
| 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 |
|
| 68 |
|
| 69 |
|
| 70 |
return $this->getMockMessage( |
| 71 |
$params ? ( $key . '-' . $params[0][0] ) : $key |
| 72 |
); |
| 73 |
} |
| 74 |
); |
| 75 |
|
| 76 |
|
| 77 |
ConvertibleTimestamp::setFakeTime( '20200527000000' ); |
| 78 |
|
| 79 |
|
| 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 |
|
| 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 |
|
| 90 |
$daysRemainingWeekText = $watchedItemWeek->getExpiryInDaysText( $messageLocalizer, true ); |
| 91 |
$this->assertSame( 'watchlist-expiry-days-left-7', $daysRemainingWeekText ); |
| 92 |
|
| 93 |
|
| 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 |
|
| 99 |
$watchedItemSoon = new WatchedItem( $user, $target, null, '20200527010001' ); |
| 100 |
$daysRemainingSoonText = $watchedItemSoon->getExpiryInDaysText( $messageLocalizer ); |
| 101 |
$this->assertSame( 'watchlist-expiring-hours-full-text', $daysRemainingSoonText ); |
| 102 |
|
| 103 |
|
| 104 |
$daysRemainingSoonText = $watchedItemSoon->getExpiryInDaysText( $messageLocalizer, true ); |
| 105 |
$this->assertSame( 'watchlist-expiry-hours-left', $daysRemainingSoonText ); |
| 106 |
|
| 107 |
|
| 108 |
$permamentlyWatchedItem = new WatchedItem( $user, $target, null, null ); |
| 109 |
$daysText = $permamentlyWatchedItem->getExpiryInDaysText( $messageLocalizer ); |
| 110 |
$this->assertSame( '', $daysText ); |
| 111 |
} |
| 112 |
|
| 113 |
} |