1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-22 15:07:44 +00:00
Files
matomo/plugins/PrivacyManager/tests/System/PurgeDataTest.php
Stefan Giehl d6d72d1fa7 [Coding Style] Enable rule PSR1.Methods.CamelCapsMethodName.NotCamelCaps (#22144)
* [Coding Style] Enable rule PSR1.Methods.CamelCapsMethodName.NotCamelCaps

* [Coding Style] Use camel case for method names in API plugin tests (#22145)

* [Coding Style] Use camel case for method names in Core* plugin tests (#22147)

* [Coding Style] Use camel case for method names in core Unit tests (#22149)

* [Coding Style] Use camel case for method names in Actions and BulkTracking plugin tests (#22146)

* [Coding Style] Use camel case for method names in CustomDimensions and CustomJSTracker plugin tests (#22148)

* [Coding Style] Use camel case for method names in core Integration tests (#22151)

* [Coding Style] Use camel case for method names in more core plugin tests (#22153)

* [Coding Style] Use camel case for method names in more core plugin tests (#22157)

* [Coding Style] Use camel case for method names in more core plugin tests

* Update plugins/Monolog/tests/Unit/Processor/ExceptionToTextProcessorTest.php

Co-authored-by: Michal Kleiner <michal@innocraft.com>

---------

Co-authored-by: Michal Kleiner <michal@innocraft.com>

* [Coding Style] Use camel case for method names in more core plugin tests (#22159)

* [Coding Style] Use camel case for method names in remaining tests (#22160)

* [Coding Style] Use camel case for method names in remaining tests

* rename expected test files

---------

Co-authored-by: Michal Kleiner <michal@innocraft.com>
2024-04-25 20:57:55 +02:00

220 خطوط
7.3 KiB
PHP

<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\PrivacyManager\tests\System;
use Piwik\API\Request;
use Piwik\Plugins\PrivacyManager\PrivacyManager;
use Piwik\Plugins\PrivacyManager\ReportsPurger;
use Piwik\Tests\Framework\TestCase\SystemTestCase;
use Piwik\Tests\Fixtures\OneVisitorTwoVisits;
/**
* @group PrivacyManager
* @group Core
* @group PurgeDataTest
*/
class PurgeDataTest extends SystemTestCase
{
public static $fixture = null; // initialized below class definition
public static function setUpBeforeClass(): void
{
}
public static function tearDownBeforeClass()
{
}
public function setUp(): void
{
parent::setUpBeforeClass();
}
public function tearDown(): void
{
parent::tearDownAfterClass();
}
public function testPurgeDataKeepAllExceptDay()
{
$this->assertHasOneDownload('day');
$this->assertHasOneDownload('week');
$this->assertHasOneDownload('month');
$this->assertHasOneDownload('year');
$deleteReportsOlderThan = 1;
$keepBasicMetrics = true;
$reportPeriodsToKeep = array(2,3,4,5);
$this->purgeData($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics);
$this->assertHasNoDownload('day');
$this->assertHasOneDownload('week');
$this->assertHasOneDownload('month');
$this->assertHasOneDownload('year');
}
public function testPurgeDataKeepOnlyDay()
{
$this->assertHasOneDownload('day');
$this->assertHasOneDownload('week');
$this->assertHasOneDownload('month');
$this->assertHasOneDownload('year');
$deleteReportsOlderThan = 1;
$keepBasicMetrics = true;
$reportPeriodsToKeep = array(1);
$this->purgeData($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics);
$this->assertNumVisits(2, 'day');
$this->assertNumVisits(2, 'week');
$this->assertHasOneDownload('day');
$this->assertHasNoDownload('week');
$this->assertHasNoDownload('month');
$this->assertHasNoDownload('year');
}
public function testPurgeDataShouldNotPurgeAnythingIfDeleteReportsOlderThanIsFarBackInThePast()
{
$this->assertHasOneDownload('day');
$this->assertHasOneDownload('week');
$this->assertHasOneDownload('month');
$this->assertHasOneDownload('year');
$deleteReportsOlderThan = 1000;
$keepBasicMetrics = true;
$reportPeriodsToKeep = array(1,2,3,4,5);
$this->purgeData($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics);
$this->assertHasOneDownload('day');
$this->assertHasOneDownload('week');
$this->assertHasOneDownload('month');
$this->assertHasOneDownload('year');
}
public function testPurgeDataShouldPurgeAllPeriodsExceptBasicMetricsIfNoPeriodToKeepIsGiven()
{
$this->assertHasOneDownload('day');
$this->assertHasOneDownload('week');
$this->assertHasOneDownload('month');
$this->assertHasOneDownload('year');
$deleteReportsOlderThan = 1;
$keepBasicMetrics = true;
$reportPeriodsToKeep = array();
$this->purgeData($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics);
$this->assertNumVisits(2, 'day');
$this->assertNumVisits(2, 'week');
$this->assertNumVisits(2, 'month');
$this->assertNumVisits(2, 'year');
$this->assertHasNoDownload('day');
$this->assertHasNoDownload('week');
$this->assertHasNoDownload('month');
$this->assertHasNoDownload('year');
}
public function testPurgeDataShouldPurgeEverythingIfNoPeriodToKeepIsGivenAndBasicMetricsNotKept()
{
$this->assertHasOneDownload('day');
$this->assertHasOneDownload('week');
$this->assertHasOneDownload('month');
$this->assertHasOneDownload('year');
$deleteReportsOlderThan = 1;
$keepBasicMetrics = false;
$reportPeriodsToKeep = array();
$this->purgeData($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics);
$this->assertNumVisits(0, 'day');
$this->assertNumVisits(0, 'week');
$this->assertNumVisits(0, 'month');
$this->assertNumVisits(0, 'year');
$this->assertHasNoDownload('day');
$this->assertHasNoDownload('week');
$this->assertHasNoDownload('month');
$this->assertHasNoDownload('year');
}
public function testPurgeDataShouldPurgeEverythingIfNoPeriodToKeepIsGivenAndBasicMetricsNotKeptSegmentsKept()
{
$this->assertHasOneDownload('day');
$this->assertHasOneDownload('week');
$this->assertHasOneDownload('month');
$this->assertHasOneDownload('year');
$deleteReportsOlderThan = 1;
$keepBasicMetrics = false;
$reportPeriodsToKeep = array();
$this->purgeData($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics, true);
$this->assertNumVisits(0, 'day');
$this->assertNumVisits(0, 'week');
$this->assertNumVisits(0, 'month');
$this->assertNumVisits(0, 'year');
$this->assertHasNoDownload('day');
$this->assertHasNoDownload('week');
$this->assertHasNoDownload('month');
$this->assertHasNoDownload('year');
}
private function assertNumVisits($expectedNumVisits, $period)
{
$url = 'method=VisitsSummary.getVisits'
. '&idSite=' . self::$fixture->idSite
. '&date=' . self::$fixture->dateTime
. '&period=' . $period
. '&format=original';
$api = new Request($url);
$table = $api->process();
$this->assertEquals($expectedNumVisits, $table->getFirstRow()->getColumn('nb_visits'));
}
private function assertHasOneDownload($period)
{
$api = new Request($this->getDownloadApiRequestUrl($period));
$table = $api->process();
$this->assertEquals(1, $table->getRowsCount(), $period . ' should have one download but has not');
}
private function assertHasNoDownload($period)
{
$api = new Request($this->getDownloadApiRequestUrl($period));
$table = $api->process();
$this->assertEquals(0, $table->getRowsCount(), $period . ' should not have a download but has one');
}
private function getDownloadApiRequestUrl($period)
{
return 'method=Actions.getDownloads'
. '&idSite=' . self::$fixture->idSite
. '&date=' . self::$fixture->dateTime
. '&period=' . $period
. '&format=original';
}
private function purgeData($deleteReportsOlderThan, $reportPeriodsToKeep, $keepBasicMetrics, $keepSegmentReports = false)
{
$metricsToKeep = PrivacyManager::getAllMetricsToKeep();
$maxRowsToDeletePerQuery = 100000;
$purger = new ReportsPurger(
$deleteReportsOlderThan,
$keepBasicMetrics,
$reportPeriodsToKeep,
$keepSegmentReports,
$metricsToKeep,
$maxRowsToDeletePerQuery
);
$purger->getPurgeEstimate();
$purger->purgeData();
}
}
PurgeDataTest::$fixture = new OneVisitorTwoVisits();