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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Integration\Archive;
use Piwik\Archive;
use Piwik\ArchiveProcessor;
use Piwik\DataTable;
use Piwik\DataTable\Row;
use Piwik\Db;
use Piwik\Period;
use Piwik\Plugins\SegmentEditor\API;
use Piwik\Segment;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Period\Factory as PeriodFactory;
use Piwik\Archive\DataTableFactory;
/**
* @group DataTableFactoryTest
* @group DataTableFactoryjj
* @group Archive
* @group Core
*/
class DataTableFactoryTest extends IntegrationTestCase
{
private $site1 = 3;
private $site2 = 4;
private $date1range = '2012-12-12,2012-12-12';
private $date2range = '2012-12-13,2012-12-13';
private $date1 = '2012-12-12';
private $date2 = '2012-12-13';
private $defaultRow = array(
'nb_visits' => 97
);
public function setUp(): void
{
parent::setUp();
// setup the access layer
FakeAccess::$superUser = true;
for ($i = 0; $i < $this->site2; $i++) {
Fixture::createWebsite('2015-01-01 00:00:00');
}
API::getInstance()->add('TEST SEGMENT', 'browserCode==ff');
}
public function test_makeMerged_numeric_noIndices_shouldContainDefaultRow_IfNoDataGiven()
{
$indices = $this->getResultIndices($period = false, $site = false);
$factory = $this->createFactory($indices);
$table = $factory->makeMerged($index = array(), $indices);
$this->assertTableIsDataTableSimpleInstance($table);
$this->assertRowCountEquals(1, $table);
$this->assertRowEquals($this->defaultRow, $this->site1, $table->getFirstRow());
$this->assertTableMetadataEquals($this->date1, $table);
}
public function test_makeMerged_numeric_noIndices_shouldContainOnlyOneRowWithTheData_IfAnyDataIsSet()
{
$indices = $this->getResultIndices($period = false, $site = false);
$factory = $this->createFactory($indices);
$index = array('nb_visits' => 10, 'nb_pageviews' => 21);
$dataTable = $factory->makeMerged($index, $indices);
$this->assertTableIsDataTableSimpleInstance($dataTable);
$this->assertRowCountEquals(1, $dataTable);
$this->assertRowEquals($index, $this->site1, $dataTable->getFirstRow());
$this->assertTableMetadataEquals($this->date1, $dataTable);
}
public function test_makeMerged_numeric_periodIndices_shouldGenerateAMapOfTables_AndUseDefaultRow_IfNoData()
{
$indices = $this->getResultIndices($period = true, $site = false);
$factory = $this->createFactory($indices);
$index = array(
$this->date1range => array(),
$this->date2range => array(),
);
$map = $factory->makeMerged($index, $indices);
$this->assertTrue($map instanceof DataTable\Map);
$this->assertRowCountEquals(2, $map);
foreach ($map->getDataTables() as $label => $table) {
$this->assertTrue(in_array($label, array($this->date1, $this->date2)));
$this->assertTableIsDataTableSimpleInstance($table);
$this->assertRowCountEquals(1, $table);
$this->assertRowEquals($this->defaultRow, $this->site1, $table->getFirstRow());
$this->assertTableMetadataEquals($label, $table);
}
}
public function test_makeMerged_numeric_periodIndices_shouldGenerateAMapOfTables_WithData()
{
$indices = $this->getResultIndices($period = true, $site = false);
$factory = $this->createFactory($indices);
$row1 = array('nb_visits' => 37, 'nb_pageviews' => 10);
$row2 = array('nb_visits' => 34, 'nb_hits' => 21);
$index = array(
$this->date1range => $row1,
$this->date2range => $row2,
);
$map = $factory->makeMerged($index, $indices);
$this->assertTrue($map instanceof DataTable\Map);
$this->assertRowCountEquals(2, $map);
foreach ($map->getDataTables() as $label => $table) {
$this->assertTableIsDataTableSimpleInstance($table);
$this->assertRowCountEquals(1, $table);
$this->assertTableMetadataEquals($label, $table);
}
$this->assertRowEquals($row1, $this->site1, $map->getTable($this->date1)->getFirstRow());
$this->assertRowEquals($row2, $this->site1, $map->getTable($this->date2)->getFirstRow());
}
public function test_makeMerged_numeric_periodIndices_shouldSetAKeyName()
{
$indices = $this->getResultIndices($period = true, $site = false);
$factory = $this->createFactory($indices);
$index = array(
$this->date1range => array(),
$this->date2range => array(),
);
$map = $factory->makeMerged($index, $indices);
$this->assertSame('date', $map->getKeyName());
}
public function test_makeMerged_numeric_siteIndices_shouldUseDefaultRow_IfNoData()
{
$indices = $this->getResultIndices($period = false, $site = true);
$factory = $this->createFactory($indices);
$index = array(
$this->site1 => array(),
$this->site2 => array(),
);
$table = $factory->makeMerged($index, $indices);
$this->assertTableIsDataTableInstance($table);
$this->assertRowCountEquals(2, $table);
$this->assertRowEquals($this->defaultRow, $this->site1, $table->getRowFromId(0));
$this->assertRowEquals($this->defaultRow, $this->site2, $table->getRowFromId(1));
$this->assertTableMetadataEquals($this->date1, $table);
}
public function test_makeMerged_numeric_siteIndices_shouldGenerateAMapOfTables_WithData()
{
$indices = $this->getResultIndices($period = false, $site = true);
$factory = $this->createFactory($indices);
$row1 = array('nb_visits' => 37, 'nb_pageviews' => 10);
$row2 = array('nb_visits' => 34, 'nb_hits' => 21);
$index = array(
$this->site1 => $row1,
$this->site2 => $row2,
);
$table = $factory->makeMerged($index, $indices);
$this->assertTableIsDataTableInstance($table);
$this->assertRowCountEquals(2, $table);
$this->assertRowEquals($row1, $this->site1, $table->getRowFromId(0));
$this->assertRowEquals($row2, $this->site2, $table->getRowFromId(1));
$this->assertTableMetadataEquals($this->date1, $table);
}
public function test_makeMerged_numeric_siteAndPeriodIndices_shouldUseDefaultRow_IfNoData()
{
$indices = $this->getResultIndices($period = true, $site = true);
$factory = $this->createFactory($indices);
$index = array(
$this->site1 => array(
$this->date1range => array(),
$this->date2range => array()
),
$this->site2 => array(
$this->date1range => array(),
$this->date2range => array()
),
);
$map = $factory->makeMerged($index, $indices);
$this->assertTrue($map instanceof DataTable\Map);
$this->assertRowCountEquals(2, $map);
$this->assertSame('date', $map->getKeyName());
foreach ($map->getDataTables() as $label => $table) {
$this->assertTrue(in_array($label, array($this->date1, $this->date2)));
$this->assertTableIsDataTableInstance($table);
$this->assertRowCountEquals(2, $table);
$this->assertTableMetadataEquals($label, $table);
}
$this->assertRowEquals($this->defaultRow, $this->site1, $map->getTable($this->date1)->getRowFromId(0));
$this->assertRowEquals($this->defaultRow, $this->site2, $map->getTable($this->date1)->getRowFromId(1));
$this->assertRowEquals($this->defaultRow, $this->site1, $map->getTable($this->date2)->getRowFromId(0));
$this->assertRowEquals($this->defaultRow, $this->site2, $map->getTable($this->date2)->getRowFromId(1));
}
public function test_makeMerged_numeric_siteAndPeriodIndices_shouldGenerateAMapOfTables_WithData()
{
$indices = $this->getResultIndices($period = true, $site = true);
$factory = $this->createFactory($indices);
$row1 = array('nb_visits' => 37, 'nb_pageviews' => 10);
$row2 = array('nb_visits' => 34, 'nb_hits' => 21);
$row3 = array('nb_visits' => 23);
$index = array(
$this->site1 => array(
$this->date1range => $row1,
$this->date2range => array()
),
$this->site2 => array(
$this->date1range => $row2,
$this->date2range => $row3
),
);
$map = $factory->makeMerged($index, $indices);
$this->assertTrue($map instanceof DataTable\Map);
$this->assertRowCountEquals(2, $map);
foreach ($map->getDataTables() as $label => $table) {
$this->assertTrue(in_array($label, array($this->date1, $this->date2)));
$this->assertTableIsDataTableInstance($table);
$this->assertRowCountEquals(2, $table);
$this->assertTableMetadataEquals($label, $table);
}
$this->assertRowEquals($row1, $this->site1, $map->getTable($this->date1)->getRowFromId(0));
$this->assertRowEquals($row2, $this->site2, $map->getTable($this->date1)->getRowFromId(1));
$this->assertRowEquals($this->defaultRow, $this->site1, $map->getTable($this->date2)->getRowFromId(0));
$this->assertRowEquals($row3, $this->site2, $map->getTable($this->date2)->getRowFromId(1));
}
public function test_makeMerged_shouldThrowAnException_IfANonNumericDataTypeIsGiven()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('supposed to work with non-numeric data types but it is not tested');
$dataType = 'blob';
$dataNames = array('nb_visits');
$factory = new DataTableFactory($dataNames, $dataType, array($this->site1), $periods = array(), new Segment('browserCode==ff', []), $this->defaultRow);
$factory->makeMerged(array(), array());
}
private function assertTableMetadataEquals($expectedPeriod, DataTable $dataTable)
{
$period = $dataTable->getMetadata(DataTableFactory::TABLE_METADATA_PERIOD_INDEX);
$this->assertFalse($dataTable->getMetadata(DataTableFactory::TABLE_METADATA_SITE_INDEX));
$this->assertTrue($period instanceof Period);
$this->assertSame($expectedPeriod, $period->toString());
$segment = $dataTable->getMetadata(DataTableFactory::TABLE_METADATA_SEGMENT_INDEX);
$this->assertEquals('browserCode==ff', $segment);
$segmentPretty = $dataTable->getMetadata(DataTableFactory::TABLE_METADATA_SEGMENT_PRETTY_INDEX);
$this->assertEquals('TEST SEGMENT', $segmentPretty);
}
private function assertRowCountEquals($expectedCount, $tableOrMap)
{
if ($tableOrMap instanceof DataTable\Map) {
$this->assertSame($expectedCount, $tableOrMap->getRowsCount());
} elseif ($tableOrMap instanceof DataTable) {
$this->assertSame($expectedCount, $tableOrMap->getRowsCountRecursive());
} else {
throw new \Exception('wrong argument passed to assertRowCountEquals()');
}
}
private function assertRowEquals($expectedColumns, $expectedSiteIdInMetadata, Row $row)
{
$this->assertEquals($expectedColumns, $row->getColumns());
$this->assertEquals(array('idsite' => $expectedSiteIdInMetadata), $row->getMetadata());
}
private function assertTableIsDataTableInstance($table)
{
$this->assertTrue($table instanceof DataTable);
$this->assertFalse($table instanceof DataTable\Simple);
}
private function assertTableIsDataTableSimpleInstance($table)
{
$this->assertTrue($table instanceof DataTable\Simple);
}
private function createFactory($resultIndices)
{
$periods = array(
$this->date1range => PeriodFactory::build('day', $this->date1),
$this->date2range => PeriodFactory::build('day', $this->date2),
);
$dataType = 'numeric';
$siteIds = array($this->site1, $this->site2);
$dataNames = array('nb_visits', 'nb_pageviews');
$defaultRow = $this->defaultRow;
if (!array_key_exists(DataTableFactory::TABLE_METADATA_PERIOD_INDEX, $resultIndices)) {
$periods = array($periods[$this->date1range]);
}
if (!array_key_exists(DataTableFactory::TABLE_METADATA_SITE_INDEX, $resultIndices)) {
$siteIds = array($siteIds[0]);
}
return new DataTableFactory($dataNames, $dataType, $siteIds, $periods, new Segment('browserCode==ff', []), $defaultRow);
}
private function getResultIndices($periodIndex = false, $siteIndex = false)
{
$indices = array();
if ($siteIndex) {
$indices[DataTableFactory::TABLE_METADATA_SITE_INDEX] = 'idSite';
}
if ($periodIndex) {
$indices[DataTableFactory::TABLE_METADATA_PERIOD_INDEX] = 'date';
}
return $indices;
}
public function provideContainerConfig()
{
return array(
'Piwik\Access' => new FakeAccess()
);
}
}
|