From 074e81319ac5019ab87778d7426e305ea9df3e36 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 10 Dec 2015 11:22:40 -0500 Subject: [PATCH] CRM-17637 - Add unit test for cron fallback --- CRM/Utils/VersionCheck.php | 10 ++++-- tests/phpunit/CRM/Utils/versionCheckTest.php | 37 ++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/CRM/Utils/VersionCheck.php b/CRM/Utils/VersionCheck.php index fd6ee3ce2b..0a11e53294 100644 --- a/CRM/Utils/VersionCheck.php +++ b/CRM/Utils/VersionCheck.php @@ -32,7 +32,6 @@ */ class CRM_Utils_VersionCheck { const - PINGBACK_URL = 'http://latest.civicrm.org/stable.php?format=json', CACHEFILE_NAME = '[civicrm.files]/persist/version-info-cache.json', // after this length of time we fall back on poor-man's cron (7+ days) CACHEFILE_EXPIRE = 605000; @@ -68,6 +67,11 @@ class CRM_Utils_VersionCheck { */ public $cronJob = array(); + /** + * @var string + */ + public $pingbackUrl = 'http://latest.civicrm.org/stable.php?format=json'; + /** * Pingback params * @@ -80,7 +84,7 @@ class CRM_Utils_VersionCheck { * * @var string */ - protected $cacheFile; + public $cacheFile; /** * Class constructor. @@ -382,7 +386,7 @@ class CRM_Utils_VersionCheck { ), ); $ctx = stream_context_create($params); - $rawJson = file_get_contents(self::PINGBACK_URL, FALSE, $ctx); + $rawJson = file_get_contents($this->pingbackUrl, FALSE, $ctx); $versionInfo = $rawJson ? json_decode($rawJson, TRUE) : NULL; // If we couldn't fetch or parse the data $versionInfo will be NULL // Otherwise it will be an array and we'll cache it. diff --git a/tests/phpunit/CRM/Utils/versionCheckTest.php b/tests/phpunit/CRM/Utils/versionCheckTest.php index 8222125803..239e98766d 100644 --- a/tests/phpunit/CRM/Utils/versionCheckTest.php +++ b/tests/phpunit/CRM/Utils/versionCheckTest.php @@ -197,4 +197,41 @@ class CRM_Utils_versionCheckTest extends CiviUnitTestCase { return $data; } + public function testCronFallback() { + // Fake "remote" source data + $tmpSrc = '/tmp/versionCheckTestFile.json'; + file_put_contents($tmpSrc, json_encode($this->sampleVersionInfo)); + + $vc = new CRM_Utils_VersionCheck(); + $vc->pingbackUrl = $tmpSrc; + + // If the cachefile doesn't exist, fallback should kick in + if (file_exists($vc->cacheFile)) { + unlink($vc->cacheFile); + } + $vc->initialize(); + $this->assertEquals($this->sampleVersionInfo, $vc->versionInfo); + unset($vc); + + // Update "remote" source data + $remoteData = array('4.3' => $this->sampleVersionInfo['4.3']); + file_put_contents($tmpSrc, json_encode($remoteData)); + + // Cache was just updated, so fallback should not happen - assert we are still using cached data + $vc = new CRM_Utils_VersionCheck(); + $vc->pingbackUrl = $tmpSrc; + $vc->initialize(); + $this->assertEquals($this->sampleVersionInfo, $vc->versionInfo); + unset($vc); + + // Ensure fallback happens if file is too old + $vc = new CRM_Utils_VersionCheck(); + $vc->pingbackUrl = $tmpSrc; + // Set cachefile to be 1 minute older than expire time + touch($vc->cacheFile, time() - 60 - $vc::CACHEFILE_EXPIRE); + clearstatcache(); + $vc->initialize(); + $this->assertEquals($remoteData, $vc->versionInfo); + } + } -- 2.25.1