848 |
florian |
1 |
<?php
|
|
|
2 |
// $Id$
|
|
|
3 |
|
|
|
4 |
require_once 'simple_include.php';
|
|
|
5 |
require_once 'pager_wrapper_include.php';
|
|
|
6 |
|
|
|
7 |
class TestOfPagerWrapper extends UnitTestCase
|
|
|
8 |
{
|
|
|
9 |
function TestOfPagerWrapper($name='Test of Pager_Wrapper') {
|
|
|
10 |
$this->UnitTestCase($name);
|
|
|
11 |
}
|
|
|
12 |
|
|
|
13 |
function setUp() { }
|
|
|
14 |
function tearDown() { }
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Basic tests for rewriteCountQuery()
|
|
|
18 |
*/
|
|
|
19 |
function testRewriteCountQuery() {
|
|
|
20 |
//test LIMIT
|
|
|
21 |
$query = 'SELECT a, b, c, d FROM mytable WHERE a=1 AND c="g" LIMIT 2';
|
|
|
22 |
$expected = 'SELECT COUNT(*) FROM mytable WHERE a=1 AND c="g"';
|
|
|
23 |
$this->assertEqual($expected, rewriteCountQuery($query));
|
|
|
24 |
|
|
|
25 |
//test ORDER BY and quotes
|
|
|
26 |
$query = 'SELECT a, b, c, d FROM mytable WHERE a=1 AND c="g" ORDER BY (a, b)';
|
|
|
27 |
$expected = 'SELECT COUNT(*) FROM mytable WHERE a=1 AND c="g"';
|
|
|
28 |
$this->assertEqual($expected, rewriteCountQuery($query));
|
|
|
29 |
|
|
|
30 |
//test CR/LF
|
|
|
31 |
$query = 'SELECT a, b, c, d FROM mytable
|
|
|
32 |
WHERE a=1
|
|
|
33 |
AND c="g"
|
|
|
34 |
ORDER BY (a, b)';
|
|
|
35 |
$expected = 'SELECT COUNT(*) FROM mytable
|
|
|
36 |
WHERE a=1
|
|
|
37 |
AND c="g"';
|
|
|
38 |
$this->assertEqual($expected, rewriteCountQuery($query));
|
|
|
39 |
|
|
|
40 |
//test GROUP BY
|
|
|
41 |
$query = 'SELECT a, b, c, d FROM mytable WHERE a=1 GROUP BY c';
|
|
|
42 |
$this->assertFalse(rewriteCountQuery($query));
|
|
|
43 |
|
|
|
44 |
//test DISTINCT
|
|
|
45 |
$query = 'SELECT DISTINCT a, b, c, d FROM mytable WHERE a=1 GROUP BY c';
|
|
|
46 |
$this->assertFalse(rewriteCountQuery($query));
|
|
|
47 |
|
|
|
48 |
//test MiXeD Keyword CaSe
|
|
|
49 |
$query = 'SELECT a, b, c, d from mytable WHERE a=1 AND c="g"';
|
|
|
50 |
$expected = 'SELECT COUNT(*) FROM mytable WHERE a=1 AND c="g"';
|
|
|
51 |
$this->assertEqual($expected, rewriteCountQuery($query));
|
|
|
52 |
|
|
|
53 |
//test function speed... this query used to be very slow to parse
|
|
|
54 |
$query = "SELECT i.item_id,
|
|
|
55 |
ia.addition,
|
|
|
56 |
u.username,
|
|
|
57 |
i.date_created,
|
|
|
58 |
i.start_date,
|
|
|
59 |
i.expiry_date
|
|
|
60 |
FROM item i, item_addition ia, item_type it, item_type_mapping itm, usr u, category c
|
|
|
61 |
WHERE ia.item_type_mapping_id = itm.item_type_mapping_id
|
|
|
62 |
AND i.updated_by_id = u.usr_id
|
|
|
63 |
AND it.item_type_id = itm.item_type_id
|
|
|
64 |
AND i.item_id = ia.item_id
|
|
|
65 |
AND i.item_type_id = it.item_type_id
|
|
|
66 |
AND itm.field_name = 'title' AND it.item_type_id = 2 AND i.category_id = 1 AND i.status = 4
|
|
|
67 |
AND i.category_id = c.category_id
|
|
|
68 |
AND 0 NOT IN (COALESCE(c.perms, '-1'))
|
|
|
69 |
ORDER BY i.last_updated DESC";
|
|
|
70 |
$expected = "SELECT COUNT(*) FROM item i, item_addition ia, item_type it, item_type_mapping itm, usr u, category c
|
|
|
71 |
WHERE ia.item_type_mapping_id = itm.item_type_mapping_id
|
|
|
72 |
AND i.updated_by_id = u.usr_id
|
|
|
73 |
AND it.item_type_id = itm.item_type_id
|
|
|
74 |
AND i.item_id = ia.item_id
|
|
|
75 |
AND i.item_type_id = it.item_type_id
|
|
|
76 |
AND itm.field_name = 'title' AND it.item_type_id = 2 AND i.category_id = 1 AND i.status = 4
|
|
|
77 |
AND i.category_id = c.category_id
|
|
|
78 |
AND 0 NOT IN (COALESCE(c.perms, '-1'))";
|
|
|
79 |
$this->assertEqual($expected, rewriteCountQuery($query));
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Test rewriteCountQuery() with queries having a subquery in the SELECT clause
|
|
|
84 |
*/
|
|
|
85 |
function testRewriteCountQuery_SubqueriesInSelectClause() {
|
|
|
86 |
$query = 'SELECT a, (SELECT a FROM b) AS b, c, d FROM mytable WHERE a=1 AND c="g" LIMIT 2';
|
|
|
87 |
$expected = 'SELECT COUNT(*) FROM mytable WHERE a=1 AND c="g"';
|
|
|
88 |
$this->assertFalse(rewriteCountQuery($query));
|
|
|
89 |
|
|
|
90 |
$query = 'SELECT a, (SELECT a FROM b) AS b, (SELECT c FROM c) AS c, d FROM mytable WHERE a=1 AND c="g" LIMIT 2';
|
|
|
91 |
//$expected = 'SELECT COUNT(*) FROM mytable WHERE a=1 AND c="g"';
|
|
|
92 |
$this->assertFalse(rewriteCountQuery($query));
|
|
|
93 |
|
|
|
94 |
$query = 'SELECT `id`, `ip`, (
|
|
|
95 |
SELECT TIMEDIFF(MAX(P.`time`), MIN(P.`time`))
|
|
|
96 |
FROM `przejscia` as P
|
|
|
97 |
WHERE P.`id_wejscia`=W.`id`
|
|
|
98 |
) as `czas`
|
|
|
99 |
FROM `wejscia` as W
|
|
|
100 |
WHERE W.id_domeny=?
|
|
|
101 |
ORDER BY W.czas_wejscia DESC';
|
|
|
102 |
$expected = 'SELECT COUNT(*)
|
|
|
103 |
FROM `wejscia` as W
|
|
|
104 |
WHERE W.id_domeny=?
|
|
|
105 |
ORDER BY W.czas_wejscia DESC';
|
|
|
106 |
$this->assertFalse(rewriteCountQuery($query));
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Test rewriteCountQuery() with queries having a subquery in the FROM clause
|
|
|
111 |
*/
|
|
|
112 |
function testRewriteCountQuery_SubqueriesInFromClause() {
|
|
|
113 |
$query = 'SELECT a, b, c, d FROM (SELECT a, b, c, d FROM mytable WHERE a=1) AS tbl_alias WHERE a=1';
|
|
|
114 |
$expected = 'SELECT COUNT(*) FROM (SELECT a, b, c, d FROM mytable WHERE a=1) AS tbl_alias WHERE a=1';
|
|
|
115 |
$this->assertEqual($expected, rewriteCountQuery($query));
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Test rewriteCountQuery() with queries having a subquery in the WHERE clause
|
|
|
120 |
*/
|
|
|
121 |
function testRewriteCountQuery_SubqueriesInWhereClause() {
|
|
|
122 |
//this one is not rewritten: subqueries with ORDER BY clauses might get truncated
|
|
|
123 |
$query = 'SELECT Version.VersionId, Version.Identifier,News.*
|
|
|
124 |
FROM VersionBroker
|
|
|
125 |
JOIN ObjectType ON ObjectType.ObjectTypeId = VersionBroker.ObjectTypeId
|
|
|
126 |
JOIN Version ON VersionBroker.Identifier = Version.Identifier
|
|
|
127 |
JOIN News ON Version.ObjectId = News.NewsId
|
|
|
128 |
WHERE Version.Status = \'Approved\'
|
|
|
129 |
AND ObjectType.Name = \'News\'
|
|
|
130 |
AND Version.ApprovedTS = (
|
|
|
131 |
SELECT SubV.ApprovedTS
|
|
|
132 |
FROM Version SubV
|
|
|
133 |
WHERE SubV.Identifier = VersionBroker.Identifier
|
|
|
134 |
ORDER BY ApprovedTS DESC
|
|
|
135 |
LIMIT 1)
|
|
|
136 |
ORDER BY ApprovedTS DESC';
|
|
|
137 |
|
|
|
138 |
$expected = 'SELECT COUNT(*)
|
|
|
139 |
FROM VersionBroker
|
|
|
140 |
JOIN ObjectType ON ObjectType.ObjectTypeId = VersionBroker.ObjectTypeId
|
|
|
141 |
JOIN Version ON VersionBroker.Identifier = Version.Identifier
|
|
|
142 |
JOIN News ON Version.ObjectId = News.NewsId
|
|
|
143 |
WHERE Version.Status = \'Approved\'
|
|
|
144 |
AND ObjectType.Name = \'News\'
|
|
|
145 |
AND Version.ApprovedTS = (
|
|
|
146 |
SELECT SubV.ApprovedTS
|
|
|
147 |
FROM Version SubV
|
|
|
148 |
WHERE SubV.Identifier = VersionBroker.Identifier
|
|
|
149 |
ORDER BY ApprovedTS DESC
|
|
|
150 |
LIMIT 1)
|
|
|
151 |
ORDER BY ApprovedTS DESC';
|
|
|
152 |
//$this->assertEqual($expected, rewriteCountQuery($query));
|
|
|
153 |
$this->assertFalse(rewriteCountQuery($query));
|
|
|
154 |
|
|
|
155 |
//this one should pass... subquery without ORDER BY or LIMIT clause
|
|
|
156 |
$query = 'SELECT Version.VersionId, Version.Identifier,News.* FROM VersionBroker JOIN
|
|
|
157 |
ObjectType ON ObjectType.ObjectTypeId = VersionBroker.ObjectTypeId JOIN
|
|
|
158 |
Version ON VersionBroker.Identifier = Version.Identifier JOIN News ON
|
|
|
159 |
Version.ObjectId = News.NewsId WHERE Version.Status = \'Approved\' AND
|
|
|
160 |
ObjectType.Name = \'News\' AND Version.ApprovedTS = ( SELECT SubV.ApprovedTS
|
|
|
161 |
FROM Version SubV WHERE SubV.Identifier = VersionBroker.Identifier ) ORDER BY ApprovedTS DESC';
|
|
|
162 |
|
|
|
163 |
$expected = 'SELECT COUNT(*) FROM VersionBroker JOIN
|
|
|
164 |
ObjectType ON ObjectType.ObjectTypeId = VersionBroker.ObjectTypeId JOIN
|
|
|
165 |
Version ON VersionBroker.Identifier = Version.Identifier JOIN News ON
|
|
|
166 |
Version.ObjectId = News.NewsId WHERE Version.Status = \'Approved\' AND
|
|
|
167 |
ObjectType.Name = \'News\' AND Version.ApprovedTS = ( SELECT SubV.ApprovedTS
|
|
|
168 |
FROM Version SubV WHERE SubV.Identifier = VersionBroker.Identifier )';
|
|
|
169 |
$this->assertEqual($expected, rewriteCountQuery($query));
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Test rewriteCountQuery() with queries having keywords embedded in other words
|
|
|
174 |
*/
|
|
|
175 |
function testRewriteCountQuery_EmbeddedKeywords() {
|
|
|
176 |
$query = 'SELECT afieldFROM, b, c, d FROM mytable WHERE a=1 AND c="g"';
|
|
|
177 |
$expected = 'SELECT COUNT(*) FROM mytable WHERE a=1 AND c="g"';
|
|
|
178 |
$this->assertEqual($expected, rewriteCountQuery($query));
|
|
|
179 |
|
|
|
180 |
$query = 'SELECT FROMafield, b, c, d FROM mytable WHERE a=1 AND c="g"';
|
|
|
181 |
$expected = 'SELECT COUNT(*) FROM mytable WHERE a=1 AND c="g"';
|
|
|
182 |
$this->assertEqual($expected, rewriteCountQuery($query));
|
|
|
183 |
|
|
|
184 |
$query = 'SELECT afieldFROMaaa, b, c, d FROM mytable WHERE a=1 AND c="gLIMIT"';
|
|
|
185 |
$expected = 'SELECT COUNT(*) FROM mytable WHERE a=1 AND c="gLIMIT"';
|
|
|
186 |
$this->assertEqual($expected, rewriteCountQuery($query));
|
|
|
187 |
|
|
|
188 |
$query = 'SELECT DISTINCTaaa, b, c, d FROM mytable WHERE a=1 AND c="g"';
|
|
|
189 |
$expected = 'SELECT COUNT(*) FROM mytable WHERE a=1 AND c="g"';
|
|
|
190 |
$this->assertEqual($expected, rewriteCountQuery($query));
|
|
|
191 |
|
|
|
192 |
//this one fails... the regexp should NOT match keywords within quotes.
|
|
|
193 |
//we need a full blown stack-based parser to catch this...
|
|
|
194 |
$query = 'SELECT afieldFROMaaa, b, c, d FROM mytable WHERE a=1 AND c="g LIMIT a"';
|
|
|
195 |
$expected = 'SELECT COUNT(*) FROM mytable WHERE a=1 AND c="g LIMIT a"';
|
|
|
196 |
$this->assertEqual($expected, rewriteCountQuery($query));
|
|
|
197 |
}
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
if (!defined('TEST_RUNNING')) {
|
|
|
201 |
define('TEST_RUNNING', true);
|
|
|
202 |
$test = &new TestOfPagerWrapper();
|
|
|
203 |
$test->run(new HtmlReporter());
|
|
|
204 |
}
|
|
|
205 |
?>
|