From ba2a639dca1197a87eab02479e1f7d3ffb511544 Mon Sep 17 00:00:00 2001 From: diosmosis <benaka@piwik.pro> Date: Fri, 10 Jul 2015 19:27:46 -0700 Subject: [PATCH] Fix function signature in VisitRequestProcessor and move tests in VisitTest regarding isVisitNew to new test case for VisitRequestProcessor. --- .../Tracker/VisitRequestProcessor.php | 6 +- .../Tracker/VisitRequestProcessorTest.php | 131 ++++++++++++++++++ .../PHPUnit/Integration/Tracker/VisitTest.php | 100 ------------- 3 files changed, 135 insertions(+), 102 deletions(-) create mode 100644 plugins/CoreHome/tests/Integration/Tracker/VisitRequestProcessorTest.php diff --git a/plugins/CoreHome/Tracker/VisitRequestProcessor.php b/plugins/CoreHome/Tracker/VisitRequestProcessor.php index 8830b2402f..ee7493c68d 100644 --- a/plugins/CoreHome/Tracker/VisitRequestProcessor.php +++ b/plugins/CoreHome/Tracker/VisitRequestProcessor.php @@ -104,7 +104,7 @@ class VisitRequestProcessor extends RequestProcessor return false; } - public function afterRequestProcessed(VisitProperties $visitProperties) + public function afterRequestProcessed(VisitProperties $visitProperties, Request $request) { /** * Triggered after visits are tested for exclusion so plugins can modify the IP address @@ -121,11 +121,13 @@ class VisitRequestProcessor extends RequestProcessor * Determines if the tracker if the current action should be treated as the start of a new visit or * an action in an existing visit. * + * Note: public only for tests. + * * @param VisitProperties $visitProperties The current visit/visitor information. * @param Request $request * @return bool */ - private function isVisitNew(VisitProperties $visitProperties, Request $request) + public function isVisitNew(VisitProperties $visitProperties, Request $request) { $isKnown = $visitProperties->getRequestMetadata('CoreHome', 'isVisitorKnown'); if (!$isKnown) { diff --git a/plugins/CoreHome/tests/Integration/Tracker/VisitRequestProcessorTest.php b/plugins/CoreHome/tests/Integration/Tracker/VisitRequestProcessorTest.php new file mode 100644 index 0000000000..fc9e9ad423 --- /dev/null +++ b/plugins/CoreHome/tests/Integration/Tracker/VisitRequestProcessorTest.php @@ -0,0 +1,131 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\CoreHome\tests\Integration\Tracker; + +use Piwik\Cache; +use Piwik\CacheId; +use Piwik\Date; +use Piwik\Plugins\CoreHome\Tracker\VisitRequestProcessor; +use Piwik\Tests\Framework\TestCase\IntegrationTestCase; +use Piwik\Plugins\SitesManager\API; +use Piwik\Tracker\Request; +use Piwik\Tracker\Visit; +use Piwik\Tracker\Visit\VisitProperties; + +/** + * @group CoreHome + * @group CoreHome_Integration + */ +class VisitRequestProcessorTest extends IntegrationTestCase +{ + public function test_isVisitNew_ReturnsFalse_IfLastActionTimestampIsWithinVisitTimeLength_AndNoDimensionForcesVisit_AndVisitorKnown() + { + $this->setDimensionsWithOnNewVisit(array(false, false, false)); + + /** @var VisitRequestProcessor $visit */ + list($visit, $visitProperties, $request) = $this->makeVisitorAndAction( + $lastActionTime = '2012-01-02 08:08:34', $thisActionTime = '2012-01-02 08:12:45', $isVisitorKnown = true); + + $result = $visit->isVisitNew($visitProperties, $request); + + $this->assertFalse($result); + } + + public function test_isVisitNew_ReturnsTrue_IfLastActionTimestampWasYesterday() + { + $this->setDimensionsWithOnNewVisit(array(false, false, false)); + + // test same day + /** @var VisitRequestProcessor $visit */ + list($visit, $visitProperties, $request) = $this->makeVisitorAndAction( + $lastActionTime = '2012-01-01 23:59:58', $thisActionTime = '2012-01-01 23:59:59', $isVisitorKnown = true); + $result = $visit->isVisitNew($visitProperties, $request); + $this->assertFalse($result); + + // test different day + list($visit, $visitProperties, $request) = $this->makeVisitorAndAction( + $lastActionTime = '2012-01-01 23:59:58', $thisActionTime = '2012-01-02 00:00:01', $isVisitorKnown = true); + $result = $visit->isVisitNew($visitProperties, $request); + $this->assertTrue($result); + } + + + public function test_isVisitNew_ReturnsTrue_IfLastActionTimestampIsNotWithinVisitTimeLength_AndNoDimensionForcesVisit_AndVisitorNotKnown() + { + $this->setDimensionsWithOnNewVisit(array(false, false, false)); + + /** @var VisitRequestProcessor $visit */ + list($visit, $visitProperties, $request) = $this->makeVisitorAndAction($lastActionTime = '2012-01-02 08:08:34', $thisActionTime = '2012-01-02 09:12:45'); + + $result = $visit->isVisitNew($visitProperties, $request); + + $this->assertTrue($result); + } + + public function test_isVisitNew_ReturnsTrue_IfLastActionTimestampIsWithinVisitTimeLength_AndDimensionForcesVisit() + { + $this->setDimensionsWithOnNewVisit(array(false, false, true)); + + /** @var VisitRequestProcessor $visit */ + list($visit, $visitProperties, $request) = $this->makeVisitorAndAction($lastActionTime = '2012-01-02 08:08:34', $thisActionTime = '2012-01-02 08:12:45'); + + $result = $visit->isVisitNew($visitProperties, $request); + + $this->assertTrue($result); + } + + public function test_isVisitNew_ReturnsTrue_IfDimensionForcesVisit_AndVisitorKnown() + { + $this->setDimensionsWithOnNewVisit(array(false, false, true)); + + /** @var VisitRequestProcessor $visit */ + list($visit, $visitProperties, $request) = $this->makeVisitorAndAction($lastActionTime = '2012-01-02 08:08:34', $thisActionTime = '2012-01-02 08:12:45'); + + $result = $visit->isVisitNew($visitProperties, $request); + + $this->assertTrue($result); + } + + private function makeVisitorAndAction($lastActionTimestamp, $currentActionTime, $isVisitorKnown = false) + { + $idsite = API::getInstance()->addSite("name", "http://piwik.net/"); + + list($visit, $request) = $this->prepareVisitWithRequest(array('idsite' => $idsite), $currentActionTime); + + $visitProperties = new VisitProperties(); + $visitProperties->visitorInfo = array('visit_last_action_time' => Date::factory($lastActionTimestamp)->getTimestamp()); + $visitProperties->setRequestMetadata('CoreHome', 'isVisitorKnown', $isVisitorKnown); + + return array($visit, $visitProperties, $request); + } + + private function setDimensionsWithOnNewVisit($dimensionOnNewVisitResults) + { + $dimensions = array(); + foreach ($dimensionOnNewVisitResults as $onNewVisitResult) { + $dim = $this->getMock('Piwik\\Plugin\\Dimension', array('shouldForceNewVisit', 'getColumnName')); + $dim->expects($this->any())->method('shouldForceNewVisit')->will($this->returnValue($onNewVisitResult)); + $dimensions[] = $dim; + } + + $cache = Cache::getTransientCache(); + $cache->save(CacheId::pluginAware('VisitDimensions'), $dimensions); + Visit::$dimensions = null; + } + + private function prepareVisitWithRequest($requestParams, $requestDate) + { + $request = new Request($requestParams); + $request->setCurrentTimestamp(Date::factory($requestDate)->getTimestamp()); + + $visit = self::$fixture->piwikEnvironment->getContainer()->get('Piwik\Plugins\CoreHome\Tracker\VisitRequestProcessor'); + + return array($visit, $request); + } +} diff --git a/tests/PHPUnit/Integration/Tracker/VisitTest.php b/tests/PHPUnit/Integration/Tracker/VisitTest.php index c5aec2995c..b89800e705 100644 --- a/tests/PHPUnit/Integration/Tracker/VisitTest.php +++ b/tests/PHPUnit/Integration/Tracker/VisitTest.php @@ -9,7 +9,6 @@ namespace Piwik\Tests\Integration\Tracker; use Piwik\Cache; -use Piwik\CacheId; use Piwik\Archive\ArchiveInvalidator; use Piwik\Date; use Piwik\Network\IPUtils; @@ -17,12 +16,10 @@ use Piwik\Plugin\Manager; use Piwik\Plugins\SitesManager\API; use Piwik\Tests\Framework\Fixture; use Piwik\Tests\Framework\Mock\FakeAccess; -use Piwik\Tracker\ActionPageview; use Piwik\Tracker\Request; use Piwik\Tracker\Visit; use Piwik\Tracker\VisitExcluded; use Piwik\Tests\Framework\TestCase\IntegrationTestCase; -use Piwik\Tracker\Visitor; /** * @group Core @@ -271,37 +268,6 @@ class VisitTest extends IntegrationTestCase } } - public function test_isVisitNew_ReturnsFalse_IfLastActionTimestampIsWithinVisitTimeLength_AndNoDimensionForcesVisit_AndVisitorKnown() - { - $this->setDimensionsWithOnNewVisit(array(false, false, false)); - - /** @var Visit $visit */ - list($visit, $visitor, $action) = $this->makeVisitorAndAction( - $lastActionTime = '2012-01-02 08:08:34', $thisActionTime = '2012-01-02 08:12:45', $isVisitorKnown = true); - - $result = $visit->isVisitNew($visitor, $action); - - $this->assertFalse($result); - } - - public function test_isVisitNew_ReturnsTrue_IfLastActionTimestampWasYesterday() - { - $this->setDimensionsWithOnNewVisit(array(false, false, false)); - - // test same day - /** @var Visit $visit */ - list($visit, $visitor, $action) = $this->makeVisitorAndAction( - $lastActionTime = '2012-01-01 23:59:58', $thisActionTime = '2012-01-01 23:59:59', $isVisitorKnown = true); - $result = $visit->isVisitNew($visitor, $action); - $this->assertFalse($result); - - // test different day - list($visit, $visitor, $action) = $this->makeVisitorAndAction( - $lastActionTime = '2012-01-01 23:59:58', $thisActionTime = '2012-01-02 00:00:01', $isVisitorKnown = true); - $result = $visit->isVisitNew($visitor, $action); - $this->assertTrue($result); - } - public function test_markArchivedReportsAsInvalidIfArchiveAlreadyFinished_ShouldRemember_IfRequestWasDoneLongAgo() { $currentActionTime = '2012-01-02 08:12:45'; @@ -389,72 +355,6 @@ class VisitTest extends IntegrationTestCase return array($visit, $request); } - public function test_isVisitNew_ReturnsTrue_IfLastActionTimestampIsNotWithinVisitTimeLength_AndNoDimensionForcesVisit_AndVisitorNotKnown() - { - $this->setDimensionsWithOnNewVisit(array(false, false, false)); - - /** @var Visit $visit */ - list($visit, $visitor, $action) = $this->makeVisitorAndAction($lastActionTime = '2012-01-02 08:08:34', $thisActionTime = '2012-01-02 09:12:45'); - - $result = $visit->isVisitNew($visitor, $action); - - $this->assertTrue($result); - } - - public function test_isVisitNew_ReturnsTrue_IfLastActionTimestampIsWithinVisitTimeLength_AndDimensionForcesVisit() - { - $this->setDimensionsWithOnNewVisit(array(false, false, true)); - - /** @var Visit $visit */ - list($visit, $visitor, $action) = $this->makeVisitorAndAction($lastActionTime = '2012-01-02 08:08:34', $thisActionTime = '2012-01-02 08:12:45'); - - $result = $visit->isVisitNew($visitor, $action); - - $this->assertTrue($result); - } - - public function test_isVisitNew_ReturnsTrue_IfDimensionForcesVisit_AndVisitorKnown() - { - $this->setDimensionsWithOnNewVisit(array(false, false, true)); - - /** @var Visit $visit */ - list($visit, $visitor, $action) = $this->makeVisitorAndAction($lastActionTime = '2012-01-02 08:08:34', $thisActionTime = '2012-01-02 08:12:45'); - - $result = $visit->isVisitNew($visitor, $action); - - $this->assertTrue($result); - } - - private function makeVisitorAndAction($lastActionTimestamp, $currentActionTime, $isVisitorKnown = false) - { - $idsite = API::getInstance()->addSite("name", "http://piwik.net/"); - - list($visit, $request) = $this->prepareVisitWithRequest(array('idsite' => $idsite), $currentActionTime); - - $visitProperties = new Visit\VisitProperties(); - $visitProperties->visitorInfo = array('visit_last_action_time' => Date::factory($lastActionTimestamp)->getTimestamp()); - - $visitor = new Visitor($request, 'configid', $visitProperties, $isVisitorKnown); - - $action = new ActionPageview($request); - - return array($visit, $visitor, $action); - } - - private function setDimensionsWithOnNewVisit($dimensionOnNewVisitResults) - { - $dimensions = array(); - foreach ($dimensionOnNewVisitResults as $onNewVisitResult) { - $dim = $this->getMock('Piwik\\Plugin\\Dimension', array('shouldForceNewVisit', 'getColumnName')); - $dim->expects($this->any())->method('shouldForceNewVisit')->will($this->returnValue($onNewVisitResult)); - $dimensions[] = $dim; - } - - $cache = Cache::getTransientCache(); - $cache->save(CacheId::pluginAware('VisitDimensions'), $dimensions); - Visit::$dimensions = null; - } - public function provideContainerConfig() { return array( -- GitLab