38b220a3ec4acef6731d88d3775913c409a8c80a
[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 public static function simpleUserProvider()
27 {
28 return array(
29 /* UFC_Hrpid
30 */
31 array('SELECT DISTINCT uid
32 FROM account_profiles
33 WHERE FIND_IN_SET(\'owner\', perms)',
34 new UFC_HasProfile(), -1),
35
36 /* UFC_Hruid
37 */
38 array(XDB::format('SELECT DISTINCT uid
39 FROM accounts
40 WHERE hruid = {?}', 'florent.bruneau.2003'),
41 new UFC_Hruid('florent.bruneau.2003'), 1),
42 array(XDB::format('SELECT DISTINCT uid
43 FROM accounts
44 WHERE hruid = {?}', 'florent.bruneau.2004'),
45 new UFC_Hruid('florent.bruneau.2004'), 0),
46 array(XDB::format('SELECT DISTINCT uid
47 FROM accounts
48 WHERE hruid IN {?}', array('florent.bruneau.2003',
49 'stephane.jacob.2004')),
50 new UFC_Hruid(array('florent.bruneau.2003', 'stephane.jacob.2004')), 2),
51 array(XDB::format('SELECT DISTINCT uid
52 FROM accounts
53 WHERE hruid IN {?}', array('florent.bruneau.2004',
54 'stephane.jacob.2004')),
55 new UFC_Hruid(array('florent.bruneau.2004', 'stephane.jacob.2004')), 1),
56
57 /* UFC_Hrpid
58 */
59 array(XDB::format('SELECT DISTINCT uid
60 FROM account_profiles AS ap
61 INNER JOIN profiles AS p ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', perms))
62 WHERE hrpid = {?}', 'florent.bruneau.2003'),
63 new UFC_Hrpid('florent.bruneau.2003'), 1),
64 array(XDB::format('SELECT DISTINCT uid
65 FROM account_profiles AS ap
66 INNER JOIN profiles AS p ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', perms))
67 WHERE hrpid = {?}', 'florent.bruneau.2004'),
68 new UFC_Hrpid('florent.bruneau.2004'), 0),
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 IN {?}', array('florent.bruneau.2003',
73 'stephane.jacob.2004')),
74 new UFC_Hrpid(array('florent.bruneau.2003', 'stephane.jacob.2004')), 2),
75 array(XDB::format('SELECT DISTINCT uid
76 FROM account_profiles AS ap
77 INNER JOIN profiles AS p ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', perms))
78 WHERE hrpid IN {?}', array('florent.bruneau.2004',
79 'stephane.jacob.2004')),
80 new UFC_Hrpid(array('florent.bruneau.2004', 'stephane.jacob.2004')), 1),
81
82 /* UFC_IP
83 */
84 array(XDB::format('SELECT DISTINCT a.uid
85 FROM log_sessions
86 INNER JOIN accounts AS a USING(uid)
87 WHERE ip = {?} OR forward_ip = {?}',
88 ip_to_uint('129.104.247.2'), ip_to_uint('129.104.247.2')),
89 new UFC_Ip('129.104.247.2'), -1),
90 /* TODO: UFC_Comment
91 */
92 /* UFC_Promo
93 */
94 array(XDB::format('SELECT DISTINCT ap.uid
95 FROM account_profiles AS ap
96 INNER JOIN profile_display AS pd ON (pd.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
97 WHERE pd.promo = {?}', 'X2004'),
98 new UFC_Promo('=', UserFilter::DISPLAY, 'X2004'), -1),
99 array(XDB::format('SELECT DISTINCT ap.uid
100 FROM account_profiles AS ap
101 INNER JOIN profile_display AS pd ON (pd.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
102 WHERE pd.promo < {?}', 'X2004'),
103 new UFC_Promo('<', UserFilter::DISPLAY, 'X2004'), -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 < {?}', 'X1900'),
113 new UFC_Promo('<', UserFilter::DISPLAY, 'X1900'), 0),
114
115 array(XDB::format('SELECT DISTINCT ap.uid
116 FROM account_profiles AS ap
117 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
118 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
119 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
120 WHERE pe.entry_year = {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
121 '2004', 'Ing.'),
122 new UFC_Promo('=', UserFilter::GRADE_ING, 2004), -1),
123 array(XDB::format('SELECT DISTINCT ap.uid
124 FROM account_profiles AS ap
125 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
126 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
127 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
128 WHERE pe.entry_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
129 '1960', 'Ing.'),
130 new UFC_Promo('<=', UserFilter::GRADE_ING, 1960), -1),
131 array(XDB::format('SELECT DISTINCT ap.uid
132 FROM account_profiles AS ap
133 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
134 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
135 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
136 WHERE pe.entry_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
137 '2004', 'Ing.'),
138 new UFC_Promo('>', UserFilter::GRADE_ING, 2004), -1),
139 array(XDB::format('SELECT DISTINCT ap.uid
140 FROM account_profiles AS ap
141 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
142 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
143 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
144 WHERE pe.entry_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
145 '1900', 'Ing.'),
146 new UFC_Promo('<', UserFilter::GRADE_ING, 1900), 0),
147
148 /* XXX : tests disabled until there are Masters and doctors in the DB
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.grad_year = {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
155 '2009', 'MSc'),
156 new UFC_Promo('=', UserFilter::GRADE_MST, 2009), -1),
157 array(XDB::format('SELECT DISTINCT ap.uid
158 FROM account_profiles AS ap
159 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
160 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
161 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
162 WHERE pe.grad_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
163 '2009', 'MSc'),
164 new UFC_Promo('<=', UserFilter::GRADE_MST, 2009), -1),
165 array(XDB::format('SELECT DISTINCT ap.uid
166 FROM account_profiles AS ap
167 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
168 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
169 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
170 WHERE pe.grad_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
171 '2009', 'MSc'),
172 new UFC_Promo('>', UserFilter::GRADE_MST, 2009), -1),
173 array(XDB::format('SELECT DISTINCT ap.uid
174 FROM account_profiles AS ap
175 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
176 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
177 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
178 WHERE pe.grad_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
179 '1980', 'MSc'),
180 new UFC_Promo('<', UserFilter::GRADE_MST, 1980), 0),
181
182 array(XDB::format('SELECT DISTINCT ap.uid
183 FROM account_profiles AS ap
184 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
185 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
186 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
187 WHERE pe.grad_year = {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
188 '2009', 'PhD'),
189 new UFC_Promo('=', UserFilter::GRADE_PHD, 2009), -1),
190 array(XDB::format('SELECT DISTINCT ap.uid
191 FROM account_profiles AS ap
192 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
193 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
194 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
195 WHERE pe.grad_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
196 '2009', 'PhD'),
197 new UFC_Promo('<=', UserFilter::GRADE_PHD, 2009), -1),
198 array(XDB::format('SELECT DISTINCT ap.uid
199 FROM account_profiles AS ap
200 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
201 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
202 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
203 WHERE pe.grad_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
204 '2009', 'PhD'),
205 new UFC_Promo('>', UserFilter::GRADE_PHD, 2009), -1),
206 array(XDB::format('SELECT DISTINCT ap.uid
207 FROM account_profiles AS ap
208 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
209 LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
210 LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
211 WHERE pe.grad_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}',
212 '1980', 'PhD'),
213 new UFC_Promo('<', UserFilter::GRADE_PHD, 1980), 0),
214 */
215 );
216 }
217
218 /**
219 * @dataProvider simpleUserProvider
220 * @param $query A MySQL query
221 * @param $cond The UFC to test
222 * @param $expcount The expected number of results (-1 if that number is unknown)
223 */
224 public function testSimpleUser($query, $cond, $expcount = null)
225 {
226 global $globals, $platal;
227 $platal = new Xorg();
228
229 $query = XDB::query($query);
230 $count = $query->numRows();
231 if (!is_null($expcount)) {
232 if ($expcount < 0) {
233 $this->assertNotEquals(0, $count);
234 } else {
235 $this->assertEquals($expcount, $count);
236 }
237 }
238 $ids = $query->fetchColumn();
239 $this->assertEquals($count, count($ids));
240 sort($ids);
241
242 $uf = new UserFilter($cond);
243 $this->assertEquals($count, $uf->getTotalUserCount());
244 $got = $uf->getUIDs();
245 $this->assertEquals($count, count($got));
246 sort($got);
247 $this->assertEquals($ids, $got);
248
249 $uf = new UserFilter($cond);
250 $got = $uf->getUIDs();
251 $this->assertEquals($count, count($got));
252 sort($got);
253 $this->assertEquals($ids, $got);
254 $this->assertEquals($count, $uf->getTotalCount());
255 }
256 }
257
258 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
259 ?>