diff options
-rw-r--r-- | core/Tracker/Visit.php | 10 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/Core/Tracker/Visit2Test.php | 241 |
2 files changed, 249 insertions, 2 deletions
diff --git a/core/Tracker/Visit.php b/core/Tracker/Visit.php index 5bd716db67..77b52299b3 100644 --- a/core/Tracker/Visit.php +++ b/core/Tracker/Visit.php @@ -306,7 +306,7 @@ class Visit implements VisitInterface $visitor->setVisitorColumn($key, $value); } - $dimensions = VisitDimension::getAllDimensions(); + $dimensions = $this->getAllVisitDimensions(); $this->triggerHookOnDimensions($dimensions, 'onNewVisit', $visitor, $action); @@ -517,7 +517,7 @@ class Visit implements VisitInterface $visitor->setVisitorColumn('idvisitor', $this->visitorInfo['idvisitor']); } - $dimensions = VisitDimension::getAllDimensions(); + $dimensions = $this->getAllVisitDimensions(); $valuesToUpdate = $this->triggerHookOnDimensions($dimensions, 'onExistingVisit', $visitor, $action, $valuesToUpdate); if ($visitIsConverted) { @@ -557,4 +557,10 @@ class Visit implements VisitInterface return $valuesToUpdate; } + + protected function getAllVisitDimensions() + { + $dimensions = VisitDimension::getAllDimensions(); + return $dimensions; + } } diff --git a/tests/PHPUnit/Integration/Core/Tracker/Visit2Test.php b/tests/PHPUnit/Integration/Core/Tracker/Visit2Test.php new file mode 100644 index 0000000000..7c3b2e9ed5 --- /dev/null +++ b/tests/PHPUnit/Integration/Core/Tracker/Visit2Test.php @@ -0,0 +1,241 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + + +// Tests Visits and Dimensions behavior which is a lot of logic so not in VisitTest + +use Piwik\Plugin\Dimension\VisitDimension; +use Piwik\Tracker\Request; +use Piwik\Tracker\Visitor; +use Piwik\Piwik; +use Piwik\EventDispatcher; +use Piwik\Tests\Fixture; + +class FakeTrackerVisitDimension1 extends VisitDimension +{ + protected $columnName = 'custom_dimension_1'; + + public function onNewVisit(Request $request, Visitor $visitor, $action) + { + return false; + } + + public function onExistingVisit(Request $request, Visitor $visitor, $action) + { + return 'existing1'; + } +} + +class FakeTrackerVisitDimension2 extends VisitDimension +{ + protected $columnName = 'custom_dimension_2'; + + public function onNewVisit(Request $request, Visitor $visitor, $action) + { + return 'onNew2'; + } + + public function onExistingVisit(Request $request, Visitor $visitor, $action) + { + return false; + } + + public function onConvertedVisit(Request $request, Visitor $visitor, $action) + { + return false; + } +} + +class FakeTrackerVisitDimension3 extends VisitDimension +{ + protected $columnName = 'custom_dimension_3'; + + public function onNewVisit(Request $request, Visitor $visitor, $action) + { + return 'onNew3'; + } + + public function onExistingVisit(Request $request, Visitor $visitor, $action) + { + return 'existing3'; + } + + public function onConvertedVisit(Request $request, Visitor $visitor, $action) + { + return 'converted3'; + } +} + +class FakeTrackerVisitDimension4 extends VisitDimension +{ + protected $columnName = 'custom_dimension_4'; + + public function onExistingVisit(Request $request, Visitor $visitor, $action) + { + return $visitor->getVisitorColumn('custom_dimension_3') . 'extended'; + } + + public function getRequiredFields() + { + return array('custom_dimension_3'); + } +} + + +class FakeTrackerVisit extends \Piwik\Tracker\Visit +{ + public function __construct($request) + { + $this->request = $request; + $this->visitorInfo['location_ip'] = $request->getIp(); + $this->visitorInfo['idvisitor'] = 1; + } + + public function handleExistingVisit($visitor, $action, $visitIsConverted) + { + parent::handleExistingVisit($visitor, $action, $visitIsConverted); + } + + public function handleNewVisit($visitor, $action, $visitIsConverted) + { + parent::handleNewVisit($visitor, $action, $visitIsConverted); + } + + public function getAllVisitDimensions() + { + return array( + new FakeTrackerVisitDimension1(), + new FakeTrackerVisitDimension2(), + new FakeTrackerVisitDimension3(), + new FakeTrackerVisitDimension4(), + ); + } + + public function getVisitorInfo() + { + return $this->visitorInfo; + } + + protected function insertNewVisit($visit) + { + + } + + protected function updateExistingVisit($valuesToUpdate) + { + + } +} + +/** + * @group Core + */ +class VisitTest extends DatabaseTestCase +{ + + public function setUp() + { + parent::setUp(); + Fixture::createWebsite('2014-01-01 00:00:00'); + Piwik::addAction('Tracker.Request.getIdSite', function (&$idSite) { + $idSite = 1; + }); + } + + public function tearDown() + { + EventDispatcher::getInstance()->clearObservers('Tracker.Request.getIdSite'); + parent::tearDown(); + } + + public function test_handleNewVisitWithoutConversion_shouldTriggerDimensions() + { + $request = new \Piwik\Tracker\Request(array()); + $request->setForceIp('127.0.0.1'); + $visitor = new \Piwik\Tracker\Visitor($request, ''); + + $visit = new FakeTrackerVisit($request); + $visit->handleNewVisit($visitor, null, false); + + $info = $visit->getVisitorInfo(); + + $this->assertEquals('onNew2', $info['custom_dimension_2']); + $this->assertEquals('onNew3', $info['custom_dimension_3']); + $this->assertArrayNotHasKey('custom_dimension_1', $info); // on new visit returns false and should be ignored + $this->assertArrayNotHasKey('custom_dimension_4', $info); // on new visit not defined + + // make sure visitor gets updated as well + $this->assertEquals('onNew2', $visitor->getVisitorColumn('custom_dimension_2')); + $this->assertEquals('onNew3', $visitor->getVisitorColumn('custom_dimension_3')); + $this->assertFalse($visitor->getVisitorColumn('custom_dimension_1')); + $this->assertFalse($visitor->getVisitorColumn('custom_dimension_4')); + } + + public function test_handleNewVisitWithConversion_shouldTriggerDimensions() + { + $request = new \Piwik\Tracker\Request(array()); + $request->setForceIp('127.0.0.1'); + $visitor = new \Piwik\Tracker\Visitor($request, ''); + + $visit = new FakeTrackerVisit($request); + $visit->handleNewVisit($visitor, null, true); + + $info = $visit->getVisitorInfo(); + + $this->assertEquals('onNew2', $info['custom_dimension_2']); // on converted visit returns false and should be ignored + $this->assertEquals('converted3', $info['custom_dimension_3']); // a conversion should overwrite an existing value + $this->assertArrayNotHasKey('custom_dimension_1', $info); + $this->assertArrayNotHasKey('custom_dimension_4', $info); + + $this->assertEquals('converted3', $visitor->getVisitorColumn('custom_dimension_3')); + } + + public function test_handleExistingVisitWithoutConversion_shouldTriggerDimensions() + { + $request = new \Piwik\Tracker\Request(array()); + $request->setForceIp('127.0.0.1'); + $visitor = new \Piwik\Tracker\Visitor($request, ''); + + $visit = new FakeTrackerVisit($request); + $visit->handleNewVisit($visitor, null, false); + $visit->handleExistingVisit($visitor, null, false); + + $info = $visit->getVisitorInfo(); + + $this->assertEquals('existing1', $info['custom_dimension_1']); + $this->assertEquals('onNew2', $info['custom_dimension_2']); // on existing visit returns false and should be ignored/ not overwrite on new value + $this->assertEquals('existing3', $info['custom_dimension_3']); + $this->assertEquals('existing3extended', $info['custom_dimension_4']); // accesses a previously set column + + // make sure visitor gets updated as well + $this->assertEquals('existing1', $visitor->getVisitorColumn('custom_dimension_1')); + $this->assertEquals('onNew2', $visitor->getVisitorColumn('custom_dimension_2')); + $this->assertEquals('existing3', $visitor->getVisitorColumn('custom_dimension_3')); + $this->assertEquals('existing3extended', $visitor->getVisitorColumn('custom_dimension_4')); + } + + public function test_handleExistingVisitWithConversion_shouldTriggerDimensions() + { + $request = new \Piwik\Tracker\Request(array()); + $request->setForceIp('127.0.0.1'); + $visitor = new \Piwik\Tracker\Visitor($request, ''); + + $visit = new FakeTrackerVisit($request); + $visit->handleNewVisit($visitor, null, false); + $visit->handleExistingVisit($visitor, null, true); + + $info = $visit->getVisitorInfo(); + + $this->assertEquals('existing1', $info['custom_dimension_1']); + $this->assertEquals('onNew2', $info['custom_dimension_2']); // on converted visit returns false and should be ignored + $this->assertEquals('converted3', $info['custom_dimension_3']); // a conversion should overwrite an existing value + $this->assertEquals('existing3extended', $info['custom_dimension_4']); + + $this->assertEquals('converted3', $visitor->getVisitorColumn('custom_dimension_3')); + } +}
\ No newline at end of file |