Newer
Older
<?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;
Thomas Steur
a validé
use Piwik\Archive as PiwikArchive;
use Piwik\ArchiveProcessor\Parameters;
use Piwik\ArchiveProcessor\Rules;
use Piwik\Common;
use Piwik\Config;
use Piwik\DataAccess\ArchiveTableCreator;
Thomas Steur
a validé
use Piwik\DataAccess\ArchiveWriter;
use Piwik\Date;
use Piwik\Db;
use Piwik\Piwik;
Thomas Steur
a validé
use Piwik\Segment;
use Piwik\Site;
use Piwik\Tests\Fixtures\OneVisitorTwoVisits;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
Thomas Steur
a validé
use Piwik\Period\Factory as PeriodFactory;
use Piwik\Archive\Chunk;
class Archive extends PiwikArchive
{
public function get($archiveNames, $archiveDataType, $idSubtable = null)
{
return parent::get($archiveNames, $archiveDataType, $idSubtable);
}
}
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
/**
* @group Core
*/
class ArchiveTest extends IntegrationTestCase
{
/**
* @var OneVisitorTwoVisits
*/
public static $fixture;
public function tearDown()
{
parent::tearDown();
unset($_GET['trigger']);
}
protected static function configureFixture($fixture)
{
$fixture->createSuperUser = true;
}
protected static function beforeTableDataCached()
{
$date = Date::factory('2010-03-01');
$archiveTableCreator = new ArchiveTableCreator();
$archiveTableCreator->getBlobTable($date);
$archiveTableCreator->getNumericTable($date);
}
public function getForceOptionsForForceArchivingOnBrowserRequest()
{
return array(
array(1),
array(null)
);
}
/**
* @dataProvider getForceOptionsForForceArchivingOnBrowserRequest
*/
public function test_ArchivingIsLaunchedForRanges_WhenForceOnBrowserRequest_IsTruthy($optionValue)
{
$this->archiveDataForIndividualDays();
Config::getInstance()->General['archiving_range_force_on_browser_request'] = $optionValue;
Rules::setBrowserTriggerArchiving(false);
$data = $this->initiateArchivingForRange();
$this->assertNotEmpty($data);
$this->assertArchiveTablesAreNotEmpty('2010_03');
}
public function test_ArchivingIsNotLaunchedForRanges_WhenForceOnBrowserRequest_IsFalse()
{
$this->archiveDataForIndividualDays();
Config::getInstance()->General['archiving_range_force_on_browser_request'] = 0;
Rules::setBrowserTriggerArchiving(false);
$data = $this->initiateArchivingForRange();
$this->assertEmpty($data);
$this->assertArchiveTablesAreEmpty('2010_03');
}
public function test_ArchiveIsLaunched_WhenForceOnBrowserRequest_IsFalse_AndArchivePhpTriggered()
{
$this->archiveDataForIndividualDays();
Config::getInstance()->General['archiving_range_force_on_browser_request'] = 0;
$_GET['trigger'] = 'archivephp';
Rules::setBrowserTriggerArchiving(false);
$data = $this->initiateArchivingForRange();
$this->assertNotEmpty($data);
$this->assertArchiveTablesAreNotEmpty('2010_03');
}
Thomas Steur
a validé
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
public function test_ArchiveBlob_ShouldBeAbleToLoadFirstLevelDataArrays()
{
$this->createManyDifferentArchiveBlobs();
$archive = $this->getArchive('day', '2013-01-01,2013-01-05');
$dataArrays = $archive->get(array('Actions_Actionsurl'), 'blob');
$this->assertArchiveBlob($dataArrays, '2013-01-01', array('Actions_Actionsurl' => 'test01'));
$this->assertArchiveBlob($dataArrays, '2013-01-02', array('Actions_Actionsurl' => 'test02'));
$this->assertArchiveBlob($dataArrays, '2013-01-03', array('Actions_Actionsurl' => 'test03'));
$this->assertArchiveBlob($dataArrays, '2013-01-04', array('Actions_Actionsurl' => 'test04'));
$this->assertArchiveBlob($dataArrays, '2013-01-05', array('Actions_Actionsurl' => 0));
}
public function test_ArchiveBlob_ShouldBeAbleToLoadOneSubtable_NoMatterWhetherTheyAreStoredSeparatelyOrInACombinedSubtableEntry()
{
$this->createManyDifferentArchiveBlobs();
$archive = $this->getArchive('day', '2013-01-01,2013-01-05');
$dataArrays = $archive->get(array('Actions_Actionsurl'), 'blob', 2);
$this->assertArchiveBlob($dataArrays, '2013-01-01', array('Actions_Actionsurl_2' => 0));
$this->assertArchiveBlob($dataArrays, '2013-01-02', array('Actions_Actionsurl_2' => 'test2'));
$this->assertArchiveBlob($dataArrays, '2013-01-03', array('Actions_Actionsurl_2' => 'subtable2'));
$this->assertArchiveBlob($dataArrays, '2013-01-04', array('Actions_Actionsurl_2' => 0));
$this->assertArchiveBlob($dataArrays, '2013-01-05', array('Actions_Actionsurl_2' => 0));
// test another one
$dataArrays = $archive->get(array('Actions_Actionsurl'), 'blob', 5);
$this->assertArchiveBlob($dataArrays, '2013-01-01', array('Actions_Actionsurl_5' => 0));
$this->assertArchiveBlob($dataArrays, '2013-01-02', array('Actions_Actionsurl_5' => 0));
$this->assertArchiveBlob($dataArrays, '2013-01-03', array('Actions_Actionsurl_5' => 'subtable5'));
$this->assertArchiveBlob($dataArrays, '2013-01-04', array('Actions_Actionsurl_5' => 'subtable45'));
$this->assertArchiveBlob($dataArrays, '2013-01-05', array('Actions_Actionsurl_5' => 0));
// test one that does not exist
$dataArrays = $archive->get(array('Actions_Actionsurl'), 'blob', 999);
$this->assertArchiveBlob($dataArrays, '2013-01-01', array('Actions_Actionsurl_999' => 0));
$this->assertArchiveBlob($dataArrays, '2013-01-02', array('Actions_Actionsurl_999' => 0));
$this->assertArchiveBlob($dataArrays, '2013-01-03', array('Actions_Actionsurl_999' => 0));
$this->assertArchiveBlob($dataArrays, '2013-01-04', array('Actions_Actionsurl_999' => 0));
$this->assertArchiveBlob($dataArrays, '2013-01-05', array('Actions_Actionsurl_999' => 0));
}
public function test_ArchiveBlob_ShouldBeAbleToLoadAllSubtables_NoMatterWhetherTheyAreStoredSeparatelyOrInACombinedSubtableEntry()
{
$this->createManyDifferentArchiveBlobs();
$archive = $this->getArchive('day', '2013-01-01,2013-01-06');
$dataArrays = $archive->get(array('Actions_Actionsurl'), 'blob', Archive::ID_SUBTABLE_LOAD_ALL_SUBTABLES);
$this->assertArchiveBlob($dataArrays, '2013-01-01', array('Actions_Actionsurl' => 'test01'));
$this->assertArchiveBlob($dataArrays, '2013-01-02', array('Actions_Actionsurl' => 'test02', 'Actions_Actionsurl_1' => 'test1', 'Actions_Actionsurl_2' => 'test2'));
$this->assertArchiveBlob($dataArrays, '2013-01-03', array('Actions_Actionsurl' => 'test03', 'Actions_Actionsurl_1' => 'subtable1', 'Actions_Actionsurl_2' => 'subtable2', 'Actions_Actionsurl_5' => 'subtable5'));
$this->assertArchiveBlob($dataArrays, '2013-01-04', array('Actions_Actionsurl' => 'test04', 'Actions_Actionsurl_5' => 'subtable45', 'Actions_Actionsurl_6' => 'subtable6'));
$this->assertArchiveBlob($dataArrays, '2013-01-05', array('Actions_Actionsurl' => 0));
$this->assertArchiveBlob($dataArrays, '2013-01-06', array('Actions_Actionsurl' => 'test06'));
}
public function test_ArchiveBlob_ShouldBeAbleToLoadDifferentArchives_NoMatterWhetherTheyAreStoredSeparatelyOrInACombinedSubtableEntry()
{
$this->createManyDifferentArchiveBlobs();
$archive = $this->getArchive('day', '2013-01-01,2013-01-06');
$dataArrays = $archive->get(array('Actions_Actionsurl', 'Actions_Actions'), 'blob', 2);
$this->assertArchiveBlob($dataArrays, '2013-01-01', array('Actions_Actionsurl_2' => 0, 'Actions_Actions_2' => 0));
$this->assertArchiveBlob($dataArrays, '2013-01-02', array('Actions_Actions_2' => 'actionsSubtable2', 'Actions_Actionsurl_2' => 'test2'));
$this->assertArchiveBlob($dataArrays, '2013-01-03', array('Actions_Actions_2' => 'actionsTest2', 'Actions_Actionsurl_2' => 'subtable2'));
$this->assertArchiveBlob($dataArrays, '2013-01-04', array('Actions_Actionsurl_2' => 0, 'Actions_Actions_2' => 0));
$this->assertArchiveBlob($dataArrays, '2013-01-05', array('Actions_Actionsurl_2' => 0, 'Actions_Actions_2' => 0));
$this->assertArchiveBlob($dataArrays, '2013-01-06', array('Actions_Actionsurl_2' => 0, 'Actions_Actions_2' => 0));
}
/**
* @dataProvider findBlobsWithinDifferentChunksDataProvider
*/
public function test_ArchiveBlob_ShouldBeFindBlobs_WithinDifferentChunks($idSubtable, $expectedBlob)
{
Thomas Steur
a validé
$recordName = 'Actions_Actions';
Thomas Steur
a validé
$chunk = new Chunk();
Thomas Steur
a validé
$chunk5 = $chunk->getRecordNameForTableId($recordName, $subtableId = 5);
$chunk152 = $chunk->getRecordNameForTableId($recordName, $subtableId = 152);
$chunk399 = $chunk->getRecordNameForTableId($recordName, $subtableId = 399);
Thomas Steur
a validé
$this->createArchiveBlobEntry('2013-01-02', array(
Thomas Steur
a validé
$recordName => 'actions_02',
$chunk5 => serialize(array(1 => 'actionsSubtable1', 2 => 'actionsSubtable2', 5 => 'actionsSubtable5')),
$chunk152 => serialize(array(151 => 'actionsSubtable151', 152 => 'actionsSubtable152')),
$chunk399 => serialize(array(399 => 'actionsSubtable399'))
Thomas Steur
a validé
));
$archive = $this->getArchive('day', '2013-01-02,2013-01-02');
$dataArrays = $archive->get(array('Actions_Actions'), 'blob', $idSubtable);
$this->assertArchiveBlob($dataArrays, '2013-01-02', $expectedBlob);
}
public function findBlobsWithinDifferentChunksDataProvider()
{
return array(
array($idSubtable = 2, $expectedBlobs = array('Actions_Actions_2' => 'actionsSubtable2')),
array(5, array('Actions_Actions_5' => 'actionsSubtable5')),
array(151, array('Actions_Actions_151' => 'actionsSubtable151')),
array(152, array('Actions_Actions_152' => 'actionsSubtable152')),
array(399, array('Actions_Actions_399' => 'actionsSubtable399')),
// this one does not exist
array(404, array('Actions_Actions_404' => 0)),
);
}
private function createManyDifferentArchiveBlobs()
{
Thomas Steur
a validé
$recordName1 = 'Actions_Actions';
$recordName2 = 'Actions_Actionsurl';
Thomas Steur
a validé
$chunk = new Chunk();
Thomas Steur
a validé
$chunk0_1 = $chunk->getRecordNameForTableId($recordName1, 0);
$chunk0_2 = $chunk->getRecordNameForTableId($recordName2, 0);
Thomas Steur
a validé
$this->createArchiveBlobEntry('2013-01-01', array(
Thomas Steur
a validé
$recordName2 => 'test01'
Thomas Steur
a validé
));
$this->createArchiveBlobEntry('2013-01-02', array(
Thomas Steur
a validé
$recordName2 => 'test02',
$recordName2 . '_1' => 'test1', // testing BC where each subtable was stored seperately
$recordName2 . '_2' => 'test2', // testing BC
$recordName1 => 'actions_02',
$chunk0_1 => serialize(array(1 => 'actionsSubtable1', 2 => 'actionsSubtable2', 5 => 'actionsSubtable5'))
Thomas Steur
a validé
));
$this->createArchiveBlobEntry('2013-01-03', array(
Thomas Steur
a validé
$recordName2 => 'test03',
$chunk0_2 => serialize(array(1 => 'subtable1', 2 => 'subtable2', 5 => 'subtable5')),
$recordName1 => 'actions_03',
$recordName1 . '_1' => 'actionsTest1',
$recordName1 . '_2' => 'actionsTest2'
));
Thomas Steur
a validé
$this->createArchiveBlobEntry('2013-01-04', array(
Thomas Steur
a validé
$recordName2 => 'test04',
$recordName2 . '_5' => 'subtable45',
$recordName2 . '_6' => 'subtable6'
));
Thomas Steur
a validé
$this->createArchiveBlobEntry('2013-01-06', array(
Thomas Steur
a validé
$recordName2 => 'test06',
$chunk0_2 => serialize(array())
));
Thomas Steur
a validé
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
}
private function assertArchiveBlob(PiwikArchive\DataCollection $dataCollection, $date, $expectedBlob)
{
$dateIndex = $date . ',' . $date;
$dataArrays = $dataCollection->get(1, $dateIndex);
if (!empty($expectedBlob) && 0 !== reset($expectedBlob)) {
$this->assertNotEmpty($dataArrays['_metadata']['ts_archived']);
$dataArrays['_metadata']['ts_archived'] = true;
unset($dataArrays['_metadata']);
}
$this->assertEquals($expectedBlob, $dataArrays);
}
private function createArchiveBlobEntry($date, $blobs)
{
$oPeriod = PeriodFactory::makePeriodFromQueryParams('UTC', 'day', $date);
$segment = new Segment(false, array(1));
$params = new Parameters(new Site(1), $oPeriod, $segment);
$writer = new ArchiveWriter($params, false);
$writer->initNewArchive();
foreach ($blobs as $name => $blob) {
$writer->insertBlobRecord($name, $blob);
}
$writer->finalizeArchive();
}
private function assertArchiveTablesAreNotEmpty($tableMonth)
{
$this->assertNotEquals(0, $this->getRangeArchiveTableCount('archive_numeric', $tableMonth));
}
private function assertArchiveTablesAreEmpty($tableMonth)
{
$this->assertEquals(0, $this->getRangeArchiveTableCount('archive_numeric', $tableMonth));
$this->assertEquals(0, $this->getRangeArchiveTableCount('archive_blob', $tableMonth));
}
private function getRangeArchiveTableCount($tableType, $tableMonth)
{
$table = Common::prefixTable($tableType . '_' . $tableMonth);
return Db::fetchOne("SELECT COUNT(*) FROM $table WHERE period = " . Piwik::$idPeriods['range']);
}
private function initiateArchivingForRange()
{
Thomas Steur
a validé
$archive = $this->getArchive('range');
return $archive->getNumeric('nb_visits');
}
private function archiveDataForIndividualDays()
{
Thomas Steur
a validé
$archive = $this->getArchive('day');
return $archive->getNumeric('nb_visits');
}
Thomas Steur
a validé
private function getArchive($period, $day = '2010-03-04,2010-03-07')
{
return Archive::build(self::$fixture->idSite, $period, $day);
}
}
ArchiveTest::$fixture = new OneVisitorTwoVisits();