xref: /dokuwiki/lib/plugins/authpdo/_test/sqlite.test.php (revision e19be5160bbe04352d6ae60d2294855d246d0dde)
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
175de3a6a5SAndreas Gohr    public function _insertGroup($group) {
185de3a6a5SAndreas Gohr        return parent::_insertGroup($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"';
47*e19be516SAndreas Gohr
485de3a6a5SAndreas Gohr        $conf['plugin']['authpdo']['insert-user'] = 'INSERT INTO user (login, pass, name, mail) VALUES (:user, :hash, :name, :mail)';
49*e19be516SAndreas Gohr        $conf['plugin']['authpdo']['delete-user'] = 'DELETE FROM user WHERE id = :uid';
504fb8dfabSAndreas Gohr
514fb8dfabSAndreas Gohr        $conf['plugin']['authpdo']['update-user-login'] = 'UPDATE user SET login = :newlogin WHERE id = :uid';
524fb8dfabSAndreas Gohr        $conf['plugin']['authpdo']['update-user-info'] = 'UPDATE user SET name = :name, mail = :mail WHERE id = :uid';
534fb8dfabSAndreas Gohr        $conf['plugin']['authpdo']['update-user-pass'] = 'UPDATE user SET pass = :hash WHERE id = :uid';
544fb8dfabSAndreas Gohr
555de3a6a5SAndreas Gohr        $conf['plugin']['authpdo']['insert-group'] = 'INSERT INTO "group" ("group") VALUES (:group)';
565de3a6a5SAndreas Gohr        $conf['plugin']['authpdo']['join-group'] = 'INSERT INTO member (uid, gid) VALUES (:uid, :gid)';
574fb8dfabSAndreas Gohr        $conf['plugin']['authpdo']['leave-group'] = 'DELETE FROM member WHERE uid = :uid AND gid = :gid';
58f64dbc90SAndreas Gohr    }
59f64dbc90SAndreas Gohr
60f64dbc90SAndreas Gohr    public function tearDown() {
61f64dbc90SAndreas Gohr        parent::tearDown();
62f64dbc90SAndreas Gohr        unlink($this->dbfile);
63f64dbc90SAndreas Gohr    }
64f64dbc90SAndreas Gohr
655de3a6a5SAndreas Gohr    public function test_internals() {
665de3a6a5SAndreas Gohr        $auth = new testable_auth_plugin_authpdo();
675de3a6a5SAndreas Gohr
685de3a6a5SAndreas Gohr        $groups = $auth->_selectGroups();
695de3a6a5SAndreas Gohr        $this->assertArrayHasKey('user', $groups);
705de3a6a5SAndreas Gohr        $this->assertEquals(1, $groups['user']['gid']);
715de3a6a5SAndreas Gohr        $this->assertArrayHasKey('admin', $groups);
725de3a6a5SAndreas Gohr        $this->assertEquals(2, $groups['admin']['gid']);
735de3a6a5SAndreas Gohr
745de3a6a5SAndreas Gohr        $ok = $auth->_insertGroup('test');
755de3a6a5SAndreas Gohr        $this->assertTrue($ok);
765de3a6a5SAndreas Gohr        $groups = $auth->_selectGroups();
775de3a6a5SAndreas Gohr        $this->assertArrayHasKey('test', $groups);
785de3a6a5SAndreas Gohr        $this->assertEquals(3, $groups['test']['gid']);
795de3a6a5SAndreas Gohr    }
805de3a6a5SAndreas Gohr
81f64dbc90SAndreas Gohr    public function test_userinfo() {
82f64dbc90SAndreas Gohr        global $conf;
83f64dbc90SAndreas Gohr        $auth = new auth_plugin_authpdo();
84f64dbc90SAndreas Gohr
85f64dbc90SAndreas Gohr        // clear text pasword (with default config above
86f64dbc90SAndreas Gohr        $this->assertFalse($auth->checkPass('nobody', 'nope'));
87f64dbc90SAndreas Gohr        $this->assertFalse($auth->checkPass('admin', 'nope'));
88f64dbc90SAndreas Gohr        $this->assertTrue($auth->checkPass('admin', 'password'));
89f64dbc90SAndreas Gohr
90f64dbc90SAndreas Gohr        // now with a hashed password
915de3a6a5SAndreas Gohr        $conf['plugin']['authpdo']['select-user'] = 'SELECT id AS uid, login AS user, name, pass AS hash, mail FROM user WHERE login = :user';
92f64dbc90SAndreas Gohr        $this->assertFalse($auth->checkPass('admin', 'password'));
93f64dbc90SAndreas Gohr        $this->assertFalse($auth->checkPass('user', md5('password')));
94f64dbc90SAndreas Gohr
9570a89417SAndreas Gohr        // access user data
9670a89417SAndreas Gohr        $info = $auth->getUserData('admin');
9770a89417SAndreas Gohr        $this->assertEquals('admin', $info['user']);
9870a89417SAndreas Gohr        $this->assertEquals('The Admin', $info['name']);
9970a89417SAndreas Gohr        $this->assertEquals('admin@example.com', $info['mail']);
10070a89417SAndreas Gohr        $this->assertEquals(array('admin', 'user'), $info['grps']);
1015de3a6a5SAndreas Gohr
1025de3a6a5SAndreas Gohr        // group retrieval
1035de3a6a5SAndreas Gohr        $this->assertEquals(array('admin', 'user'), $auth->retrieveGroups());
1045de3a6a5SAndreas Gohr        $this->assertEquals(array('user'), $auth->retrieveGroups(1));
1055de3a6a5SAndreas Gohr        $this->assertEquals(array('admin'), $auth->retrieveGroups(0, 1));
1065de3a6a5SAndreas Gohr
1075de3a6a5SAndreas Gohr        // user creation
1085de3a6a5SAndreas Gohr        $auth->createUser('test', 'password', 'A Test user', 'test@example.com', array('newgroup'));
1095de3a6a5SAndreas Gohr        $info = $auth->getUserData('test');
1105de3a6a5SAndreas Gohr        $this->assertEquals('test', $info['user']);
1115de3a6a5SAndreas Gohr        $this->assertEquals('A Test user', $info['name']);
1125de3a6a5SAndreas Gohr        $this->assertEquals('test@example.com', $info['mail']);
1135de3a6a5SAndreas Gohr        $this->assertEquals(array('newgroup', 'user'), $info['grps']);
1145de3a6a5SAndreas Gohr        $this->assertEquals(array('admin', 'newgroup', 'user'), $auth->retrieveGroups());
1154fb8dfabSAndreas Gohr
1164fb8dfabSAndreas Gohr        // user modification
1174fb8dfabSAndreas Gohr        $auth->modifyUser('test', array('user' => 'tester', 'name' => 'The Test User', 'pass' => 'secret'));
1184fb8dfabSAndreas Gohr        $info = $auth->getUserData('tester');
1194fb8dfabSAndreas Gohr        $this->assertEquals('tester', $info['user']);
1204fb8dfabSAndreas Gohr        $this->assertEquals('The Test User', $info['name']);
1214fb8dfabSAndreas Gohr        $this->assertTrue($auth->checkPass('tester','secret'));
1224fb8dfabSAndreas Gohr
1234fb8dfabSAndreas Gohr        // move user to different groups
1244fb8dfabSAndreas Gohr        $auth->modifyUser('tester', array('grps' => array('user', 'admin', 'another')));
1254fb8dfabSAndreas Gohr        $info = $auth->getUserData('tester');
1264fb8dfabSAndreas Gohr        $this->assertEquals(array('admin', 'another', 'user'), $info['grps']);
127*e19be516SAndreas Gohr
128*e19be516SAndreas Gohr        // delete user
129*e19be516SAndreas Gohr        $num = $auth->deleteUsers(array('tester', 'foobar'));
130*e19be516SAndreas Gohr        $this->assertEquals(1, $num);
131f64dbc90SAndreas Gohr    }
1325de3a6a5SAndreas Gohr
133f64dbc90SAndreas Gohr}
134