Improve design of userfiltertest
[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 return array(
39 /* UFC_Hrpid
40 */
41 array('SELECT DISTINCT uid
42 FROM account_profiles
43 WHERE FIND_IN_SET(\'owner\', perms)',
44 new UFC_HasProfile(), -1),
45
46 /* UFC_Hruid
47 */
48 array(XDB::format('SELECT DISTINCT uid
49 FROM accounts
50 WHERE hruid = {?}', 'florent.bruneau.2003'),
51 new UFC_Hruid('florent.bruneau.2003'), 1),
52 array(XDB::format('SELECT DISTINCT uid
53 FROM accounts
54 WHERE hruid = {?}', 'florent.bruneau.2004'),
55 new UFC_Hruid('florent.bruneau.2004'), 0),
56 array(XDB::format('SELECT DISTINCT uid
57 FROM accounts
58 WHERE hruid IN {?}', array('florent.bruneau.2003',
59 'stephane.jacob.2004')),
60 new UFC_Hruid(array('florent.bruneau.2003', 'stephane.jacob.2004')), 2),
61 array(XDB::format('SELECT DISTINCT uid
62 FROM accounts
63 WHERE hruid IN {?}', array('florent.bruneau.2004',
64 'stephane.jacob.2004')),
65 new UFC_Hruid(array('florent.bruneau.2004', 'stephane.jacob.2004')), 1),
66
67 /* UFC_Hrpid
68 */
69 array(XDB::format('SELECT DISTINCT uid
70 FROM account_profiles AS ap
71 INNER JOIN profiles AS p ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', perms))
72 WHERE hrpid = {?}', 'florent.bruneau.2003'),
73 new UFC_Hrpid('florent.bruneau.2003'), 1),
74 array(XDB::format('SELECT DISTINCT uid
75 FROM account_profiles AS ap
76 INNER JOIN profiles AS p ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', perms))
77 WHERE hrpid = {?}', 'florent.bruneau.2004'),
78 new UFC_Hrpid('florent.bruneau.2004'), 0),
79 array(XDB::format('SELECT DISTINCT uid
80 FROM account_profiles AS ap
81 INNER JOIN profiles AS p ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', perms))
82 WHERE hrpid IN {?}', array('florent.bruneau.2003',
83 'stephane.jacob.2004')),
84 new UFC_Hrpid(array('florent.bruneau.2003', 'stephane.jacob.2004')), 2),
85 array(XDB::format('SELECT DISTINCT uid
86 FROM account_profiles AS ap
87 INNER JOIN profiles AS p ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', perms))
88 WHERE hrpid IN {?}', array('florent.bruneau.2004',
89 'stephane.jacob.2004')),
90 new UFC_Hrpid(array('florent.bruneau.2004', 'stephane.jacob.2004')), 1),
91
92 /* UFC_IP
93 */
94 array(XDB::format('SELECT DISTINCT a.uid
95 FROM log_sessions
96 INNER JOIN accounts AS a USING(uid)
97 WHERE ip = {?} OR forward_ip = {?}',
98 ip_to_uint('129.104.247.2'), ip_to_uint('129.104.247.2')),
99 new UFC_Ip('129.104.247.2'), -1),
100 /* TODO: UFC_Comment
101 */
102 /* UFC_Promo
103 */
104 array(XDB::format('SELECT DISTINCT ap.uid
105 FROM account_profiles AS ap
106 INNER JOIN profile_display AS pd ON (pd.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
107 WHERE pd.promo = {?}', 'X2004'),
108 new UFC_Promo('=', UserFilter::DISPLAY, 'X2004'), -1),
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 < {?}', 'X1900'),
123 new UFC_Promo('<', UserFilter::DISPLAY, 'X1900'), 0),
124
125 array(XDB::format('SELECT DISTINCT ap.uid
126 FROM account_profiles AS ap
127 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
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(XDB::format('SELECT DISTINCT ap.uid
134 FROM account_profiles AS ap
135 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
136 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
137 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
138 WHERE pe.entry_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
139 '1960', 'Ing.'),
140 new UFC_Promo('<=', UserFilter::GRADE_ING, 1960), -1),
141 array(XDB::format('SELECT DISTINCT ap.uid
142 FROM account_profiles AS ap
143 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
144 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
145 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
146 WHERE pe.entry_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
147 '2004', 'Ing.'),
148 new UFC_Promo('>', UserFilter::GRADE_ING, 2004), -1),
149 array(XDB::format('SELECT DISTINCT ap.uid
150 FROM account_profiles AS ap
151 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
152 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
153 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
154 WHERE pe.entry_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
155 '1900', 'Ing.'),
156 new UFC_Promo('<', UserFilter::GRADE_ING, 1900), 0),
157
158 /* XXX : tests disabled until there are Masters and doctors in the DB
159 array(XDB::format('SELECT DISTINCT ap.uid
160 FROM account_profiles AS ap
161 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
162 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
163 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
164 WHERE pe.grad_year = {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
165 '2009', 'MSc'),
166 new UFC_Promo('=', UserFilter::GRADE_MST, 2009), -1),
167 array(XDB::format('SELECT DISTINCT ap.uid
168 FROM account_profiles AS ap
169 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
170 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
171 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
172 WHERE pe.grad_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
173 '2009', 'MSc'),
174 new UFC_Promo('<=', UserFilter::GRADE_MST, 2009), -1),
175 array(XDB::format('SELECT DISTINCT ap.uid
176 FROM account_profiles AS ap
177 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
178 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
179 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
180 WHERE pe.grad_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
181 '2009', 'MSc'),
182 new UFC_Promo('>', UserFilter::GRADE_MST, 2009), -1),
183 array(XDB::format('SELECT DISTINCT ap.uid
184 FROM account_profiles AS ap
185 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
186 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
187 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
188 WHERE pe.grad_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
189 '1980', 'MSc'),
190 new UFC_Promo('<', UserFilter::GRADE_MST, 1980), 0),
191
192 array(XDB::format('SELECT DISTINCT ap.uid
193 FROM account_profiles AS ap
194 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
195 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
196 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
197 WHERE pe.grad_year = {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
198 '2009', 'PhD'),
199 new UFC_Promo('=', UserFilter::GRADE_PHD, 2009), -1),
200 array(XDB::format('SELECT DISTINCT ap.uid
201 FROM account_profiles AS ap
202 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
203 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
204 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
205 WHERE pe.grad_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
206 '2009', 'PhD'),
207 new UFC_Promo('<=', UserFilter::GRADE_PHD, 2009), -1),
208 array(XDB::format('SELECT DISTINCT ap.uid
209 FROM account_profiles AS ap
210 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
211 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
212 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
213 WHERE pe.grad_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
214 '2009', 'PhD'),
215 new UFC_Promo('>', UserFilter::GRADE_PHD, 2009), -1),
216 array(XDB::format('SELECT DISTINCT ap.uid
217 FROM account_profiles AS ap
218 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
219 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
220 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
221 WHERE pe.grad_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
222 '1980', 'PhD'),
223 new UFC_Promo('<', UserFilter::GRADE_PHD, 1980), 0),
224 */
225 /* TODO: UFC_SchoolId
226 */
227 /* UFC_EducationSchool
228 */
229 array(XDB::format('SELECT DISTINCT ap.uid
230 FROM account_profiles AS ap
231 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
232 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
233 WHERE pee.abbreviation = {?}', 'X'),
234 new UFC_EducationSchool(DirEnum::getId(DirEnum::EDUSCHOOLS, 'X')), -1),
235 array(XDB::format('SELECT DISTINCT ap.uid
236 FROM account_profiles AS ap
237 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
238 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
239 WHERE pee.abbreviation IN {?}', array('X', 'HEC')),
240 new UFC_EducationSchool(array(
241 DirEnum::getId(DirEnum::EDUSCHOOLS, 'X'),
242 DirEnum::getId(DirEnum::EDUSCHOOLS, 'HEC'),
243 )), -1),
244 );
245 }
246
247 /**
248 * @dataProvider simpleUserProvider
249 */
250 public function testSimpleUser($query, $cond, $expcount = null)
251 {
252 /*
253 * @param $query A MySQL query
254 * @param $cond The UFC to test
255 * @param $expcount The expected number of results (-1 if that number is unknown)
256 */
257
258 self::checkPlatal();
259
260 $query = XDB::query($query);
261 $count = $query->numRows();
262 if (!is_null($expcount)) {
263 if ($expcount < 0) {
264 $this->assertNotEquals(0, $count);
265 } else {
266 $this->assertEquals($expcount, $count);
267 }
268 }
269 $ids = $query->fetchColumn();
270 $this->assertEquals($count, count($ids));
271 sort($ids);
272
273 $uf = new UserFilter($cond);
274 $this->assertEquals($count, $uf->getTotalUserCount());
275 $got = $uf->getUIDs();
276 $this->assertEquals($count, count($got));
277 sort($got);
278 $this->assertEquals($ids, $got);
279
280 $uf = new UserFilter($cond);
281 $got = $uf->getUIDs();
282 $this->assertEquals($count, count($got));
283 sort($got);
284 $this->assertEquals($ids, $got);
285 $this->assertEquals($count, $uf->getTotalCount());
286 }
287 }
288
289 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
290 ?>