xref: /dokuwiki/lib/plugins/authpdo/_test/sqlite.test.php (revision 4fb8dfab9fa5bc3e6249a16d39713415988a5ec2)
1f64dbc90SAndreas Gohr<?php
2f64dbc90SAndreas Gohr
3*4fb8dfabSAndreas Gohr/**
4*4fb8dfabSAndreas Gohr * Class testable_auth_plugin_authpdo
5*4fb8dfabSAndreas Gohr *
6*4fb8dfabSAndreas Gohr * makes protected methods public for testing
7*4fb8dfabSAndreas 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"';
475de3a6a5SAndreas Gohr        $conf['plugin']['authpdo']['insert-user'] = 'INSERT INTO user (login, pass, name, mail) VALUES (:user, :hash, :name, :mail)';
48*4fb8dfabSAndreas Gohr
49*4fb8dfabSAndreas Gohr        $conf['plugin']['authpdo']['update-user-login'] = 'UPDATE user SET login = :newlogin WHERE id = :uid';
50*4fb8dfabSAndreas Gohr        $conf['plugin']['authpdo']['update-user-info'] = 'UPDATE user SET name = :name, mail = :mail WHERE id = :uid';
51*4fb8dfabSAndreas Gohr        $conf['plugin']['authpdo']['update-user-pass'] = 'UPDATE user SET pass = :hash WHERE id = :uid';
52*4fb8dfabSAndreas Gohr
535de3a6a5SAndreas Gohr        $conf['plugin']['authpdo']['insert-group'] = 'INSERT INTO "group" ("group") VALUES (:group)';
545de3a6a5SAndreas Gohr        $conf['plugin']['authpdo']['join-group'] = 'INSERT INTO member (uid, gid) VALUES (:uid, :gid)';
55*4fb8dfabSAndreas Gohr        $conf['plugin']['authpdo']['leave-group'] = 'DELETE FROM member WHERE uid = :uid AND gid = :gid';
56f64dbc90SAndreas Gohr    }
57f64dbc90SAndreas Gohr
58f64dbc90SAndreas Gohr    public function tearDown() {
59f64dbc90SAndreas Gohr        parent::tearDown();
60f64dbc90SAndreas Gohr        unlink($this->dbfile);
61f64dbc90SAndreas Gohr    }
62f64dbc90SAndreas Gohr
635de3a6a5SAndreas Gohr    public function test_internals() {
645de3a6a5SAndreas Gohr        $auth = new testable_auth_plugin_authpdo();
655de3a6a5SAndreas Gohr
665de3a6a5SAndreas Gohr        $groups = $auth->_selectGroups();
675de3a6a5SAndreas Gohr        $this->assertArrayHasKey('user', $groups);
685de3a6a5SAndreas Gohr        $this->assertEquals(1, $groups['user']['gid']);
695de3a6a5SAndreas Gohr        $this->assertArrayHasKey('admin', $groups);
705de3a6a5SAndreas Gohr        $this->assertEquals(2, $groups['admin']['gid']);
715de3a6a5SAndreas Gohr
725de3a6a5SAndreas Gohr        $ok = $auth->_insertGroup('test');
735de3a6a5SAndreas Gohr        $this->assertTrue($ok);
745de3a6a5SAndreas Gohr        $groups = $auth->_selectGroups();
755de3a6a5SAndreas Gohr        $this->assertArrayHasKey('test', $groups);
765de3a6a5SAndreas Gohr        $this->assertEquals(3, $groups['test']['gid']);
775de3a6a5SAndreas Gohr    }
785de3a6a5SAndreas Gohr
79f64dbc90SAndreas Gohr    public function test_userinfo() {
80f64dbc90SAndreas Gohr        global $conf;
81f64dbc90SAndreas Gohr        $auth = new auth_plugin_authpdo();
82f64dbc90SAndreas Gohr
83f64dbc90SAndreas Gohr        // clear text pasword (with default config above
84f64dbc90SAndreas Gohr        $this->assertFalse($auth->checkPass('nobody', 'nope'));
85f64dbc90SAndreas Gohr        $this->assertFalse($auth->checkPass('admin', 'nope'));
86f64dbc90SAndreas Gohr        $this->assertTrue($auth->checkPass('admin', 'password'));
87f64dbc90SAndreas Gohr
88f64dbc90SAndreas Gohr        // now with a hashed password
895de3a6a5SAndreas Gohr        $conf['plugin']['authpdo']['select-user'] = 'SELECT id AS uid, login AS user, name, pass AS hash, mail FROM user WHERE login = :user';
90f64dbc90SAndreas Gohr        $this->assertFalse($auth->checkPass('admin', 'password'));
91f64dbc90SAndreas Gohr        $this->assertFalse($auth->checkPass('user', md5('password')));
92f64dbc90SAndreas Gohr
9370a89417SAndreas Gohr        // access user data
9470a89417SAndreas Gohr        $info = $auth->getUserData('admin');
9570a89417SAndreas Gohr        $this->assertEquals('admin', $info['user']);
9670a89417SAndreas Gohr        $this->assertEquals('The Admin', $info['name']);
9770a89417SAndreas Gohr        $this->assertEquals('admin@example.com', $info['mail']);
9870a89417SAndreas Gohr        $this->assertEquals(array('admin', 'user'), $info['grps']);
995de3a6a5SAndreas Gohr
1005de3a6a5SAndreas Gohr        // group retrieval
1015de3a6a5SAndreas Gohr        $this->assertEquals(array('admin', 'user'), $auth->retrieveGroups());
1025de3a6a5SAndreas Gohr        $this->assertEquals(array('user'), $auth->retrieveGroups(1));
1035de3a6a5SAndreas Gohr        $this->assertEquals(array('admin'), $auth->retrieveGroups(0, 1));
1045de3a6a5SAndreas Gohr
1055de3a6a5SAndreas Gohr        // user creation
1065de3a6a5SAndreas Gohr        $auth->createUser('test', 'password', 'A Test user', 'test@example.com', array('newgroup'));
1075de3a6a5SAndreas Gohr        $info = $auth->getUserData('test');
1085de3a6a5SAndreas Gohr        $this->assertEquals('test', $info['user']);
1095de3a6a5SAndreas Gohr        $this->assertEquals('A Test user', $info['name']);
1105de3a6a5SAndreas Gohr        $this->assertEquals('test@example.com', $info['mail']);
1115de3a6a5SAndreas Gohr        $this->assertEquals(array('newgroup', 'user'), $info['grps']);
1125de3a6a5SAndreas Gohr        $this->assertEquals(array('admin', 'newgroup', 'user'), $auth->retrieveGroups());
113*4fb8dfabSAndreas Gohr
114*4fb8dfabSAndreas Gohr        // user modification
115*4fb8dfabSAndreas Gohr        $auth->modifyUser('test', array('user' => 'tester', 'name' => 'The Test User', 'pass' => 'secret'));
116*4fb8dfabSAndreas Gohr        $info = $auth->getUserData('tester');
117*4fb8dfabSAndreas Gohr        $this->assertEquals('tester', $info['user']);
118*4fb8dfabSAndreas Gohr        $this->assertEquals('The Test User', $info['name']);
119*4fb8dfabSAndreas Gohr        $this->assertTrue($auth->checkPass('tester','secret'));
120*4fb8dfabSAndreas Gohr
121*4fb8dfabSAndreas Gohr        // move user to different groups
122*4fb8dfabSAndreas Gohr        $auth->modifyUser('tester', array('grps' => array('user', 'admin', 'another')));
123*4fb8dfabSAndreas Gohr        $info = $auth->getUserData('tester');
124*4fb8dfabSAndreas Gohr        $this->assertEquals(array('admin', 'another', 'user'), $info['grps']);
125f64dbc90SAndreas Gohr    }
1265de3a6a5SAndreas Gohr
127f64dbc90SAndreas Gohr}
128