e953ce52d0d4e9e5625b6019dedad01db68943f6
[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 private static function buildAccountQuery()
36 {
37 $args = func_get_args();
38 $joinsAndWhere = XDB::_prepare($args);
39 return array('SELECT DISTINCT a.uid
40 FROM accounts AS a
41 ' . $joinsAndWhere,
42 'SELECT DISTINCT p.pid
43 FROM profiles AS p
44 INNER JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', perms))
45 INNER JOIN accounts AS a ON (a.uid = ap.uid)
46 ' . $joinsAndWhere);
47 }
48
49 private static function buildProfileQuery()
50 {
51 $args = func_get_args();
52 $joinsAndWhere = XDB::_prepare($args);
53 return array('SELECT DISTINCT a.uid
54 FROM accounts AS a
55 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', perms))
56 INNER JOIN profiles AS p ON (p.pid = ap.pid)
57 ' . $joinsAndWhere,
58 'SELECT DISTINCT p.pid
59 FROM profiles AS p
60 ' . $joinsAndWhere);
61 }
62
63 public static function simpleUserProvider()
64 {
65 self::checkPlatal();
66 $tests = array();
67
68 $tests[] = array(
69 /* UFC_Hrpid
70 */
71 array(self::buildAccountQuery('INNER JOIN account_profiles AS ap2 ON (ap2.uid = a.uid)
72 WHERE FIND_IN_SET(\'owner\', ap2.perms)'),
73 new UFC_HasProfile(), -1),
74
75 /* UFC_Hruid
76 */
77 array(self::buildAccountQuery('WHERE a.hruid = {?}', 'florent.bruneau.2003'),
78 new UFC_Hruid('florent.bruneau.2003'), 1),
79 array(self::buildAccountQuery('WHERE a.hruid = {?}', 'florent.bruneau.2004'),
80 new UFC_Hruid('florent.bruneau.2004'), 0),
81 array(self::buildAccountQuery('WHERE a.hruid IN {?}', array('florent.bruneau.2003',
82 'stephane.jacob.2004')),
83 new UFC_Hruid('florent.bruneau.2003', 'stephane.jacob.2004'), 2),
84 array(self::buildAccountQuery('WHERE a.hruid IN {?}', array('florent.bruneau.2004',
85 'stephane.jacob.2004')),
86 new UFC_Hruid('florent.bruneau.2004', 'stephane.jacob.2004'), 1),
87
88 /* UFC_Hrpid
89 */
90 array(self::buildProfileQuery('WHERE p.hrpid = {?}', 'florent.bruneau.2003'),
91 new UFC_Hrpid('florent.bruneau.2003'), 1),
92 array(self::buildProfileQuery('WHERE p.hrpid = {?}', 'florent.bruneau.2004'),
93 new UFC_Hrpid('florent.bruneau.2004'), 0),
94 array(self::buildProfileQuery('WHERE p.hrpid IN {?}', array('florent.bruneau.2003',
95 'stephane.jacob.2004')),
96 new UFC_Hrpid('florent.bruneau.2003', 'stephane.jacob.2004'), 2),
97 array(self::buildProfileQuery('WHERE p.hrpid IN {?}', array('florent.bruneau.2004',
98 'stephane.jacob.2004')),
99 new UFC_Hrpid('florent.bruneau.2004', 'stephane.jacob.2004'), 1),
100
101 /* UFC_IP
102 */
103 array(self::buildAccountQuery('INNER JOIN log_sessions AS s ON (s.uid = a.uid)
104 WHERE s.ip = {?} OR s.forward_ip = {?}',
105 ip_to_uint('129.104.247.2'), ip_to_uint('129.104.247.2')),
106 new UFC_Ip('129.104.247.2'), -1),
107 );
108 /* TODO: UFC_Comment
109 */
110
111 /* UFC_Promo
112 */
113 $tests[] = array(
114 array(self::buildProfileQuery('INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
115 WHERE pd.promo = {?}', 'X2004'),
116 new UFC_Promo('=', UserFilter::DISPLAY, 'X2004'), -1),
117 array(self::buildProfileQuery('INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
118 WHERE pd.promo < {?}', 'X2004'),
119 new UFC_Promo('<', UserFilter::DISPLAY, 'X2004'), -1),
120 array(self::buildProfileQuery('INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
121 WHERE pd.promo > {?}', 'X2004'),
122 new UFC_Promo('>', UserFilter::DISPLAY, 'X2004'), -1),
123 array(self::buildProfileQuery('INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
124 WHERE pd.promo < {?}', 'X1900'),
125 new UFC_Promo('<', UserFilter::DISPLAY, 'X1900'), 0),
126
127 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
128 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
129 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
130 WHERE pe.entry_year = {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
131 '2004', 'Ing.'),
132 new UFC_Promo('=', UserFilter::GRADE_ING, 2004), -1),
133 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
134 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
135 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
136 WHERE pe.entry_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
137 '1960', 'Ing.'),
138 new UFC_Promo('<=', UserFilter::GRADE_ING, 1960), -1),
139 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
140 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
141 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
142 WHERE pe.entry_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
143 '2004', 'Ing.'),
144 new UFC_Promo('>', UserFilter::GRADE_ING, 2004), -1),
145 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
146 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
147 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
148 WHERE pe.entry_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
149 '1900', 'Ing.'),
150 new UFC_Promo('<', UserFilter::GRADE_ING, 1900), 0),
151
152 /* XXX : tests disabled until there are Masters and doctors in the DB
153 array(XDB::format('SELECT DISTINCT ap.uid
154 FROM account_profiles AS ap
155 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
156 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
157 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
158 WHERE pe.grad_year = {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
159 '2009', 'MSc'),
160 new UFC_Promo('=', UserFilter::GRADE_MST, 2009), -1),
161 array(XDB::format('SELECT DISTINCT ap.uid
162 FROM account_profiles AS ap
163 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
164 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
165 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
166 WHERE pe.grad_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
167 '2009', 'MSc'),
168 new UFC_Promo('<=', UserFilter::GRADE_MST, 2009), -1),
169 array(XDB::format('SELECT DISTINCT ap.uid
170 FROM account_profiles AS ap
171 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
172 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
173 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
174 WHERE pe.grad_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
175 '2009', 'MSc'),
176 new UFC_Promo('>', UserFilter::GRADE_MST, 2009), -1),
177 array(XDB::format('SELECT DISTINCT ap.uid
178 FROM account_profiles AS ap
179 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
180 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
181 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
182 WHERE pe.grad_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
183 '1980', 'MSc'),
184 new UFC_Promo('<', UserFilter::GRADE_MST, 1980), 0),
185
186 array(XDB::format('SELECT DISTINCT ap.uid
187 FROM account_profiles AS ap
188 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
189 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
190 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
191 WHERE pe.grad_year = {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
192 '2009', 'PhD'),
193 new UFC_Promo('=', UserFilter::GRADE_PHD, 2009), -1),
194 array(XDB::format('SELECT DISTINCT ap.uid
195 FROM account_profiles AS ap
196 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
197 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
198 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
199 WHERE pe.grad_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
200 '2009', 'PhD'),
201 new UFC_Promo('<=', UserFilter::GRADE_PHD, 2009), -1),
202 array(XDB::format('SELECT DISTINCT ap.uid
203 FROM account_profiles AS ap
204 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
205 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
206 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
207 WHERE pe.grad_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
208 '2009', 'PhD'),
209 new UFC_Promo('>', UserFilter::GRADE_PHD, 2009), -1),
210 array(XDB::format('SELECT DISTINCT ap.uid
211 FROM account_profiles AS ap
212 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
213 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
214 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
215 WHERE pe.grad_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
216 '1980', 'PhD'),
217 new UFC_Promo('<', UserFilter::GRADE_PHD, 1980), 0),
218 */
219 );
220
221 /* UFC_SchoolId
222 */
223 $tests[] = array(
224 array(self::buildProfileQuery('WHERE p.xorg_id = {?}', 20060076),
225 new UFC_SchoolId(UFC_SchoolId::Xorg, 20060076), 1),
226 array(self::buildProfileQuery('WHERE p.ax_id = {?}', 20060062),
227 new UFC_SchoolId(UFC_SchoolId::AX, 20060062), 1),
228 array(self::buildProfileQuery('WHERE p.xorg_id = {?}', 007),
229 new UFC_SchoolId(UFC_SchoolId::Xorg, 007), 0),
230 array(self::buildProfileQuery('WHERE p.ax_id = {?}', 007),
231 new UFC_SchoolId(UFC_SchoolId::AX, 007), 0),
232 /* FIXME: disabled until we have some examples of school_id
233 array(self::buildProfileQuery('WHERE p.school_id = {?}', 12345678),
234 new UFC_SchoolId(UFC_SchoolId::School, 12345678), 1),
235 array(self::buildProfileQuery('WHERE p.school_id = {?}', 007),
236 new UFC_SchoolId(UFC_SchoolId::School, 007), 0),
237 */
238 );
239 /* UFC_EducationSchool
240 */
241 $id_X = XDB::fetchOneCell('SELECT id
242 FROM profile_education_enum
243 WHERE abbreviation = {?}', 'X');
244 $id_HEC = XDB::fetchOneCell('SELECT id
245 FROM profile_education_enum
246 WHERE abbreviation = {?}', 'HEC');
247 $tests[] = array(
248 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
249 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
250 WHERE pee.abbreviation = {?}', 'X'),
251 new UFC_EducationSchool($id_X), -1),
252 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
253 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
254 WHERE pee.abbreviation IN {?}', array('X', 'HEC')),
255 new UFC_EducationSchool($id_X, $id_HEC), -1),
256 );
257
258 /* UFC_EducationDegree
259 */
260 $id_DegreeIng = XDB::fetchOneCell('SELECT id
261 FROM profile_education_degree_enum
262 WHERE abbreviation = {?}', 'Ing.');
263 $id_DegreePhd = XDB::fetchOneCell('SELECT id
264 FROM profile_education_degree_enum
265 WHERE abbreviation = {?}', 'PhD');
266 $tests[] = array(
267 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
268 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
269 WHERE pede.abbreviation = {?}', 'Ing.'),
270 new UFC_EducationDegree($id_DegreeIng), -1),
271 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
272 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
273 WHERE pede.abbreviation IN {?}', array('Ing.', 'PhD')),
274 new UFC_EducationDegree($id_DegreeIng, $id_DegreePhd), -1),
275 );
276 /* UFC_EducationField
277 */
278 $id_FieldInfo = XDB::fetchOneCell('SELECT id
279 FROM profile_education_field_enum
280 WHERE field = {?}', 'Informatique');
281 $id_FieldDroit = XDB::fetchOneCell('SELECT id
282 FROM profile_education_field_enum
283 WHERE field = {?}', 'Droit');
284 // FIXME: Replace 0 by -1 in following queries when profile_education will be filled with fieldids
285 $tests[] = array(
286 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
287 LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id)
288 WHERE pefe.field = {?}', 'Informatique'),
289 new UFC_EducationField($id_FieldInfo), 0), // FIXME: should be -1
290 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
291 LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id)
292 WHERE pefe.field IN {?}', array('Informatique', 'Droit')),
293 new UFC_EducationField($id_FieldInfo, $id_FieldDroit), 0), // FIXME: should be -1
294 );
295
296 /* UFC_Name
297 */
298 $id_Lastname = DirEnum::getID(DirEnum::NAMETYPES, Profile::LASTNAME);
299 $id_Firstname = DirEnum::getID(DirEnum::NAMETYPES, Profile::FIRSTNAME);
300 $id_Nickname = DirEnum::getID(DirEnum::NAMETYPES, Profile::NICKNAME);
301 $id_Lastname_Marital = DirEnum::getID(DirEnum::NAMETYPES, Profile::LASTNAME . '_' . Profile::VN_MARITAL);
302 $id_Lastname_Ordinary = DirEnum::getID(DirEnum::NAMETYPES, Profile::LASTNAME . '_' . Profile::VN_ORDINARY);
303
304 $tests[] = array(
305 // Lastname
306 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (pn.pid = p.pid)
307 WHERE pn.name LIKE {?} AND pn.typeid = {?}', 'BARROIS', $id_Lastname),
308 new UFC_Name(Profile::LASTNAME, 'BARROIS', UFC_Name::EXACT), -1),
309 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (pn.pid = p.pid)
310 WHERE pn.name LIKE \'BARR%\' AND pn.typeid = {?}', $id_Lastname),
311 new UFC_Name(Profile::LASTNAME, 'BARR', UFC_Name::PREFIX), -1),
312 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (pn.pid = p.pid)
313 WHERE pn.name LIKE \'%OIS\' AND pn.typeid = {?}', $id_Lastname),
314 new UFC_Name(Profile::LASTNAME, 'OIS', UFC_Name::SUFFIX), -1),
315 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
316 WHERE pn.name LIKE \'%ARRO%\' AND pn.typeid = {?}', $id_Lastname),
317 new UFC_Name(Profile::LASTNAME, 'ARRO', UFC_Name::CONTAINS), -1),
318 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
319 WHERE pn.name LIKE \'%ZZZZZZ%\' AND pn.typeid = {?}', $id_Lastname),
320 new UFC_Name(Profile::LASTNAME, 'ZZZZZZ', UFC_Name::CONTAINS), 0),
321
322 // Firstname
323 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
324 WHERE pn.name LIKE {?} AND pn.typeid = {?}', 'Raphaël', $id_Firstname),
325 new UFC_Name(Profile::FIRSTNAME, 'Raphaël', UFC_Name::EXACT), -1),
326 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
327 WHERE pn.name LIKE \'Raph%\' AND pn.typeid = {?}', $id_Firstname),
328 new UFC_Name(Profile::FIRSTNAME, 'Raph', UFC_Name::PREFIX), -1),
329 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
330 WHERE pn.name LIKE \'%aël\' AND pn.typeid = {?}', $id_Firstname),
331 new UFC_Name(Profile::FIRSTNAME, 'aël', UFC_Name::SUFFIX), -1),
332 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
333 WHERE pn.name LIKE \'%apha%\' AND pn.typeid = {?}', $id_Firstname),
334 new UFC_Name(Profile::FIRSTNAME, 'apha', UFC_Name::CONTAINS), -1),
335 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
336 WHERE pn.name LIKE \'%zzzzzz%\' AND pn.typeid = {?}', $id_Firstname),
337 new UFC_Name(Profile::FIRSTNAME, 'zzzzzz', UFC_Name::CONTAINS), 0),
338
339 // Nickname
340 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
341 WHERE pn.name LIKE {?} AND pn.typeid = {?}', 'Xelnor', $id_Nickname),
342 new UFC_Name(Profile::NICKNAME, 'Xelnor', UFC_Name::EXACT), -1),
343 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
344 WHERE pn.name LIKE \'Xel%\' AND pn.typeid = {?}', $id_Nickname),
345 new UFC_Name(Profile::NICKNAME, 'Xel', UFC_Name::PREFIX), -1),
346 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
347 WHERE pn.name LIKE \'%nor\' AND pn.typeid = {?}', $id_Nickname),
348 new UFC_Name(Profile::NICKNAME, 'nor', UFC_Name::SUFFIX), -1),
349 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
350 WHERE pn.name LIKE \'%lno%\' AND pn.typeid = {?}', $id_Nickname),
351 new UFC_Name(Profile::NICKNAME, 'lno', UFC_Name::CONTAINS), -1),
352 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
353 WHERE pn.name LIKE \'%zzzzzz%\' AND pn.typeid = {?}', $id_Nickname),
354 new UFC_Name(Profile::NICKNAME, 'zzzzzz', UFC_Name::CONTAINS), 0),
355
356 // Lastname + particle
357 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
358 WHERE CONCAT(pn.particle, \' \', pn.name) LIKE {?} AND pn.typeid = {?}', 'DE SINGLY', $id_Lastname),
359 new UFC_Name(Profile::LASTNAME, 'DE SINGLY', UFC_Name::PARTICLE | UFC_Name::EXACT), -1),
360 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
361 WHERE CONCAT(pn.particle, \' \', pn.name) LIKE \'DE SI%\' AND pn.typeid = {?}', $id_Lastname),
362 new UFC_Name(Profile::LASTNAME, 'DE SI', UFC_Name::PARTICLE | UFC_Name::PREFIX), -1),
363 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
364 WHERE CONCAT(pn.particle, \' \', pn.name) LIKE \'%GLY\' AND pn.typeid = {?}', $id_Lastname),
365 new UFC_Name(Profile::LASTNAME, 'GLY', UFC_NAME::PARTICLE | UFC_Name::SUFFIX), -1),
366 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
367 WHERE CONCAT(pn.particle, \' \', pn.name) LIKE \'%E SIN%\' AND pn.typeid = {?}', $id_Lastname),
368 new UFC_Name(Profile::LASTNAME, 'E SIN', UFC_Name::PARTICLE | UFC_Name::CONTAINS), -1),
369 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
370 WHERE CONCAT(pn.particle, \' \', pn.name) LIKE \'%ZZZZZZ%\' AND pn.typeid = {?}', $id_Lastname),
371 new UFC_Name(Profile::LASTNAME, 'ZZZZZZ', UFC_Name::PARTICLE | UFC_Name::CONTAINS), 0),
372
373 // Lastname_ordinary
374 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
375 WHERE pn.name LIKE {?} AND pn.typeid IN {?}',
376 'ALBIZZATI', array($id_Lastname, $id_Lastname_Marital, $id_Lastname_Ordinary)),
377 new UFC_Name(Profile::LASTNAME, 'ALBIZZATI', UFC_Name::VARIANTS | UFC_Name::EXACT), -1),
378 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
379 WHERE pn.name LIKE {?} AND pn.typeid IN {?}',
380 'ALBIZ%', array($id_Lastname, $id_Lastname_Marital, $id_Lastname_Ordinary)),
381 new UFC_Name(Profile::LASTNAME, 'ALBIZ', UFC_Name::VARIANTS | UFC_Name::PREFIX), -1),
382 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
383 WHERE pn.name LIKE {?} AND pn.typeid IN {?}',
384 '%ZATI', array($id_Lastname, $id_Lastname_Marital, $id_Lastname_Ordinary)),
385 new UFC_Name(Profile::LASTNAME, 'ZATI', UFC_NAME::VARIANTS | UFC_Name::SUFFIX), -1),
386 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
387 WHERE pn.name LIKE {?} AND pn.typeid IN {?}',
388 '%BIZZ%', array($id_Lastname, $id_Lastname_Marital, $id_Lastname_Ordinary)),
389 new UFC_Name(Profile::LASTNAME, 'BIZZ', UFC_Name::VARIANTS | UFC_Name::CONTAINS), -1),
390 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
391 WHERE pn.name LIKE {?} AND pn.typeid IN {?}',
392 '%ZZZZZZ%', array($id_Lastname, $id_Lastname_Marital, $id_Lastname_Ordinary)),
393 new UFC_Name(Profile::LASTNAME, 'ZZZZZZ', UFC_Name::VARIANTS | UFC_Name::CONTAINS), 0),
394 );
395
396 /* UFC_NameTokens
397 */
398 $tests[] = array(
399 // !soundex, !exact
400 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
401 WHERE sn.token LIKE \'xelnor%\''),
402 new UFC_NameTokens('xelnor'), 1),
403 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
404 WHERE sn.token LIKE \'xe%\''),
405 new UFC_NameTokens('xe'), -1),
406 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
407 WHERE sn.token LIKE \'xe%\' OR sn.token LIKE \'barr%\''),
408 new UFC_NameTokens(array('xe', 'barr')), -1),
409 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
410 WHERE sn.token LIKE \'zzzzzzzz%\''),
411 new UFC_NameTokens('zzzzzzzz'), 0),
412 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
413 WHERE sn.token LIKE \'barr%\' AND FIND_IN_SET(\'public\', sn.flags)'),
414 new UFC_NameTokens('barr', UFC_NameTokens::FLAG_PUBLIC), -1),
415
416 // !soundex, exact
417 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
418 WHERE sn.token = \'xelnor\''),
419 new UFC_NameTokens('xelnor', array(), false, true), 1),
420 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
421 WHERE sn.token IN (\'xelnor\', \'barrois\')'),
422 new UFC_NameTokens(array('xelnor', 'barrois'), array(), false, true), -1),
423 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
424 WHERE sn.token = \'zzzzzzzz\''),
425 new UFC_NameTokens('zzzzzzzz', array(), false, true), 0),
426 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
427 WHERE sn.token IN (\'zzzzzzzz\', \'yyyyyyyy\')'),
428 new UFC_NameTokens(array('zzzzzzzz', 'yyyyyyyy'), array(), false, true), 0),
429 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
430 WHERE sn.token = \'barrois\' AND FIND_IN_SET(\'public\', sn.flags)'),
431 new UFC_NameTokens('barrois', UFC_NameTokens::FLAG_PUBLIC, false, true), -1),
432
433 // soundex, !exact
434 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
435 WHERE sn.soundex = \'XLNO\''),
436 new UFC_NameTokens('XLNO', array(), true), -1),
437 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
438 WHERE sn.soundex IN (\'XLNO\', \'BROS\')'),
439 new UFC_NameTokens(array('XLNO', 'BROS'), array(), true), -1),
440 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
441 WHERE sn.soundex = \'ZZZZZZ\''),
442 new UFC_NameTokens('ZZZZZZ', array(), true), 0),
443 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
444 WHERE sn.soundex = \'BROS\' AND FIND_IN_SET(\'public\', sn.flags)'),
445 new UFC_NameTokens('BROS', UFC_NameTokens::FLAG_PUBLIC, true), -1),
446 );
447
448 /* UFC_Nationality
449 */
450 $tests[] = array(
451 array(self::buildProfileQuery('WHERE p.nationality1 IN {?} OR p.nationality2 IN {?} OR p.nationality3 IN {?}', array('BR'), array('BR'), array('BR')),
452 new UFC_Nationality('BR'), -1),
453 array(self::buildProfileQuery('WHERE p.nationality1 IN {?} OR p.nationality2 IN {?} OR p.nationality3 IN {?}', array('BR', 'US'), array('BR', 'US'), array('BR', 'US')),
454 new UFC_Nationality('BR', 'US'), -1),
455 array(self::buildProfileQuery('WHERE p.nationality1 IN {?} OR p.nationality2 IN {?} OR p.nationality3 IN {?}', array('__'), array('__'), array('__')),
456 new UFC_Nationality('__'), 0),
457 );
458
459 /* UFC_Dead
460 */
461 $tests[] = array(
462 array(self::buildProfileQuery('WHERE p.deathdate IS NOT NULL'),
463 new UFC_Dead(), -1),
464 array(self::buildProfileQuery('WHERE p.deathdate IS NOT NULL AND p.deathdate > {?}', '2008-01-01'),
465 new UFC_Dead('>', '2008-01-01'), -1),
466 array(self::buildProfileQuery('WHERE p.deathdate IS NOT NULL AND p.deathdate < {?}', '1600-01-01'),
467 new UFC_Dead('<', '1600-01-01'), 0),
468 array(self::buildProfileQuery('WHERE p.deathdate IS NOT NULL AND p.deathdate > {?}', date('Y-m-d')),
469 new UFC_Dead('>', 'now'), 0),
470 );
471
472 /* UFC_Registered
473 */
474 $tests[] = array(
475 array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state = \'active\''),
476 new UFC_Registered(true), -1),
477 array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state != \'pending\''),
478 new UFC_Registered(), -1),
479 array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state = \'active\' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date > {?}', '2008-01-01'),
480 new UFC_Registered(true, '>', '2008-01-01'), -1),
481 array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state != \'pending\' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date > {?}', '2008-01-01'),
482 new UFC_Registered(false, '>', '2008-01-01'), -1),
483 array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state = \'active\' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date < {?}', '1700-01-01'),
484 new UFC_Registered(true, '<', '1700-01-01'), 0),
485 array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state != \'pending\' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date < {?}', '1700-01-01'),
486 new UFC_Registered(false, '<', '1700-01-01'), 0),
487 array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state = \'active\' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date > {?}', date('Y-m-d')),
488 new UFC_Registered(true, '>', 'now'), 0),
489 array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state != \'pending\' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date > {?}', date('Y-m-d')),
490 new UFC_Registered(false, '>', 'now'), 0),
491 );
492
493 $testcases = array();
494 foreach ($tests as $t) {
495 $testcases = array_merge($testcases, $t);
496 }
497 return $testcases;
498 }
499
500 /**
501 * @dataProvider simpleUserProvider
502 */
503 public function testSimpleUser($query, $cond, $expcount = null)
504 {
505 /*
506 * @param $query A pair MySQL query (one for user selector, one for profile selector)
507 * @param $cond The UFC to test
508 * @param $expcount The expected number of results (-1 if that number is unknown)
509 */
510
511 $query = $query[0];
512
513 self::checkPlatal();
514
515 $query = XDB::query($query);
516 $count = $query->numRows();
517 if (!is_null($expcount)) {
518 if ($expcount < 0) {
519 $this->assertNotEquals(0, $count);
520 } else {
521 $this->assertEquals($expcount, $count);
522 }
523 }
524 $ids = $query->fetchColumn();
525 $this->assertEquals($count, count($ids));
526 sort($ids);
527
528 $uf = new UserFilter($cond);
529 $this->assertEquals($count, $uf->getTotalUserCount());
530 $got = $uf->getUIDs();
531 $this->assertEquals($count, count($got));
532 sort($got);
533 $this->assertEquals($ids, $got);
534
535 $uf = new UserFilter($cond);
536 $got = $uf->getUIDs();
537 $this->assertEquals($count, count($got));
538 sort($got);
539 $this->assertEquals($ids, $got);
540 $this->assertEquals($count, $uf->getTotalUserCount());
541 }
542
543 /**
544 * @dataProvider simpleUserProvider
545 */
546 public function testSimpleProfile($query, $cond, $expcount = null)
547 {
548 /*
549 * @param $query A pair MySQL query (one for user selector, one for profile selector)
550 * @param $cond The UFC to test
551 * @param $expcount The expected number of results (-1 if that number is unknown)
552 */
553
554 $query = $query[1];
555
556 self::checkPlatal();
557
558 $query = XDB::query($query);
559 $count = $query->numRows();
560 if (!is_null($expcount)) {
561 if ($expcount < 0) {
562 $this->assertNotEquals(0, $count);
563 } else {
564 $this->assertEquals($expcount, $count);
565 }
566 }
567 $ids = $query->fetchColumn();
568 $this->assertEquals($count, count($ids));
569 sort($ids);
570
571 $uf = new UserFilter($cond);
572 $this->assertEquals($count, $uf->getTotalProfileCount());
573 $got = $uf->getPIDs();
574 $this->assertEquals($count, count($got));
575 sort($got);
576 $this->assertEquals($ids, $got);
577
578 $uf = new UserFilter($cond);
579 $got = $uf->getPIDs();
580 $this->assertEquals($count, count($got));
581 sort($got);
582 $this->assertEquals($ids, $got);
583 $this->assertEquals($count, $uf->getTotalProfileCount());
584 }
585
586 }
587
588 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
589 ?>