From 61bf67bdb87f59fd098e0206417f50efb1f9bac7 Mon Sep 17 00:00:00 2001 From: diosmosis Date: Thu, 24 May 2018 07:24:57 -0700 Subject: Use DI to inject test now value for ApiCounterTest. (#12977) --- config/environment/test.php | 3 +++ plugins/Live/Model.php | 5 ++++- plugins/Live/tests/System/ApiCounterTest.php | 17 +++++++++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/config/environment/test.php b/config/environment/test.php index c2bdb11dea..8e75e1d970 100644 --- a/config/environment/test.php +++ b/config/environment/test.php @@ -15,6 +15,9 @@ return array( }, 'cache.eager.cache_id' => 'eagercache-test-', + // set in individual tests to override now value when needed + 'Tests.now' => false, + // Disable loading core translations 'Piwik\Translation\Translator' => DI\decorate(function ($previous, ContainerInterface $c) { $loadRealTranslations = $c->get('test.vars.loadRealTranslations'); diff --git a/plugins/Live/Model.php b/plugins/Live/Model.php index 4ec45dc59a..de2587c96e 100644 --- a/plugins/Live/Model.php +++ b/plugins/Live/Model.php @@ -12,6 +12,7 @@ namespace Piwik\Plugins\Live; use Exception; use Piwik\API\Request; use Piwik\Common; +use Piwik\Container\StaticContainer; use Piwik\Date; use Piwik\Db; use Piwik\Period; @@ -127,8 +128,10 @@ class Model list($whereIdSites, $idSites) = $this->getIdSitesWhereClause($idSite, $from); + $now = StaticContainer::get('Tests.now') ?: time(); + $bind = $idSites; - $bind[] = Date::factory(time() - $lastMinutes * 60)->toString('Y-m-d H:i:s'); + $bind[] = Date::factory($now - $lastMinutes * 60)->toString('Y-m-d H:i:s'); $where = $whereIdSites . "AND " . $where; diff --git a/plugins/Live/tests/System/ApiCounterTest.php b/plugins/Live/tests/System/ApiCounterTest.php index f957b09608..47b2e24623 100644 --- a/plugins/Live/tests/System/ApiCounterTest.php +++ b/plugins/Live/tests/System/ApiCounterTest.php @@ -24,12 +24,24 @@ use Piwik\Tests\Framework\TestCase\SystemTestCase; */ class ApiCounterTest extends SystemTestCase { + /** + * @var int + */ + private static $testNow; + /** * @var API */ private $api; private $idSite = 1; + public static function setUpBeforeClass() + { + self::$testNow = strtotime('2018-02-03 04:45:40'); + + parent::setUpBeforeClass(); + } + public function setUp() { parent::setUp(); @@ -104,7 +116,7 @@ class ApiCounterTest extends SystemTestCase private function trackSomeVisits() { - $nowTimestamp = time(); + $nowTimestamp = self::$testNow; // use local tracker so mock location provider can be used $t = Fixture::getTracker($this->idSite, $nowTimestamp, $defaultInit = true, $useLocal = false); @@ -180,7 +192,8 @@ class ApiCounterTest extends SystemTestCase public static function provideContainerConfigBeforeClass() { return array( - 'Piwik\Access' => new FakeAccess() + 'Piwik\Access' => new FakeAccess(), + 'Tests.now' => self::$testNow, ); } } -- cgit v1.2.3