diff options
author | Thomas Steur <tsteur@users.noreply.github.com> | 2017-11-30 04:47:49 +0300 |
---|---|---|
committer | Matthieu Aubry <mattab@users.noreply.github.com> | 2017-11-30 04:47:49 +0300 |
commit | 0309e0b016a1a2458c3ffae0836225fb1a98ee4a (patch) | |
tree | 6da8e130dadc29c1260c8c35f2b3e147964311d5 /plugins/PrivacyManager | |
parent | 0595804491ecb8de30a1e1090c31188d8781f4df (diff) |
Show message if visitor log shows no data because the data was purged (#12294)
Diffstat (limited to 'plugins/PrivacyManager')
-rw-r--r-- | plugins/PrivacyManager/PrivacyManager.php | 94 | ||||
-rw-r--r-- | plugins/PrivacyManager/tests/Integration/PrivacyManagerTest.php | 84 |
2 files changed, 155 insertions, 23 deletions
diff --git a/plugins/PrivacyManager/PrivacyManager.php b/plugins/PrivacyManager/PrivacyManager.php index b5f9066d7b..e446720cf0 100644 --- a/plugins/PrivacyManager/PrivacyManager.php +++ b/plugins/PrivacyManager/PrivacyManager.php @@ -12,6 +12,7 @@ use HTML_QuickForm2_DataSource_Array; use Piwik\Common; use Piwik\Config as PiwikConfig; use Piwik\Container\StaticContainer; +use Piwik\DataTable; use Piwik\DataTable\DataTableInterface; use Piwik\Date; use Piwik\Db; @@ -93,29 +94,10 @@ class PrivacyManager extends Plugin && (is_null($dataTable) || (!empty($dataTable) && $dataTable->getRowsCount() == 0)) ) { - // if range, only look at the first date - if ($strPeriod == 'range') { + $reportDate = self::getReportDate($strPeriod, $strDate); - $idSite = Common::getRequestVar('idSite', ''); - - if (intval($idSite) != 0) { - $site = new Site($idSite); - $timezone = $site->getTimezone(); - } else { - $timezone = 'UTC'; - } - - $period = new Range('range', $strDate, $timezone); - $reportDate = $period->getDateStart(); - - } elseif (Period::isMultiplePeriod($strDate, $strPeriod)) { - - // if a multiple period, this function is irrelevant + if (empty($reportDate)) { return false; - - } else { - // otherwise, use the date as given - $reportDate = Date::factory($strDate); } $reportYear = $reportDate->toString('Y'); @@ -130,6 +112,46 @@ class PrivacyManager extends Plugin } /** + * @param DataTable $dataTable + * @param int|null $logsOlderThan If set, it is assumed that log deletion is enabled with the given amount of days + * @return bool|void + */ + public static function haveLogsBeenPurged($dataTable, $logsOlderThan = null) + { + if (!empty($dataTable) && $dataTable->getRowsCount() != 0) { + return false; + } + + if ($logsOlderThan === null) { + $settings = PrivacyManager::getPurgeDataSettings(); + + if ($settings['delete_logs_enable'] == 0) { + return false; + } + + $logsOlderThan = $settings['delete_logs_older_than']; + } + + $logsOlderThan = (int) $logsOlderThan; + + $strPeriod = Common::getRequestVar('period', false); + $strDate = Common::getRequestVar('date', false); + + if (false === $strPeriod || false === $strDate) { + return false; + } + + $logsOlderThan = Date::now()->subDay(1 + $logsOlderThan); + $reportDate = self::getReportDate($strPeriod, $strDate); + + if (empty($reportDate)) { + return false; + } + + return $reportDate->isEarlier($logsOlderThan); + } + + /** * @see Piwik\Plugin::registerEvents */ public function registerEvents() @@ -373,6 +395,36 @@ class PrivacyManager extends Plugin return $result; } + private static function getReportDate($strPeriod, $strDate) + { + // if range, only look at the first date + if ($strPeriod == 'range') { + + $idSite = Common::getRequestVar('idSite', ''); + + if (intval($idSite) != 0) { + $site = new Site($idSite); + $timezone = $site->getTimezone(); + } else { + $timezone = 'UTC'; + } + + $period = new Range('range', $strDate, $timezone); + $reportDate = $period->getDateStart(); + + } elseif (Period::isMultiplePeriod($strDate, $strPeriod)) { + + // if a multiple period, this function is irrelevant + return false; + + } else { + // otherwise, use the date as given + $reportDate = Date::factory($strDate); + } + + return $reportDate; + } + /** * Returns true if a report with the given year & month should be purged or not. * diff --git a/plugins/PrivacyManager/tests/Integration/PrivacyManagerTest.php b/plugins/PrivacyManager/tests/Integration/PrivacyManagerTest.php index 2b022185a4..7a72882c24 100644 --- a/plugins/PrivacyManager/tests/Integration/PrivacyManagerTest.php +++ b/plugins/PrivacyManager/tests/Integration/PrivacyManagerTest.php @@ -8,6 +8,9 @@ namespace Piwik\Plugins\PrivacyManager\tests\Integration; +use CpChart\Chart\Data; +use Piwik\DataTable; +use Piwik\Date; use Piwik\Plugins\PrivacyManager\PrivacyManager; use Piwik\Tests\Framework\TestCase\IntegrationTestCase; @@ -18,6 +21,8 @@ use Piwik\Tests\Framework\TestCase\IntegrationTestCase; */ class PrivacyManagerTest extends IntegrationTestCase { + const DELETE_LOGS_OLDER_THAN = 270; + /** * @var PrivacyManager */ @@ -29,10 +34,17 @@ class PrivacyManagerTest extends IntegrationTestCase $this->manager = new PrivacyManager(); \Piwik\Option::set('delete_logs_enable', 1); - \Piwik\Option::set('delete_logs_older_than', 270); + \Piwik\Option::set('delete_logs_older_than', self::DELETE_LOGS_OLDER_THAN); \Piwik\Option::set('delete_reports_keep_week_reports', 1); } + public function tearDown() + { + unset($_GET['date']); + unset($_GET['period']); + parent::tearDown(); + } + public function test_getPurgeDataSettings_shouldUseOnlyConfigValuesIfUIisDisabled() { $this->setUIEnabled(false); @@ -51,12 +63,80 @@ class PrivacyManagerTest extends IntegrationTestCase $expected = $this->getDefaultPurgeSettings(); $expected['delete_logs_enable'] = 1; - $expected['delete_logs_older_than'] = 270; + $expected['delete_logs_older_than'] = self::DELETE_LOGS_OLDER_THAN; $expected['delete_reports_keep_week_reports'] = 1; $this->assertEquals($expected, $settings); } + public function test_haveLogsBeenPurged_whenDateIsRecent() + { + $this->setUIEnabled(true); + $_GET['date'] = Date::now()->subDay(self::DELETE_LOGS_OLDER_THAN - 2)->toString(); + $_GET['period'] = 'date'; + + $this->assertFalse(PrivacyManager::haveLogsBeenPurged($dataTable = null)); + } + + public function test_haveLogsBeenPurged_whenDateIsPastLogDeleteAndNoDataTableIsGiven() + { + $this->setUIEnabled(true); + $_GET['date'] = Date::now()->subDay(self::DELETE_LOGS_OLDER_THAN + 2)->toString(); + $_GET['period'] = 'date'; + + $this->assertTrue(PrivacyManager::haveLogsBeenPurged($dataTable = null)); + } + + public function test_haveLogsBeenPurged_whenDateIsPastLogDeleteButLogsAreDisabled() + { + $this->setUIEnabled(true); + \Piwik\Option::set('delete_logs_enable', 0); + + $_GET['date'] = Date::now()->subDay(self::DELETE_LOGS_OLDER_THAN + 1000)->toString(); + $_GET['period'] = 'date'; + + $this->assertFalse(PrivacyManager::haveLogsBeenPurged($dataTable = null)); + } + + public function test_haveLogsBeenPurged_whenDateIsPastLogDeleteShouldNotBeDeletedIfDataTableHasData() + { + $this->setUIEnabled(true); + $_GET['date'] = Date::now()->subDay(self::DELETE_LOGS_OLDER_THAN + 2)->toString(); + $_GET['period'] = 'date'; + + $dataTable = DataTable::makeFromSimpleArray(array( + array('label' => 'myLabel'), + array('label' => 'my2ndLabel'), + )); + $this->assertFalse(PrivacyManager::haveLogsBeenPurged($dataTable)); + } + + public function test_haveLogsBeenPurged_whenDateIsPastLogDeleteShouldBeDeletedIfDataTableHasNoData() + { + $this->setUIEnabled(true); + $_GET['date'] = Date::now()->subDay(self::DELETE_LOGS_OLDER_THAN + 2)->toString(); + $_GET['period'] = 'date'; + + $dataTable = new DataTable(); + $this->assertTrue(PrivacyManager::haveLogsBeenPurged($dataTable)); + } + + public function test_haveLogsBeenPurged_whenPassingManualLogDeletionDateValue_shouldAssumeLogDeletionIsEnabled() + { + $this->setUIEnabled(true); + \Piwik\Option::set('delete_logs_enable', 0); + + $_GET['date'] = Date::now()->subDay(498)->toString(); + $_GET['period'] = 'date'; + + $this->assertFalse(PrivacyManager::haveLogsBeenPurged($dataTable = null, $days = 500)); + + $_GET['date'] = Date::now()->subDay(502)->toString(); + $_GET['period'] = 'date'; + + $this->assertTrue(PrivacyManager::haveLogsBeenPurged($dataTable = null, $days = 500)); + } + private function setUIEnabled($enabled) { \Piwik\Config::getInstance()->General['enable_delete_old_data_settings_admin'] = $enabled; |