xref: /dokuwiki/lib/plugins/authpdo/_test/sqlite.test.php (revision 6459f496c8689d7960a4c338677ae020055d1038)
1<?php
2
3/**
4 * Class testable_auth_plugin_authpdo
5 *
6 * makes protected methods public for testing
7 */
8class testable_auth_plugin_authpdo extends auth_plugin_authpdo {
9    public function getPluginName() {
10        return 'authpdo';
11    }
12
13    public function _selectGroups() {
14        return parent::_selectGroups();
15    }
16
17    public function addGroup($group) {
18        return parent::addGroup($group);
19    }
20}
21
22/**
23 * General tests for the authpdo plugin
24 *
25 * @group plugin_authpdo
26 * @group plugins
27 */
28class sqlite_plugin_authpdo_test extends DokuWikiTest {
29
30    protected $dbfile;
31
32    public function setUp() {
33        parent::setUp();
34        $this->dbfile = tempnam('/tmp/', 'pluginpdo_test_');
35        copy(__DIR__ . '/test.sqlite3', $this->dbfile);
36
37        global $conf;
38
39        $conf['plugin']['authpdo']['debug'] = 1;
40        $conf['plugin']['authpdo']['dsn'] = 'sqlite:' . $this->dbfile;
41        $conf['plugin']['authpdo']['user'] = '';
42        $conf['plugin']['authpdo']['pass'] = '';
43
44        $conf['plugin']['authpdo']['select-user'] = 'SELECT id AS uid, login AS user, name, pass AS clear, mail FROM user WHERE login = :user';
45        $conf['plugin']['authpdo']['select-user-groups'] = 'SELECT * FROM member AS m, "group" AS g  WHERE m.gid = g.id AND  m.uid = :uid';
46        $conf['plugin']['authpdo']['select-groups'] = 'SELECT id AS gid, "group" FROM "group"';
47
48        $conf['plugin']['authpdo']['insert-user'] = 'INSERT INTO user (login, pass, name, mail) VALUES (:user, :hash, :name, :mail)';
49        $conf['plugin']['authpdo']['delete-user'] = 'DELETE FROM user WHERE id = :uid';
50
51        $conf['plugin']['authpdo']['list-users'] = 'SELECT DISTINCT login as user
52                                                      FROM user U, member M, "group" G
53                                                     WHERE U.id = M.uid
54                                                       AND M.gid = G.id
55                                                       AND G."group" LIKE :group
56                                                       AND U.login LIKE :user
57                                                       AND U.name LIKE :name
58                                                       AND U.mail LIKE :mail
59                                                  ORDER BY login
60                                                     LIMIT :start,:limit';
61
62        $conf['plugin']['authpdo']['count-users'] = 'SELECT COUNT(DISTINCT login) as count
63                                                      FROM user U, member M, "group" G
64                                                     WHERE U.id = M.uid
65                                                       AND M.gid = G.id
66                                                       AND G."group" LIKE :group
67                                                       AND U.login LIKE :user
68                                                       AND U.name LIKE :name
69                                                       AND U.mail LIKE :mail';
70
71
72        $conf['plugin']['authpdo']['update-user-login'] = 'UPDATE user SET login = :newlogin WHERE id = :uid';
73        $conf['plugin']['authpdo']['update-user-info'] = 'UPDATE user SET name = :name, mail = :mail WHERE id = :uid';
74        $conf['plugin']['authpdo']['update-user-pass'] = 'UPDATE user SET pass = :hash WHERE id = :uid';
75
76        $conf['plugin']['authpdo']['insert-group'] = 'INSERT INTO "group" ("group") VALUES (:group)';
77        $conf['plugin']['authpdo']['join-group'] = 'INSERT INTO member (uid, gid) VALUES (:uid, :gid)';
78        $conf['plugin']['authpdo']['leave-group'] = 'DELETE FROM member WHERE uid = :uid AND gid = :gid';
79    }
80
81    public function tearDown() {
82        parent::tearDown();
83        unlink($this->dbfile);
84    }
85
86    public function test_internals() {
87        $auth = new testable_auth_plugin_authpdo();
88
89        $groups = $auth->_selectGroups();
90        $this->assertArrayHasKey('user', $groups);
91        $this->assertEquals(1, $groups['user']['gid']);
92        $this->assertArrayHasKey('admin', $groups);
93        $this->assertEquals(2, $groups['admin']['gid']);
94
95        $ok = $auth->addGroup('test');
96        $this->assertTrue($ok);
97        $groups = $auth->_selectGroups();
98        $this->assertArrayHasKey('test', $groups);
99        $this->assertEquals(3, $groups['test']['gid']);
100    }
101
102    public function test_userinfo() {
103        global $conf;
104        $auth = new auth_plugin_authpdo();
105
106        // clear text pasword (with default config above
107        $this->assertFalse($auth->checkPass('nobody', 'nope'));
108        $this->assertFalse($auth->checkPass('admin', 'nope'));
109        $this->assertTrue($auth->checkPass('admin', 'password'));
110
111        // now with a hashed password
112        $conf['plugin']['authpdo']['select-user'] = 'SELECT id AS uid, login AS user, name, pass AS hash, mail FROM user WHERE login = :user';
113        $this->assertFalse($auth->checkPass('admin', 'password'));
114        $this->assertFalse($auth->checkPass('user', md5('password')));
115
116        // access user data
117        $info = $auth->getUserData('admin');
118        $this->assertEquals('admin', $info['user']);
119        $this->assertEquals('The Admin', $info['name']);
120        $this->assertEquals('admin@example.com', $info['mail']);
121        $this->assertEquals(array('admin', 'user'), $info['grps']);
122
123        // group retrieval
124        $this->assertEquals(array('admin', 'user'), $auth->retrieveGroups());
125        $this->assertEquals(array('user'), $auth->retrieveGroups(1));
126        $this->assertEquals(array('admin'), $auth->retrieveGroups(0, 1));
127
128        // user creation
129        $auth->createUser('test', 'password', 'A Test user', 'test@example.com', array('newgroup'));
130        $info = $auth->getUserData('test');
131        $this->assertEquals('test', $info['user']);
132        $this->assertEquals('A Test user', $info['name']);
133        $this->assertEquals('test@example.com', $info['mail']);
134        $this->assertEquals(array('newgroup', 'user'), $info['grps']);
135        $this->assertEquals(array('admin', 'newgroup', 'user'), $auth->retrieveGroups());
136
137        // user modification
138        $auth->modifyUser('test', array('user' => 'tester', 'name' => 'The Test User', 'pass' => 'secret'));
139        $info = $auth->getUserData('tester');
140        $this->assertEquals('tester', $info['user']);
141        $this->assertEquals('The Test User', $info['name']);
142        $this->assertTrue($auth->checkPass('tester','secret'));
143
144        // move user to different groups
145        $auth->modifyUser('tester', array('grps' => array('user', 'admin', 'another')));
146        $info = $auth->getUserData('tester');
147        $this->assertEquals(array('admin', 'another', 'user'), $info['grps']);
148
149        // list users
150        $users = $auth->retrieveUsers();
151        $this->assertEquals(array('admin', 'tester', 'user'), $users);
152
153        $users = $auth->retrieveUsers(1); // offset
154        $this->assertEquals(array('tester', 'user'), $users);
155
156        $users = $auth->retrieveUsers(1, 1); // offset + limit
157        $this->assertEquals(array('tester'), $users);
158
159        $users = $auth->retrieveUsers(0, -1, array('group' => 'admin')); // full group
160        $this->assertEquals(array('admin', 'tester'), $users);
161        $count = $auth->getUserCount(array('group' => 'admin'));
162        $this->assertEquals(2, $count);
163
164        $users = $auth->retrieveUsers(0, -1, array('group' => 'dmi')); // substring
165        $this->assertEquals(array('admin', 'tester'), $users);
166        $count = $auth->getUserCount(array('group' => 'dmi'));
167        $this->assertEquals(2, $count);
168
169        $users = $auth->retrieveUsers(0, -1, array('user' => 'dmi')); // substring
170        $this->assertEquals(array('admin'), $users);
171        $count = $auth->getUserCount(array('user' => 'dmi'));
172        $this->assertEquals(1, $count);
173
174        // delete user
175        $num = $auth->deleteUsers(array('tester', 'foobar'));
176        $this->assertEquals(1, $num);
177
178    }
179
180}
181