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