2012-07-31 19:37:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
2021-05-12 05:57:33 +00:00
|
|
|
use MediaWiki\User\StaticUserOptionsLookup;
|
2021-03-10 21:43:55 +00:00
|
|
|
use MediaWiki\User\UserIdentityValue;
|
2023-08-19 03:35:06 +00:00
|
|
|
use MediaWiki\Utils\MWTimestamp;
|
2021-03-10 21:43:55 +00:00
|
|
|
|
2012-07-31 19:37:01 +00:00
|
|
|
/**
|
MWTimestamp,Message: Widen `@covers` annotations in unit tests
Follows-up I6d845bdfbb80, I69b5385868, I4c7d826c7e, I1287f3979ab, which
widened the `@covers` annotations of other test suites:
> We lose useful coverage and spend valuable time keeping these tags
> accurate through refactors (or worse, forget to do so).
>
> I've audited each test to confirm it is a general test of the
> subject class, where tagging missing methods would be an accepted
> change, thus widening it is merely a no-op that clarifies intent
> and reduces maintenance. I am not disabling the "only track coverage
> of specified subject" mechanism, nor am I claiming coverage in
> in classes outside the subject under test.
>
> Tracking this narrow detail wastes time to keep methods in sync during
> refactors, time to realize (and fix) when other people inevitably
> dodn't keep them in sync, time lost in finding uncovered code to
> write tests for only to realize it is already covered but not tagged.
Change-Id: I7555c9b6b5100e861694dada45e691fd306b72be
2023-06-02 19:16:17 +00:00
|
|
|
* @covers MWTimestamp
|
2012-07-31 19:37:01 +00:00
|
|
|
*/
|
2014-01-25 15:21:09 +00:00
|
|
|
class MWTimestampTest extends MediaWikiLangTestCase {
|
2013-02-14 11:36:35 +00:00
|
|
|
|
2021-03-10 21:43:55 +00:00
|
|
|
private function setMockUserOptions( array $options ) {
|
|
|
|
|
$defaults = $this->getServiceContainer()->getMainConfig()->get( 'DefaultUserOptions' );
|
|
|
|
|
|
2021-05-12 05:57:33 +00:00
|
|
|
// $options are set as the options for "Pamela", the name used in the tests
|
|
|
|
|
$userOptionsLookup = new StaticUserOptionsLookup(
|
|
|
|
|
[ 'Pamela' => $options ],
|
|
|
|
|
$defaults
|
2021-03-10 21:43:55 +00:00
|
|
|
);
|
|
|
|
|
|
2021-05-12 05:57:33 +00:00
|
|
|
$this->setService( 'UserOptionsLookup', $userOptionsLookup );
|
2021-03-10 21:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
2013-01-24 22:38:36 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideRelativeTimestampTests
|
|
|
|
|
*/
|
|
|
|
|
public function testRelativeTimestamp(
|
|
|
|
|
$tsTime, // The timestamp to format
|
|
|
|
|
$currentTime, // The time to consider "now"
|
|
|
|
|
$timeCorrection, // The time offset to use
|
|
|
|
|
$dateFormat, // The date preference to use
|
|
|
|
|
$expectedOutput, // The expected output
|
|
|
|
|
$desc // Description
|
|
|
|
|
) {
|
2021-03-10 21:43:55 +00:00
|
|
|
$this->setMockUserOptions( [
|
|
|
|
|
'timecorrection' => $timeCorrection,
|
|
|
|
|
'date' => $dateFormat
|
|
|
|
|
] );
|
|
|
|
|
|
2021-02-15 18:58:09 +00:00
|
|
|
$user = new UserIdentityValue( 13, 'Pamela' );
|
2013-01-24 22:38:36 +00:00
|
|
|
|
|
|
|
|
$tsTime = new MWTimestamp( $tsTime );
|
|
|
|
|
$currentTime = new MWTimestamp( $currentTime );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$expectedOutput,
|
|
|
|
|
$tsTime->getRelativeTimestamp( $currentTime, $user ),
|
|
|
|
|
$desc
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideRelativeTimestampTests() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2013-01-24 22:38:36 +00:00
|
|
|
'20111231170000',
|
|
|
|
|
'20120101000000',
|
|
|
|
|
'Offset|0',
|
|
|
|
|
'mdy',
|
|
|
|
|
'7 hours ago',
|
|
|
|
|
'"Yesterday" across years',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-01-24 22:38:36 +00:00
|
|
|
'20120717190900',
|
|
|
|
|
'20120717190929',
|
|
|
|
|
'Offset|0',
|
|
|
|
|
'mdy',
|
|
|
|
|
'29 seconds ago',
|
|
|
|
|
'"Just now"',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-01-24 22:38:36 +00:00
|
|
|
'20120717190900',
|
|
|
|
|
'20120717191530',
|
|
|
|
|
'Offset|0',
|
|
|
|
|
'mdy',
|
|
|
|
|
'6 minutes and 30 seconds ago',
|
|
|
|
|
'Combination of multiple units',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-01-24 22:38:36 +00:00
|
|
|
'20121006173100',
|
|
|
|
|
'20121006173200',
|
|
|
|
|
'Offset|0',
|
|
|
|
|
'mdy',
|
|
|
|
|
'1 minute ago',
|
|
|
|
|
'"1 minute ago"',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-01-24 22:38:36 +00:00
|
|
|
'19910130151500',
|
|
|
|
|
'20120716193700',
|
|
|
|
|
'Offset|0',
|
|
|
|
|
'mdy',
|
|
|
|
|
'2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
|
|
|
|
|
'A long time ago',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-01-24 22:38:36 +00:00
|
|
|
'20120101050000',
|
|
|
|
|
'20120101080000',
|
|
|
|
|
'Offset|-360',
|
|
|
|
|
'mdy',
|
|
|
|
|
'3 hours ago',
|
|
|
|
|
'"Yesterday" across years with time correction',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-01-24 22:38:36 +00:00
|
|
|
'20120714184300',
|
|
|
|
|
'20120716184300',
|
|
|
|
|
'Offset|-420',
|
|
|
|
|
'mdy',
|
|
|
|
|
'2 days ago',
|
|
|
|
|
'Recent weekday with time correction',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-01-24 22:38:36 +00:00
|
|
|
'20120714184300',
|
|
|
|
|
'20120715040000',
|
|
|
|
|
'Offset|-420',
|
|
|
|
|
'mdy',
|
|
|
|
|
'9 hours and 17 minutes ago',
|
|
|
|
|
'Today at another time with time correction',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2013-01-24 22:38:36 +00:00
|
|
|
}
|
2012-07-31 19:37:01 +00:00
|
|
|
}
|