Moving to GitHub.
[platal.git] / ut / userfiltertest.php
CommitLineData
aaf70eb8
FB
1<?php
2/***************************************************************************
c441aabe 3 * Copyright (C) 2003-2014 Polytechnique.org *
aaf70eb8
FB
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
22require_once dirname(__FILE__) . '/../include/test.inc.php';
23
24class UserFilterTest extends PlTestCase
25{
d836f8db
RB
26 private static function checkPlatal()
27 {
28 global $platal;
29 if($platal == null)
30 {
31 $platal = new Xorg();
32 }
33 }
34
5b3f6e44
FB
35 private static function buildAccountQuery()
36 {
37 $args = func_get_args();
d3f023dd 38 $joinsAndWhere = XDB::prepare($args);
5b3f6e44
FB
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();
d3f023dd 52 $joinsAndWhere = XDB::prepare($args);
5b3f6e44
FB
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
aaf70eb8
FB
63 public static function simpleUserProvider()
64 {
d836f8db 65 self::checkPlatal();
ed4d85c3
RB
66 $tests = array();
67
deb26184 68 $tests['id'] = array(
aaf70eb8
FB
69 /* UFC_Hrpid
70 */
5b3f6e44
FB
71 array(self::buildAccountQuery('INNER JOIN account_profiles AS ap2 ON (ap2.uid = a.uid)
72 WHERE FIND_IN_SET(\'owner\', ap2.perms)'),
aaf70eb8
FB
73 new UFC_HasProfile(), -1),
74
75 /* UFC_Hruid
76 */
5b3f6e44 77 array(self::buildAccountQuery('WHERE a.hruid = {?}', 'florent.bruneau.2003'),
aaf70eb8 78 new UFC_Hruid('florent.bruneau.2003'), 1),
5b3f6e44 79 array(self::buildAccountQuery('WHERE a.hruid = {?}', 'florent.bruneau.2004'),
aaf70eb8 80 new UFC_Hruid('florent.bruneau.2004'), 0),
5b3f6e44
FB
81 array(self::buildAccountQuery('WHERE a.hruid IN {?}', array('florent.bruneau.2003',
82 'stephane.jacob.2004')),
61d1fd8b 83 new UFC_Hruid('florent.bruneau.2003', 'stephane.jacob.2004'), 2),
5b3f6e44
FB
84 array(self::buildAccountQuery('WHERE a.hruid IN {?}', array('florent.bruneau.2004',
85 'stephane.jacob.2004')),
61d1fd8b 86 new UFC_Hruid('florent.bruneau.2004', 'stephane.jacob.2004'), 1),
aaf70eb8
FB
87
88 /* UFC_Hrpid
89 */
5b3f6e44 90 array(self::buildProfileQuery('WHERE p.hrpid = {?}', 'florent.bruneau.2003'),
aaf70eb8 91 new UFC_Hrpid('florent.bruneau.2003'), 1),
5b3f6e44 92 array(self::buildProfileQuery('WHERE p.hrpid = {?}', 'florent.bruneau.2004'),
aaf70eb8 93 new UFC_Hrpid('florent.bruneau.2004'), 0),
5b3f6e44
FB
94 array(self::buildProfileQuery('WHERE p.hrpid IN {?}', array('florent.bruneau.2003',
95 'stephane.jacob.2004')),
61d1fd8b 96 new UFC_Hrpid('florent.bruneau.2003', 'stephane.jacob.2004'), 2),
5b3f6e44
FB
97 array(self::buildProfileQuery('WHERE p.hrpid IN {?}', array('florent.bruneau.2004',
98 'stephane.jacob.2004')),
61d1fd8b 99 new UFC_Hrpid('florent.bruneau.2004', 'stephane.jacob.2004'), 1),
aaf70eb8
FB
100
101 /* UFC_IP
102 */
5b3f6e44
FB
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')),
aaf70eb8 106 new UFC_Ip('129.104.247.2'), -1),
ed4d85c3 107 );
3867f628
RB
108 /* TODO: UFC_Comment
109 */
ed4d85c3 110
3867f628
RB
111 /* UFC_Promo
112 */
deb26184 113 $tests['promo'] = array(
5b3f6e44
FB
114 array(self::buildProfileQuery('INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
115 WHERE pd.promo = {?}', 'X2004'),
3867f628 116 new UFC_Promo('=', UserFilter::DISPLAY, 'X2004'), -1),
3867f628 117
5b3f6e44
FB
118 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
119 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
120 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
121 WHERE pe.entry_year = {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
122 '2004', 'Ing.'),
3867f628 123 new UFC_Promo('=', UserFilter::GRADE_ING, 2004), -1),
5b3f6e44
FB
124 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
125 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
126 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
127 WHERE pe.entry_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
128 '1960', 'Ing.'),
3867f628 129 new UFC_Promo('<=', UserFilter::GRADE_ING, 1960), -1),
5b3f6e44
FB
130 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
131 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
132 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
133 WHERE pe.entry_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
134 '2004', 'Ing.'),
3867f628 135 new UFC_Promo('>', UserFilter::GRADE_ING, 2004), -1),
5b3f6e44
FB
136 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
137 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
138 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
139 WHERE pe.entry_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
140 '1900', 'Ing.'),
3867f628
RB
141 new UFC_Promo('<', UserFilter::GRADE_ING, 1900), 0),
142
143 /* XXX : tests disabled until there are Masters and doctors in the DB
144 array(XDB::format('SELECT DISTINCT ap.uid
145 FROM account_profiles AS ap
146 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
147 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
148 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
149 WHERE pe.grad_year = {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
150 '2009', 'MSc'),
151 new UFC_Promo('=', UserFilter::GRADE_MST, 2009), -1),
152 array(XDB::format('SELECT DISTINCT ap.uid
153 FROM account_profiles AS ap
154 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
155 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
156 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
157 WHERE pe.grad_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
158 '2009', 'MSc'),
159 new UFC_Promo('<=', UserFilter::GRADE_MST, 2009), -1),
160 array(XDB::format('SELECT DISTINCT ap.uid
161 FROM account_profiles AS ap
162 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
163 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
164 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
165 WHERE pe.grad_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
166 '2009', 'MSc'),
167 new UFC_Promo('>', UserFilter::GRADE_MST, 2009), -1),
168 array(XDB::format('SELECT DISTINCT ap.uid
169 FROM account_profiles AS ap
170 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
171 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
172 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
173 WHERE pe.grad_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
174 '1980', 'MSc'),
175 new UFC_Promo('<', UserFilter::GRADE_MST, 1980), 0),
176
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 '2009', 'PhD'),
184 new UFC_Promo('=', UserFilter::GRADE_PHD, 2009), -1),
185 array(XDB::format('SELECT DISTINCT ap.uid
186 FROM account_profiles AS ap
187 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
188 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
189 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
190 WHERE pe.grad_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
191 '2009', 'PhD'),
192 new UFC_Promo('<=', UserFilter::GRADE_PHD, 2009), -1),
193 array(XDB::format('SELECT DISTINCT ap.uid
194 FROM account_profiles AS ap
195 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
196 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
197 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
198 WHERE pe.grad_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
199 '2009', 'PhD'),
200 new UFC_Promo('>', UserFilter::GRADE_PHD, 2009), -1),
201 array(XDB::format('SELECT DISTINCT ap.uid
202 FROM account_profiles AS ap
203 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
204 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
205 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
206 WHERE pe.grad_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
207 '1980', 'PhD'),
208 new UFC_Promo('<', UserFilter::GRADE_PHD, 1980), 0),
ed4d85c3
RB
209 */
210 );
211
f5aea068 212 /* UFC_SchoolId
d836f8db 213 */
deb26184 214 $tests['schoolid'] = array(
f5aea068
RB
215 array(self::buildProfileQuery('WHERE p.xorg_id = {?}', 20060076),
216 new UFC_SchoolId(UFC_SchoolId::Xorg, 20060076), 1),
217 array(self::buildProfileQuery('WHERE p.ax_id = {?}', 20060062),
218 new UFC_SchoolId(UFC_SchoolId::AX, 20060062), 1),
219 array(self::buildProfileQuery('WHERE p.xorg_id = {?}', 007),
220 new UFC_SchoolId(UFC_SchoolId::Xorg, 007), 0),
221 array(self::buildProfileQuery('WHERE p.ax_id = {?}', 007),
222 new UFC_SchoolId(UFC_SchoolId::AX, 007), 0),
223 /* FIXME: disabled until we have some examples of school_id
224 array(self::buildProfileQuery('WHERE p.school_id = {?}', 12345678),
225 new UFC_SchoolId(UFC_SchoolId::School, 12345678), 1),
226 array(self::buildProfileQuery('WHERE p.school_id = {?}', 007),
227 new UFC_SchoolId(UFC_SchoolId::School, 007), 0),
228 */
229 );
d836f8db
RB
230 /* UFC_EducationSchool
231 */
ed4d85c3
RB
232 $id_X = XDB::fetchOneCell('SELECT id
233 FROM profile_education_enum
234 WHERE abbreviation = {?}', 'X');
235 $id_HEC = XDB::fetchOneCell('SELECT id
236 FROM profile_education_enum
237 WHERE abbreviation = {?}', 'HEC');
deb26184 238 $tests['school'] = array(
5b3f6e44
FB
239 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
240 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
241 WHERE pee.abbreviation = {?}', 'X'),
ed4d85c3 242 new UFC_EducationSchool($id_X), -1),
5b3f6e44
FB
243 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
244 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
245 WHERE pee.abbreviation IN {?}', array('X', 'HEC')),
61d1fd8b 246 new UFC_EducationSchool($id_X, $id_HEC), -1),
aaf70eb8 247 );
ed4d85c3 248
4894915d
RB
249 /* UFC_EducationDegree
250 */
251 $id_DegreeIng = XDB::fetchOneCell('SELECT id
252 FROM profile_education_degree_enum
253 WHERE abbreviation = {?}', 'Ing.');
254 $id_DegreePhd = XDB::fetchOneCell('SELECT id
255 FROM profile_education_degree_enum
256 WHERE abbreviation = {?}', 'PhD');
deb26184 257 $tests['degree'] = array(
5b3f6e44
FB
258 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
259 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
260 WHERE pede.abbreviation = {?}', 'Ing.'),
4894915d 261 new UFC_EducationDegree($id_DegreeIng), -1),
5b3f6e44
FB
262 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
263 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
264 WHERE pede.abbreviation IN {?}', array('Ing.', 'PhD')),
61d1fd8b 265 new UFC_EducationDegree($id_DegreeIng, $id_DegreePhd), -1),
4894915d 266 );
01df65b5
RB
267 /* UFC_EducationField
268 */
269 $id_FieldInfo = XDB::fetchOneCell('SELECT id
270 FROM profile_education_field_enum
271 WHERE field = {?}', 'Informatique');
272 $id_FieldDroit = XDB::fetchOneCell('SELECT id
273 FROM profile_education_field_enum
274 WHERE field = {?}', 'Droit');
b5a9f12a 275 // FIXME: Replace 0 by -1 in following queries when profile_education will be filled with fieldids
deb26184 276 $tests['edufield'] = array(
5b3f6e44
FB
277 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
278 LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id)
279 WHERE pefe.field = {?}', 'Informatique'),
01df65b5 280 new UFC_EducationField($id_FieldInfo), 0), // FIXME: should be -1
5b3f6e44
FB
281 array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid)
282 LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id)
283 WHERE pefe.field IN {?}', array('Informatique', 'Droit')),
61d1fd8b 284 new UFC_EducationField($id_FieldInfo, $id_FieldDroit), 0), // FIXME: should be -1
01df65b5 285 );
4894915d 286
b5a9f12a
RB
287 /* UFC_Name
288 */
289 $id_Lastname = DirEnum::getID(DirEnum::NAMETYPES, Profile::LASTNAME);
290 $id_Firstname = DirEnum::getID(DirEnum::NAMETYPES, Profile::FIRSTNAME);
cf183020
RB
291 $id_Nickname = DirEnum::getID(DirEnum::NAMETYPES, Profile::NICKNAME);
292 $id_Lastname_Marital = DirEnum::getID(DirEnum::NAMETYPES, Profile::LASTNAME . '_' . Profile::VN_MARITAL);
293 $id_Lastname_Ordinary = DirEnum::getID(DirEnum::NAMETYPES, Profile::LASTNAME . '_' . Profile::VN_ORDINARY);
294
deb26184 295 $tests['name'] = array(
cf183020 296 // Lastname
5b3f6e44
FB
297 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (pn.pid = p.pid)
298 WHERE pn.name LIKE {?} AND pn.typeid = {?}', 'BARROIS', $id_Lastname),
b5a9f12a 299 new UFC_Name(Profile::LASTNAME, 'BARROIS', UFC_Name::EXACT), -1),
5b3f6e44
FB
300 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (pn.pid = p.pid)
301 WHERE pn.name LIKE \'BARR%\' AND pn.typeid = {?}', $id_Lastname),
b5a9f12a 302 new UFC_Name(Profile::LASTNAME, 'BARR', UFC_Name::PREFIX), -1),
5b3f6e44
FB
303 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (pn.pid = p.pid)
304 WHERE pn.name LIKE \'%OIS\' AND pn.typeid = {?}', $id_Lastname),
b5a9f12a 305 new UFC_Name(Profile::LASTNAME, 'OIS', UFC_Name::SUFFIX), -1),
5b3f6e44
FB
306 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
307 WHERE pn.name LIKE \'%ARRO%\' AND pn.typeid = {?}', $id_Lastname),
b5a9f12a 308 new UFC_Name(Profile::LASTNAME, 'ARRO', UFC_Name::CONTAINS), -1),
5b3f6e44
FB
309 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
310 WHERE pn.name LIKE \'%ZZZZZZ%\' AND pn.typeid = {?}', $id_Lastname),
b5a9f12a
RB
311 new UFC_Name(Profile::LASTNAME, 'ZZZZZZ', UFC_Name::CONTAINS), 0),
312
cf183020 313 // Firstname
5b3f6e44
FB
314 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
315 WHERE pn.name LIKE {?} AND pn.typeid = {?}', 'Raphaël', $id_Firstname),
b5a9f12a 316 new UFC_Name(Profile::FIRSTNAME, 'Raphaël', UFC_Name::EXACT), -1),
5b3f6e44
FB
317 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
318 WHERE pn.name LIKE \'Raph%\' AND pn.typeid = {?}', $id_Firstname),
b5a9f12a 319 new UFC_Name(Profile::FIRSTNAME, 'Raph', UFC_Name::PREFIX), -1),
5b3f6e44
FB
320 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
321 WHERE pn.name LIKE \'%aël\' AND pn.typeid = {?}', $id_Firstname),
b5a9f12a 322 new UFC_Name(Profile::FIRSTNAME, 'aël', UFC_Name::SUFFIX), -1),
5b3f6e44
FB
323 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
324 WHERE pn.name LIKE \'%apha%\' AND pn.typeid = {?}', $id_Firstname),
b5a9f12a 325 new UFC_Name(Profile::FIRSTNAME, 'apha', UFC_Name::CONTAINS), -1),
5b3f6e44
FB
326 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
327 WHERE pn.name LIKE \'%zzzzzz%\' AND pn.typeid = {?}', $id_Firstname),
b5a9f12a 328 new UFC_Name(Profile::FIRSTNAME, 'zzzzzz', UFC_Name::CONTAINS), 0),
cf183020
RB
329
330 // Nickname
5b3f6e44
FB
331 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
332 WHERE pn.name LIKE {?} AND pn.typeid = {?}', 'Xelnor', $id_Nickname),
cf183020 333 new UFC_Name(Profile::NICKNAME, 'Xelnor', UFC_Name::EXACT), -1),
5b3f6e44
FB
334 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
335 WHERE pn.name LIKE \'Xel%\' AND pn.typeid = {?}', $id_Nickname),
cf183020 336 new UFC_Name(Profile::NICKNAME, 'Xel', UFC_Name::PREFIX), -1),
5b3f6e44
FB
337 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
338 WHERE pn.name LIKE \'%nor\' AND pn.typeid = {?}', $id_Nickname),
cf183020 339 new UFC_Name(Profile::NICKNAME, 'nor', UFC_Name::SUFFIX), -1),
5b3f6e44
FB
340 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
341 WHERE pn.name LIKE \'%lno%\' AND pn.typeid = {?}', $id_Nickname),
cf183020 342 new UFC_Name(Profile::NICKNAME, 'lno', UFC_Name::CONTAINS), -1),
5b3f6e44
FB
343 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
344 WHERE pn.name LIKE \'%zzzzzz%\' AND pn.typeid = {?}', $id_Nickname),
cf183020
RB
345 new UFC_Name(Profile::NICKNAME, 'zzzzzz', UFC_Name::CONTAINS), 0),
346
347 // Lastname + particle
5b3f6e44
FB
348 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
349 WHERE CONCAT(pn.particle, \' \', pn.name) LIKE {?} AND pn.typeid = {?}', 'DE SINGLY', $id_Lastname),
cf183020 350 new UFC_Name(Profile::LASTNAME, 'DE SINGLY', UFC_Name::PARTICLE | UFC_Name::EXACT), -1),
5b3f6e44
FB
351 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
352 WHERE CONCAT(pn.particle, \' \', pn.name) LIKE \'DE SI%\' AND pn.typeid = {?}', $id_Lastname),
cf183020 353 new UFC_Name(Profile::LASTNAME, 'DE SI', UFC_Name::PARTICLE | UFC_Name::PREFIX), -1),
5b3f6e44
FB
354 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
355 WHERE CONCAT(pn.particle, \' \', pn.name) LIKE \'%GLY\' AND pn.typeid = {?}', $id_Lastname),
cf183020 356 new UFC_Name(Profile::LASTNAME, 'GLY', UFC_NAME::PARTICLE | UFC_Name::SUFFIX), -1),
5b3f6e44
FB
357 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
358 WHERE CONCAT(pn.particle, \' \', pn.name) LIKE \'%E SIN%\' AND pn.typeid = {?}', $id_Lastname),
cf183020 359 new UFC_Name(Profile::LASTNAME, 'E SIN', UFC_Name::PARTICLE | UFC_Name::CONTAINS), -1),
5b3f6e44
FB
360 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
361 WHERE CONCAT(pn.particle, \' \', pn.name) LIKE \'%ZZZZZZ%\' AND pn.typeid = {?}', $id_Lastname),
cf183020
RB
362 new UFC_Name(Profile::LASTNAME, 'ZZZZZZ', UFC_Name::PARTICLE | UFC_Name::CONTAINS), 0),
363
364 // Lastname_ordinary
5b3f6e44
FB
365 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
366 WHERE pn.name LIKE {?} AND pn.typeid IN {?}',
cf183020
RB
367 'ALBIZZATI', array($id_Lastname, $id_Lastname_Marital, $id_Lastname_Ordinary)),
368 new UFC_Name(Profile::LASTNAME, 'ALBIZZATI', UFC_Name::VARIANTS | UFC_Name::EXACT), -1),
5b3f6e44
FB
369 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
370 WHERE pn.name LIKE {?} AND pn.typeid IN {?}',
cf183020
RB
371 'ALBIZ%', array($id_Lastname, $id_Lastname_Marital, $id_Lastname_Ordinary)),
372 new UFC_Name(Profile::LASTNAME, 'ALBIZ', UFC_Name::VARIANTS | UFC_Name::PREFIX), -1),
5b3f6e44
FB
373 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
374 WHERE pn.name LIKE {?} AND pn.typeid IN {?}',
cf183020
RB
375 '%ZATI', array($id_Lastname, $id_Lastname_Marital, $id_Lastname_Ordinary)),
376 new UFC_Name(Profile::LASTNAME, 'ZATI', UFC_NAME::VARIANTS | UFC_Name::SUFFIX), -1),
5b3f6e44
FB
377 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
378 WHERE pn.name LIKE {?} AND pn.typeid IN {?}',
cf183020
RB
379 '%BIZZ%', array($id_Lastname, $id_Lastname_Marital, $id_Lastname_Ordinary)),
380 new UFC_Name(Profile::LASTNAME, 'BIZZ', UFC_Name::VARIANTS | UFC_Name::CONTAINS), -1),
5b3f6e44
FB
381 array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid)
382 WHERE pn.name LIKE {?} AND pn.typeid IN {?}',
cf183020
RB
383 '%ZZZZZZ%', array($id_Lastname, $id_Lastname_Marital, $id_Lastname_Ordinary)),
384 new UFC_Name(Profile::LASTNAME, 'ZZZZZZ', UFC_Name::VARIANTS | UFC_Name::CONTAINS), 0),
b5a9f12a
RB
385 );
386
6a47d09f
RB
387 /* UFC_NameTokens
388 */
deb26184 389 $tests['nametoken'] = array(
6a47d09f 390 // !soundex, !exact
5b3f6e44
FB
391 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
392 WHERE sn.token LIKE \'xelnor%\''),
6a47d09f 393 new UFC_NameTokens('xelnor'), 1),
5b3f6e44
FB
394 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
395 WHERE sn.token LIKE \'xe%\''),
6a47d09f 396 new UFC_NameTokens('xe'), -1),
5b3f6e44
FB
397 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
398 WHERE sn.token LIKE \'xe%\' OR sn.token LIKE \'barr%\''),
6a47d09f 399 new UFC_NameTokens(array('xe', 'barr')), -1),
5b3f6e44
FB
400 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
401 WHERE sn.token LIKE \'zzzzzzzz%\''),
6a47d09f 402 new UFC_NameTokens('zzzzzzzz'), 0),
5b3f6e44
FB
403 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
404 WHERE sn.token LIKE \'barr%\' AND FIND_IN_SET(\'public\', sn.flags)'),
6a47d09f
RB
405 new UFC_NameTokens('barr', UFC_NameTokens::FLAG_PUBLIC), -1),
406
407 // !soundex, exact
5b3f6e44
FB
408 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
409 WHERE sn.token = \'xelnor\''),
6a47d09f 410 new UFC_NameTokens('xelnor', array(), false, true), 1),
5b3f6e44
FB
411 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
412 WHERE sn.token IN (\'xelnor\', \'barrois\')'),
6a47d09f 413 new UFC_NameTokens(array('xelnor', 'barrois'), array(), false, true), -1),
5b3f6e44
FB
414 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
415 WHERE sn.token = \'zzzzzzzz\''),
6a47d09f 416 new UFC_NameTokens('zzzzzzzz', array(), false, true), 0),
5b3f6e44
FB
417 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
418 WHERE sn.token IN (\'zzzzzzzz\', \'yyyyyyyy\')'),
6a47d09f 419 new UFC_NameTokens(array('zzzzzzzz', 'yyyyyyyy'), array(), false, true), 0),
5b3f6e44
FB
420 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
421 WHERE sn.token = \'barrois\' AND FIND_IN_SET(\'public\', sn.flags)'),
6a47d09f
RB
422 new UFC_NameTokens('barrois', UFC_NameTokens::FLAG_PUBLIC, false, true), -1),
423
424 // soundex, !exact
5b3f6e44
FB
425 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
426 WHERE sn.soundex = \'XLNO\''),
71886016 427 new UFC_NameTokens('xelnor', array(), true), -1),
5b3f6e44
FB
428 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
429 WHERE sn.soundex IN (\'XLNO\', \'BROS\')'),
71886016 430 new UFC_NameTokens(array('xelnor', 'barrois'), array(), true), -1),
5b3f6e44
FB
431 array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid)
432 WHERE sn.soundex = \'BROS\' AND FIND_IN_SET(\'public\', sn.flags)'),
71886016 433 new UFC_NameTokens('barrois', UFC_NameTokens::FLAG_PUBLIC, true), -1),
6a47d09f
RB
434 );
435
9ce6479b
RB
436 /* UFC_Nationality
437 */
deb26184 438 $tests['nationality'] = array(
9ce6479b
RB
439 array(self::buildProfileQuery('WHERE p.nationality1 IN {?} OR p.nationality2 IN {?} OR p.nationality3 IN {?}', array('BR'), array('BR'), array('BR')),
440 new UFC_Nationality('BR'), -1),
441 array(self::buildProfileQuery('WHERE p.nationality1 IN {?} OR p.nationality2 IN {?} OR p.nationality3 IN {?}', array('BR', 'US'), array('BR', 'US'), array('BR', 'US')),
442 new UFC_Nationality('BR', 'US'), -1),
443 array(self::buildProfileQuery('WHERE p.nationality1 IN {?} OR p.nationality2 IN {?} OR p.nationality3 IN {?}', array('__'), array('__'), array('__')),
444 new UFC_Nationality('__'), 0),
445 );
446
842c2042
RB
447 /* UFC_Dead
448 */
deb26184 449 $tests['dead'] = array(
842c2042
RB
450 array(self::buildProfileQuery('WHERE p.deathdate IS NOT NULL'),
451 new UFC_Dead(), -1),
452 array(self::buildProfileQuery('WHERE p.deathdate IS NOT NULL AND p.deathdate > {?}', '2008-01-01'),
453 new UFC_Dead('>', '2008-01-01'), -1),
842c2042
RB
454 array(self::buildProfileQuery('WHERE p.deathdate IS NOT NULL AND p.deathdate < {?}', '1600-01-01'),
455 new UFC_Dead('<', '1600-01-01'), 0),
842c2042
RB
456 array(self::buildProfileQuery('WHERE p.deathdate IS NOT NULL AND p.deathdate > {?}', date('Y-m-d')),
457 new UFC_Dead('>', 'now'), 0),
0d49fd8b
RB
458 );
459
460 /* UFC_Registered
461 */
deb26184 462 $tests['register'] = array(
0d49fd8b
RB
463 array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state = \'active\''),
464 new UFC_Registered(true), -1),
465 array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state != \'pending\''),
466 new UFC_Registered(), -1),
467 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'),
468 new UFC_Registered(true, '>', '2008-01-01'), -1),
469 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'),
470 new UFC_Registered(false, '>', '2008-01-01'), -1),
471 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'),
472 new UFC_Registered(true, '<', '1700-01-01'), 0),
473 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'),
474 new UFC_Registered(false, '<', '1700-01-01'), 0),
475 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')),
476 new UFC_Registered(true, '>', 'now'), 0),
477 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')),
478 new UFC_Registered(false, '>', 'now'), 0),
842c2042 479 );
9ce6479b 480
ed4d85c3 481 $testcases = array();
deb26184
FB
482 foreach ($tests as $name => $t) {
483 foreach ($t as $id => $case) {
484 $testcases[$name . '-' . $id] = $case;
485 }
ed4d85c3
RB
486 }
487 return $testcases;
aaf70eb8
FB
488 }
489
490 /**
491 * @dataProvider simpleUserProvider
492 */
493 public function testSimpleUser($query, $cond, $expcount = null)
494 {
d836f8db 495 /*
5b3f6e44 496 * @param $query A pair MySQL query (one for user selector, one for profile selector)
d836f8db
RB
497 * @param $cond The UFC to test
498 * @param $expcount The expected number of results (-1 if that number is unknown)
499 */
500
5b3f6e44
FB
501 $query = $query[0];
502
d836f8db 503 self::checkPlatal();
aaf70eb8
FB
504
505 $query = XDB::query($query);
506 $count = $query->numRows();
507 if (!is_null($expcount)) {
508 if ($expcount < 0) {
509 $this->assertNotEquals(0, $count);
510 } else {
511 $this->assertEquals($expcount, $count);
512 }
513 }
514 $ids = $query->fetchColumn();
515 $this->assertEquals($count, count($ids));
516 sort($ids);
517
518 $uf = new UserFilter($cond);
af746402 519 $this->assertEquals($count, $uf->getTotalUserCount());
aaf70eb8
FB
520 $got = $uf->getUIDs();
521 $this->assertEquals($count, count($got));
522 sort($got);
523 $this->assertEquals($ids, $got);
524
525 $uf = new UserFilter($cond);
526 $got = $uf->getUIDs();
527 $this->assertEquals($count, count($got));
528 sort($got);
529 $this->assertEquals($ids, $got);
5b3f6e44
FB
530 $this->assertEquals($count, $uf->getTotalUserCount());
531 }
532
533 /**
534 * @dataProvider simpleUserProvider
535 */
536 public function testSimpleProfile($query, $cond, $expcount = null)
537 {
538 /*
539 * @param $query A pair MySQL query (one for user selector, one for profile selector)
540 * @param $cond The UFC to test
541 * @param $expcount The expected number of results (-1 if that number is unknown)
542 */
543
544 $query = $query[1];
545
546 self::checkPlatal();
547
548 $query = XDB::query($query);
549 $count = $query->numRows();
550 if (!is_null($expcount)) {
551 if ($expcount < 0) {
552 $this->assertNotEquals(0, $count);
553 } else {
554 $this->assertEquals($expcount, $count);
555 }
556 }
557 $ids = $query->fetchColumn();
558 $this->assertEquals($count, count($ids));
559 sort($ids);
560
561 $uf = new UserFilter($cond);
562 $this->assertEquals($count, $uf->getTotalProfileCount());
563 $got = $uf->getPIDs();
564 $this->assertEquals($count, count($got));
565 sort($got);
566 $this->assertEquals($ids, $got);
567
568 $uf = new UserFilter($cond);
569 $got = $uf->getPIDs();
570 $this->assertEquals($count, count($got));
571 sort($got);
572 $this->assertEquals($ids, $got);
573 $this->assertEquals($count, $uf->getTotalProfileCount());
aaf70eb8 574 }
5b3f6e44 575
6c1e97ae
FB
576
577 public static function sortProvider()
578 {
579 return array(
580 array(self::buildAccountQuery('ORDER BY a.uid'), new UFO_Uid()),
581 array(self::buildAccountQuery('ORDER BY a.hruid'), new UFO_Hruid()),
582 array(self::buildAccountQuery('ORDER BY a.uid DESC'), new UFO_Uid(true)),
583 array(self::buildAccountQuery('ORDER BY a.hruid DESC'), new UFO_Hruid(true)),
584 array(self::buildProfileQuery('ORDER BY p.pid'), new UFO_Pid()),
585 array(self::buildProfileQuery('ORDER BY p.hrpid'), new UFO_Hrpid()),
586 array(self::buildProfileQuery('ORDER BY p.pid DESC'), new UFO_Pid(true)),
587 array(self::buildProfileQuery('ORDER BY p.hrpid DESC'), new UFO_Hrpid(true)),
588 array(self::buildProfileQuery('WHERE p.deathdate IS NOT NULL
589 ORDER BY p.deathdate, p.pid'),
590 array(new UFO_Death(), new UFO_Pid()), new UFC_Dead()),
591 array(self::buildProfileQuery('WHERE p.deathdate IS NOT NULL
592 ORDER BY p.deathdate DESC, p.pid'),
593 array(new UFO_Death(true), new UFO_Pid()), new UFC_Dead()),
7c9474af
FB
594 array(self::buildProfileQuery('ORDER BY p.next_birthday, p.pid'),
595 array(new UFO_Birthday(), new UFO_Pid())),
596 array(self::buildProfileQuery('ORDER BY p.next_birthday DESC, p.pid'),
597 array(new UFO_Birthday(true), new UFO_Pid())),
598 array(self::buildProfileQuery('ORDER BY p.last_change, p.pid'),
599 array(new UFO_ProfileUpdate(), new UFO_Pid())),
600 array(self::buildProfileQuery('ORDER BY p.last_change DESC, p.pid'),
601 array(new UFO_ProfileUpdate(true), new UFO_Pid())),
602 array(self::buildAccountQuery('ORDER BY a.registration_date, a.uid'),
603 array(new UFO_Registration(), new UFO_Uid())),
604 array(self::buildAccountQuery('ORDER BY a.registration_date DESC, a.uid'),
605 array(new UFO_Registration(true), new UFO_Uid())),
6c1e97ae
FB
606 );
607 }
608
609 /**
610 * @dataProvider sortProvider
611 */
612 public function testUserSortAndLimits($query, $sort, $cond = null)
613 {
614 self::checkPlatal();
615
616 $query = XDB::query($query[0]);
617 $count = $query->numRows();
618 $ids = $query->fetchColumn();
619 $this->assertSame($count, count($ids));
620
621 if ($cond == null ) {
622 $cond = new PFC_True();
623 }
624 $uf = new UserFilter($cond, $sort);
625 for ($i = 0 ; $i < $count ; $i += 7987) {
626 $got = $uf->getUIDs(new PlLimit(100, $i));
627 $this->assertSame($count, $uf->getTotalUserCount());
628 $part = array_slice($ids, $i, 100);
629 $this->assertSame($part, $got);
630 }
631 }
632
633 /**
634 * @dataProvider sortProvider
635 */
636 public function testProfileSortAndLimits($query, $sort, $cond = null)
637 {
638 self::checkPlatal();
639
640 $query = XDB::query($query[1]);
641 $count = $query->numRows();
642 $ids = $query->fetchColumn();
643 $this->assertSame($count, count($ids));
644
645 if ($cond == null ) {
646 $cond = new PFC_True();
647 }
648 $uf = new UserFilter($cond, $sort);
649 for ($i = 0 ; $i < $count ; $i += 7987) {
650 $got = $uf->getPIDs(new PlLimit(100, $i));
651 $this->assertSame($count, $uf->getTotalProfileCount());
652 $part = array_slice($ids, $i, 100);
653 $this->assertSame($part, $got);
654 }
655 }
aaf70eb8
FB
656}
657
448c8cdc 658// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
aaf70eb8 659?>