Add ut for UFC_EducationDegree
[platal.git] / ut / userfiltertest.php
CommitLineData
aaf70eb8
FB
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
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
aaf70eb8
FB
35 public static function simpleUserProvider()
36 {
d836f8db 37 self::checkPlatal();
ed4d85c3
RB
38 $tests = array();
39
40 $tests[] = array(
aaf70eb8
FB
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),
ed4d85c3 102 );
3867f628
RB
103 /* TODO: UFC_Comment
104 */
ed4d85c3 105
3867f628
RB
106 /* UFC_Promo
107 */
ed4d85c3 108 $tests[] = array(
3867f628
RB
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),
3867f628
RB
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),
ed4d85c3
RB
229 */
230 );
231
d836f8db
RB
232 /* TODO: UFC_SchoolId
233 */
234 /* UFC_EducationSchool
235 */
ed4d85c3
RB
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(
d836f8db
RB
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'),
ed4d85c3 248 new UFC_EducationSchool($id_X), -1),
d836f8db
RB
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')),
ed4d85c3 254 new UFC_EducationSchool(array($id_X, $id_HEC)), -1),
aaf70eb8 255 );
ed4d85c3 256
4894915d
RB
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
ed4d85c3
RB
280 $testcases = array();
281 foreach ($tests as $t) {
282 $testcases = array_merge($testcases, $t);
283 }
284 return $testcases;
aaf70eb8
FB
285 }
286
287 /**
288 * @dataProvider simpleUserProvider
289 */
290 public function testSimpleUser($query, $cond, $expcount = null)
291 {
d836f8db
RB
292 /*
293 * @param $query A MySQL query
294 * @param $cond The UFC to test
295 * @param $expcount The expected number of results (-1 if that number is unknown)
296 */
297
298 self::checkPlatal();
aaf70eb8
FB
299
300 $query = XDB::query($query);
301 $count = $query->numRows();
302 if (!is_null($expcount)) {
303 if ($expcount < 0) {
304 $this->assertNotEquals(0, $count);
305 } else {
306 $this->assertEquals($expcount, $count);
307 }
308 }
309 $ids = $query->fetchColumn();
310 $this->assertEquals($count, count($ids));
311 sort($ids);
312
313 $uf = new UserFilter($cond);
af746402 314 $this->assertEquals($count, $uf->getTotalUserCount());
aaf70eb8
FB
315 $got = $uf->getUIDs();
316 $this->assertEquals($count, count($got));
317 sort($got);
318 $this->assertEquals($ids, $got);
319
320 $uf = new UserFilter($cond);
321 $got = $uf->getUIDs();
322 $this->assertEquals($count, count($got));
323 sort($got);
324 $this->assertEquals($ids, $got);
325 $this->assertEquals($count, $uf->getTotalCount());
326 }
327}
328
329// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
330?>