xref: /dokuwiki/lib/plugins/authpdo/_test/sqlite.test.php (revision 6459f496c8689d7960a4c338677ae020055d1038)
1f64dbc90SAndreas Gohr<?php
2f64dbc90SAndreas Gohr
34fb8dfabSAndreas Gohr/**
44fb8dfabSAndreas Gohr * Class testable_auth_plugin_authpdo
54fb8dfabSAndreas Gohr *
64fb8dfabSAndreas Gohr * makes protected methods public for testing
74fb8dfabSAndreas Gohr */
85de3a6a5SAndreas Gohrclass testable_auth_plugin_authpdo extends auth_plugin_authpdo {
95de3a6a5SAndreas Gohr    public function getPluginName() {
105de3a6a5SAndreas Gohr        return 'authpdo';
115de3a6a5SAndreas Gohr    }
125de3a6a5SAndreas Gohr
135de3a6a5SAndreas Gohr    public function _selectGroups() {
145de3a6a5SAndreas Gohr        return parent::_selectGroups();
155de3a6a5SAndreas Gohr    }
165de3a6a5SAndreas Gohr
17*6459f496SAndreas Gohr    public function addGroup($group) {
18*6459f496SAndreas Gohr        return parent::addGroup($group);
195de3a6a5SAndreas Gohr    }
205de3a6a5SAndreas Gohr}
215de3a6a5SAndreas Gohr
22f64dbc90SAndreas Gohr/**
23f64dbc90SAndreas Gohr * General tests for the authpdo plugin
24f64dbc90SAndreas Gohr *
25f64dbc90SAndreas Gohr * @group plugin_authpdo
26f64dbc90SAndreas Gohr * @group plugins
27f64dbc90SAndreas Gohr */
28f64dbc90SAndreas Gohrclass sqlite_plugin_authpdo_test extends DokuWikiTest {
29f64dbc90SAndreas Gohr
30f64dbc90SAndreas Gohr    protected $dbfile;
31f64dbc90SAndreas Gohr
32f64dbc90SAndreas Gohr    public function setUp() {
33f64dbc90SAndreas Gohr        parent::setUp();
34f64dbc90SAndreas Gohr        $this->dbfile = tempnam('/tmp/', 'pluginpdo_test_');
35f64dbc90SAndreas Gohr        copy(__DIR__ . '/test.sqlite3', $this->dbfile);
36f64dbc90SAndreas Gohr
37f64dbc90SAndreas Gohr        global $conf;
38f64dbc90SAndreas Gohr
39f64dbc90SAndreas Gohr        $conf['plugin']['authpdo']['debug'] = 1;
40f64dbc90SAndreas Gohr        $conf['plugin']['authpdo']['dsn'] = 'sqlite:' . $this->dbfile;
41f64dbc90SAndreas Gohr        $conf['plugin']['authpdo']['user'] = '';
42f64dbc90SAndreas Gohr        $conf['plugin']['authpdo']['pass'] = '';
43f64dbc90SAndreas Gohr
445de3a6a5SAndreas Gohr        $conf['plugin']['authpdo']['select-user'] = 'SELECT id AS uid, login AS user, name, pass AS clear, mail FROM user WHERE login = :user';
4570a89417SAndreas Gohr        $conf['plugin']['authpdo']['select-user-groups'] = 'SELECT * FROM member AS m, "group" AS g  WHERE m.gid = g.id AND  m.uid = :uid';
465de3a6a5SAndreas Gohr        $conf['plugin']['authpdo']['select-groups'] = 'SELECT id AS gid, "group" FROM "group"';
47e19be516SAndreas Gohr
485de3a6a5SAndreas Gohr        $conf['plugin']['authpdo']['insert-user'] = 'INSERT INTO user (login, pass, name, mail) VALUES (:user, :hash, :name, :mail)';
49e19be516SAndreas Gohr        $conf['plugin']['authpdo']['delete-user'] = 'DELETE FROM user WHERE id = :uid';
504fb8dfabSAndreas Gohr
51*6459f496SAndreas Gohr        $conf['plugin']['authpdo']['list-users'] = 'SELECT DISTINCT login as user
52*6459f496SAndreas Gohr                                                      FROM user U, member M, "group" G
53*6459f496SAndreas Gohr                                                     WHERE U.id = M.uid
54*6459f496SAndreas Gohr                                                       AND M.gid = G.id
55*6459f496SAndreas Gohr                                                       AND G."group" LIKE :group
56*6459f496SAndreas Gohr                                                       AND U.login LIKE :user
57*6459f496SAndreas Gohr                                                       AND U.name LIKE :name
58*6459f496SAndreas Gohr                                                       AND U.mail LIKE :mail
59*6459f496SAndreas Gohr                                                  ORDER BY login
60*6459f496SAndreas Gohr                                                     LIMIT :start,:limit';
61*6459f496SAndreas Gohr
62*6459f496SAndreas Gohr        $conf['plugin']['authpdo']['count-users'] = 'SELECT COUNT(DISTINCT login) as count
63*6459f496SAndreas Gohr                                                      FROM user U, member M, "group" G
64*6459f496SAndreas Gohr                                                     WHERE U.id = M.uid
65*6459f496SAndreas Gohr                                                       AND M.gid = G.id
66*6459f496SAndreas Gohr                                                       AND G."group" LIKE :group
67*6459f496SAndreas Gohr                                                       AND U.login LIKE :user
68*6459f496SAndreas Gohr                                                       AND U.name LIKE :name
69*6459f496SAndreas Gohr                                                       AND U.mail LIKE :mail';
70*6459f496SAndreas Gohr
71*6459f496SAndreas Gohr
724fb8dfabSAndreas Gohr        $conf['plugin']['authpdo']['update-user-login'] = 'UPDATE user SET login = :newlogin WHERE id = :uid';
734fb8dfabSAndreas Gohr        $conf['plugin']['authpdo']['update-user-info'] = 'UPDATE user SET name = :name, mail = :mail WHERE id = :uid';
744fb8dfabSAndreas Gohr        $conf['plugin']['authpdo']['update-user-pass'] = 'UPDATE user SET pass = :hash WHERE id = :uid';
754fb8dfabSAndreas Gohr
765de3a6a5SAndreas Gohr        $conf['plugin']['authpdo']['insert-group'] = 'INSERT INTO "group" ("group") VALUES (:group)';
775de3a6a5SAndreas Gohr        $conf['plugin']['authpdo']['join-group'] = 'INSERT INTO member (uid, gid) VALUES (:uid, :gid)';
784fb8dfabSAndreas Gohr        $conf['plugin']['authpdo']['leave-group'] = 'DELETE FROM member WHERE uid = :uid AND gid = :gid';
79f64dbc90SAndreas Gohr    }
80f64dbc90SAndreas Gohr
81f64dbc90SAndreas Gohr    public function tearDown() {
82f64dbc90SAndreas Gohr        parent::tearDown();
83f64dbc90SAndreas Gohr        unlink($this->dbfile);
84f64dbc90SAndreas Gohr    }
85f64dbc90SAndreas Gohr
865de3a6a5SAndreas Gohr    public function test_internals() {
875de3a6a5SAndreas Gohr        $auth = new testable_auth_plugin_authpdo();
885de3a6a5SAndreas Gohr
895de3a6a5SAndreas Gohr        $groups = $auth->_selectGroups();
905de3a6a5SAndreas Gohr        $this->assertArrayHasKey('user', $groups);
915de3a6a5SAndreas Gohr        $this->assertEquals(1, $groups['user']['gid']);
925de3a6a5SAndreas Gohr        $this->assertArrayHasKey('admin', $groups);
935de3a6a5SAndreas Gohr        $this->assertEquals(2, $groups['admin']['gid']);
945de3a6a5SAndreas Gohr
95*6459f496SAndreas Gohr        $ok = $auth->addGroup('test');
965de3a6a5SAndreas Gohr        $this->assertTrue($ok);
975de3a6a5SAndreas Gohr        $groups = $auth->_selectGroups();
985de3a6a5SAndreas Gohr        $this->assertArrayHasKey('test', $groups);
995de3a6a5SAndreas Gohr        $this->assertEquals(3, $groups['test']['gid']);
1005de3a6a5SAndreas Gohr    }
1015de3a6a5SAndreas Gohr
102f64dbc90SAndreas Gohr    public function test_userinfo() {
103f64dbc90SAndreas Gohr        global $conf;
104f64dbc90SAndreas Gohr        $auth = new auth_plugin_authpdo();
105f64dbc90SAndreas Gohr
106f64dbc90SAndreas Gohr        // clear text pasword (with default config above
107f64dbc90SAndreas Gohr        $this->assertFalse($auth->checkPass('nobody', 'nope'));
108f64dbc90SAndreas Gohr        $this->assertFalse($auth->checkPass('admin', 'nope'));
109f64dbc90SAndreas Gohr        $this->assertTrue($auth->checkPass('admin', 'password'));
110f64dbc90SAndreas Gohr
111f64dbc90SAndreas Gohr        // now with a hashed password
1125de3a6a5SAndreas Gohr        $conf['plugin']['authpdo']['select-user'] = 'SELECT id AS uid, login AS user, name, pass AS hash, mail FROM user WHERE login = :user';
113f64dbc90SAndreas Gohr        $this->assertFalse($auth->checkPass('admin', 'password'));
114f64dbc90SAndreas Gohr        $this->assertFalse($auth->checkPass('user', md5('password')));
115f64dbc90SAndreas Gohr
11670a89417SAndreas Gohr        // access user data
11770a89417SAndreas Gohr        $info = $auth->getUserData('admin');
11870a89417SAndreas Gohr        $this->assertEquals('admin', $info['user']);
11970a89417SAndreas Gohr        $this->assertEquals('The Admin', $info['name']);
12070a89417SAndreas Gohr        $this->assertEquals('admin@example.com', $info['mail']);
12170a89417SAndreas Gohr        $this->assertEquals(array('admin', 'user'), $info['grps']);
1225de3a6a5SAndreas Gohr
1235de3a6a5SAndreas Gohr        // group retrieval
1245de3a6a5SAndreas Gohr        $this->assertEquals(array('admin', 'user'), $auth->retrieveGroups());
1255de3a6a5SAndreas Gohr        $this->assertEquals(array('user'), $auth->retrieveGroups(1));
1265de3a6a5SAndreas Gohr        $this->assertEquals(array('admin'), $auth->retrieveGroups(0, 1));
1275de3a6a5SAndreas Gohr
1285de3a6a5SAndreas Gohr        // user creation
1295de3a6a5SAndreas Gohr        $auth->createUser('test', 'password', 'A Test user', 'test@example.com', array('newgroup'));
1305de3a6a5SAndreas Gohr        $info = $auth->getUserData('test');
1315de3a6a5SAndreas Gohr        $this->assertEquals('test', $info['user']);
1325de3a6a5SAndreas Gohr        $this->assertEquals('A Test user', $info['name']);
1335de3a6a5SAndreas Gohr        $this->assertEquals('test@example.com', $info['mail']);
1345de3a6a5SAndreas Gohr        $this->assertEquals(array('newgroup', 'user'), $info['grps']);
1355de3a6a5SAndreas Gohr        $this->assertEquals(array('admin', 'newgroup', 'user'), $auth->retrieveGroups());
1364fb8dfabSAndreas Gohr
1374fb8dfabSAndreas Gohr        // user modification
1384fb8dfabSAndreas Gohr        $auth->modifyUser('test', array('user' => 'tester', 'name' => 'The Test User', 'pass' => 'secret'));
1394fb8dfabSAndreas Gohr        $info = $auth->getUserData('tester');
1404fb8dfabSAndreas Gohr        $this->assertEquals('tester', $info['user']);
1414fb8dfabSAndreas Gohr        $this->assertEquals('The Test User', $info['name']);
1424fb8dfabSAndreas Gohr        $this->assertTrue($auth->checkPass('tester','secret'));
1434fb8dfabSAndreas Gohr
1444fb8dfabSAndreas Gohr        // move user to different groups
1454fb8dfabSAndreas Gohr        $auth->modifyUser('tester', array('grps' => array('user', 'admin', 'another')));
1464fb8dfabSAndreas Gohr        $info = $auth->getUserData('tester');
1474fb8dfabSAndreas Gohr        $this->assertEquals(array('admin', 'another', 'user'), $info['grps']);
148e19be516SAndreas Gohr
149*6459f496SAndreas Gohr        // list users
150*6459f496SAndreas Gohr        $users = $auth->retrieveUsers();
151*6459f496SAndreas Gohr        $this->assertEquals(array('admin', 'tester', 'user'), $users);
152*6459f496SAndreas Gohr
153*6459f496SAndreas Gohr        $users = $auth->retrieveUsers(1); // offset
154*6459f496SAndreas Gohr        $this->assertEquals(array('tester', 'user'), $users);
155*6459f496SAndreas Gohr
156*6459f496SAndreas Gohr        $users = $auth->retrieveUsers(1, 1); // offset + limit
157*6459f496SAndreas Gohr        $this->assertEquals(array('tester'), $users);
158*6459f496SAndreas Gohr
159*6459f496SAndreas Gohr        $users = $auth->retrieveUsers(0, -1, array('group' => 'admin')); // full group
160*6459f496SAndreas Gohr        $this->assertEquals(array('admin', 'tester'), $users);
161*6459f496SAndreas Gohr        $count = $auth->getUserCount(array('group' => 'admin'));
162*6459f496SAndreas Gohr        $this->assertEquals(2, $count);
163*6459f496SAndreas Gohr
164*6459f496SAndreas Gohr        $users = $auth->retrieveUsers(0, -1, array('group' => 'dmi')); // substring
165*6459f496SAndreas Gohr        $this->assertEquals(array('admin', 'tester'), $users);
166*6459f496SAndreas Gohr        $count = $auth->getUserCount(array('group' => 'dmi'));
167*6459f496SAndreas Gohr        $this->assertEquals(2, $count);
168*6459f496SAndreas Gohr
169*6459f496SAndreas Gohr        $users = $auth->retrieveUsers(0, -1, array('user' => 'dmi')); // substring
170*6459f496SAndreas Gohr        $this->assertEquals(array('admin'), $users);
171*6459f496SAndreas Gohr        $count = $auth->getUserCount(array('user' => 'dmi'));
172*6459f496SAndreas Gohr        $this->assertEquals(1, $count);
173*6459f496SAndreas Gohr
174e19be516SAndreas Gohr        // delete user
175e19be516SAndreas Gohr        $num = $auth->deleteUsers(array('tester', 'foobar'));
176e19be516SAndreas Gohr        $this->assertEquals(1, $num);
177*6459f496SAndreas Gohr
178f64dbc90SAndreas Gohr    }
1795de3a6a5SAndreas Gohr
180f64dbc90SAndreas Gohr}
181