Newer
Older
Thomas Steur
a validé
<?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\Archiver\Request;
use Piwik\CliMulti;
Thomas Steur
a validé
use Piwik\CronArchive;
diosmosis
a validé
use Piwik\Archive\ArchiveInvalidator;
Thomas Steur
a validé
use Piwik\Date;
use Piwik\Db;
use Piwik\Plugins\CoreAdminHome\tests\Framework\Mock\API;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeLogger;
Thomas Steur
a validé
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Plugins\SegmentEditor\API as SegmentAPI;
Thomas Steur
a validé
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
class FakeCliMulti extends CliMulti
{
public static $specifiedResults = null;
public function request(array $piwikUrls)
{
if (empty(FakeCliMulti::$specifiedResults)) {
return parent::request($piwikUrls);
}
$results = array();
foreach ($piwikUrls as $url) {
if ($url instanceof Request) {
$url->start();
$url = (string)$url;
}
$results[] = $this->getSpecifiedResult($url);
}
return $results;
}
private function getSpecifiedResult($url)
{
foreach (FakeCliMulti::$specifiedResults as $pattern => $result) {
if (substr($pattern, 0, 1) == '/'
&& substr($pattern, strlen($pattern) - 1, 1) == '/'
) {
$isMatch = preg_match($pattern, $url);
} else {
$isMatch = $pattern == $url;
}
if (!$isMatch) {
continue;
}
if (is_callable($result)) {
return $result($url);
} else {
return $result;
}
}
return null;
}
}
Thomas Steur
a validé
/**
* @group Archiver
* @group CronArchive
*/
class CronArchiveTest extends IntegrationTestCase
{
public function test_getColumnNamesFromTable()
Thomas Steur
a validé
{
Fixture::createWebsite('2014-12-12 00:01:02');
Fixture::createWebsite('2014-12-12 00:01:02');
diosmosis
a validé
$ar = new ArchiveInvalidator();
Thomas Steur
a validé
$ar->rememberToInvalidateArchivedReportsLater(1, Date::factory('2014-04-05'));
$ar->rememberToInvalidateArchivedReportsLater(2, Date::factory('2014-04-05'));
$ar->rememberToInvalidateArchivedReportsLater(2, Date::factory('2014-04-06'));
$api = API::getInstance();
$cronarchive = new TestCronArchive(Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php');
$cronarchive->setApiToInvalidateArchivedReport($api);
$cronarchive->init();
$expectedInvalidations = array(
array(array(1,2), '2014-04-05'),
array(array(2), '2014-04-06')
);
$this->assertEquals($expectedInvalidations, $api->getInvalidatedReports());
}
public function test_setSegmentsToForceFromSegmentIds_CorrectlyGetsSegmentDefinitions_FromSegmentIds()
{
Fixture::createWebsite('2014-12-12 00:01:02');
SegmentAPI::getInstance()->add('foo', 'actions>=1', 1, true, true);
SegmentAPI::getInstance()->add('barb', 'actions>=2', 1, true, true);
SegmentAPI::getInstance()->add('burb', 'actions>=3', 1, true, true);
SegmentAPI::getInstance()->add('sub', 'actions>=4', 1, true, true);
$cronarchive = new TestCronArchive(Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php');
$cronarchive->setSegmentsToForceFromSegmentIds(array(2, 4));
$expectedSegments = array('actions>=2', 'actions>=4');
$this->assertEquals($expectedSegments, array_values($cronarchive->segmentsToForce));
}
FakeCliMulti::$specifiedResults = array(
'/method=API.get/' => serialize(array(array('nb_visits' => 1)))
);
Fixture::createWebsite('2014-12-12 00:01:02');
SegmentAPI::getInstance()->add('foo', 'actions>=2', 1, true, true);
SegmentAPI::getInstance()->add('burr', 'actions>=4', 1, true, true);
diosmosis
a validé
$archiver = new CronArchive(null, $logger);
$archiver->shouldArchiveAllSites = true;
$archiver->shouldArchiveAllPeriodsSince = true;
$archiver->segmentsToForce = array('actions>=2;browserCode=FF', 'actions>=2');
$archiver->init();
$archiver->run();
$expected = <<<LOG
---------------------------
INIT
Running Piwik %s as Super User
---------------------------
NOTES
- If you execute this script at least once per hour (or more often) in a crontab, you may disable 'Browser trigger archiving' in Piwik UI > Settings > General Settings.
See the doc at: http://piwik.org/docs/setup-auto-archiving/
- Reports for today will be processed at most every %s seconds. You can change this value in Piwik UI > Settings > General Settings.
- Reports for the current week/month/year will be refreshed at most every %s seconds.
- Will process all 1 websites
- Limiting segment archiving to following segments:
* actions>=2;browserCode=FF
* actions>=2
---------------------------
START
Starting Piwik reports archiving...
Will pre-process for website id = 1, day period
- skipping segment archiving for 'actions>=4'.
- pre-processing segment 1/1 actions>=2
Archived website id = 1, period = day, 1 segments, 0 visits in last %s days, 0 visits today, Time elapsed: %s
Will pre-process for website id = 1, week period
- skipping segment archiving for 'actions>=4'.
- pre-processing segment 1/1 actions>=2
Archived website id = 1, period = week, 1 segments, 1 visits in last %s weeks, 1 visits this week, Time elapsed: %s
Will pre-process for website id = 1, month period
- skipping segment archiving for 'actions>=4'.
- pre-processing segment 1/1 actions>=2
Archived website id = 1, period = month, 1 segments, 1 visits in last %s months, 1 visits this month, Time elapsed: %s
Will pre-process for website id = 1, year period
- skipping segment archiving for 'actions>=4'.
- pre-processing segment 1/1 actions>=2
Archived website id = 1, period = year, 1 segments, 1 visits in last %s years, 1 visits this year, Time elapsed: %s
Archived website id = 1, %s API requests, Time elapsed: %s [1/1 done]
Done archiving!
---------------------------
SUMMARY
Total visits for today across archived websites: 1
Archived today's reports for 1 websites
Archived week/month/year for 1 websites
Skipped 0 websites: no new visit since the last script execution
Skipped 0 websites day archiving: existing daily reports are less than 150 seconds old
Skipped 0 websites week/month/year archiving: existing periods reports are less than 3600 seconds old
done: 1/1 100%, 1 vtoday, 1 wtoday, 1 wperiods, %s req, %s ms, no error
Time elapsed: %s
LOG;
$this->assertStringMatchesFormat($expected, $logger->output);
}
public function provideContainerConfig()
{
return array(
'Piwik\CliMulti' => \DI\object('Piwik\Tests\Integration\FakeCliMulti')
);
}
}
class TestCronArchive extends CronArchive
{
protected function checkPiwikUrlIsValid()
{
}
protected function initPiwikHost($piwikUrl = false)
{
}