xref: /dokuwiki/lib/plugins/authpdo/_test/sqlite.test.php (revision e19be5160bbe04352d6ae60d2294855d246d0dde)
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 _insertGroup($group) {
18        return parent::_insertGroup($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']['update-user-login'] = 'UPDATE user SET login = :newlogin WHERE id = :uid';
52        $conf['plugin']['authpdo']['update-user-info'] = 'UPDATE user SET name = :name, mail = :mail WHERE id = :uid';
53        $conf['plugin']['authpdo']['update-user-pass'] = 'UPDATE user SET pass = :hash WHERE id = :uid';
54
55        $conf['plugin']['authpdo']['insert-group'] = 'INSERT INTO "group" ("group") VALUES (:group)';
56        $conf['plugin']['authpdo']['join-group'] = 'INSERT INTO member (uid, gid) VALUES (:uid, :gid)';
57        $conf['plugin']['authpdo']['leave-group'] = 'DELETE FROM member WHERE uid = :uid AND gid = :gid';
58    }
59
60    public function tearDown() {
61        parent::tearDown();
62        unlink($this->dbfile);
63    }
64
65    public function test_internals() {
66        $auth = new testable_auth_plugin_authpdo();
67
68        $groups = $auth->_selectGroups();
69        $this->assertArrayHasKey('user', $groups);
70        $this->assertEquals(1, $groups['user']['gid']);
71        $this->assertArrayHasKey('admin', $groups);
72        $this->assertEquals(2, $groups['admin']['gid']);
73
74        $ok = $auth->_insertGroup('test');
75        $this->assertTrue($ok);
76        $groups = $auth->_selectGroups();
77        $this->assertArrayHasKey('test', $groups);
78        $this->assertEquals(3, $groups['test']['gid']);
79    }
80
81    public function test_userinfo() {
82        global $conf;
83        $auth = new auth_plugin_authpdo();
84
85        // clear text pasword (with default config above
86        $this->assertFalse($auth->checkPass('nobody', 'nope'));
87        $this->assertFalse($auth->checkPass('admin', 'nope'));
88        $this->assertTrue($auth->checkPass('admin', 'password'));
89
90        // now with a hashed password
91        $conf['plugin']['authpdo']['select-user'] = 'SELECT id AS uid, login AS user, name, pass AS hash, mail FROM user WHERE login = :user';
92        $this->assertFalse($auth->checkPass('admin', 'password'));
93        $this->assertFalse($auth->checkPass('user', md5('password')));
94
95        // access user data
96        $info = $auth->getUserData('admin');
97        $this->assertEquals('admin', $info['user']);
98        $this->assertEquals('The Admin', $info['name']);
99        $this->assertEquals('admin@example.com', $info['mail']);
100        $this->assertEquals(array('admin', 'user'), $info['grps']);
101
102        // group retrieval
103        $this->assertEquals(array('admin', 'user'), $auth->retrieveGroups());
104        $this->assertEquals(array('user'), $auth->retrieveGroups(1));
105        $this->assertEquals(array('admin'), $auth->retrieveGroups(0, 1));
106
107        // user creation
108        $auth->createUser('test', 'password', 'A Test user', 'test@example.com', array('newgroup'));
109        $info = $auth->getUserData('test');
110        $this->assertEquals('test', $info['user']);
111        $this->assertEquals('A Test user', $info['name']);
112        $this->assertEquals('test@example.com', $info['mail']);
113        $this->assertEquals(array('newgroup', 'user'), $info['grps']);
114        $this->assertEquals(array('admin', 'newgroup', 'user'), $auth->retrieveGroups());
115
116        // user modification
117        $auth->modifyUser('test', array('user' => 'tester', 'name' => 'The Test User', 'pass' => 'secret'));
118        $info = $auth->getUserData('tester');
119        $this->assertEquals('tester', $info['user']);
120        $this->assertEquals('The Test User', $info['name']);
121        $this->assertTrue($auth->checkPass('tester','secret'));
122
123        // move user to different groups
124        $auth->modifyUser('tester', array('grps' => array('user', 'admin', 'another')));
125        $info = $auth->getUserData('tester');
126        $this->assertEquals(array('admin', 'another', 'user'), $info['grps']);
127
128        // delete user
129        $num = $auth->deleteUsers(array('tester', 'foobar'));
130        $this->assertEquals(1, $num);
131    }
132
133}
134