Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SegmentExpressionTest.php « Segment « Unit « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3003f56716a5b0950baf9268149eb4ac3ccf0cb8 (plain)
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
<?php
/**
 * Piwik - 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\Unit;

use Piwik\Segment\SegmentExpression;

/**
 * @group SegmentExpressionTest
 * @group Segment
 */
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
     */
    public function testSegmentSqlSimpleNoOperation($expression, $expectedSql)
    {
        $segment = new 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 IS NULL OR 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 IS NULL OR A <> ? ) AND C = ? ", 'bind' => array('B', 'D'))),
            array('A!=B;C==D,E!=Hello World!=', array('where' => " ( A IS NULL OR A <> ? ) AND (C = ? OR ( E IS NULL OR E <> ? ) )", 'bind' => array('B', 'D', 'Hello World!='))),
            array('A=@B;C=$D', array('where' => " A LIKE ? AND C LIKE ? ", 'bind' => array('%B%', '%D'))),

            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'))),
            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 IS NULL OR A NOT LIKE ? ) ", 'bind' => array('%B\%%'))),
            array('A=$B%', array('where' => " A LIKE ? ", 'bind' => array('%B\%'))),
            array('A=^B%', array('where' => " A LIKE ? ", 'bind' => array('B\%%'))),

            array('log_visit.A==3', ['where' => ' log_visit.A = ? ', 'bind' => ['3']], [], ['log_visit']),
            array('log_visit.A==3;log_conversion.B>4', ['where' => ' log_visit.A = ? AND log_conversion.B > ? ', 'bind' => ['3', '4']], [], ['log_visit', 'log_conversion']),
            array('(UNIX_TIMESTAMP(log_visit.A)-log_visit.B)==3', ['where' => ' (UNIX_TIMESTAMP(log_visit.A)-log_visit.B) = ? ', 'bind' => ['3']], ['log_conversion'], ['log_conversion', 'log_visit']),
            array('(UNIX_TIMESTAMP(`log_visit`.A)-log_visit.`B`)==3', ['where' => ' (UNIX_TIMESTAMP(`log_visit`.A)-log_visit.`B`) = ? ', 'bind' => ['3']], ['log_conversion'], ['log_conversion', 'log_visit']),
            array('(UNIX_TIMESTAMP(`log_visit.A`)-`log_visit`.`B`)==3', ['where' => ' (UNIX_TIMESTAMP(`log_visit.A`)-`log_visit`.`B`) = ? ', 'bind' => ['3']], ['log_conversion'], ['log_conversion', 'log_visit']),
        );
    }

    /**
     * @dataProvider getOperationSegmentExpressions
     * @group Core
     */
    public function testSegmentSqlWithOperations($expression, $expectedSql, $initialFrom = [], $expectedTables = [])
    {
        $segment = new SegmentExpression($expression);
        $segment->parseSubExpressions();
        $segment->parseSubExpressionsIntoSqlExpressions($initialFrom);
        $processed = $segment->getSql();
        if (empty($expectedSql['join'])) {
            $expectedSql['join'] = '';
        }
        $this->assertEquals($expectedSql, $processed);
        $this->assertEquals($expectedTables, $initialFrom);
    }

    /**
     * Dataprovider for testBogusFiltersExpectExceptionThrown
     * @return array
     */
    public function getBogusFilters()
    {
        return array(
            array('A=B'),
            array('C!D'),
            array(''),
            array('      '),
            array(',;,'),
            array(','),
            array(',,'),
            array('!='),
        );
    }

    /**
     * @dataProvider getBogusFilters
     * @group Core
     */
    public function testBogusFiltersExpectExceptionThrown($bogus)
    {
        $this->expectException(\Exception::class);

        $segment = new SegmentExpression($bogus);
        $segment->parseSubExpressions();
        $segment->getSql();
    }

    /**
     * @dataProvider getTestDataForParseColumnsFromSqlExpr
     */
    public function test_parseColumnsFromSqlExpr($field, $expected)
    {
        $actual = SegmentExpression::parseColumnsFromSqlExpr($field);
        $this->assertEquals($expected, $actual);
    }

    public function getTestDataForParseColumnsFromSqlExpr()
    {
        return [
            [
                'log_visit.column',
                ['log_visit.column'],
            ],
            [
                'log_visitcolumn',
                [],
            ],
            [
                '`log_visit.column`',
                ['log_visit.column'],
            ],
            [
                '`log_visit`.column',
                ['log_visit.column'],
            ],
            [
                '`log_visit`.`column`',
                ['log_visit.column'],
            ],
            [
                'log_visit.`column`',
                ['log_visit.column'],
            ],
            [
                'log_visit.column == 5 OR log_link_visit_action.othercolumn <> 3',
                ['log_visit.column', 'log_link_visit_action.othercolumn'],
            ],
            [
                '(log_visit.column == 5)',
                ['log_visit.column'],
            ],
            [
                '(log_visit.column = 5) AND ((HOUR(log_visit.column) == 12)) AND FUNC(mytable.mycolumn) - OTHERFUNC(`myothertable`.`myothercolumn`) = LASTFUNC(mylasttable.mylastcolumn)',
                ['log_visit.column', 'mytable.mycolumn', 'myothertable.myothercolumn', 'mylasttable.mylastcolumn'],
            ],
            [
                'log_visit.column = 5 OR @@session.whatever == 5',
                ['log_visit.column'],
            ],
            [
                '@something.whatever = 5',
                [],
            ],
            [
                '@something.whatever <> 5.0',
                [],
            ],
            [
                'log_visit.thing = 3.45 AND @log_visit.what < 23.00',
                ['log_visit.thing'],
            ],
        ];
    }
}