From 995aad376af72419dd2fe8870954c9b400be4766 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Thu, 26 Sep 2019 12:29:29 -0400 Subject: [PATCH] API: Use ConvertibleTimestamp::setFakeTime for testing curtimestamp Mainly to avoid spurious test failures when CI is being extremely slow. Bug: T233752 Change-Id: Ie2cdd84dc076a852fbdce52f661ef893f9a2d45b --- includes/api/ApiMain.php | 3 +-- tests/phpunit/includes/api/ApiMainTest.php | 7 ++++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 7bbce976aa..d2c957d485 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -1154,8 +1154,7 @@ class ApiMain extends ApiBase { } if ( $this->getParameter( 'curtimestamp' ) ) { - $result->addValue( null, 'curtimestamp', wfTimestamp( TS_ISO_8601, time() ), - ApiResult::NO_SIZE_CHECK ); + $result->addValue( null, 'curtimestamp', wfTimestamp( TS_ISO_8601 ), ApiResult::NO_SIZE_CHECK ); } if ( $this->getParameter( 'responselanginfo' ) ) { diff --git a/tests/phpunit/includes/api/ApiMainTest.php b/tests/phpunit/includes/api/ApiMainTest.php index 3a3f5f115b..16062752df 100644 --- a/tests/phpunit/includes/api/ApiMainTest.php +++ b/tests/phpunit/includes/api/ApiMainTest.php @@ -2,6 +2,7 @@ use Wikimedia\Rdbms\DBQueryError; use Wikimedia\TestingAccessWrapper; +use Wikimedia\Timestamp\ConvertibleTimestamp; /** * @group API @@ -169,6 +170,10 @@ class ApiMainTest extends ApiTestCase { } public function testAddRequestedFieldsCurTimestamp() { + // Fake timestamp for better testability, CI can sometimes take + // unreasonably long to run the simple test request here. + $reset = ConvertibleTimestamp::setFakeTime( '20190102030405' ); + $req = new FauxRequest( [ 'action' => 'query', 'meta' => 'siteinfo', @@ -177,7 +182,7 @@ class ApiMainTest extends ApiTestCase { $api = new ApiMain( $req ); $api->execute(); $timestamp = $api->getResult()->getResultData()['curtimestamp']; - $this->assertLessThanOrEqual( 1, abs( strtotime( $timestamp ) - time() ) ); + $this->assertSame( '2019-01-02T03:04:05Z', $timestamp ); } public function testAddRequestedFieldsResponseLangInfo() { -- 2.20.1