6b4db76149e5e47d04b592f67afde07ae1753604
[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 $testcases = array();
258 foreach ($tests as $t) {
259 $testcases = array_merge($testcases, $t);
260 }
261 return $testcases;
262 }
263
264 /**
265 * @dataProvider simpleUserProvider
266 */
267 public function testSimpleUser($query, $cond, $expcount = null)
268 {
269 /*
270 * @param $query A MySQL query
271 * @param $cond The UFC to test
272 * @param $expcount The expected number of results (-1 if that number is unknown)
273 */
274
275 self::checkPlatal();
276
277 $query = XDB::query($query);
278 $count = $query->numRows();
279 if (!is_null($expcount)) {
280 if ($expcount < 0) {
281 $this->assertNotEquals(0, $count);
282 } else {
283 $this->assertEquals($expcount, $count);
284 }
285 }
286 $ids = $query->fetchColumn();
287 $this->assertEquals($count, count($ids));
288 sort($ids);
289
290 $uf = new UserFilter($cond);
291 $this->assertEquals($count, $uf->getTotalUserCount());
292 $got = $uf->getUIDs();
293 $this->assertEquals($count, count($got));
294 sort($got);
295 $this->assertEquals($ids, $got);
296
297 $uf = new UserFilter($cond);
298 $got = $uf->getUIDs();
299 $this->assertEquals($count, count($got));
300 sort($got);
301 $this->assertEquals($ids, $got);
302 $this->assertEquals($count, $uf->getTotalCount());
303 }
304 }
305
306 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
307 ?>