Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PurgeDataTest.php « System « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6573f63db040025f01a96189dcb8809aa400ccf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link    http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
namespace Piwik\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;

/**
 * TODO: shouldn't this be merged w/ PrivacyManagerTest? the original data purging tests are there
 *
 * @group Core
 * @group PurgeDataTest
 */
class PurgeDataTest extends SystemTestCase
{
    public static $fixture = null; // initialized below class definition

    public static function setUpBeforeClass()
    {

    }
    public static function tearDownBeforeClass()
    {

    }

    public function setUp()
    {
        parent::setUpBeforeClass();
    }

    public function tearDown()
    {
        parent::tearDownAfterClass();
    }

    public function test_purgeData_keepAllExceptDay()
    {
        $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 test_purgeData_keepOnlyDay()
    {
        $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 test_purgeData_shouldNotPurgeAnything_IfDeleteReportsOlderThanIsFarBackInThePast()
    {
        $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 test_purgeData_shouldPurgeAllPeriodsExceptBasicMetrics_IfNoPeriodToKeepIsGiven()
    {
        $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 test_purgeData_shouldPurgeEverything_IfNoPeriodToKeepIsGivenAndBasicMetricsNotKept()
    {
        $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');
    }

    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)
    {
        $metricsToKeep           = PrivacyManager::getAllMetricsToKeep();
        $maxRowsToDeletePerQuery = 100000;
        $keepSegmentReports      = false;

        $purger = new ReportsPurger($deleteReportsOlderThan, $keepBasicMetrics, $reportPeriodsToKeep,
                                    $keepSegmentReports, $metricsToKeep, $maxRowsToDeletePerQuery);
        $purger->purgeData();
    }
}

PurgeDataTest::$fixture = new OneVisitorTwoVisits();