b4fbd794714199605da10cfac4a32082b1baef70
[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 );
91 }
92
93 /**
94 * @dataProvider simpleUserProvider
95 * @param $query A MySQL query
96 * @param $cond The UFC to test
97 * @param $expcount The expected number of results (-1 if that number is unknown)
98 */
99 public function testSimpleUser($query, $cond, $expcount = null)
100 {
101 global $globals, $platal;
102 $platal = new Xorg();
103
104 $query = XDB::query($query);
105 $count = $query->numRows();
106 if (!is_null($expcount)) {
107 if ($expcount < 0) {
108 $this->assertNotEquals(0, $count);
109 } else {
110 $this->assertEquals($expcount, $count);
111 }
112 }
113 $ids = $query->fetchColumn();
114 $this->assertEquals($count, count($ids));
115 sort($ids);
116
117 $uf = new UserFilter($cond);
118 $this->assertEquals($count, $uf->getTotalUserCount());
119 $got = $uf->getUIDs();
120 $this->assertEquals($count, count($got));
121 sort($got);
122 $this->assertEquals($ids, $got);
123
124 $uf = new UserFilter($cond);
125 $got = $uf->getUIDs();
126 $this->assertEquals($count, count($got));
127 sort($got);
128 $this->assertEquals($ids, $got);
129 $this->assertEquals($count, $uf->getTotalCount());
130 }
131 }
132
133 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
134 ?>