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
|
<?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\Plugins\SegmentEditor\tests\Integration;
use Piwik\ArchiveProcessor\Rules;
use Piwik\Plugins\SegmentEditor\API;
use Piwik\Plugins\SegmentEditor\SegmentQueryDecorator;
use Piwik\Segment;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
/**
* @group SegmentEditor
* @group SegmentEditor_Integration
*/
class SegmentQueryDecoratorTest extends IntegrationTestCase
{
/**
* @var SegmentQueryDecorator
*/
private $segmentQueryDecorator;
public function setUp(): void
{
parent::setUp();
Fixture::createWebsite('2011-01-01');
Fixture::createWebsite('2011-01-01');
Fixture::createWebsite('2011-01-01');
$this->segmentQueryDecorator = self::$fixture->piwikEnvironment->getContainer()->get(
'Piwik\Plugins\SegmentEditor\SegmentQueryDecorator');
Rules::setBrowserTriggerArchiving(false);
/** @var API $segmentEditorApi */
$segmentEditorApi = self::$fixture->piwikEnvironment->getContainer()->get(
'Piwik\Plugins\SegmentEditor\API');
$segmentEditorApi->add('segment 1', 'visitCount<2', $idSite = false, $autoArchive = true);
$segmentEditorApi->add('segment 2', 'countryCode==fr', $idSite = false, $autoArchive = true);
$segmentEditorApi->add('segment 3', 'visitCount<2', 1, $autoArchive = true);
$segmentEditorApi->add('segment 4', 'visitCount<2', 2, $autoArchive = true);
// test that segments w/ auto archive == false are included
$segmentEditorApi->add('segment 5', 'visitCount<2', 3, $autoArchive = false);
$segmentEditorApi->add('segment 6', 'countryCode!=fr', 3, $autoArchive = false);
Rules::setBrowserTriggerArchiving(true);
}
/**
* @dataProvider getTestDataForSegmentSqlTest
*/
public function test_SegmentSql_IsCorrectlyDecoratedWithIdSegment($segment, $triggerValue, $expectedPrefix)
{
if (!empty($triggerValue)) {
$_GET['trigger'] = $triggerValue;
}
$segment = new Segment($segment, array());
$query = $segment->getSelectQuery('*', 'log_visit');
$sql = $query['sql'];
if (empty($expectedPrefix)) {
$this->assertStringStartsNotWith("/* idSegments", $sql);
} else {
$this->assertStringStartsWith($expectedPrefix, $sql);
}
}
public function getTestDataForSegmentSqlTest()
{
return array(
array('countryCode==fr', null, '/* idSegments = [2] */'),
array('visitCount<2', null, '/* idSegments = [1, 3, 4, 5] */'),
array('', null, null),
array('countryCode!=fr', null, '/* idSegments = [6] */'),
array('', 'archivephp', '/* trigger = CronArchive */'),
array('countryCode!=fr', 'archivephp', '/* trigger = CronArchive, idSegments = [6] */'),
array('', 'garbage', null),
array('countryCode!=fr', 'garbage', '/* idSegments = [6] */'),
);
}
}
|