diff options
author | diosmosis <benaka@piwik.pro> | 2015-05-12 07:54:00 +0300 |
---|---|---|
committer | diosmosis <benaka@piwik.pro> | 2015-06-11 09:42:04 +0300 |
commit | 1223d1c892cccab917ca304614068e0140ea2e45 (patch) | |
tree | 4bbe7c03d1390c0be72ee6778a5050beb5d432f3 /plugins/UserCountry | |
parent | 97abd79c07c8fcf86b27f244dbb042f64c01afe6 (diff) |
Use LogPurger::deleteVisitsFor in LogDataPurger class in PrivacyManager plugin, Rename LogPurger to LogDeleter, add test for VisitorGeolocator::reattributeVisits and add empty tests for LogDeleter service.
Diffstat (limited to 'plugins/UserCountry')
-rw-r--r-- | plugins/UserCountry/VisitorGeolocator.php | 66 | ||||
-rw-r--r-- | plugins/UserCountry/tests/Integration/VisitorGeolocatorTest.php | 95 |
2 files changed, 128 insertions, 33 deletions
diff --git a/plugins/UserCountry/VisitorGeolocator.php b/plugins/UserCountry/VisitorGeolocator.php index a12cbb5118..4ef710b77d 100644 --- a/plugins/UserCountry/VisitorGeolocator.php +++ b/plugins/UserCountry/VisitorGeolocator.php @@ -234,6 +234,40 @@ class VisitorGeolocator } /** + * TODO + * + * @param $from + * @param $to + * @param null $idSite + * @param int $segmentLimit + * @param callable $onLogProcessed + */ + public function reattributeVisitLogs($from, $to, $idSite = null, $segmentLimit = 1000, $onLogProcessed = null) + { + $visitFieldsToSelect = array_merge(array('idvisit', 'location_ip'), array_keys(VisitorGeolocator::$logVisitFieldsToUpdate)); + + $conditions = array( + array('visit_last_action_time', '>=', $from), + array('visit_last_action_time', '<', $to) + ); + + if (!empty($idSite)) { + $conditions[] = array('idsite', '=', $idSite); + } + + $self = $this; + $this->dao->forAllLogs('log_visit', $visitFieldsToSelect, $conditions, $segmentLimit, function ($logs) use ($self, $onLogProcessed) { + foreach ($logs as $row) { + $self->attributeExistingVisit($row); + + if (!empty($onLogProcessed)) { + $onLogProcessed($row); + } + } + }); + } + + /** * @return LocationProvider */ public function getProvider() @@ -274,36 +308,4 @@ class VisitorGeolocator } return self::$defaultLocationCache; } - - /** - * @param $from - * @param $to - * @param null $idSite - * @param int $segmentLimit - * @param callable $onLogProcessed - */ - public function reattributeVisitLogs($from, $to, $idSite = null, $segmentLimit = 1000, $onLogProcessed = null) - { - $visitFieldsToSelect = array_merge(array('idvisit', 'location_ip'), array_keys(VisitorGeolocator::$logVisitFieldsToUpdate)); - - $conditions = array( - array('visit_last_action_time', '>=', $from), - array('visit_last_action_time', '<', $to) - ); - - if (!empty($idSite)) { - $conditions[] = array('idsite', '=', $idSite); - } - - $self = $this; - $this->dao->forAllLogs('log_visit', $visitFieldsToSelect, $conditions, $segmentLimit, function ($logs) use ($self, $onLogProcessed) { - foreach ($logs as $row) { - $self->attributeExistingVisit($row); - - if (!empty($onLogProcessed)) { - $onLogProcessed($row); - } - } - }); - } }
\ No newline at end of file diff --git a/plugins/UserCountry/tests/Integration/VisitorGeolocatorTest.php b/plugins/UserCountry/tests/Integration/VisitorGeolocatorTest.php index 9c414a5708..ea4e75585e 100644 --- a/plugins/UserCountry/tests/Integration/VisitorGeolocatorTest.php +++ b/plugins/UserCountry/tests/Integration/VisitorGeolocatorTest.php @@ -247,6 +247,99 @@ class VisitorGeolocatorTest extends IntegrationTestCase $this->assertNull($result); } + public function test_reattributeVisitLogs_ReattributesVisitsInDateRangeAndFromSite_AndCallsCallbackWithEveryProcessedRow() + { + foreach (array(1, 2) as $idSite) { + foreach (array('2012-01-01', '2012-01-02', '2012-01-03', '2012-01-04') as $date) { + $this->insertVisit(array( + 'visit_last_action_time' => $date, + 'idsite' => $idSite + )); + } + } + + $mockLocationProvider = $this->getProviderMockThatGeolocates(array( + 'location_country' => 'US', + 'location_region' => 'rg', + 'location_city' => 'the city' + )); + $geolocator = new VisitorGeolocator($mockLocationProvider); + + $reattributedVisits = array(); + $geolocator->reattributeVisitLogs('2012-01-02', '2012-01-04', 2, $segmentLimit = 1000, function ($row) use (&$reattributedVisits) { + $reattributedVisits[] = $row['idvisit']; + }); + + sort($reattributedVisits); + + $expectedVisitsVisited = array(6, 7); + $this->assertEquals($expectedVisitsVisited, $reattributedVisits); + + // check that no visits were re-attributed for site 1 + $actualVisits = Db::fetchAll("SELECT visit_last_action_time, idsite, location_country, location_region, location_city FROM " + . Common::prefixTable('log_visit') . " ORDER BY idsite ASC, visit_last_action_time ASC"); + $expectedVisits = array( + array( + 'visit_last_action_time' => '2012-01-01 00:00:00', + 'idsite' => '1', + 'location_country' => 'xx', + 'location_region' => null, + 'location_city' => null + ), + array( + 'visit_last_action_time' => '2012-01-02 00:00:00', + 'idsite' => '1', + 'location_country' => 'xx', + 'location_region' => null, + 'location_city' => null + ), + array( + 'visit_last_action_time' => '2012-01-03 00:00:00', + 'idsite' => '1', + 'location_country' => 'xx', + 'location_region' => null, + 'location_city' => null + ), + array( + 'visit_last_action_time' => '2012-01-04 00:00:00', + 'idsite' => '1', + 'location_country' => 'xx', + 'location_region' => null, + 'location_city' => null + ), + array( + 'visit_last_action_time' => '2012-01-01 00:00:00', + 'idsite' => '2', + 'location_country' => 'xx', + 'location_region' => null, + 'location_city' => null + ), + array( + 'visit_last_action_time' => '2012-01-02 00:00:00', + 'idsite' => '2', + 'location_country' => 'us', + 'location_region' => 'rg', + 'location_city' => 'the city' + ), + array( + 'visit_last_action_time' => '2012-01-03 00:00:00', + 'idsite' => '2', + 'location_country' => 'us', + 'location_region' => 'rg', + 'location_city' => 'the city' + ), + array( + 'visit_last_action_time' => '2012-01-04 00:00:00', + 'idsite' => '2', + 'location_country' => 'xx', + 'location_region' => null, + 'location_city' => null + ), + ); + + $this->assertEquals($expectedVisits, $actualVisits); + } + /** * @return PHPUnit_Framework_MockObject_MockObject|LocationProvider */ @@ -261,7 +354,7 @@ class VisitorGeolocatorTest extends IntegrationTestCase private function getProviderMockThatGeolocates($locationResult) { $mock = $this->getProviderMock(); - $mock->expects($this->once())->method('getLocation')->will($this->returnCallback(function ($info) use ($locationResult) { + $mock->expects($this->any())->method('getLocation')->will($this->returnCallback(function ($info) use ($locationResult) { if ($info['ip'] == VisitorGeolocatorTest::TEST_IP) { return $locationResult; } else { |