File size: 9,091 Bytes
968114b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
193
194
195
196
197
198
199
200
201
<?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\Actions\tests\Unit;

use Piwik\DataTable;
use Piwik\DataTable\Row;
use Piwik\Metrics as PiwikMetrics;
use Piwik\Plugins\Actions\ArchivingHelper;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tracker\Action;

require_once PIWIK_INCLUDE_PATH . '/plugins/Actions/Actions.php';

/**
 * @group Actions
 * @group ArchiverTest
 * @group Plugins
 */
class ArchiverTests extends \PHPUnit\Framework\TestCase
{
    public function setUp(): void
    {
        Fixture::loadAllTranslations();
    }

    public function tearDown(): void
    {
        Fixture::resetTranslations();
    }

    public function getActionNameTestData()
    {
        return array(
            array(
                'params'   => array('name' => 'http://example.org/', 'type' => Action::TYPE_PAGE_URL, 'urlPrefix' => null),
                'expected' => array('/index'),
            ),
            array(
                'params'   => array('name' => 'example.org/', 'type' => Action::TYPE_PAGE_URL, 'urlPrefix' => 1),
                'expected' => array('/index'),
            ),
            array(
                'params'   => array('name' => 'example.org/', 'type' => Action::TYPE_PAGE_URL, 'urlPrefix' => 2),
                'expected' => array('/index'),
            ),
            array(
                'params'   => array('name' => 'example.org/', 'type' => Action::TYPE_PAGE_URL, 'urlPrefix' => 3),
                'expected' => array('/index'),
            ),
            array(
                'params'   => array('name' => 'example.org/', 'type' => Action::TYPE_PAGE_URL, 'urlPrefix' => 4),
                'expected' => array('/index'),
            ),
            array(
                'params'   => array('name' => 'example.org/path/', 'type' => Action::TYPE_PAGE_URL, 'urlPrefix' => 4),
                'expected' => array('path', '/index'),
            ),
            array(
                'params'   => array('name' => 'example.org/test/path', 'type' => Action::TYPE_PAGE_URL, 'urlPrefix' => 1),
                'expected' => array('test', '/path'),
            ),
            array(
                'params'   => array('name' => 'http://example.org/path/', 'type' => Action::TYPE_PAGE_URL),
                'expected' => array('path', '/index'),
            ),
            array(
                'params'   => array('name' => 'example.org/test/path', 'type' => Action::TYPE_PAGE_URL, 'urlPrefix' => 1),
                'expected' => array('test', '/path'),
            ),
            array(
                'params'   => array('name' => 'Test / Path', 'type' => Action::TYPE_PAGE_URL),
                'expected' => array('Test', '/Path'),
            ),
            array(
                'params'   => array('name' => '    Test trim   ', 'type' => Action::TYPE_PAGE_URL),
                'expected' => array('/Test trim'),
            ),
            array(
                'params'   => array('name' => 'Category / Subcategory', 'type' => Action::TYPE_PAGE_TITLE),
                'expected' => array(' Category / Subcategory'),
            ),
            array(
                'params'   => array('name' => '/path/index.php?var=test', 'type' => Action::TYPE_PAGE_TITLE),
                'expected' => array(' /path/index.php?var=test'),
            ),
            array(
                'params'   => array('name' => 'http://example.org/path/Default.aspx#anchor', 'type' => Action::TYPE_PAGE_TITLE),
                'expected' => array(' http://example.org/path/Default.aspx#anchor'),
            ),
            array(
                'params'   => array('name' => '', 'type' => Action::TYPE_PAGE_TITLE),
                'expected' => array(' Page Name not defined'),
            ),
            array(
                'params'   => array('name' => '', 'type' => Action::TYPE_PAGE_URL),
                'expected' => array('Page URL not defined'),
            ),
            array(
                'params'   => array('name' => 'http://example.org/download.zip', 'type' => Action::TYPE_DOWNLOAD),
                'expected' => array('example.org', '/download.zip'),
            ),
            array(
                'params'   => array('name' => 'http://example.org/download/1/', 'type' => Action::TYPE_DOWNLOAD),
                'expected' => array('example.org', '/download/1/'),
            ),
            array(
                'params'   => array('name' => 'http://example.org/link', 'type' => Action::TYPE_OUTLINK),
                'expected' => array('example.org', '/link'),
            ),
            array(
                'params'   => array('name' => 'http://example.org/some/path/', 'type' => Action::TYPE_OUTLINK),
                'expected' => array('example.org', '/some/path/'),
            ),
        );
    }

    /**
     * @dataProvider getActionNameTestData
     */
    public function testGetActionExplodedNames($params, $expected)
    {
        ArchivingHelper::reloadConfig();
        $processed = ArchivingHelper::getActionExplodedNames($params['name'], $params['type'], (isset($params['urlPrefix']) ? $params['urlPrefix'] : null));
        $this->assertEquals($expected, $processed);
    }

    public function testDeleteInvalidSummedColumnsFromDataTableRemovesRenamedUniqMetricsFromParentRowsOnly()
    {
        $table = new DataTable();

        $parentRow = new Row([Row::COLUMNS => [
            'label' => '/parent',
            PiwikMetrics::INDEX_NB_UNIQ_VISITORS => 12,
            PiwikMetrics::INDEX_PAGE_ENTRY_NB_UNIQ_VISITORS => 8,
            PiwikMetrics::INDEX_PAGE_EXIT_NB_UNIQ_VISITORS => 4,
            PiwikMetrics::INDEX_SUM_DAILY_NB_UNIQ_VISITORS => 20,
            PiwikMetrics::INDEX_PAGE_ENTRY_SUM_DAILY_NB_UNIQ_VISITORS => 16,
            PiwikMetrics::INDEX_PAGE_EXIT_SUM_DAILY_NB_UNIQ_VISITORS => 14,
        ]]);
        $parentRow->setSubtable(new DataTable());
        $table->addRow($parentRow);

        $leafRow = new Row([Row::COLUMNS => [
            'label' => '/leaf',
            PiwikMetrics::INDEX_NB_UNIQ_VISITORS => 5,
            PiwikMetrics::INDEX_PAGE_ENTRY_NB_UNIQ_VISITORS => 4,
            PiwikMetrics::INDEX_PAGE_EXIT_NB_UNIQ_VISITORS => 3,
            PiwikMetrics::INDEX_SUM_DAILY_NB_UNIQ_VISITORS => 9,
            PiwikMetrics::INDEX_PAGE_ENTRY_SUM_DAILY_NB_UNIQ_VISITORS => 7,
            PiwikMetrics::INDEX_PAGE_EXIT_SUM_DAILY_NB_UNIQ_VISITORS => 6,
        ]]);
        $table->addRow($leafRow);

        ArchivingHelper::deleteInvalidSummedColumnsFromDataTable($table);

        $this->assertFalse($parentRow->hasColumn(PiwikMetrics::INDEX_NB_UNIQ_VISITORS));
        $this->assertFalse($parentRow->hasColumn(PiwikMetrics::INDEX_PAGE_ENTRY_NB_UNIQ_VISITORS));
        $this->assertFalse($parentRow->hasColumn(PiwikMetrics::INDEX_PAGE_EXIT_NB_UNIQ_VISITORS));
        $this->assertFalse($parentRow->hasColumn(PiwikMetrics::INDEX_SUM_DAILY_NB_UNIQ_VISITORS));
        $this->assertFalse($parentRow->hasColumn(PiwikMetrics::INDEX_PAGE_ENTRY_SUM_DAILY_NB_UNIQ_VISITORS));
        $this->assertFalse($parentRow->hasColumn(PiwikMetrics::INDEX_PAGE_EXIT_SUM_DAILY_NB_UNIQ_VISITORS));

        $this->assertSame(9, $leafRow->getColumn(PiwikMetrics::INDEX_SUM_DAILY_NB_UNIQ_VISITORS));
        $this->assertSame(7, $leafRow->getColumn(PiwikMetrics::INDEX_PAGE_ENTRY_SUM_DAILY_NB_UNIQ_VISITORS));
        $this->assertSame(6, $leafRow->getColumn(PiwikMetrics::INDEX_PAGE_EXIT_SUM_DAILY_NB_UNIQ_VISITORS));
    }

    public function testDeleteInvalidSummedColumnsFromDataTableKeepsRenamedUniqMetricsOnSummaryRow()
    {
        $table = new DataTable();
        $summaryRow = new Row([Row::COLUMNS => [
            'label' => DataTable::LABEL_SUMMARY_ROW,
            PiwikMetrics::INDEX_NB_UNIQ_VISITORS => 12,
            PiwikMetrics::INDEX_PAGE_ENTRY_NB_UNIQ_VISITORS => 8,
            PiwikMetrics::INDEX_PAGE_EXIT_NB_UNIQ_VISITORS => 4,
            PiwikMetrics::INDEX_SUM_DAILY_NB_UNIQ_VISITORS => 20,
            PiwikMetrics::INDEX_PAGE_ENTRY_SUM_DAILY_NB_UNIQ_VISITORS => 16,
            PiwikMetrics::INDEX_PAGE_EXIT_SUM_DAILY_NB_UNIQ_VISITORS => 14,
        ]]);
        $table->addSummaryRow($summaryRow);

        ArchivingHelper::deleteInvalidSummedColumnsFromDataTable($table);

        $summaryRow = $table->getRowFromId(DataTable::ID_SUMMARY_ROW);
        $this->assertNotNull($summaryRow);
        $this->assertFalse($summaryRow->hasColumn(PiwikMetrics::INDEX_NB_UNIQ_VISITORS));
        $this->assertFalse($summaryRow->hasColumn(PiwikMetrics::INDEX_PAGE_ENTRY_NB_UNIQ_VISITORS));
        $this->assertFalse($summaryRow->hasColumn(PiwikMetrics::INDEX_PAGE_EXIT_NB_UNIQ_VISITORS));
        $this->assertSame(20, $summaryRow->getColumn(PiwikMetrics::INDEX_SUM_DAILY_NB_UNIQ_VISITORS));
        $this->assertSame(16, $summaryRow->getColumn(PiwikMetrics::INDEX_PAGE_ENTRY_SUM_DAILY_NB_UNIQ_VISITORS));
        $this->assertSame(14, $summaryRow->getColumn(PiwikMetrics::INDEX_PAGE_EXIT_SUM_DAILY_NB_UNIQ_VISITORS));
    }
}