| 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 | private static function buildAccountQuery() |
| 36 | { |
| 37 | $args = func_get_args(); |
| 38 | $joinsAndWhere = XDB::_prepare($args); |
| 39 | return array('SELECT DISTINCT a.uid |
| 40 | FROM accounts AS a |
| 41 | ' . $joinsAndWhere, |
| 42 | 'SELECT DISTINCT p.pid |
| 43 | FROM profiles AS p |
| 44 | INNER JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', perms)) |
| 45 | INNER JOIN accounts AS a ON (a.uid = ap.uid) |
| 46 | ' . $joinsAndWhere); |
| 47 | } |
| 48 | |
| 49 | private static function buildProfileQuery() |
| 50 | { |
| 51 | $args = func_get_args(); |
| 52 | $joinsAndWhere = XDB::_prepare($args); |
| 53 | return array('SELECT DISTINCT a.uid |
| 54 | FROM accounts AS a |
| 55 | INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', perms)) |
| 56 | INNER JOIN profiles AS p ON (p.pid = ap.pid) |
| 57 | ' . $joinsAndWhere, |
| 58 | 'SELECT DISTINCT p.pid |
| 59 | FROM profiles AS p |
| 60 | ' . $joinsAndWhere); |
| 61 | } |
| 62 | |
| 63 | public static function simpleUserProvider() |
| 64 | { |
| 65 | self::checkPlatal(); |
| 66 | $tests = array(); |
| 67 | |
| 68 | $tests['id'] = array( |
| 69 | /* UFC_Hrpid |
| 70 | */ |
| 71 | array(self::buildAccountQuery('INNER JOIN account_profiles AS ap2 ON (ap2.uid = a.uid) |
| 72 | WHERE FIND_IN_SET(\'owner\', ap2.perms)'), |
| 73 | new UFC_HasProfile(), -1), |
| 74 | |
| 75 | /* UFC_Hruid |
| 76 | */ |
| 77 | array(self::buildAccountQuery('WHERE a.hruid = {?}', 'florent.bruneau.2003'), |
| 78 | new UFC_Hruid('florent.bruneau.2003'), 1), |
| 79 | array(self::buildAccountQuery('WHERE a.hruid = {?}', 'florent.bruneau.2004'), |
| 80 | new UFC_Hruid('florent.bruneau.2004'), 0), |
| 81 | array(self::buildAccountQuery('WHERE a.hruid IN {?}', array('florent.bruneau.2003', |
| 82 | 'stephane.jacob.2004')), |
| 83 | new UFC_Hruid('florent.bruneau.2003', 'stephane.jacob.2004'), 2), |
| 84 | array(self::buildAccountQuery('WHERE a.hruid IN {?}', array('florent.bruneau.2004', |
| 85 | 'stephane.jacob.2004')), |
| 86 | new UFC_Hruid('florent.bruneau.2004', 'stephane.jacob.2004'), 1), |
| 87 | |
| 88 | /* UFC_Hrpid |
| 89 | */ |
| 90 | array(self::buildProfileQuery('WHERE p.hrpid = {?}', 'florent.bruneau.2003'), |
| 91 | new UFC_Hrpid('florent.bruneau.2003'), 1), |
| 92 | array(self::buildProfileQuery('WHERE p.hrpid = {?}', 'florent.bruneau.2004'), |
| 93 | new UFC_Hrpid('florent.bruneau.2004'), 0), |
| 94 | array(self::buildProfileQuery('WHERE p.hrpid IN {?}', array('florent.bruneau.2003', |
| 95 | 'stephane.jacob.2004')), |
| 96 | new UFC_Hrpid('florent.bruneau.2003', 'stephane.jacob.2004'), 2), |
| 97 | array(self::buildProfileQuery('WHERE p.hrpid IN {?}', array('florent.bruneau.2004', |
| 98 | 'stephane.jacob.2004')), |
| 99 | new UFC_Hrpid('florent.bruneau.2004', 'stephane.jacob.2004'), 1), |
| 100 | |
| 101 | /* UFC_IP |
| 102 | */ |
| 103 | array(self::buildAccountQuery('INNER JOIN log_sessions AS s ON (s.uid = a.uid) |
| 104 | WHERE s.ip = {?} OR s.forward_ip = {?}', |
| 105 | ip_to_uint('129.104.247.2'), ip_to_uint('129.104.247.2')), |
| 106 | new UFC_Ip('129.104.247.2'), -1), |
| 107 | ); |
| 108 | /* TODO: UFC_Comment |
| 109 | */ |
| 110 | |
| 111 | /* UFC_Promo |
| 112 | */ |
| 113 | $tests['promo'] = array( |
| 114 | array(self::buildProfileQuery('INNER JOIN profile_display AS pd ON (pd.pid = p.pid) |
| 115 | WHERE pd.promo = {?}', 'X2004'), |
| 116 | new UFC_Promo('=', UserFilter::DISPLAY, 'X2004'), -1), |
| 117 | |
| 118 | array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid) |
| 119 | LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) |
| 120 | LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) |
| 121 | WHERE pe.entry_year = {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}', |
| 122 | '2004', 'Ing.'), |
| 123 | new UFC_Promo('=', UserFilter::GRADE_ING, 2004), -1), |
| 124 | array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid) |
| 125 | LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) |
| 126 | LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) |
| 127 | WHERE pe.entry_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}', |
| 128 | '1960', 'Ing.'), |
| 129 | new UFC_Promo('<=', UserFilter::GRADE_ING, 1960), -1), |
| 130 | array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid) |
| 131 | LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) |
| 132 | LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) |
| 133 | WHERE pe.entry_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}', |
| 134 | '2004', 'Ing.'), |
| 135 | new UFC_Promo('>', UserFilter::GRADE_ING, 2004), -1), |
| 136 | array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid) |
| 137 | LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) |
| 138 | LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) |
| 139 | WHERE pe.entry_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}', |
| 140 | '1900', 'Ing.'), |
| 141 | new UFC_Promo('<', UserFilter::GRADE_ING, 1900), 0), |
| 142 | |
| 143 | /* XXX : tests disabled until there are Masters and doctors in the DB |
| 144 | array(XDB::format('SELECT DISTINCT ap.uid |
| 145 | FROM account_profiles AS ap |
| 146 | INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
| 147 | LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) |
| 148 | LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) |
| 149 | WHERE pe.grad_year = {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}', |
| 150 | '2009', 'MSc'), |
| 151 | new UFC_Promo('=', UserFilter::GRADE_MST, 2009), -1), |
| 152 | array(XDB::format('SELECT DISTINCT ap.uid |
| 153 | FROM account_profiles AS ap |
| 154 | INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
| 155 | LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) |
| 156 | LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) |
| 157 | WHERE pe.grad_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}', |
| 158 | '2009', 'MSc'), |
| 159 | new UFC_Promo('<=', UserFilter::GRADE_MST, 2009), -1), |
| 160 | array(XDB::format('SELECT DISTINCT ap.uid |
| 161 | FROM account_profiles AS ap |
| 162 | INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
| 163 | LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) |
| 164 | LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) |
| 165 | WHERE pe.grad_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}', |
| 166 | '2009', 'MSc'), |
| 167 | new UFC_Promo('>', UserFilter::GRADE_MST, 2009), -1), |
| 168 | array(XDB::format('SELECT DISTINCT ap.uid |
| 169 | FROM account_profiles AS ap |
| 170 | INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
| 171 | LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) |
| 172 | LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) |
| 173 | WHERE pe.grad_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}', |
| 174 | '1980', 'MSc'), |
| 175 | new UFC_Promo('<', UserFilter::GRADE_MST, 1980), 0), |
| 176 | |
| 177 | array(XDB::format('SELECT DISTINCT ap.uid |
| 178 | FROM account_profiles AS ap |
| 179 | INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
| 180 | LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) |
| 181 | LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) |
| 182 | WHERE pe.grad_year = {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}', |
| 183 | '2009', 'PhD'), |
| 184 | new UFC_Promo('=', UserFilter::GRADE_PHD, 2009), -1), |
| 185 | array(XDB::format('SELECT DISTINCT ap.uid |
| 186 | FROM account_profiles AS ap |
| 187 | INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
| 188 | LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) |
| 189 | LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) |
| 190 | WHERE pe.grad_year <= {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}', |
| 191 | '2009', 'PhD'), |
| 192 | new UFC_Promo('<=', UserFilter::GRADE_PHD, 2009), -1), |
| 193 | array(XDB::format('SELECT DISTINCT ap.uid |
| 194 | FROM account_profiles AS ap |
| 195 | INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
| 196 | LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) |
| 197 | LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) |
| 198 | WHERE pe.grad_year > {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}', |
| 199 | '2009', 'PhD'), |
| 200 | new UFC_Promo('>', UserFilter::GRADE_PHD, 2009), -1), |
| 201 | array(XDB::format('SELECT DISTINCT ap.uid |
| 202 | FROM account_profiles AS ap |
| 203 | INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
| 204 | LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) |
| 205 | LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) |
| 206 | WHERE pe.grad_year < {?} AND pee.abbreviation = \'X\' AND pede.abbreviation = {?}', |
| 207 | '1980', 'PhD'), |
| 208 | new UFC_Promo('<', UserFilter::GRADE_PHD, 1980), 0), |
| 209 | */ |
| 210 | ); |
| 211 | |
| 212 | /* UFC_SchoolId |
| 213 | */ |
| 214 | $tests['schoolid'] = array( |
| 215 | array(self::buildProfileQuery('WHERE p.xorg_id = {?}', 20060076), |
| 216 | new UFC_SchoolId(UFC_SchoolId::Xorg, 20060076), 1), |
| 217 | array(self::buildProfileQuery('WHERE p.ax_id = {?}', 20060062), |
| 218 | new UFC_SchoolId(UFC_SchoolId::AX, 20060062), 1), |
| 219 | array(self::buildProfileQuery('WHERE p.xorg_id = {?}', 007), |
| 220 | new UFC_SchoolId(UFC_SchoolId::Xorg, 007), 0), |
| 221 | array(self::buildProfileQuery('WHERE p.ax_id = {?}', 007), |
| 222 | new UFC_SchoolId(UFC_SchoolId::AX, 007), 0), |
| 223 | /* FIXME: disabled until we have some examples of school_id |
| 224 | array(self::buildProfileQuery('WHERE p.school_id = {?}', 12345678), |
| 225 | new UFC_SchoolId(UFC_SchoolId::School, 12345678), 1), |
| 226 | array(self::buildProfileQuery('WHERE p.school_id = {?}', 007), |
| 227 | new UFC_SchoolId(UFC_SchoolId::School, 007), 0), |
| 228 | */ |
| 229 | ); |
| 230 | /* UFC_EducationSchool |
| 231 | */ |
| 232 | $id_X = XDB::fetchOneCell('SELECT id |
| 233 | FROM profile_education_enum |
| 234 | WHERE abbreviation = {?}', 'X'); |
| 235 | $id_HEC = XDB::fetchOneCell('SELECT id |
| 236 | FROM profile_education_enum |
| 237 | WHERE abbreviation = {?}', 'HEC'); |
| 238 | $tests['school'] = array( |
| 239 | array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid) |
| 240 | LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) |
| 241 | WHERE pee.abbreviation = {?}', 'X'), |
| 242 | new UFC_EducationSchool($id_X), -1), |
| 243 | array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid) |
| 244 | LEFT JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) |
| 245 | WHERE pee.abbreviation IN {?}', array('X', 'HEC')), |
| 246 | new UFC_EducationSchool($id_X, $id_HEC), -1), |
| 247 | ); |
| 248 | |
| 249 | /* UFC_EducationDegree |
| 250 | */ |
| 251 | $id_DegreeIng = XDB::fetchOneCell('SELECT id |
| 252 | FROM profile_education_degree_enum |
| 253 | WHERE abbreviation = {?}', 'Ing.'); |
| 254 | $id_DegreePhd = XDB::fetchOneCell('SELECT id |
| 255 | FROM profile_education_degree_enum |
| 256 | WHERE abbreviation = {?}', 'PhD'); |
| 257 | $tests['degree'] = array( |
| 258 | array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid) |
| 259 | LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) |
| 260 | WHERE pede.abbreviation = {?}', 'Ing.'), |
| 261 | new UFC_EducationDegree($id_DegreeIng), -1), |
| 262 | array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid) |
| 263 | LEFT JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) |
| 264 | WHERE pede.abbreviation IN {?}', array('Ing.', 'PhD')), |
| 265 | new UFC_EducationDegree($id_DegreeIng, $id_DegreePhd), -1), |
| 266 | ); |
| 267 | /* UFC_EducationField |
| 268 | */ |
| 269 | $id_FieldInfo = XDB::fetchOneCell('SELECT id |
| 270 | FROM profile_education_field_enum |
| 271 | WHERE field = {?}', 'Informatique'); |
| 272 | $id_FieldDroit = XDB::fetchOneCell('SELECT id |
| 273 | FROM profile_education_field_enum |
| 274 | WHERE field = {?}', 'Droit'); |
| 275 | // FIXME: Replace 0 by -1 in following queries when profile_education will be filled with fieldids |
| 276 | $tests['edufield'] = array( |
| 277 | array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid) |
| 278 | LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id) |
| 279 | WHERE pefe.field = {?}', 'Informatique'), |
| 280 | new UFC_EducationField($id_FieldInfo), 0), // FIXME: should be -1 |
| 281 | array(self::buildProfileQuery('INNER JOIN profile_education AS pe ON (pe.pid = p.pid) |
| 282 | LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id) |
| 283 | WHERE pefe.field IN {?}', array('Informatique', 'Droit')), |
| 284 | new UFC_EducationField($id_FieldInfo, $id_FieldDroit), 0), // FIXME: should be -1 |
| 285 | ); |
| 286 | |
| 287 | /* UFC_Name |
| 288 | */ |
| 289 | $id_Lastname = DirEnum::getID(DirEnum::NAMETYPES, Profile::LASTNAME); |
| 290 | $id_Firstname = DirEnum::getID(DirEnum::NAMETYPES, Profile::FIRSTNAME); |
| 291 | $id_Nickname = DirEnum::getID(DirEnum::NAMETYPES, Profile::NICKNAME); |
| 292 | $id_Lastname_Marital = DirEnum::getID(DirEnum::NAMETYPES, Profile::LASTNAME . '_' . Profile::VN_MARITAL); |
| 293 | $id_Lastname_Ordinary = DirEnum::getID(DirEnum::NAMETYPES, Profile::LASTNAME . '_' . Profile::VN_ORDINARY); |
| 294 | |
| 295 | $tests['name'] = array( |
| 296 | // Lastname |
| 297 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (pn.pid = p.pid) |
| 298 | WHERE pn.name LIKE {?} AND pn.typeid = {?}', 'BARROIS', $id_Lastname), |
| 299 | new UFC_Name(Profile::LASTNAME, 'BARROIS', UFC_Name::EXACT), -1), |
| 300 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (pn.pid = p.pid) |
| 301 | WHERE pn.name LIKE \'BARR%\' AND pn.typeid = {?}', $id_Lastname), |
| 302 | new UFC_Name(Profile::LASTNAME, 'BARR', UFC_Name::PREFIX), -1), |
| 303 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (pn.pid = p.pid) |
| 304 | WHERE pn.name LIKE \'%OIS\' AND pn.typeid = {?}', $id_Lastname), |
| 305 | new UFC_Name(Profile::LASTNAME, 'OIS', UFC_Name::SUFFIX), -1), |
| 306 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 307 | WHERE pn.name LIKE \'%ARRO%\' AND pn.typeid = {?}', $id_Lastname), |
| 308 | new UFC_Name(Profile::LASTNAME, 'ARRO', UFC_Name::CONTAINS), -1), |
| 309 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 310 | WHERE pn.name LIKE \'%ZZZZZZ%\' AND pn.typeid = {?}', $id_Lastname), |
| 311 | new UFC_Name(Profile::LASTNAME, 'ZZZZZZ', UFC_Name::CONTAINS), 0), |
| 312 | |
| 313 | // Firstname |
| 314 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 315 | WHERE pn.name LIKE {?} AND pn.typeid = {?}', 'Raphaël', $id_Firstname), |
| 316 | new UFC_Name(Profile::FIRSTNAME, 'Raphaël', UFC_Name::EXACT), -1), |
| 317 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 318 | WHERE pn.name LIKE \'Raph%\' AND pn.typeid = {?}', $id_Firstname), |
| 319 | new UFC_Name(Profile::FIRSTNAME, 'Raph', UFC_Name::PREFIX), -1), |
| 320 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 321 | WHERE pn.name LIKE \'%aël\' AND pn.typeid = {?}', $id_Firstname), |
| 322 | new UFC_Name(Profile::FIRSTNAME, 'aël', UFC_Name::SUFFIX), -1), |
| 323 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 324 | WHERE pn.name LIKE \'%apha%\' AND pn.typeid = {?}', $id_Firstname), |
| 325 | new UFC_Name(Profile::FIRSTNAME, 'apha', UFC_Name::CONTAINS), -1), |
| 326 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 327 | WHERE pn.name LIKE \'%zzzzzz%\' AND pn.typeid = {?}', $id_Firstname), |
| 328 | new UFC_Name(Profile::FIRSTNAME, 'zzzzzz', UFC_Name::CONTAINS), 0), |
| 329 | |
| 330 | // Nickname |
| 331 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 332 | WHERE pn.name LIKE {?} AND pn.typeid = {?}', 'Xelnor', $id_Nickname), |
| 333 | new UFC_Name(Profile::NICKNAME, 'Xelnor', UFC_Name::EXACT), -1), |
| 334 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 335 | WHERE pn.name LIKE \'Xel%\' AND pn.typeid = {?}', $id_Nickname), |
| 336 | new UFC_Name(Profile::NICKNAME, 'Xel', UFC_Name::PREFIX), -1), |
| 337 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 338 | WHERE pn.name LIKE \'%nor\' AND pn.typeid = {?}', $id_Nickname), |
| 339 | new UFC_Name(Profile::NICKNAME, 'nor', UFC_Name::SUFFIX), -1), |
| 340 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 341 | WHERE pn.name LIKE \'%lno%\' AND pn.typeid = {?}', $id_Nickname), |
| 342 | new UFC_Name(Profile::NICKNAME, 'lno', UFC_Name::CONTAINS), -1), |
| 343 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 344 | WHERE pn.name LIKE \'%zzzzzz%\' AND pn.typeid = {?}', $id_Nickname), |
| 345 | new UFC_Name(Profile::NICKNAME, 'zzzzzz', UFC_Name::CONTAINS), 0), |
| 346 | |
| 347 | // Lastname + particle |
| 348 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 349 | WHERE CONCAT(pn.particle, \' \', pn.name) LIKE {?} AND pn.typeid = {?}', 'DE SINGLY', $id_Lastname), |
| 350 | new UFC_Name(Profile::LASTNAME, 'DE SINGLY', UFC_Name::PARTICLE | UFC_Name::EXACT), -1), |
| 351 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 352 | WHERE CONCAT(pn.particle, \' \', pn.name) LIKE \'DE SI%\' AND pn.typeid = {?}', $id_Lastname), |
| 353 | new UFC_Name(Profile::LASTNAME, 'DE SI', UFC_Name::PARTICLE | UFC_Name::PREFIX), -1), |
| 354 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 355 | WHERE CONCAT(pn.particle, \' \', pn.name) LIKE \'%GLY\' AND pn.typeid = {?}', $id_Lastname), |
| 356 | new UFC_Name(Profile::LASTNAME, 'GLY', UFC_NAME::PARTICLE | UFC_Name::SUFFIX), -1), |
| 357 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 358 | WHERE CONCAT(pn.particle, \' \', pn.name) LIKE \'%E SIN%\' AND pn.typeid = {?}', $id_Lastname), |
| 359 | new UFC_Name(Profile::LASTNAME, 'E SIN', UFC_Name::PARTICLE | UFC_Name::CONTAINS), -1), |
| 360 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 361 | WHERE CONCAT(pn.particle, \' \', pn.name) LIKE \'%ZZZZZZ%\' AND pn.typeid = {?}', $id_Lastname), |
| 362 | new UFC_Name(Profile::LASTNAME, 'ZZZZZZ', UFC_Name::PARTICLE | UFC_Name::CONTAINS), 0), |
| 363 | |
| 364 | // Lastname_ordinary |
| 365 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 366 | WHERE pn.name LIKE {?} AND pn.typeid IN {?}', |
| 367 | 'ALBIZZATI', array($id_Lastname, $id_Lastname_Marital, $id_Lastname_Ordinary)), |
| 368 | new UFC_Name(Profile::LASTNAME, 'ALBIZZATI', UFC_Name::VARIANTS | UFC_Name::EXACT), -1), |
| 369 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 370 | WHERE pn.name LIKE {?} AND pn.typeid IN {?}', |
| 371 | 'ALBIZ%', array($id_Lastname, $id_Lastname_Marital, $id_Lastname_Ordinary)), |
| 372 | new UFC_Name(Profile::LASTNAME, 'ALBIZ', UFC_Name::VARIANTS | UFC_Name::PREFIX), -1), |
| 373 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 374 | WHERE pn.name LIKE {?} AND pn.typeid IN {?}', |
| 375 | '%ZATI', array($id_Lastname, $id_Lastname_Marital, $id_Lastname_Ordinary)), |
| 376 | new UFC_Name(Profile::LASTNAME, 'ZATI', UFC_NAME::VARIANTS | UFC_Name::SUFFIX), -1), |
| 377 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 378 | WHERE pn.name LIKE {?} AND pn.typeid IN {?}', |
| 379 | '%BIZZ%', array($id_Lastname, $id_Lastname_Marital, $id_Lastname_Ordinary)), |
| 380 | new UFC_Name(Profile::LASTNAME, 'BIZZ', UFC_Name::VARIANTS | UFC_Name::CONTAINS), -1), |
| 381 | array(self::buildProfileQuery('LEFT JOIN profile_name AS pn ON (p.pid = pn.pid) |
| 382 | WHERE pn.name LIKE {?} AND pn.typeid IN {?}', |
| 383 | '%ZZZZZZ%', array($id_Lastname, $id_Lastname_Marital, $id_Lastname_Ordinary)), |
| 384 | new UFC_Name(Profile::LASTNAME, 'ZZZZZZ', UFC_Name::VARIANTS | UFC_Name::CONTAINS), 0), |
| 385 | ); |
| 386 | |
| 387 | /* UFC_NameTokens |
| 388 | */ |
| 389 | $tests['nametoken'] = array( |
| 390 | // !soundex, !exact |
| 391 | array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid) |
| 392 | WHERE sn.token LIKE \'xelnor%\''), |
| 393 | new UFC_NameTokens('xelnor'), 1), |
| 394 | array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid) |
| 395 | WHERE sn.token LIKE \'xe%\''), |
| 396 | new UFC_NameTokens('xe'), -1), |
| 397 | array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid) |
| 398 | WHERE sn.token LIKE \'xe%\' OR sn.token LIKE \'barr%\''), |
| 399 | new UFC_NameTokens(array('xe', 'barr')), -1), |
| 400 | array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid) |
| 401 | WHERE sn.token LIKE \'zzzzzzzz%\''), |
| 402 | new UFC_NameTokens('zzzzzzzz'), 0), |
| 403 | array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid) |
| 404 | WHERE sn.token LIKE \'barr%\' AND FIND_IN_SET(\'public\', sn.flags)'), |
| 405 | new UFC_NameTokens('barr', UFC_NameTokens::FLAG_PUBLIC), -1), |
| 406 | |
| 407 | // !soundex, exact |
| 408 | array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid) |
| 409 | WHERE sn.token = \'xelnor\''), |
| 410 | new UFC_NameTokens('xelnor', array(), false, true), 1), |
| 411 | array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid) |
| 412 | WHERE sn.token IN (\'xelnor\', \'barrois\')'), |
| 413 | new UFC_NameTokens(array('xelnor', 'barrois'), array(), false, true), -1), |
| 414 | array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid) |
| 415 | WHERE sn.token = \'zzzzzzzz\''), |
| 416 | new UFC_NameTokens('zzzzzzzz', array(), false, true), 0), |
| 417 | array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid) |
| 418 | WHERE sn.token IN (\'zzzzzzzz\', \'yyyyyyyy\')'), |
| 419 | new UFC_NameTokens(array('zzzzzzzz', 'yyyyyyyy'), array(), false, true), 0), |
| 420 | array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid) |
| 421 | WHERE sn.token = \'barrois\' AND FIND_IN_SET(\'public\', sn.flags)'), |
| 422 | new UFC_NameTokens('barrois', UFC_NameTokens::FLAG_PUBLIC, false, true), -1), |
| 423 | |
| 424 | // soundex, !exact |
| 425 | array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid) |
| 426 | WHERE sn.soundex = \'XLNO\''), |
| 427 | new UFC_NameTokens('XLNO', array(), true), -1), |
| 428 | array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid) |
| 429 | WHERE sn.soundex IN (\'XLNO\', \'BROS\')'), |
| 430 | new UFC_NameTokens(array('XLNO', 'BROS'), array(), true), -1), |
| 431 | array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid) |
| 432 | WHERE sn.soundex = \'ZZZZZZ\''), |
| 433 | new UFC_NameTokens('ZZZZZZ', array(), true), 0), |
| 434 | array(self::buildProfileQuery('LEFT JOIN search_name AS sn ON (p.pid = sn.pid) |
| 435 | WHERE sn.soundex = \'BROS\' AND FIND_IN_SET(\'public\', sn.flags)'), |
| 436 | new UFC_NameTokens('BROS', UFC_NameTokens::FLAG_PUBLIC, true), -1), |
| 437 | ); |
| 438 | |
| 439 | /* UFC_Nationality |
| 440 | */ |
| 441 | $tests['nationality'] = array( |
| 442 | array(self::buildProfileQuery('WHERE p.nationality1 IN {?} OR p.nationality2 IN {?} OR p.nationality3 IN {?}', array('BR'), array('BR'), array('BR')), |
| 443 | new UFC_Nationality('BR'), -1), |
| 444 | array(self::buildProfileQuery('WHERE p.nationality1 IN {?} OR p.nationality2 IN {?} OR p.nationality3 IN {?}', array('BR', 'US'), array('BR', 'US'), array('BR', 'US')), |
| 445 | new UFC_Nationality('BR', 'US'), -1), |
| 446 | array(self::buildProfileQuery('WHERE p.nationality1 IN {?} OR p.nationality2 IN {?} OR p.nationality3 IN {?}', array('__'), array('__'), array('__')), |
| 447 | new UFC_Nationality('__'), 0), |
| 448 | ); |
| 449 | |
| 450 | /* UFC_Dead |
| 451 | */ |
| 452 | $tests['dead'] = array( |
| 453 | array(self::buildProfileQuery('WHERE p.deathdate IS NOT NULL'), |
| 454 | new UFC_Dead(), -1), |
| 455 | array(self::buildProfileQuery('WHERE p.deathdate IS NOT NULL AND p.deathdate > {?}', '2008-01-01'), |
| 456 | new UFC_Dead('>', '2008-01-01'), -1), |
| 457 | array(self::buildProfileQuery('WHERE p.deathdate IS NOT NULL AND p.deathdate < {?}', '1600-01-01'), |
| 458 | new UFC_Dead('<', '1600-01-01'), 0), |
| 459 | array(self::buildProfileQuery('WHERE p.deathdate IS NOT NULL AND p.deathdate > {?}', date('Y-m-d')), |
| 460 | new UFC_Dead('>', 'now'), 0), |
| 461 | ); |
| 462 | |
| 463 | /* UFC_Registered |
| 464 | */ |
| 465 | $tests['register'] = array( |
| 466 | array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state = \'active\''), |
| 467 | new UFC_Registered(true), -1), |
| 468 | array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state != \'pending\''), |
| 469 | new UFC_Registered(), -1), |
| 470 | array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state = \'active\' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date > {?}', '2008-01-01'), |
| 471 | new UFC_Registered(true, '>', '2008-01-01'), -1), |
| 472 | array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state != \'pending\' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date > {?}', '2008-01-01'), |
| 473 | new UFC_Registered(false, '>', '2008-01-01'), -1), |
| 474 | array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state = \'active\' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date < {?}', '1700-01-01'), |
| 475 | new UFC_Registered(true, '<', '1700-01-01'), 0), |
| 476 | array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state != \'pending\' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date < {?}', '1700-01-01'), |
| 477 | new UFC_Registered(false, '<', '1700-01-01'), 0), |
| 478 | array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state = \'active\' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date > {?}', date('Y-m-d')), |
| 479 | new UFC_Registered(true, '>', 'now'), 0), |
| 480 | array(self::buildAccountQuery('WHERE a.uid IS NOT NULL AND a.state != \'pending\' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date > {?}', date('Y-m-d')), |
| 481 | new UFC_Registered(false, '>', 'now'), 0), |
| 482 | ); |
| 483 | |
| 484 | $testcases = array(); |
| 485 | foreach ($tests as $name => $t) { |
| 486 | foreach ($t as $id => $case) { |
| 487 | $testcases[$name . '-' . $id] = $case; |
| 488 | } |
| 489 | } |
| 490 | return $testcases; |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * @dataProvider simpleUserProvider |
| 495 | */ |
| 496 | public function testSimpleUser($query, $cond, $expcount = null) |
| 497 | { |
| 498 | /* |
| 499 | * @param $query A pair MySQL query (one for user selector, one for profile selector) |
| 500 | * @param $cond The UFC to test |
| 501 | * @param $expcount The expected number of results (-1 if that number is unknown) |
| 502 | */ |
| 503 | |
| 504 | $query = $query[0]; |
| 505 | |
| 506 | self::checkPlatal(); |
| 507 | |
| 508 | $query = XDB::query($query); |
| 509 | $count = $query->numRows(); |
| 510 | if (!is_null($expcount)) { |
| 511 | if ($expcount < 0) { |
| 512 | $this->assertNotEquals(0, $count); |
| 513 | } else { |
| 514 | $this->assertEquals($expcount, $count); |
| 515 | } |
| 516 | } |
| 517 | $ids = $query->fetchColumn(); |
| 518 | $this->assertEquals($count, count($ids)); |
| 519 | sort($ids); |
| 520 | |
| 521 | $uf = new UserFilter($cond); |
| 522 | $this->assertEquals($count, $uf->getTotalUserCount()); |
| 523 | $got = $uf->getUIDs(); |
| 524 | $this->assertEquals($count, count($got)); |
| 525 | sort($got); |
| 526 | $this->assertEquals($ids, $got); |
| 527 | |
| 528 | $uf = new UserFilter($cond); |
| 529 | $got = $uf->getUIDs(); |
| 530 | $this->assertEquals($count, count($got)); |
| 531 | sort($got); |
| 532 | $this->assertEquals($ids, $got); |
| 533 | $this->assertEquals($count, $uf->getTotalUserCount()); |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * @dataProvider simpleUserProvider |
| 538 | */ |
| 539 | public function testSimpleProfile($query, $cond, $expcount = null) |
| 540 | { |
| 541 | /* |
| 542 | * @param $query A pair MySQL query (one for user selector, one for profile selector) |
| 543 | * @param $cond The UFC to test |
| 544 | * @param $expcount The expected number of results (-1 if that number is unknown) |
| 545 | */ |
| 546 | |
| 547 | $query = $query[1]; |
| 548 | |
| 549 | self::checkPlatal(); |
| 550 | |
| 551 | $query = XDB::query($query); |
| 552 | $count = $query->numRows(); |
| 553 | if (!is_null($expcount)) { |
| 554 | if ($expcount < 0) { |
| 555 | $this->assertNotEquals(0, $count); |
| 556 | } else { |
| 557 | $this->assertEquals($expcount, $count); |
| 558 | } |
| 559 | } |
| 560 | $ids = $query->fetchColumn(); |
| 561 | $this->assertEquals($count, count($ids)); |
| 562 | sort($ids); |
| 563 | |
| 564 | $uf = new UserFilter($cond); |
| 565 | $this->assertEquals($count, $uf->getTotalProfileCount()); |
| 566 | $got = $uf->getPIDs(); |
| 567 | $this->assertEquals($count, count($got)); |
| 568 | sort($got); |
| 569 | $this->assertEquals($ids, $got); |
| 570 | |
| 571 | $uf = new UserFilter($cond); |
| 572 | $got = $uf->getPIDs(); |
| 573 | $this->assertEquals($count, count($got)); |
| 574 | sort($got); |
| 575 | $this->assertEquals($ids, $got); |
| 576 | $this->assertEquals($count, $uf->getTotalProfileCount()); |
| 577 | } |
| 578 | |
| 579 | |
| 580 | public static function sortProvider() |
| 581 | { |
| 582 | return array( |
| 583 | array(self::buildAccountQuery('ORDER BY a.uid'), new UFO_Uid()), |
| 584 | array(self::buildAccountQuery('ORDER BY a.hruid'), new UFO_Hruid()), |
| 585 | array(self::buildAccountQuery('ORDER BY a.uid DESC'), new UFO_Uid(true)), |
| 586 | array(self::buildAccountQuery('ORDER BY a.hruid DESC'), new UFO_Hruid(true)), |
| 587 | array(self::buildProfileQuery('ORDER BY p.pid'), new UFO_Pid()), |
| 588 | array(self::buildProfileQuery('ORDER BY p.hrpid'), new UFO_Hrpid()), |
| 589 | array(self::buildProfileQuery('ORDER BY p.pid DESC'), new UFO_Pid(true)), |
| 590 | array(self::buildProfileQuery('ORDER BY p.hrpid DESC'), new UFO_Hrpid(true)), |
| 591 | array(self::buildProfileQuery('WHERE p.deathdate IS NOT NULL |
| 592 | ORDER BY p.deathdate, p.pid'), |
| 593 | array(new UFO_Death(), new UFO_Pid()), new UFC_Dead()), |
| 594 | array(self::buildProfileQuery('WHERE p.deathdate IS NOT NULL |
| 595 | ORDER BY p.deathdate DESC, p.pid'), |
| 596 | array(new UFO_Death(true), new UFO_Pid()), new UFC_Dead()), |
| 597 | array(self::buildProfileQuery('ORDER BY p.next_birthday, p.pid'), |
| 598 | array(new UFO_Birthday(), new UFO_Pid())), |
| 599 | array(self::buildProfileQuery('ORDER BY p.next_birthday DESC, p.pid'), |
| 600 | array(new UFO_Birthday(true), new UFO_Pid())), |
| 601 | array(self::buildProfileQuery('ORDER BY p.last_change, p.pid'), |
| 602 | array(new UFO_ProfileUpdate(), new UFO_Pid())), |
| 603 | array(self::buildProfileQuery('ORDER BY p.last_change DESC, p.pid'), |
| 604 | array(new UFO_ProfileUpdate(true), new UFO_Pid())), |
| 605 | array(self::buildAccountQuery('ORDER BY a.registration_date, a.uid'), |
| 606 | array(new UFO_Registration(), new UFO_Uid())), |
| 607 | array(self::buildAccountQuery('ORDER BY a.registration_date DESC, a.uid'), |
| 608 | array(new UFO_Registration(true), new UFO_Uid())), |
| 609 | ); |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * @dataProvider sortProvider |
| 614 | */ |
| 615 | public function testUserSortAndLimits($query, $sort, $cond = null) |
| 616 | { |
| 617 | self::checkPlatal(); |
| 618 | |
| 619 | $query = XDB::query($query[0]); |
| 620 | $count = $query->numRows(); |
| 621 | $ids = $query->fetchColumn(); |
| 622 | $this->assertSame($count, count($ids)); |
| 623 | |
| 624 | if ($cond == null ) { |
| 625 | $cond = new PFC_True(); |
| 626 | } |
| 627 | $uf = new UserFilter($cond, $sort); |
| 628 | for ($i = 0 ; $i < $count ; $i += 7987) { |
| 629 | $got = $uf->getUIDs(new PlLimit(100, $i)); |
| 630 | $this->assertSame($count, $uf->getTotalUserCount()); |
| 631 | $part = array_slice($ids, $i, 100); |
| 632 | $this->assertSame($part, $got); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * @dataProvider sortProvider |
| 638 | */ |
| 639 | public function testProfileSortAndLimits($query, $sort, $cond = null) |
| 640 | { |
| 641 | self::checkPlatal(); |
| 642 | |
| 643 | $query = XDB::query($query[1]); |
| 644 | $count = $query->numRows(); |
| 645 | $ids = $query->fetchColumn(); |
| 646 | $this->assertSame($count, count($ids)); |
| 647 | |
| 648 | if ($cond == null ) { |
| 649 | $cond = new PFC_True(); |
| 650 | } |
| 651 | $uf = new UserFilter($cond, $sort); |
| 652 | for ($i = 0 ; $i < $count ; $i += 7987) { |
| 653 | $got = $uf->getPIDs(new PlLimit(100, $i)); |
| 654 | $this->assertSame($count, $uf->getTotalProfileCount()); |
| 655 | $part = array_slice($ids, $i, 100); |
| 656 | $this->assertSame($part, $got); |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 662 | ?> |