From: David Barratt Date: Mon, 1 Apr 2019 14:27:39 +0000 (-0400) Subject: Fix flaky test BlockListPagerTest::testFormatValue X-Git-Tag: 1.34.0-rc.0~2210^2 X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=commitdiff_plain;h=af0720e5ed66bf00d8165c64ad0aa3c613560e22;p=lhc%2Fweb%2Fwiklou.git Fix flaky test BlockListPagerTest::testFormatValue The test relied on being executed within the same minute as when it starts which is not always the case. Bug: T219695 Change-Id: I99eb0d25138069ca08c2975ff2e60c7f1df0a99f --- diff --git a/includes/specials/pagers/BlockListPager.php b/includes/specials/pagers/BlockListPager.php index 496013b58c..2b8d1d5fee 100644 --- a/includes/specials/pagers/BlockListPager.php +++ b/includes/specials/pagers/BlockListPager.php @@ -161,7 +161,7 @@ class BlockListPager extends TablePager { $formatted .= '
' . $this->msg( 'ipb-blocklist-duration-left', $language->formatDuration( - $timestamp->getTimestamp() - time(), + $timestamp->getTimestamp() - MWTimestamp::time(), // reasonable output [ 'minutes', diff --git a/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php b/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php index 1810ef88c1..bd37c04cd5 100644 --- a/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php +++ b/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php @@ -16,13 +16,18 @@ class BlockListPagerTest extends MediaWikiTestCase { * @dataProvider formatValueEmptyProvider * @dataProvider formatValueDefaultProvider * @param string $name - * @param string $value * @param string $expected */ - public function testFormatValue( $name, $value, $expected, $row = null ) { + public function testFormatValue( $name, $expected = null, $row = null ) { $this->setMwGlobals( [ 'wgEnablePartialBlocks' => false, ] ); + // Set the time to now so it does not get off during the test. + MWTimestamp::setFakeTime( MWTimestamp::time() ); + + $value = $name === 'ipb_timestamp' ? MWTimestamp::time() : ''; + $expected = $expected ?? MWTimestamp::getInstance()->format( 'H:i, j F Y' ); + $row = $row ?: new stdClass; $pager = new BlockListPager( new SpecialPage(), [] ); $wrappedPager = TestingAccessWrapper::newFromObject( $pager ); @@ -30,6 +35,9 @@ class BlockListPagerTest extends MediaWikiTestCase { $formatted = $pager->formatValue( $name, $value ); $this->assertEquals( $expected, $formatted ); + + // Reset the time. + MWTimestamp::setFakeTime( false ); } /** @@ -39,17 +47,13 @@ class BlockListPagerTest extends MediaWikiTestCase { return [ [ 'test', - '', 'Unable to format test', ], [ 'ipb_timestamp', - wfTimestamp( TS_UNIX ), - date( 'H:i, j F Y' ), ], [ 'ipb_expiry', - '', 'infinite
0 minutes left', ], ]; @@ -77,31 +81,26 @@ class BlockListPagerTest extends MediaWikiTestCase { return [ [ 'test', - '', 'Unable to format test', $row, ], [ 'ipb_timestamp', - wfTimestamp( TS_UNIX ), - date( 'H:i, j F Y' ), + null, $row, ], [ 'ipb_expiry', - '', 'infinite
0 minutes left', $row, ], [ 'ipb_by', - '', $row->ipb_by_text, $row, ], [ 'ipb_params', - '', '', $row, ]