7142fc5974c3d3fc2c97a25d72b73383f293f43a
[platal.git] / ut / userfiltertest.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 require_once dirname(__FILE__) . '/../include/test.inc.php';
23
24 class UserFilterTest extends PlTestCase
25 {
26 private static function checkPlatal()
27 {
28 global $platal;
29 if($platal == null)
30 {
31 $platal = new Xorg();
32 }
33 }
34
35 public static function simpleUserProvider()
36 {
37 self::checkPlatal();
38 $tests = array();
39
40 $tests[] = array(
41 /* UFC_Hrpid
42 */
43 array('SELECT DISTINCT uid
44 FROM account_profiles
45 WHERE FIND_IN_SET(\'owner\', perms)',
46 new UFC_HasProfile(), -1),
47
48 /* UFC_Hruid
49 */
50 array(XDB::format('SELECT DISTINCT uid
51 FROM accounts
52 WHERE hruid = {?}', 'florent.bruneau.2003'),
53 new UFC_Hruid('florent.bruneau.2003'), 1),
54 array(XDB::format('SELECT DISTINCT uid
55 FROM accounts
56 WHERE hruid = {?}', 'florent.bruneau.2004'),
57 new UFC_Hruid('florent.bruneau.2004'), 0),
58 array(XDB::format('SELECT DISTINCT uid
59 FROM accounts
60 WHERE hruid IN {?}', array('florent.bruneau.2003',
61 'stephane.jacob.2004')),
62 new UFC_Hruid(array('florent.bruneau.2003', 'stephane.jacob.2004')), 2),
63 array(XDB::format('SELECT DISTINCT uid
64 FROM accounts
65 WHERE hruid IN {?}', array('florent.bruneau.2004',
66 'stephane.jacob.2004')),
67 new UFC_Hruid(array('florent.bruneau.2004', 'stephane.jacob.2004')), 1),
68
69 /* UFC_Hrpid
70 */
71 array(XDB::format('SELECT DISTINCT uid
72 FROM account_profiles AS ap
73 INNER JOIN profiles AS p ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', perms))
74 WHERE hrpid = {?}', 'florent.bruneau.2003'),
75 new UFC_Hrpid('florent.bruneau.2003'), 1),
76 array(XDB::format('SELECT DISTINCT uid
77 FROM account_profiles AS ap
78 INNER JOIN profiles AS p ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', perms))
79 WHERE hrpid = {?}', 'florent.bruneau.2004'),
80 new UFC_Hrpid('florent.bruneau.2004'), 0),
81 array(XDB::format('SELECT DISTINCT uid
82 FROM account_profiles AS ap
83 INNER JOIN profiles AS p ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', perms))
84 WHERE hrpid IN {?}', array('florent.bruneau.2003',
85 'stephane.jacob.2004')),
86 new UFC_Hrpid(array('florent.bruneau.2003', 'stephane.jacob.2004')), 2),
87 array(XDB::format('SELECT DISTINCT uid
88 FROM account_profiles AS ap
89 INNER JOIN profiles AS p ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', perms))
90 WHERE hrpid IN {?}', array('florent.bruneau.2004',
91 'stephane.jacob.2004')),
92 new UFC_Hrpid(array('florent.bruneau.2004', 'stephane.jacob.2004')), 1),
93
94 /* UFC_IP
95 */
96 array(XDB::format('SELECT DISTINCT a.uid
97 FROM log_sessions
98 INNER JOIN accounts AS a USING(uid)
99 WHERE ip = {?} OR forward_ip = {?}',
100 ip_to_uint('129.104.247.2'), ip_to_uint('129.104.247.2')),
101 new UFC_Ip('129.104.247.2'), -1),
102 );
103 /* TODO: UFC_Comment
104 */
105
106 /* UFC_Promo
107 */
108 $tests[] = array(
109 array(XDB::format('SELECT DISTINCT ap.uid
110 FROM account_profiles AS ap
111 INNER JOIN profile_display AS pd ON (pd.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
112 WHERE pd.promo = {?}', 'X2004'),
113 new UFC_Promo('=', UserFilter::DISPLAY, 'X2004'), -1),
114 array(XDB::format('SELECT DISTINCT ap.uid
115 FROM account_profiles AS ap
116 INNER JOIN profile_display AS pd ON (pd.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
117 WHERE pd.promo < {?}', 'X2004'),
118 new UFC_Promo('<', UserFilter::DISPLAY, 'X2004'), -1),
119 array(XDB::format('SELECT DISTINCT ap.uid
120 FROM account_profiles AS ap
121 INNER JOIN profile_display AS pd ON (pd.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
122 WHERE pd.promo > {?}', 'X2004'),
123 new UFC_Promo('>', UserFilter::DISPLAY, 'X2004'), -1),
124 array(XDB::format('SELECT DISTINCT ap.uid
125 FROM account_profiles AS ap
126 INNER JOIN profile_display AS pd ON (pd.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
127 WHERE pd.promo < {?}', 'X1900'),
128 new UFC_Promo('<', UserFilter::DISPLAY, 'X1900'), 0),
129
130 array(XDB::format('SELECT DISTINCT ap.uid
131 FROM account_profiles AS ap
132 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
133 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
134 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
135 WHERE pe.entry_year = {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
136 '2004', 'Ing.'),
137 new UFC_Promo('=', UserFilter::GRADE_ING, 2004), -1),
138 array(XDB::format('SELECT DISTINCT ap.uid
139 FROM account_profiles AS ap
140 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
141 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
142 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
143 WHERE pe.entry_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
144 '1960', 'Ing.'),
145 new UFC_Promo('<=', UserFilter::GRADE_ING, 1960), -1),
146 array(XDB::format('SELECT DISTINCT ap.uid
147 FROM account_profiles AS ap
148 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
149 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
150 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
151 WHERE pe.entry_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
152 '2004', 'Ing.'),
153 new UFC_Promo('>', UserFilter::GRADE_ING, 2004), -1),
154 array(XDB::format('SELECT DISTINCT ap.uid
155 FROM account_profiles AS ap
156 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
157 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
158 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
159 WHERE pe.entry_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
160 '1900', 'Ing.'),
161 new UFC_Promo('<', UserFilter::GRADE_ING, 1900), 0),
162
163 /* XXX : tests disabled until there are Masters and doctors in the DB
164 array(XDB::format('SELECT DISTINCT ap.uid
165 FROM account_profiles AS ap
166 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
167 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
168 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
169 WHERE pe.grad_year = {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
170 '2009', 'MSc'),
171 new UFC_Promo('=', UserFilter::GRADE_MST, 2009), -1),
172 array(XDB::format('SELECT DISTINCT ap.uid
173 FROM account_profiles AS ap
174 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
175 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
176 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
177 WHERE pe.grad_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
178 '2009', 'MSc'),
179 new UFC_Promo('<=', UserFilter::GRADE_MST, 2009), -1),
180 array(XDB::format('SELECT DISTINCT ap.uid
181 FROM account_profiles AS ap
182 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
183 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
184 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
185 WHERE pe.grad_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
186 '2009', 'MSc'),
187 new UFC_Promo('>', UserFilter::GRADE_MST, 2009), -1),
188 array(XDB::format('SELECT DISTINCT ap.uid
189 FROM account_profiles AS ap
190 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
191 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
192 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
193 WHERE pe.grad_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
194 '1980', 'MSc'),
195 new UFC_Promo('<', UserFilter::GRADE_MST, 1980), 0),
196
197 array(XDB::format('SELECT DISTINCT ap.uid
198 FROM account_profiles AS ap
199 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
200 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
201 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
202 WHERE pe.grad_year = {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
203 '2009', 'PhD'),
204 new UFC_Promo('=', UserFilter::GRADE_PHD, 2009), -1),
205 array(XDB::format('SELECT DISTINCT ap.uid
206 FROM account_profiles AS ap
207 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
208 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
209 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
210 WHERE pe.grad_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
211 '2009', 'PhD'),
212 new UFC_Promo('<=', UserFilter::GRADE_PHD, 2009), -1),
213 array(XDB::format('SELECT DISTINCT ap.uid
214 FROM account_profiles AS ap
215 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
216 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
217 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
218 WHERE pe.grad_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
219 '2009', 'PhD'),
220 new UFC_Promo('>', UserFilter::GRADE_PHD, 2009), -1),
221 array(XDB::format('SELECT DISTINCT ap.uid
222 FROM account_profiles AS ap
223 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
224 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
225 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
226 WHERE pe.grad_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
227 '1980', 'PhD'),
228 new UFC_Promo('<', UserFilter::GRADE_PHD, 1980), 0),
229 */
230 );
231
232 /* TODO: UFC_SchoolId
233 */
234 /* UFC_EducationSchool
235 */
236 $id_X = XDB::fetchOneCell('SELECT id
237 FROM profile_education_enum
238 WHERE abbreviation = {?}', 'X');
239 $id_HEC = XDB::fetchOneCell('SELECT id
240 FROM profile_education_enum
241 WHERE abbreviation = {?}', 'HEC');
242 $tests[] = array(
243 array(XDB::format('SELECT DISTINCT ap.uid
244 FROM account_profiles AS ap
245 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
246 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
247 WHERE pee.abbreviation = {?}', 'X'),
248 new UFC_EducationSchool($id_X), -1),
249 array(XDB::format('SELECT DISTINCT ap.uid
250 FROM account_profiles AS ap
251 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
252 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
253 WHERE pee.abbreviation IN {?}', array('X', 'HEC')),
254 new UFC_EducationSchool(array($id_X, $id_HEC)), -1),
255 );
256
257 /* UFC_EducationDegree
258 */
259 $id_DegreeIng = XDB::fetchOneCell('SELECT id
260 FROM profile_education_degree_enum
261 WHERE abbreviation = {?}', 'Ing.');
262 $id_DegreePhd = XDB::fetchOneCell('SELECT id
263 FROM profile_education_degree_enum
264 WHERE abbreviation = {?}', 'PhD');
265 $tests[] = array(
266 array(XDB::format('SELECT DISTINCT ap.uid
267 FROM account_profiles AS ap
268 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
269 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
270 WHERE pede.abbreviation = {?}', 'Ing.'),
271 new UFC_EducationDegree($id_DegreeIng), -1),
272 array(XDB::format('SELECT DISTINCT ap.uid
273 FROM account_profiles AS ap
274 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
275 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
276 WHERE pede.abbreviation IN {?}', array('Ing.', 'PhD')),
277 new UFC_EducationDegree(array($id_DegreeIng, $id_DegreePhd)), -1),
278 );
279 /* UFC_EducationField
280 */
281 $id_FieldInfo = XDB::fetchOneCell('SELECT id
282 FROM profile_education_field_enum
283 WHERE field = {?}', 'Informatique');
284 $id_FieldDroit = XDB::fetchOneCell('SELECT id
285 FROM profile_education_field_enum
286 WHERE field = {?}', 'Droit');
287 // FIXME: Replaces 0 by -1 in following queries when profile_education will be filled with fieldids
288 $tests[] = array(
289 array(XDB::format('SELECT DISTINCT ap.uid
290 FROM account_profiles AS ap
291 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
292 LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id)
293 WHERE pefe.field = {?}', 'Informatique'),
294 new UFC_EducationField($id_FieldInfo), 0), // FIXME: should be -1
295 array(XDB::format('SELECT DISTINCT ap.uid
296 FROM account_profiles AS ap
297 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
298 LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id)
299 WHERE pefe.field IN {?}', array('Informatique', 'Droit')),
300 new UFC_EducationField(array($id_FieldInfo, $id_FieldDroit)), 0), // FIXME: should be -1
301 );
302
303 $testcases = array();
304 foreach ($tests as $t) {
305 $testcases = array_merge($testcases, $t);
306 }
307 return $testcases;
308 }
309
310 /**
311 * @dataProvider simpleUserProvider
312 */
313 public function testSimpleUser($query, $cond, $expcount = null)
314 {
315 /*
316 * @param $query A MySQL query
317 * @param $cond The UFC to test
318 * @param $expcount The expected number of results (-1 if that number is unknown)
319 */
320
321 self::checkPlatal();
322
323 $query = XDB::query($query);
324 $count = $query->numRows();
325 if (!is_null($expcount)) {
326 if ($expcount < 0) {
327 $this->assertNotEquals(0, $count);
328 } else {
329 $this->assertEquals($expcount, $count);
330 }
331 }
332 $ids = $query->fetchColumn();
333 $this->assertEquals($count, count($ids));
334 sort($ids);
335
336 $uf = new UserFilter($cond);
337 $this->assertEquals($count, $uf->getTotalUserCount());
338 $got = $uf->getUIDs();
339 $this->assertEquals($count, count($got));
340 sort($got);
341 $this->assertEquals($ids, $got);
342
343 $uf = new UserFilter($cond);
344 $got = $uf->getUIDs();
345 $this->assertEquals($count, count($got));
346 sort($got);
347 $this->assertEquals($ids, $got);
348 $this->assertEquals($count, $uf->getTotalCount());
349 }
350 }
351
352 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
353 ?>