diff options
Diffstat (limited to 'plugins/CoreHome/tests/Unit')
4 files changed, 375 insertions, 0 deletions
diff --git a/plugins/CoreHome/tests/Unit/Columns/VisitorReturningTest.php b/plugins/CoreHome/tests/Unit/Columns/VisitorReturningTest.php new file mode 100644 index 0000000000..86be732733 --- /dev/null +++ b/plugins/CoreHome/tests/Unit/Columns/VisitorReturningTest.php @@ -0,0 +1,82 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ + +namespace Piwik\Plugins\CoreHome\tests\Unit\Columns; + +use PHPUnit\Framework\TestCase; +use Piwik\Plugins\CoreHome\Columns\VisitorReturning; +use Piwik\Tracker\Request; +use Piwik\Tracker\Visit\VisitProperties; +use Piwik\Tracker\Visitor; + +class VisitorReturningTest extends TestCase +{ + public function test_onNewVisit_returnsReturningCustomerIfVisitHasOrder() + { + $dim = new VisitorReturning(); + $result = $dim->onNewVisit($this->makeMockRequest([ 'ec_id' => 'abcdefg' ]), $this->makeMockVisitor(true), null); + $this->assertEquals(VisitorReturning::IS_RETURNING_CUSTOMER, $result); + } + + public function test_onNewVisit_returnsReturningCustomerIfVisitHasSecondsSinceLastOrder() + { + $dim = new VisitorReturning(); + $result = $dim->onNewVisit($this->makeMockRequest(), $this->makeMockVisitor(true, [ 'visitor_seconds_since_order' => 1234 ]), null); + $this->assertEquals(VisitorReturning::IS_RETURNING_CUSTOMER, $result); + } + + public function test_onNewVisit_returnsReturningCustomerIfPrevVisitHasSecondsSinceLastOrder() + { + $dim = new VisitorReturning(); + $result = $dim->onNewVisit($this->makeMockRequest(), $this->makeMockVisitor(true, [], [ 'visitor_seconds_since_order' => 5678 ]), null); + $this->assertEquals(VisitorReturning::IS_RETURNING_CUSTOMER, $result); + } + + public function test_onNewVisit_returnsReturningVisitorIfVisitorIsKnown() + { + $dim = new VisitorReturning(); + $result = $dim->onNewVisit($this->makeMockRequest(), $this->makeMockVisitor(true), null); + $this->assertEquals(VisitorReturning::IS_RETURNING, $result); + } + + public function test_onNewVisit_returnsNewVisitIfVisitorIsNotKnown() + { + $dim = new VisitorReturning(); + $result = $dim->onNewVisit($this->makeMockRequest(), $this->makeMockVisitor(false), null); + $this->assertEquals(VisitorReturning::IS_NEW, $result); + } + + /** + * @return Request + */ + private function makeMockRequest($params = [], $currentTime = null) + { + $mockBuilder = $this->getMockBuilder(Request::class)->disableOriginalConstructor(); + if ($currentTime) { + $mockBuilder->onlyMethods(['getCurrentTimestamp']); + } + $mock = $mockBuilder->getMock(); + if ($currentTime) { + $mock->method('getCurrentTimestamp')->willReturn($currentTime); + } + $mock->method('getParam')->willReturnCallback(function ($name) use ($params) { + return isset($params[$name]) ? $params[$name] : false; + }); + + /** @var Request $mock */ + $result = $mock; + return $result; + } + + private function makeMockVisitor($isKnown, $visitProps = [], $previousVisitProps = []) + { + $visitor = new Visitor(new VisitProperties($visitProps), $isKnown, new VisitProperties($previousVisitProps)); + return $visitor; + } +}
\ No newline at end of file diff --git a/plugins/CoreHome/tests/Unit/Columns/VisitorSecondsSinceFirstTest.php b/plugins/CoreHome/tests/Unit/Columns/VisitorSecondsSinceFirstTest.php new file mode 100644 index 0000000000..71f468a331 --- /dev/null +++ b/plugins/CoreHome/tests/Unit/Columns/VisitorSecondsSinceFirstTest.php @@ -0,0 +1,140 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ + +namespace Piwik\Plugins\CoreHome\tests\Unit\Columns; + +use PHPUnit\Framework\TestCase; +use Piwik\Plugins\CoreHome\Columns\VisitorSecondsSinceFirst; +use Piwik\Tracker\Request; +use Piwik\Tracker\Visit\VisitProperties; +use Piwik\Tracker\Visitor; + +class VisitorSecondsSinceFirstTest extends TestCase +{ + public function test_onNewVisit_returnsZeroIfVisitorIsUnknown() + { + $request = $this->makeMockRequest(); + $visitor = $this->makeMockVisitor(false); + + $dim = new VisitorSecondsSinceFirst(); + $result = $dim->onNewVisit($request, $visitor, null); + + $this->assertEquals(0, $result); + } + + /** + * @dataProvider getInvalidTestDataForOnNewVisit + */ + public function test_onNewVisit_returnsNullIfPreviousColumnValueOrCurrentColumnValueIsInvalid($currentProps, $oldProps) + { + $request = $this->makeMockRequest(); + $visitor = $this->makeMockVisitor(true, $currentProps, $oldProps); + + $dim = new VisitorSecondsSinceFirst(); + $result = $dim->onNewVisit($request, $visitor, null); + + $this->assertNull($result); + } + + public function getInvalidTestDataForOnNewVisit() + { + return [ + [ + ['visit_first_action_time' => ''], + ['visit_first_action_time' => '2016-02-03 00:00:00', 'visitor_seconds_since_first' => '250'], + ], + + [ + ['visit_first_action_time' => 0], + ['visit_first_action_time' => '2016-02-03 00:00:00', 'visitor_seconds_since_first' => '250'], + ], + + [ + ['visit_first_action_time' => null], + ['visit_first_action_time' => '2016-02-03 00:00:00', 'visitor_seconds_since_first' => '250'], + ], + + [ + ['visit_first_action_time' => 'asdlfj alskdjf'], + ['visit_first_action_time' => '2016-02-03 00:00:00', 'visitor_seconds_since_first' => '250'], + ], + + [ + ['visit_first_action_time' => '2016-02-03 00:00:00'], + ['visit_first_action_time' => '', 'visitor_seconds_since_first' => '250'], + ], + + [ + ['visit_first_action_time' => '2016-02-03 00:00:00'], + ['visit_first_action_time' => 0, 'visitor_seconds_since_first' => '250'], + ], + + [ + ['visit_first_action_time' => '2016-02-03 00:00:00'], + ['visit_first_action_time' => null, 'visitor_seconds_since_first' => '250'], + ], + + [ + ['visit_first_action_time' => '2016-02-03 00:00:00'], + ['visit_first_action_time' => 'asdlfj alskdjf', 'visitor_seconds_since_first' => '250'], + ], + + [ + ['visit_first_action_time' => '2016-02-03 00:00:00'], + ['visit_first_action_time' => '2016-04-05 00:00:00', 'visitor_seconds_since_first' => ''], + ], + [ + ['visit_first_action_time' => '2016-02-03 00:00:00'], + ['visit_first_action_time' => '2016-04-05 00:00:00', 'visitor_seconds_since_first' => null], + ], + [ + ['visit_first_action_time' => '2016-02-03 00:00:00'], + ['visit_first_action_time' => '2016-04-05 00:00:00', 'visitor_seconds_since_first' => false], + ], + ]; + } + + public function test_onNewVisit_returnsCorrectValueWhenVisitorIsKnownAndAllFirstActionTimesPresent() + { + $currentTime = time(); + + $request = $this->makeMockRequest($currentTime); + $visitor = $this->makeMockVisitor(true, ['visit_first_action_time' => $currentTime - 200], ['visit_first_action_time' => $currentTime - 300, 'visitor_seconds_since_first' => 500]); + + $dim = new VisitorSecondsSinceFirst(); + $result = $dim->onNewVisit($request, $visitor, null); + + $this->assertEquals($result, 600); + } + + /** + * @return Request + */ + private function makeMockRequest($currentTime = null) + { + $mockBuilder = $this->getMockBuilder(Request::class)->disableOriginalConstructor(); + if ($currentTime) { + $mockBuilder->onlyMethods(['getCurrentTimestamp']); + } + $mock = $mockBuilder->getMock(); + if ($currentTime) { + $mock->method('getCurrentTimestamp')->willReturn($currentTime); + } + + /** @var Request $mock */ + $result = $mock; + return $result; + } + + private function makeMockVisitor($isKnown, $visitProps = [], $previousVisitProps = []) + { + $visitor = new Visitor(new VisitProperties($visitProps), $isKnown, new VisitProperties($previousVisitProps)); + return $visitor; + } +}
\ No newline at end of file diff --git a/plugins/CoreHome/tests/Unit/Columns/VisitorSecondsSinceOrderTest.php b/plugins/CoreHome/tests/Unit/Columns/VisitorSecondsSinceOrderTest.php new file mode 100644 index 0000000000..92d3f5b178 --- /dev/null +++ b/plugins/CoreHome/tests/Unit/Columns/VisitorSecondsSinceOrderTest.php @@ -0,0 +1,86 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ + +namespace Piwik\Plugins\CoreHome\tests\Unit\Columns; + +use PHPUnit\Framework\TestCase; +use Piwik\Plugins\CoreHome\Columns\VisitorSecondsSinceOrder; +use Piwik\Tracker\Request; +use Piwik\Tracker\Visit\VisitProperties; +use Piwik\Tracker\Visitor; + +class VisitorSecondsSinceOrderTest extends TestCase +{ + public function test_onExistingVisit_returnsZeroIfTheVisitHasAnOrder() + { + $dim = new VisitorSecondsSinceOrder(); + $result = $dim->onExistingVisit($this->makeMockRequest([ 'ec_id' => 'abljasdf' ]), $this->makeMockVisitor(true), null); + $this->assertEquals(0, $result); + } + + public function test_onExistingVisit_returnsTheExistingValueIfThereIsOne() + { + $dim = new VisitorSecondsSinceOrder(); + $result = $dim->onExistingVisit($this->makeMockRequest(), $this->makeMockVisitor(true, [ 'visitor_seconds_since_order' => 20 ]), null); + $this->assertEquals(20, $result); + } + + public function test_onExistingVisit_returnsNullIfDimensionValueIsNotSetForPreviousVisit() + { + $dim = new VisitorSecondsSinceOrder(); + $result = $dim->onExistingVisit($this->makeMockRequest(), $this->makeMockVisitor(true), null); + $this->assertEquals(null, $result); + } + + public function test_onExistingVisit_returnsCorrectValueIfPreviousValueWasForOrder() + { + $dim = new VisitorSecondsSinceOrder(); + $result = $dim->onExistingVisit($this->makeMockRequest([], '2020-03-04 03:04:28'), $this->makeMockVisitor(true, [], [ 'visitor_seconds_since_order' => 0, 'visit_first_action_time' => '2020-03-04 03:04:20' ]), null); + $this->assertEquals(8, $result); + } + + public function test_onExistingVisit_returnsCorrectValueIfPreviousValueWasAfterOrder() + { + $dim = new VisitorSecondsSinceOrder(); + $result = $dim->onExistingVisit($this->makeMockRequest([], '2020-03-04 03:04:28'), $this->makeMockVisitor(true, [], [ 'visitor_seconds_since_order' => 45, 'visit_first_action_time' => '2020-03-04 03:04:20' ]), null); + $this->assertEquals(53, $result); + } + + /** + * @return Request + */ + private function makeMockRequest($params = [], $currentTime = null) + { + if (is_string($currentTime)) { + $currentTime = strtotime($currentTime); + } + + $mockBuilder = $this->getMockBuilder(Request::class)->disableOriginalConstructor(); + if ($currentTime) { + $mockBuilder->onlyMethods(['getCurrentTimestamp', 'getParam']); + } + $mock = $mockBuilder->getMock(); + if ($currentTime) { + $mock->method('getCurrentTimestamp')->willReturn($currentTime); + } + $mock->method('getParam')->willReturnCallback(function ($name) use ($params) { + return isset($params[$name]) ? $params[$name] : false; + }); + + /** @var Request $mock */ + $result = $mock; + return $result; + } + + private function makeMockVisitor($isKnown, $visitProps = [], $previousVisitProps = []) + { + $visitor = new Visitor(new VisitProperties($visitProps), $isKnown, new VisitProperties($previousVisitProps)); + return $visitor; + } +}
\ No newline at end of file diff --git a/plugins/CoreHome/tests/Unit/Columns/VisitsCountTest.php b/plugins/CoreHome/tests/Unit/Columns/VisitsCountTest.php new file mode 100644 index 0000000000..2a62848159 --- /dev/null +++ b/plugins/CoreHome/tests/Unit/Columns/VisitsCountTest.php @@ -0,0 +1,67 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ + +namespace Piwik\Plugins\CoreHome\tests\Unit\Columns; + + +use PHPUnit\Framework\TestCase; +use Piwik\Plugins\CoreHome\Columns\VisitsCount; +use Piwik\Tracker\Request; +use Piwik\Tracker\Visit\VisitProperties; +use Piwik\Tracker\Visitor; + +class VisitsCountTest extends TestCase +{ + public function test_onNewVisit_returnsZeroIfPreviousVisitCountDoesNotExist() + { + $request = $this->makeMockRequest(); + $visitor = $this->makeMockVisitor(false); + + $dim = new VisitsCount(); + $visitCount = $dim->onNewVisit($request, $visitor, null); + + $this->assertEquals(1, $visitCount); + } + + public function test_onNewVisit_returnsIncrementedValueIfPreviousVisitCountIsPresent() + { + $request = $this->makeMockRequest(); + $visitor = $this->makeMockVisitor(false, [], ['visitor_count_visits' => 10]); + + $dim = new VisitsCount(); + $visitCount = $dim->onNewVisit($request, $visitor, null); + + $this->assertEquals(11, $visitCount); + } + + /** + * @return Request + */ + private function makeMockRequest($currentTime = null) + { + $mockBuilder = $this->getMockBuilder(Request::class)->disableOriginalConstructor(); + if ($currentTime) { + $mockBuilder->onlyMethods(['getCurrentTimestamp']); + } + $mock = $mockBuilder->getMock(); + if ($currentTime) { + $mock->method('getCurrentTimestamp')->willReturn($currentTime); + } + + /** @var Request $mock */ + $result = $mock; + return $result; + } + + private function makeMockVisitor($isKnown, $visitProps = [], $previousVisitProps = []) + { + $visitor = new Visitor(new VisitProperties($visitProps), $isKnown, new VisitProperties($previousVisitProps)); + return $visitor; + } +}
\ No newline at end of file |