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

LogDeleterTest.php « Integration « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 62138ec5c45733e6045ab34e7a3dc748722f6d32 (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
<?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\Integration;

use Piwik\Common;
use Piwik\DataAccess\RawLogDao;
use Piwik\Db;
use Piwik\LogDeleter;
use Piwik\Tests\Framework\Mock\Plugin\LogTablesProvider;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Tests\Framework\TestDataHelper\LogHelper;

/**
 * @group Core
 */
class LogDeleterTest extends IntegrationTestCase
{
    /**
     * @var LogHelper
     */
    private $logInserter;

    /**
     * @var LogDeleter
     */
    private $logDeleter;

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

        $this->logDeleter = new LogDeleter(new RawLogDao(), new LogTablesProvider());

        $this->logInserter = new LogHelper();
        $this->insertTestData();
    }

    public function test_deleteVisits_RemovesVisitsAndOtherRelatedLogs()
    {
        $this->logDeleter->deleteVisits(array(2, 3));

        $this->assertVisitExists(1);
        $this->assertVisitNotExists(2);
        $this->assertVisitNotExists(3);
        $this->assertVisitExists(4);
    }

    public function test_deleteVisitsFor_DeletesVisitsForSpecifiedRangeAndSites_AndInvokesCallbackAfterEveryChunkIsDeleted()
    {
        $iterationCount = 0;
        $this->logDeleter->deleteVisitsFor('2012-01-01', '2012-01-02 05:05:05', 2, $iterationStep = 1, function () use (&$iterationCount) {
            ++$iterationCount;
        });

        $this->assertEquals(2, $iterationCount);

        // visits for idSite = 1 do not get deleted
        $this->assertVisitExists(1);
        $this->assertVisitExists(2);

        // visits for idSite = 2 do get deleted
        $this->assertVisitNotExists(3);
        $this->assertVisitNotExists(4);
    }

    private function insertTestData()
    {
        // two visits for site = 1
        $this->insertVisit($idSite = 1, $dateTime = '2012-01-01 00:00:00');
        $this->insertVisit($idSite = 1, $dateTime = '2012-01-02 00:00:00');

        // two visits for site = 2
        $this->insertVisit($idSite = 2, $dateTime = '2012-01-01 00:00:00');
        $this->insertVisit($idSite = 2, $dateTime = '2012-01-02 00:00:00');
    }

    private function insertVisit($idSite, $dateTime)
    {
        $visit = $this->logInserter->insertVisit(array('idsite' => $idSite, 'visit_last_action_time' => $dateTime));

        $orderId = 'idorder_' . $visit['idvisit'];

        // insert two actions
        $this->logInserter->insertVisitAction($visit['idvisit'], array('idsite' => $idSite));
        $this->logInserter->insertVisitAction($visit['idvisit'], array('idsite' => $idSite));

        // insert two conversions
        $this->logInserter->insertConversion($visit['idvisit'], array('idsite' => $idSite, 'buster' => 1));
        $this->logInserter->insertConversion($visit['idvisit'], array('idsite' => $idSite, 'buster' => 2, 'idorder' => $orderId));

        // insert two conversion items for last conversion
        $this->logInserter->insertConversionItem($visit['idvisit'], $orderId, array('idsite' => $idSite));
        $this->logInserter->insertConversionItem($visit['idvisit'], $orderId, array('idsite' => $idSite, 'idaction_sku' => 123));
    }

    private function assertVisitExists($idVisit, $checkAggregates = true)
    {
        $this->assertEquals(1, $this->getRowCountWithIdVisit('log_visit', $idVisit));
        $this->assertEquals(2, $this->getRowCountWithIdVisit('log_link_visit_action', $idVisit));

        if ($checkAggregates) {
            $this->assertConversionsExists($idVisit);
        }
    }

    private function assertConversionsExists($idVisit, $checkAggregates = true)
    {
        $this->assertEquals(2, $this->getRowCountWithIdVisit('log_conversion', $idVisit));

        if ($checkAggregates) {
            $this->assertConversionItemsExist($idVisit);
        }
    }

    private function assertConversionItemsExist($idVisit)
    {
        $this->assertEquals(2, $this->getRowCountWithIdVisit('log_conversion_item', $idVisit));
    }

    private function assertVisitNotExists($idVisit)
    {
        $this->assertEquals(0, $this->getRowCountWithIdVisit('log_visit', $idVisit));
        $this->assertEquals(0, $this->getRowCountWithIdVisit('log_link_visit_action', $idVisit));

        $this->assertConversionsNotExists($idVisit);
    }

    private function getRowCountWithIdVisit($table, $idVisit)
    {
        return Db::fetchOne("SELECT COUNT(*) FROM " . Common::prefixTable($table) . " WHERE idvisit = $idVisit");
    }

    private function assertConversionsNotExists($idVisit)
    {
        $this->assertEquals(0, $this->getRowCountWithIdVisit('log_conversion', $idVisit));

        $this->assertConversionItemsNotExist($idVisit);
    }

    private function assertConversionItemsNotExist($idVisit)
    {
        $this->assertEquals(0, $this->getRowCountWithIdVisit('log_conversion_item', $idVisit));
    }
}