xref: /dokuwiki/lib/plugins/authpdo/_test/sqlite.test.php (revision 5de3a6a5ad40cfd5f62c9a028f248d53e3f9da6d)
1<?php
2
3
4class testable_auth_plugin_authpdo extends auth_plugin_authpdo {
5    public function getPluginName() {
6        return 'authpdo';
7    }
8
9    public function _selectGroups() {
10        return parent::_selectGroups();
11    }
12
13    public function _insertGroup($group) {
14        return parent::_insertGroup($group);
15    }
16
17}
18
19/**
20 * General tests for the authpdo plugin
21 *
22 * @group plugin_authpdo
23 * @group plugins
24 */
25class sqlite_plugin_authpdo_test extends DokuWikiTest {
26
27    protected $dbfile;
28
29    public function setUp() {
30        parent::setUp();
31        $this->dbfile = tempnam('/tmp/', 'pluginpdo_test_');
32        copy(__DIR__ . '/test.sqlite3', $this->dbfile);
33
34        global $conf;
35
36        $conf['plugin']['authpdo']['debug'] = 1;
37        $conf['plugin']['authpdo']['dsn'] = 'sqlite:' . $this->dbfile;
38        $conf['plugin']['authpdo']['user'] = '';
39        $conf['plugin']['authpdo']['pass'] = '';
40
41        $conf['plugin']['authpdo']['select-user'] = 'SELECT id AS uid, login AS user, name, pass AS clear, mail FROM user WHERE login = :user';
42        $conf['plugin']['authpdo']['select-user-groups'] = 'SELECT * FROM member AS m, "group" AS g  WHERE m.gid = g.id AND  m.uid = :uid';
43        $conf['plugin']['authpdo']['select-groups'] = 'SELECT id AS gid, "group" FROM "group"';
44        $conf['plugin']['authpdo']['insert-user'] = 'INSERT INTO user (login, pass, name, mail) VALUES (:user, :hash, :name, :mail)';
45        $conf['plugin']['authpdo']['insert-group'] = 'INSERT INTO "group" ("group") VALUES (:group)';
46        $conf['plugin']['authpdo']['join-group'] = 'INSERT INTO member (uid, gid) VALUES (:uid, :gid)';
47    }
48
49    public function tearDown() {
50        parent::tearDown();
51        unlink($this->dbfile);
52    }
53
54    public function test_internals() {
55        $auth = new testable_auth_plugin_authpdo();
56
57        $groups = $auth->_selectGroups();
58        $this->assertArrayHasKey('user', $groups);
59        $this->assertEquals(1, $groups['user']['gid']);
60        $this->assertArrayHasKey('admin', $groups);
61        $this->assertEquals(2, $groups['admin']['gid']);
62
63        $ok = $auth->_insertGroup('test');
64        $this->assertTrue($ok);
65        $groups = $auth->_selectGroups();
66        $this->assertArrayHasKey('test', $groups);
67        $this->assertEquals(3, $groups['test']['gid']);
68    }
69
70    public function test_userinfo() {
71        global $conf;
72        $auth = new auth_plugin_authpdo();
73
74        // clear text pasword (with default config above
75        $this->assertFalse($auth->checkPass('nobody', 'nope'));
76        $this->assertFalse($auth->checkPass('admin', 'nope'));
77        $this->assertTrue($auth->checkPass('admin', 'password'));
78
79        // now with a hashed password
80        $conf['plugin']['authpdo']['select-user'] = 'SELECT id AS uid, login AS user, name, pass AS hash, mail FROM user WHERE login = :user';
81        $this->assertFalse($auth->checkPass('admin', 'password'));
82        $this->assertFalse($auth->checkPass('user', md5('password')));
83
84        // access user data
85        $info = $auth->getUserData('admin');
86        $this->assertEquals('admin', $info['user']);
87        $this->assertEquals('The Admin', $info['name']);
88        $this->assertEquals('admin@example.com', $info['mail']);
89        $this->assertEquals(array('admin', 'user'), $info['grps']);
90
91        // group retrieval
92        $this->assertEquals(array('admin', 'user'), $auth->retrieveGroups());
93        $this->assertEquals(array('user'), $auth->retrieveGroups(1));
94        $this->assertEquals(array('admin'), $auth->retrieveGroups(0, 1));
95
96        // user creation
97        $auth->createUser('test', 'password', 'A Test user', 'test@example.com', array('newgroup'));
98        $info = $auth->getUserData('test');
99        $this->assertEquals('test', $info['user']);
100        $this->assertEquals('A Test user', $info['name']);
101        $this->assertEquals('test@example.com', $info['mail']);
102        $this->assertEquals(array('newgroup', 'user'), $info['grps']);
103        $this->assertEquals(array('admin', 'newgroup', 'user'), $auth->retrieveGroups());
104    }
105
106}
107