This patch simplifies and fixes a few issues in code related to the 'timecorrection' user setting: - Always re-apply UserTimeCorrection normalization to the preference value, although it should be already normalized - Avoid duplicating code from UserTimeCorrection, both for the pipe-splitting and the offset computation/fallback - Use better variable names - Inject an ITextFormatter for generating the dropdown options, instead of a ContextSource (ew) or a Language, removing calls to wfMessage as well. Note that the ITextFormatter is not injected into the preferences factory because the eventual goal is to move the code to a new HTMLFormField class. - In TimezoneFilter, remove a redundant check: the value comes from the form, and the option for using the system time is always "System|XXX", never just "System". This seems to have been introduced in I2cadac00e46dff2bc7d81ac2f294ea2ae4e72f47; the previous code was only comparing $data[0], and not $tz. Change the test accordingly and add a test case. - Add missing star to docblocks in UserTimeCorrection, as well as a missing int cast. - Fix typo and other style issues in UserTimeCorrectionTest - Bonus: add missing docblock star in HTMLApiField Bug: T309629 Change-Id: Iab35eb17259826429e4b6bc1ba7385ab57884e98
129 lines
4.9 KiB
PHP
129 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Tests\User;
|
|
|
|
use DateTime;
|
|
use MediaWiki\User\UserTimeCorrection;
|
|
use MediaWikiUnitTestCase;
|
|
|
|
class UserTimeCorrectionTest extends MediaWikiUnitTestCase {
|
|
|
|
/**
|
|
* @covers \MediaWiki\User\UserTimeCorrection::__construct
|
|
* @covers \MediaWiki\User\UserTimeCorrection::__toString
|
|
* @covers \MediaWiki\User\UserTimeCorrection::isValid
|
|
* @dataProvider provideTimeCorrectionExamples
|
|
*
|
|
* @param string $input
|
|
* @param string $expected
|
|
* @param bool $isValid
|
|
*/
|
|
public function testParser( $input, $expected, $isValid ) {
|
|
$value = new UserTimeCorrection( $input );
|
|
self::assertEquals( $expected, (string)$value );
|
|
self::assertEquals( $isValid, $value->isValid() );
|
|
}
|
|
|
|
public function provideTimeCorrectionExamples() {
|
|
return [
|
|
[ '', 'System|0', false ],
|
|
[ 'bogus', 'System|0', false ],
|
|
[ 'ZoneInfo', 'Offset|0', false ],
|
|
[ 'ZoneInfo|bogus', 'Offset|0', false ],
|
|
[ 'ZoneInfo|120|Africa/Johannesburg|bogus', 'Offset|120', false ],
|
|
[ 'ZoneInfo|120|Unknown/Unknown', 'Offset|120', false ],
|
|
// Africa/Johannesburg has not DST
|
|
[ 'ZoneInfo|0|Africa/Johannesburg', 'ZoneInfo|120|Africa/Johannesburg', true ],
|
|
[ 'ZoneInfo|120|Africa/Johannesburg', 'ZoneInfo|120|Africa/Johannesburg', true ],
|
|
// Deprecated timezone name
|
|
[ 'ZoneInfo|330|Asia/Calcutta', 'ZoneInfo|330|Asia/Calcutta', true ],
|
|
// Timezone identifier with space in name
|
|
[ 'ZoneInfo|-420|America/Dawson_Creek', 'ZoneInfo|-420|America/Dawson_Creek', true ],
|
|
[ 'System', 'System|0', true ],
|
|
[ 'System|0', 'System|0', true ],
|
|
[ 'System|120', 'System|0', true ],
|
|
[ '2:30', 'Offset|150', true ],
|
|
[ '02:30', 'Offset|150', true ],
|
|
[ '+02:30', 'Offset|150', true ],
|
|
[ '0230', 'Offset|840', false ],
|
|
[ '2', 'Offset|120', true ],
|
|
[ '14:00', 'Offset|840', true ],
|
|
[ '-12:00', 'Offset|-720', true ],
|
|
[ '15:00', 'Offset|840', false ],
|
|
[ '-13:00', 'Offset|-720', false ],
|
|
[ '2:30:40', 'Offset|150', true ],
|
|
[ '2:30bogus', 'Offset|150', true ],
|
|
[ '2.50', 'System|0', false ],
|
|
[ 'UTC-8', 'System|0', false ],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @covers \MediaWiki\User\UserTimeCorrection::__construct
|
|
* @covers \MediaWiki\User\UserTimeCorrection::__toString
|
|
* @covers \MediaWiki\User\UserTimeCorrection::isValid
|
|
* @dataProvider provideServerTZoffsetExamples
|
|
*
|
|
* @param int $serverOffset
|
|
* @param string $input
|
|
* @param string $expected
|
|
* @param bool $isValid
|
|
*/
|
|
public function testServerOffset( int $serverOffset, string $input, string $expected, bool $isValid ) {
|
|
$value = new UserTimeCorrection( $input, null, $serverOffset );
|
|
self::assertEquals( $expected, (string)$value );
|
|
self::assertEquals( $isValid, $value->isValid() );
|
|
}
|
|
|
|
public function provideServerTZoffsetExamples() {
|
|
return [
|
|
[ 120, '', 'System|120', false ],
|
|
[ 120, 'bogus', 'System|120', false ],
|
|
[ 120, 'System', 'System|120', true ],
|
|
[ 120, 'System|120', 'System|120', true ],
|
|
[ -120, 'System|-120', 'System|-120', true ],
|
|
[ 840, 'System|840', 'System|840', true ],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @covers \MediaWiki\User\UserTimeCorrection::__construct
|
|
* @covers \MediaWiki\User\UserTimeCorrection::__toString
|
|
* @covers \MediaWiki\User\UserTimeCorrection::isValid
|
|
* @dataProvider provideDSTVariations
|
|
*
|
|
* @param DateTime $date Date/time to which the correction would apply
|
|
* @param string $input
|
|
* @param string $expected
|
|
* @param bool $isValid
|
|
*/
|
|
public function testDSTVariations( DateTime $date, $input, $expected, $isValid ) {
|
|
$value = new UserTimeCorrection( $input, $date );
|
|
self::assertEquals( $expected, (string)$value );
|
|
self::assertEquals( $isValid, $value->isValid() );
|
|
}
|
|
|
|
public function provideDSTVariations() {
|
|
// Amsterdam observes DST. Johannesburg does not
|
|
return [
|
|
[ new DateTime( '2020-12-01' ), 'ZoneInfo|60|Europe/Amsterdam', 'ZoneInfo|60|Europe/Amsterdam', true ],
|
|
[ new DateTime( '2020-12-01' ), 'ZoneInfo|120|Europe/Amsterdam', 'ZoneInfo|60|Europe/Amsterdam', true ],
|
|
[ new DateTime( '2020-12-01' ), 'ZoneInfo|120|Africa/Johannesburg', 'ZoneInfo|120|Africa/Johannesburg', true ],
|
|
[ new DateTime( '2020-06-01' ), 'ZoneInfo|60|Europe/Amsterdam', 'ZoneInfo|120|Europe/Amsterdam', true ],
|
|
[ new DateTime( '2020-06-01' ), 'ZoneInfo|120|Europe/Amsterdam', 'ZoneInfo|120|Europe/Amsterdam', true ],
|
|
[ new DateTime( '2020-06-01' ), 'ZoneInfo|120|Africa/Johannesburg', 'ZoneInfo|120|Africa/Johannesburg', true ],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @covers \MediaWiki\User\UserTimeCorrection::getTimeOffset
|
|
* @covers \MediaWiki\User\UserTimeCorrection::getTimeOffsetInterval
|
|
* @covers \MediaWiki\User\UserTimeCorrection::getTimeZone
|
|
*/
|
|
public function testAccessors(): void {
|
|
$value = new UserTimeCorrection( 'ZoneInfo|120|Africa/Johannesburg' );
|
|
self::assertEquals( 120, $value->getTimeOffset() );
|
|
self::assertEquals( 120, (int)$value->getTimeOffsetInterval()->format( '%i' ) );
|
|
self::assertEquals( 'Africa/Johannesburg', $value->getTimeZone()->getName() );
|
|
}
|
|
}
|