Newer
Older
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
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
class SegmentExpressionTest extends PHPUnit_Framework_TestCase
{
/**
* Dataprovider for testSegmentSqlSimpleNoOperation
* @return array
*/
public function getSimpleSegmentExpressions()
{
return array(
// classic expressions
array('A', " A "),
array('A,B', " (A OR B )"),
array('A;B', " A AND B "),
array('A;B;C', " A AND B AND C "),
array('A,B;C,D;E,F,G', " (A OR B) AND (C OR D) AND (E OR F OR G )"),
// unescape the backslash
array('A\,B\,C,D', " (A,B,C OR D )"),
array('\,A', ' ,A '),
// unescape only when it was escaping a known delimiter
array('\\\A', ' \\\A '),
// unescape at the end
array('\,\;\A\B,\,C,D\;E\,', ' (,;\A\B OR ,C OR D;E, )'),
// only replace when a following expression is detected
array('A,', ' A, '),
array('A;', ' A; '),
array('A;B;', ' A AND B; '),
array('A,B,', ' (A OR B, )'),
);
}
/**
* @dataProvider getSimpleSegmentExpressions
* @group Core
* @group SegmentExpression
*/
public function testSegmentSqlSimpleNoOperation($expression, $expectedSql)
{
$segment = new Piwik_SegmentExpression($expression);
$expected = array('where' => $expectedSql, 'bind' => array(), 'join' => '');
$processed = $segment->getSql();
$this->assertEquals($expected, $processed);
}
/**
* Dataprovider for testSegmentSqlWithOperations
* @return array
*/
public function getOperationSegmentExpressions()
{
// Filter expression => SQL string + Bind values
return array(
array('A==B%', array('where' => " A = ? ", 'bind' => array('B%'))),
array('ABCDEF====B===', array('where' => " ABCDEF = ? ", 'bind' => array('==B==='))),
array('A===B;CDEF!=C!=', array('where' => " A = ? AND CDEF <> ? ", 'bind' => array('=B', 'C!='))),
array('A==B,C==D', array('where' => " (A = ? OR C = ? )", 'bind' => array('B', 'D'))),
array('A!=B;C==D', array('where' => " A <> ? AND C = ? ", 'bind' => array('B', 'D'))),
array('A!=B;C==D,E!=Hello World!=', array('where' => " A <> ? AND (C = ? OR E <> ? )", 'bind' => array('B', 'D', 'Hello World!='))),
array('A>B', array('where' => " A > ? ", 'bind' => array('B'))),
array('A<B', array('where' => " A < ? ", 'bind' => array('B'))),
array('A<=B', array('where' => " A <= ? ", 'bind' => array('B'))),
array('A>=B', array('where' => " A >= ? ", 'bind' => array('B'))),
array('ABCDEF>=>=>=B===', array('where' => " ABCDEF >= ? ", 'bind' => array('>=>=B==='))),
array('A>=<=B;CDEF>G;H>=I;J<K;L<=M', array('where' => " A >= ? AND CDEF > ? AND H >= ? AND J < ? AND L <= ? ", 'bind' => array('<=B', 'G', 'I', 'K', 'M'))),
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
array('A>=B;C>=D,E<w_ow great!', array('where' => " A >= ? AND (C >= ? OR E < ? )", 'bind' => array('B', 'D', 'w_ow great!'))),
array('A=@B_', array('where' => " A LIKE ? ", 'bind' => array('%B\_%'))),
array('A!@B%', array('where' => " A NOT LIKE ? ", 'bind' => array('%B\%%'))),
);
}
/**
* @dataProvider getOperationSegmentExpressions
* @group Core
* @group SegmentExpression
*/
public function testSegmentSqlWithOperations($expression, $expectedSql)
{
$segment = new Piwik_SegmentExpression($expression);
$segment->parseSubExpressions();
$segment->parseSubExpressionsIntoSqlExpressions();
$processed = $segment->getSql();
$expectedSql['join'] = '';
$this->assertEquals($expectedSql, $processed);
}
/**
* Dataprovider for testBogusFiltersExpectExceptionThrown
* @return array
*/
public function getBogusFilters()
{
return array(
array('A=B'),
array('C!D'),
array(''),
array(' '),
array(',;,'),
array(','),
array(',,'),
array('==='),
array('!=')
);
}
/**
* @dataProvider getBogusFilters
* @group Core
* @group SegmentExpression
*/
public function testBogusFiltersExpectExceptionThrown($bogus)
{
try {
$segment = new Piwik_SegmentExpression($bogus);
$segment->parseSubExpressions();
$segment->getSql();
} catch (Exception $e) {
return;
}
$this->fail('Expected exception not raised');