Update Codex from v1.20.0 to v1.20.1
[mediawiki.git] / tests / phpunit / includes / language / TimeAdjustTest.php
blob2dfd3960cb9106b06d8e461d3d590d79f81bc3da
1 <?php
3 use MediaWiki\MainConfigNames;
5 /**
6 * @group Language
7 * @covers \MediaWiki\Language\Language::userAdjust
8 */
9 class TimeAdjustTest extends MediaWikiLangTestCase {
10 private const LOCAL_TZ_OFFSET = 17;
12 /**
13 * Test offset usage for a given Language::userAdjust
14 * @dataProvider dataUserAdjust
16 public function testUserAdjust( string $date, $correction, string $expected ) {
17 $this->overrideConfigValue( MainConfigNames::LocalTZoffset, self::LOCAL_TZ_OFFSET );
19 $this->assertSame(
20 $expected,
21 $this->getServiceContainer()->getContentLanguage()->userAdjust( $date, $correction )
25 public static function dataUserAdjust() {
26 // Note: make sure to use dates in the past, especially with geographical time zones, to avoid any
27 // chance of tests failing due to a change to the time zone rules.
28 yield 'Literal int 0 (technically undocumented)' => [ '20221015120000', 0, '20221015120000' ];
29 yield 'Literal int 2 (technically undocumented)' => [ '20221015120000', 2, '20221015140000' ];
30 yield 'Literal int -2 (technically undocumented)' => [ '20221015120000', -2, '20221015100000' ];
32 yield 'Literal 0' => [ '20221015120000', '0', '20221015120000' ];
33 yield 'Literal 5' => [ '20221015120000', '5', '20221015170000' ];
34 yield 'Literal -5' => [ '20221015120000', '-5', '20221015070000' ];
36 $offsetsData = [
37 '+00:00' => [ '20221015120000', '20221015120000', 0 ],
38 '+02:00' => [ '20221015120000', '20221015140000', 2 * 60 ],
39 '+02:15' => [ '20221015120000', '20221015141500', 2.25 * 60 ],
40 '+14:00' => [ '20221015120000', '20221016020000', 14 * 60 ],
41 '-06:00' => [ '20221015120000', '20221015060000', -6 * 60 ],
42 '-06:45' => [ '20221015120000', '20221015051500', -6.75 * 60 ],
43 '-12:00' => [ '20221015120000', '20221015000000', -12 * 60 ],
45 foreach ( $offsetsData as $offset => [ $time, $expected, $minutesVal ] ) {
46 yield "Literal $offset" => [ $time, $offset, $expected ];
47 yield "Full format $offset" => [ $time, "Offset|$minutesVal", $expected ];
49 yield 'Literal +15:00, capped at +14' => [ '20221015120000', '+15:00', '20221016020000' ];
50 yield 'Full format +15:00, capped at +14' => [ '20221015120000', 'Offset|' . ( 15 * 60 ), '20221016020000' ];
51 yield 'Literal -13:00, capped at -12' => [ '20221015120000', '-13:00', '20221015000000' ];
52 yield 'Full format -13:00, capped at -12' => [ '20221015120000', 'Offset|' . ( -13 * 60 ), '20221015000000' ];
54 yield 'Geo: Europe/Rome when +2 and +2 is stored' => [
55 '20221015120000',
56 'ZoneInfo|120|Europe/Rome',
57 '20221015140000'
59 yield 'Geo: Europe/Rome when +2 and +1 is stored' => [
60 '20221015120000',
61 'ZoneInfo|60|Europe/Rome',
62 '20221015140000'
64 yield 'Geo: Europe/Rome when +1 and +2 is stored' => [
65 '20220320120000',
66 'ZoneInfo|120|Europe/Rome',
67 '20220320130000'
69 yield 'Geo: Europe/Rome when +1 and +1 is stored' => [
70 '20220320120000',
71 'ZoneInfo|60|Europe/Rome',
72 '20220320130000'
75 yield 'Invalid geographical zone, fall back to offset' => [
76 '20221015120000',
77 'ZoneInfo|42|Eriador/Hobbiton',
78 '20221015124200'
81 // These fall back to the local offset
82 yield 'System 0, fallback to local offset' => [ '20221015120000', 'System|0', '20221015121700' ];
83 yield 'System 120, fallback to local offset' => [ '20221015120000', 'System|120', '20221015121700' ];
84 yield 'System -60, fallback to local offset' => [ '20221015120000', 'System|-60', '20221015121700' ];
86 yield 'Garbage, fallback to local offset' => [ '20221015120000', 'WhatAmIEvenDoingHere', '20221015121700' ];
87 yield 'Empty string, fallback to local offset' => [ '20221015120000', '', '20221015121700' ];
89 yield 'T32148 - local date in year 10000' => [
90 '99991231235959',
91 'ZoneInfo|600|Asia/Vladivostok',
92 '99991231235959'
94 yield 'T32148 - date in year 10000 due to local offset' => [
95 '99991231235959',
96 'System|0',
97 '99991231235959'