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