From 0effd172ce2d3b690814dd1b1af3e39e89550154 Mon Sep 17 00:00:00 2001 From: Zhuyifei1999 Date: Fri, 27 Jan 2017 08:51:06 +0000 Subject: [PATCH] translateBlockExpiry: Duration is block expiry minus current time For relative timestamps in $str, strtotime( $str, $now ) returns an absolute Unix timestamp $str since $now, and this timestamp is given to $time. However, Language::formatDuration expects a time duration, not an absolute timestamp. We obtain this duration from the difference between $time, the absolute timestamp of block expiry, and $now, the absolute timestamp of the time in which the block action happened. Tests have been added to test both this patch and 01936fa, the patch that caused this regression. Bug: T156453 Change-Id: I6fd8c02dc3c6456067fe25cb9f33f5b4c78332aa --- languages/Language.php | 5 +-- tests/phpunit/languages/LanguageTest.php | 39 ++++++++++++++++-------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/languages/Language.php b/languages/Language.php index 8c7ce31629..68727bb233 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -4000,8 +4000,9 @@ class Language { if ( $time === false ) { // Unknown format. Return it as-is in case. return $str; } elseif ( $time !== strtotime( $str, $now + 1 ) ) { // It's a relative timestamp. - // The result differs based on current time, so it's a duration length. - return $this->formatDuration( $time ); + // The result differs based on current time, so the difference + // is a fixed duration length. + return $this->formatDuration( $time - $now ); } else { // It's an absolute timestamp. if ( $time === 0 ) { // wfTimestamp() handles 0 as current time instead of epoch. diff --git a/tests/phpunit/languages/LanguageTest.php b/tests/phpunit/languages/LanguageTest.php index e2e64920ef..88bab90799 100644 --- a/tests/phpunit/languages/LanguageTest.php +++ b/tests/phpunit/languages/LanguageTest.php @@ -1585,7 +1585,7 @@ class LanguageTest extends LanguageClassesTestCase { * @covers Language::translateBlockExpiry() * @dataProvider provideTranslateBlockExpiry */ - public function testTranslateBlockExpiry( $expectedData, $str, $desc ) { + public function testTranslateBlockExpiry( $expectedData, $str, $now, $desc ) { $lang = $this->getLang(); if ( is_array( $expectedData ) ) { list( $func, $arg ) = $expectedData; @@ -1593,27 +1593,40 @@ class LanguageTest extends LanguageClassesTestCase { } else { $expected = $expectedData; } - $this->assertEquals( $expected, $lang->translateBlockExpiry( $str ), $desc ); + $this->assertEquals( $expected, $lang->translateBlockExpiry( $str, null, $now ), $desc ); } public static function provideTranslateBlockExpiry() { return [ - [ '2 hours', '2 hours', 'simple data from ipboptions' ], - [ 'indefinite', 'infinite', 'infinite from ipboptions' ], - [ 'indefinite', 'infinity', 'alternative infinite from ipboptions' ], - [ 'indefinite', 'indefinite', 'another alternative infinite from ipboptions' ], - [ [ 'formatDuration', 1023 * 60 * 60 ], '1023 hours', 'relative' ], - [ [ 'formatDuration', -1023 ], '-1023 seconds', 'negative relative' ], - [ [ 'formatDuration', 0 ], 'now', 'now' ], + [ '2 hours', '2 hours', 0, 'simple data from ipboptions' ], + [ 'indefinite', 'infinite', 0, 'infinite from ipboptions' ], + [ 'indefinite', 'infinity', 0, 'alternative infinite from ipboptions' ], + [ 'indefinite', 'indefinite', 0, 'another alternative infinite from ipboptions' ], + [ [ 'formatDuration', 1023 * 60 * 60 ], '1023 hours', 0, 'relative' ], + [ [ 'formatDuration', -1023 ], '-1023 seconds', 0, 'negative relative' ], + [ + [ 'formatDuration', 1023 * 60 * 60 ], + '1023 hours', + wfTimestamp( TS_UNIX, '19910203040506' ), + 'relative with initial timestamp' + ], + [ [ 'formatDuration', 0 ], 'now', 0, 'now' ], [ [ 'timeanddate', '20120102070000' ], '2012-1-1 7:00 +1 day', + 0, 'mixed, handled as absolute' ], - [ [ 'timeanddate', '19910203040506' ], '1991-2-3 4:05:06', 'absolute' ], - [ [ 'timeanddate', '19700101000000' ], '1970-1-1 0:00:00', 'absolute at epoch' ], - [ [ 'timeanddate', '19691231235959' ], '1969-12-31 23:59:59', 'time before epoch' ], - [ 'dummy', 'dummy', 'return garbage as is' ], + [ [ 'timeanddate', '19910203040506' ], '1991-2-3 4:05:06', 0, 'absolute' ], + [ [ 'timeanddate', '19700101000000' ], '1970-1-1 0:00:00', 0, 'absolute at epoch' ], + [ [ 'timeanddate', '19691231235959' ], '1969-12-31 23:59:59', 0, 'time before epoch' ], + [ + [ 'timeanddate', '19910910000000' ], + '10 september', + wfTimestamp( TS_UNIX, '19910203040506' ), + 'partial' + ], + [ 'dummy', 'dummy', 0, 'return garbage as is' ], ]; } -- 2.20.1