2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 require_once dirname(__FILE__
) . '/../include/test.inc.php';
24 class UserFilterTest
extends PlTestCase
26 private static function checkPlatal()
35 public static function simpleUserProvider()
41 array('SELECT DISTINCT uid
43 WHERE FIND_IN_SET(\'owner\', perms)',
44 new UFC_HasProfile(), -1),
48 array(XDB
::format('SELECT DISTINCT uid
50 WHERE hruid = {?}', 'florent.bruneau.2003'),
51 new UFC_Hruid('florent.bruneau.2003'), 1),
52 array(XDB
::format('SELECT DISTINCT uid
54 WHERE hruid = {?}', 'florent.bruneau.2004'),
55 new UFC_Hruid('florent.bruneau.2004'), 0),
56 array(XDB
::format('SELECT DISTINCT uid
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
63 WHERE hruid IN {?}', array('florent.bruneau.2004',
64 'stephane.jacob.2004')),
65 new UFC_Hruid(array('florent.bruneau.2004', 'stephane.jacob.2004')), 1),
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),
94 array(XDB
::format('SELECT DISTINCT a.uid
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),
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),
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 = {?}',
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 = {?}',
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 = {?}',
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 = {?}',
156 new UFC_Promo('<', UserFilter
::GRADE_ING
, 1900), 0),
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 = {?}',
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 = {?}',
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 = {?}',
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 = {?}',
190 new UFC_Promo('<', UserFilter::GRADE_MST, 1980), 0),
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 = {?}',
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 = {?}',
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 = {?}',
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 = {?}',
223 new UFC_Promo('<', UserFilter::GRADE_PHD, 1980), 0),
225 /* TODO: UFC_SchoolId
227 /* UFC_EducationSchool
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'),
248 * @dataProvider simpleUserProvider
250 public function testSimpleUser($query, $cond, $expcount = null
)
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)
260 $query = XDB
::query($query);
261 $count = $query->numRows();
262 if (!is_null($expcount)) {
264 $this->assertNotEquals(0, $count);
266 $this->assertEquals($expcount, $count);
269 $ids = $query->fetchColumn();
270 $this->assertEquals($count, count($ids));
273 $uf = new UserFilter($cond);
274 $this->assertEquals($count, $uf->getTotalUserCount());
275 $got = $uf->getUIDs();
276 $this->assertEquals($count, count($got));
278 $this->assertEquals($ids, $got);
280 $uf = new UserFilter($cond);
281 $got = $uf->getUIDs();
282 $this->assertEquals($count, count($got));
284 $this->assertEquals($ids, $got);
285 $this->assertEquals($count, $uf->getTotalCount());
289 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: