2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
2022-07-15 00:07:38 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
|
|
|
|
|
2011-06-14 21:21:26 +00:00
|
|
|
class TimeAdjustTest extends MediaWikiLangTestCase {
|
Use UserTimeCorrection in Language::userAdjust
There might be slight behaviour differences, particularly in case of
offsets that are > 14 or < -12, but I don't think it's really worth
special-casing that, since other parts of the code already limits
offsets to values in that interval.
Also fix a bug in UserTimeCorrection where ctype_digit wouldn't
recognize negative offsets specified only as an integer.
Also add a lot of tests for userAdjust, actually testing the parsing
logic, and not just the fallback to the local offset (which is what
**all** the previous test cases were doing). Ideally, this test would be
a unit test and be moved elsewhere, but I don't think it's worth doing
that now, since I assume the Language class will be refactored anyway at
some point.
Finally, use DateTime::modify instead of mktime. It's more readable, it
works the same, but is much more compact. It also means we don't have to
suppress any errors (which I think was already unnecessary, but still).
Bug: T322352
Change-Id: Ie0ccdb0c16aaa6ac888ca1e13008a8b214ac8994
2022-10-23 18:12:30 +00:00
|
|
|
private const LOCAL_TZ_OFFSET = 17;
|
|
|
|
|
|
2013-03-22 14:23:28 +00:00
|
|
|
/**
|
2013-10-21 21:09:13 +00:00
|
|
|
* Test offset usage for a given Language::userAdjust
|
2013-03-22 14:23:28 +00:00
|
|
|
* @dataProvider dataUserAdjust
|
2013-10-21 21:09:13 +00:00
|
|
|
* @covers Language::userAdjust
|
2013-03-22 14:23:28 +00:00
|
|
|
*/
|
Use UserTimeCorrection in Language::userAdjust
There might be slight behaviour differences, particularly in case of
offsets that are > 14 or < -12, but I don't think it's really worth
special-casing that, since other parts of the code already limits
offsets to values in that interval.
Also fix a bug in UserTimeCorrection where ctype_digit wouldn't
recognize negative offsets specified only as an integer.
Also add a lot of tests for userAdjust, actually testing the parsing
logic, and not just the fallback to the local offset (which is what
**all** the previous test cases were doing). Ideally, this test would be
a unit test and be moved elsewhere, but I don't think it's worth doing
that now, since I assume the Language class will be refactored anyway at
some point.
Finally, use DateTime::modify instead of mktime. It's more readable, it
works the same, but is much more compact. It also means we don't have to
suppress any errors (which I think was already unnecessary, but still).
Bug: T322352
Change-Id: Ie0ccdb0c16aaa6ac888ca1e13008a8b214ac8994
2022-10-23 18:12:30 +00:00
|
|
|
public function testUserAdjust( string $date, $correction, string $expected ) {
|
|
|
|
|
$this->overrideConfigValue( MainConfigNames::LocalTZoffset, self::LOCAL_TZ_OFFSET );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
Use UserTimeCorrection in Language::userAdjust
There might be slight behaviour differences, particularly in case of
offsets that are > 14 or < -12, but I don't think it's really worth
special-casing that, since other parts of the code already limits
offsets to values in that interval.
Also fix a bug in UserTimeCorrection where ctype_digit wouldn't
recognize negative offsets specified only as an integer.
Also add a lot of tests for userAdjust, actually testing the parsing
logic, and not just the fallback to the local offset (which is what
**all** the previous test cases were doing). Ideally, this test would be
a unit test and be moved elsewhere, but I don't think it's worth doing
that now, since I assume the Language class will be refactored anyway at
some point.
Finally, use DateTime::modify instead of mktime. It's more readable, it
works the same, but is much more compact. It also means we don't have to
suppress any errors (which I think was already unnecessary, but still).
Bug: T322352
Change-Id: Ie0ccdb0c16aaa6ac888ca1e13008a8b214ac8994
2022-10-23 18:12:30 +00:00
|
|
|
$this->assertSame(
|
2014-06-18 21:53:07 +00:00
|
|
|
$expected,
|
Use UserTimeCorrection in Language::userAdjust
There might be slight behaviour differences, particularly in case of
offsets that are > 14 or < -12, but I don't think it's really worth
special-casing that, since other parts of the code already limits
offsets to values in that interval.
Also fix a bug in UserTimeCorrection where ctype_digit wouldn't
recognize negative offsets specified only as an integer.
Also add a lot of tests for userAdjust, actually testing the parsing
logic, and not just the fallback to the local offset (which is what
**all** the previous test cases were doing). Ideally, this test would be
a unit test and be moved elsewhere, but I don't think it's worth doing
that now, since I assume the Language class will be refactored anyway at
some point.
Finally, use DateTime::modify instead of mktime. It's more readable, it
works the same, but is much more compact. It also means we don't have to
suppress any errors (which I think was already unnecessary, but still).
Bug: T322352
Change-Id: Ie0ccdb0c16aaa6ac888ca1e13008a8b214ac8994
2022-10-23 18:12:30 +00:00
|
|
|
$this->getServiceContainer()->getContentLanguage()->userAdjust( $date, $correction )
|
2013-03-22 14:23:28 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function dataUserAdjust() {
|
Use UserTimeCorrection in Language::userAdjust
There might be slight behaviour differences, particularly in case of
offsets that are > 14 or < -12, but I don't think it's really worth
special-casing that, since other parts of the code already limits
offsets to values in that interval.
Also fix a bug in UserTimeCorrection where ctype_digit wouldn't
recognize negative offsets specified only as an integer.
Also add a lot of tests for userAdjust, actually testing the parsing
logic, and not just the fallback to the local offset (which is what
**all** the previous test cases were doing). Ideally, this test would be
a unit test and be moved elsewhere, but I don't think it's worth doing
that now, since I assume the Language class will be refactored anyway at
some point.
Finally, use DateTime::modify instead of mktime. It's more readable, it
works the same, but is much more compact. It also means we don't have to
suppress any errors (which I think was already unnecessary, but still).
Bug: T322352
Change-Id: Ie0ccdb0c16aaa6ac888ca1e13008a8b214ac8994
2022-10-23 18:12:30 +00:00
|
|
|
// Note: make sure to use dates in the past, especially with geographical time zones, to avoid any
|
|
|
|
|
// chance of tests failing due to a change to the time zone rules.
|
|
|
|
|
yield 'Literal int 0 (technically undocumented)' => [ '20221015120000', 0, '20221015120000' ];
|
|
|
|
|
yield 'Literal int 2 (technically undocumented)' => [ '20221015120000', 2, '20221015140000' ];
|
|
|
|
|
yield 'Literal int -2 (technically undocumented)' => [ '20221015120000', -2, '20221015100000' ];
|
|
|
|
|
|
|
|
|
|
yield 'Literal 0' => [ '20221015120000', '0', '20221015120000' ];
|
|
|
|
|
yield 'Literal 5' => [ '20221015120000', '5', '20221015170000' ];
|
|
|
|
|
yield 'Literal -5' => [ '20221015120000', '-5', '20221015070000' ];
|
|
|
|
|
|
|
|
|
|
$offsetsData = [
|
|
|
|
|
'+00:00' => [ '20221015120000', '20221015120000', 0 ],
|
|
|
|
|
'+02:00' => [ '20221015120000', '20221015140000', 2 * 60 ],
|
|
|
|
|
'+02:15' => [ '20221015120000', '20221015141500', 2.25 * 60 ],
|
|
|
|
|
'+14:00' => [ '20221015120000', '20221016020000', 14 * 60 ],
|
|
|
|
|
'-06:00' => [ '20221015120000', '20221015060000', -6 * 60 ],
|
|
|
|
|
'-06:45' => [ '20221015120000', '20221015051500', -6.75 * 60 ],
|
|
|
|
|
'-12:00' => [ '20221015120000', '20221015000000', -12 * 60 ],
|
|
|
|
|
];
|
|
|
|
|
foreach ( $offsetsData as $offset => [ $time, $expected, $minutesVal ] ) {
|
|
|
|
|
yield "Literal $offset" => [ $time, $offset, $expected ];
|
|
|
|
|
yield "Full format $offset" => [ $time, "Offset|$minutesVal", $expected ];
|
|
|
|
|
}
|
|
|
|
|
yield 'Literal +15:00, capped at +14' => [ '20221015120000', '+15:00', '20221016020000' ];
|
|
|
|
|
yield 'Full format +15:00, capped at +14' => [ '20221015120000', 'Offset|' . ( 15 * 60 ), '20221016020000' ];
|
|
|
|
|
yield 'Literal -13:00, capped at -12' => [ '20221015120000', '-13:00', '20221015000000' ];
|
|
|
|
|
yield 'Full format -13:00, capped at -12' => [ '20221015120000', 'Offset|' . ( -13 * 60 ), '20221015000000' ];
|
|
|
|
|
|
|
|
|
|
yield 'Geo: Europe/Rome when +2 and +2 is stored' => [
|
|
|
|
|
'20221015120000',
|
|
|
|
|
'ZoneInfo|120|Europe/Rome',
|
|
|
|
|
'20221015140000'
|
|
|
|
|
];
|
|
|
|
|
yield 'Geo: Europe/Rome when +2 and +1 is stored' => [
|
|
|
|
|
'20221015120000',
|
|
|
|
|
'ZoneInfo|60|Europe/Rome',
|
|
|
|
|
'20221015140000'
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
Use UserTimeCorrection in Language::userAdjust
There might be slight behaviour differences, particularly in case of
offsets that are > 14 or < -12, but I don't think it's really worth
special-casing that, since other parts of the code already limits
offsets to values in that interval.
Also fix a bug in UserTimeCorrection where ctype_digit wouldn't
recognize negative offsets specified only as an integer.
Also add a lot of tests for userAdjust, actually testing the parsing
logic, and not just the fallback to the local offset (which is what
**all** the previous test cases were doing). Ideally, this test would be
a unit test and be moved elsewhere, but I don't think it's worth doing
that now, since I assume the Language class will be refactored anyway at
some point.
Finally, use DateTime::modify instead of mktime. It's more readable, it
works the same, but is much more compact. It also means we don't have to
suppress any errors (which I think was already unnecessary, but still).
Bug: T322352
Change-Id: Ie0ccdb0c16aaa6ac888ca1e13008a8b214ac8994
2022-10-23 18:12:30 +00:00
|
|
|
yield 'Geo: Europe/Rome when +1 and +2 is stored' => [
|
|
|
|
|
'20220320120000',
|
|
|
|
|
'ZoneInfo|120|Europe/Rome',
|
|
|
|
|
'20220320130000'
|
|
|
|
|
];
|
|
|
|
|
yield 'Geo: Europe/Rome when +1 and +1 is stored' => [
|
|
|
|
|
'20220320120000',
|
|
|
|
|
'ZoneInfo|60|Europe/Rome',
|
|
|
|
|
'20220320130000'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield 'Invalid geographical zone, fall back to offset' => [
|
|
|
|
|
'20221015120000',
|
|
|
|
|
'ZoneInfo|42|Eriador/Hobbiton',
|
|
|
|
|
'20221015124200'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// These fall back to the local offset
|
|
|
|
|
yield 'System 0, fallback to local offset' => [ '20221015120000', 'System|0', '20221015121700' ];
|
|
|
|
|
yield 'System 120, fallback to local offset' => [ '20221015120000', 'System|120', '20221015121700' ];
|
|
|
|
|
yield 'System -60, fallback to local offset' => [ '20221015120000', 'System|-60', '20221015121700' ];
|
|
|
|
|
|
|
|
|
|
yield 'Garbage, fallback to local offset' => [ '20221015120000', 'WhatAmIEvenDoingHere', '20221015121700' ];
|
|
|
|
|
yield 'Empty string, fallback to local offset' => [ '20221015120000', '', '20221015121700' ];
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
}
|