diff options
author | diosmosis <benaka@piwik.pro> | 2015-07-11 05:27:46 +0300 |
---|---|---|
committer | diosmosis <benaka@piwik.pro> | 2015-08-06 17:37:59 +0300 |
commit | ba2a639dca1197a87eab02479e1f7d3ffb511544 (patch) | |
tree | 4a678c47edcd37480e71ca5c81897c138606605a /plugins/CoreHome | |
parent | 25613735e5467806b33f651dbeffb16570851646 (diff) |
Fix function signature in VisitRequestProcessor and move tests in VisitTest regarding isVisitNew to new test case for VisitRequestProcessor.
Diffstat (limited to 'plugins/CoreHome')
-rw-r--r-- | plugins/CoreHome/Tracker/VisitRequestProcessor.php | 6 | ||||
-rw-r--r-- | plugins/CoreHome/tests/Integration/Tracker/VisitRequestProcessorTest.php | 131 |
2 files changed, 135 insertions, 2 deletions
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); + } +} |