| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MainConfigNames; |
| 4 |
use MediaWiki\User\Options\StaticUserOptionsLookup; |
| 5 |
use MediaWiki\User\UserIdentityValue; |
| 6 |
use MediaWiki\Utils\MWTimestamp; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class MWTimestampTest extends MediaWikiLangTestCase { |
| 12 |
|
| 13 |
private function setMockUserOptions( array $options ) { |
| 14 |
$defaults = $this->getServiceContainer()->getMainConfig()->get( MainConfigNames::DefaultUserOptions ); |
| 15 |
|
| 16 |
|
| 17 |
$userOptionsLookup = new StaticUserOptionsLookup( |
| 18 |
[ 'Pamela' => $options ], |
| 19 |
$defaults |
| 20 |
); |
| 21 |
|
| 22 |
$this->setService( 'UserOptionsLookup', $userOptionsLookup ); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public function testRelativeTimestamp( |
| 29 |
$tsTime, |
| 30 |
$currentTime, |
| 31 |
$timeCorrection, |
| 32 |
$dateFormat, |
| 33 |
$expectedOutput, |
| 34 |
$desc |
| 35 |
) { |
| 36 |
$this->setMockUserOptions( [ |
| 37 |
'timecorrection' => $timeCorrection, |
| 38 |
'date' => $dateFormat |
| 39 |
] ); |
| 40 |
|
| 41 |
$user = new UserIdentityValue( 13, 'Pamela' ); |
| 42 |
|
| 43 |
$tsTime = new MWTimestamp( $tsTime ); |
| 44 |
$currentTime = new MWTimestamp( $currentTime ); |
| 45 |
|
| 46 |
$this->assertEquals( |
| 47 |
$expectedOutput, |
| 48 |
$tsTime->getRelativeTimestamp( $currentTime, $user ), |
| 49 |
$desc |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
public static function provideRelativeTimestampTests() { |
| 54 |
return [ |
| 55 |
[ |
| 56 |
'20111231170000', |
| 57 |
'20120101000000', |
| 58 |
'Offset|0', |
| 59 |
'mdy', |
| 60 |
'7 hours ago', |
| 61 |
'"Yesterday" across years', |
| 62 |
], |
| 63 |
[ |
| 64 |
'20120717190900', |
| 65 |
'20120717190929', |
| 66 |
'Offset|0', |
| 67 |
'mdy', |
| 68 |
'29 seconds ago', |
| 69 |
'"Just now"', |
| 70 |
], |
| 71 |
[ |
| 72 |
'20120717190900', |
| 73 |
'20120717191530', |
| 74 |
'Offset|0', |
| 75 |
'mdy', |
| 76 |
'6 minutes and 30 seconds ago', |
| 77 |
'Combination of multiple units', |
| 78 |
], |
| 79 |
[ |
| 80 |
'20121006173100', |
| 81 |
'20121006173200', |
| 82 |
'Offset|0', |
| 83 |
'mdy', |
| 84 |
'1 minute ago', |
| 85 |
'"1 minute ago"', |
| 86 |
], |
| 87 |
[ |
| 88 |
'19910130151500', |
| 89 |
'20120716193700', |
| 90 |
'Offset|0', |
| 91 |
'mdy', |
| 92 |
'2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago', |
| 93 |
'A long time ago', |
| 94 |
], |
| 95 |
[ |
| 96 |
'20120101050000', |
| 97 |
'20120101080000', |
| 98 |
'Offset|-360', |
| 99 |
'mdy', |
| 100 |
'3 hours ago', |
| 101 |
'"Yesterday" across years with time correction', |
| 102 |
], |
| 103 |
[ |
| 104 |
'20120714184300', |
| 105 |
'20120716184300', |
| 106 |
'Offset|-420', |
| 107 |
'mdy', |
| 108 |
'2 days ago', |
| 109 |
'Recent weekday with time correction', |
| 110 |
], |
| 111 |
[ |
| 112 |
'20120714184300', |
| 113 |
'20120715040000', |
| 114 |
'Offset|-420', |
| 115 |
'mdy', |
| 116 |
'9 hours and 17 minutes ago', |
| 117 |
'Today at another time with time correction', |
| 118 |
], |
| 119 |
]; |
| 120 |
} |
| 121 |
} |