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 176459f496SAndreas Gohr public function addGroup($group) { 186459f496SAndreas Gohr return parent::addGroup($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 3229a1d291SAndreas Gohr public function test_pdo_sqlite_support() { 3329a1d291SAndreas Gohr if(!class_exists('PDO') || !in_array('sqlite',PDO::getAvailableDrivers())) { 3429a1d291SAndreas Gohr $this->markTestSkipped('skipping all authpdo tests for sqlite. Need PDO_sqlite extension'); 3529a1d291SAndreas Gohr } 36*1bff2abaSAndreas Gohr $this->assertTrue(true); // avoid being marked as risky for having no assertion 3729a1d291SAndreas Gohr } 3829a1d291SAndreas Gohr 39f64dbc90SAndreas Gohr public function setUp() { 40f64dbc90SAndreas Gohr parent::setUp(); 41f64dbc90SAndreas Gohr $this->dbfile = tempnam('/tmp/', 'pluginpdo_test_'); 42f64dbc90SAndreas Gohr copy(__DIR__ . '/test.sqlite3', $this->dbfile); 43f64dbc90SAndreas Gohr 44f64dbc90SAndreas Gohr global $conf; 45f64dbc90SAndreas Gohr 46f64dbc90SAndreas Gohr $conf['plugin']['authpdo']['debug'] = 1; 47f64dbc90SAndreas Gohr $conf['plugin']['authpdo']['dsn'] = 'sqlite:' . $this->dbfile; 48f64dbc90SAndreas Gohr $conf['plugin']['authpdo']['user'] = ''; 49f64dbc90SAndreas Gohr $conf['plugin']['authpdo']['pass'] = ''; 50f64dbc90SAndreas Gohr 515de3a6a5SAndreas Gohr $conf['plugin']['authpdo']['select-user'] = 'SELECT id AS uid, login AS user, name, pass AS clear, mail FROM user WHERE login = :user'; 5270a89417SAndreas Gohr $conf['plugin']['authpdo']['select-user-groups'] = 'SELECT * FROM member AS m, "group" AS g WHERE m.gid = g.id AND m.uid = :uid'; 535de3a6a5SAndreas Gohr $conf['plugin']['authpdo']['select-groups'] = 'SELECT id AS gid, "group" FROM "group"'; 54e19be516SAndreas Gohr 555de3a6a5SAndreas Gohr $conf['plugin']['authpdo']['insert-user'] = 'INSERT INTO user (login, pass, name, mail) VALUES (:user, :hash, :name, :mail)'; 56e19be516SAndreas Gohr $conf['plugin']['authpdo']['delete-user'] = 'DELETE FROM user WHERE id = :uid'; 574fb8dfabSAndreas Gohr 586459f496SAndreas Gohr $conf['plugin']['authpdo']['list-users'] = 'SELECT DISTINCT login as user 596459f496SAndreas Gohr FROM user U, member M, "group" G 606459f496SAndreas Gohr WHERE U.id = M.uid 616459f496SAndreas Gohr AND M.gid = G.id 626459f496SAndreas Gohr AND G."group" LIKE :group 636459f496SAndreas Gohr AND U.login LIKE :user 646459f496SAndreas Gohr AND U.name LIKE :name 656459f496SAndreas Gohr AND U.mail LIKE :mail 666459f496SAndreas Gohr ORDER BY login 676459f496SAndreas Gohr LIMIT :start,:limit'; 686459f496SAndreas Gohr 696459f496SAndreas Gohr $conf['plugin']['authpdo']['count-users'] = 'SELECT COUNT(DISTINCT login) as count 706459f496SAndreas Gohr FROM user U, member M, "group" G 716459f496SAndreas Gohr WHERE U.id = M.uid 726459f496SAndreas Gohr AND M.gid = G.id 736459f496SAndreas Gohr AND G."group" LIKE :group 746459f496SAndreas Gohr AND U.login LIKE :user 756459f496SAndreas Gohr AND U.name LIKE :name 766459f496SAndreas Gohr AND U.mail LIKE :mail'; 776459f496SAndreas Gohr 786459f496SAndreas Gohr 794fb8dfabSAndreas Gohr $conf['plugin']['authpdo']['update-user-login'] = 'UPDATE user SET login = :newlogin WHERE id = :uid'; 804fb8dfabSAndreas Gohr $conf['plugin']['authpdo']['update-user-info'] = 'UPDATE user SET name = :name, mail = :mail WHERE id = :uid'; 814fb8dfabSAndreas Gohr $conf['plugin']['authpdo']['update-user-pass'] = 'UPDATE user SET pass = :hash WHERE id = :uid'; 824fb8dfabSAndreas Gohr 835de3a6a5SAndreas Gohr $conf['plugin']['authpdo']['insert-group'] = 'INSERT INTO "group" ("group") VALUES (:group)'; 845de3a6a5SAndreas Gohr $conf['plugin']['authpdo']['join-group'] = 'INSERT INTO member (uid, gid) VALUES (:uid, :gid)'; 854fb8dfabSAndreas Gohr $conf['plugin']['authpdo']['leave-group'] = 'DELETE FROM member WHERE uid = :uid AND gid = :gid'; 86f64dbc90SAndreas Gohr } 87f64dbc90SAndreas Gohr 88f64dbc90SAndreas Gohr public function tearDown() { 89f64dbc90SAndreas Gohr parent::tearDown(); 90f64dbc90SAndreas Gohr unlink($this->dbfile); 91f64dbc90SAndreas Gohr } 92f64dbc90SAndreas Gohr 9329a1d291SAndreas Gohr /** 9429a1d291SAndreas Gohr * @depends test_pdo_sqlite_support 9529a1d291SAndreas Gohr */ 965de3a6a5SAndreas Gohr public function test_internals() { 975de3a6a5SAndreas Gohr $auth = new testable_auth_plugin_authpdo(); 985de3a6a5SAndreas Gohr 995de3a6a5SAndreas Gohr $groups = $auth->_selectGroups(); 1005de3a6a5SAndreas Gohr $this->assertArrayHasKey('user', $groups); 1015de3a6a5SAndreas Gohr $this->assertEquals(1, $groups['user']['gid']); 1025de3a6a5SAndreas Gohr $this->assertArrayHasKey('admin', $groups); 1035de3a6a5SAndreas Gohr $this->assertEquals(2, $groups['admin']['gid']); 1045de3a6a5SAndreas Gohr 1056459f496SAndreas Gohr $ok = $auth->addGroup('test'); 1065de3a6a5SAndreas Gohr $this->assertTrue($ok); 1075de3a6a5SAndreas Gohr $groups = $auth->_selectGroups(); 1085de3a6a5SAndreas Gohr $this->assertArrayHasKey('test', $groups); 1095de3a6a5SAndreas Gohr $this->assertEquals(3, $groups['test']['gid']); 1105de3a6a5SAndreas Gohr } 1115de3a6a5SAndreas Gohr 11229a1d291SAndreas Gohr /** 11329a1d291SAndreas Gohr * @depends test_pdo_sqlite_support 11429a1d291SAndreas Gohr */ 115f64dbc90SAndreas Gohr public function test_userinfo() { 116f64dbc90SAndreas Gohr global $conf; 117f64dbc90SAndreas Gohr $auth = new auth_plugin_authpdo(); 118f64dbc90SAndreas Gohr 119f64dbc90SAndreas Gohr // clear text pasword (with default config above 120f64dbc90SAndreas Gohr $this->assertFalse($auth->checkPass('nobody', 'nope')); 121f64dbc90SAndreas Gohr $this->assertFalse($auth->checkPass('admin', 'nope')); 122f64dbc90SAndreas Gohr $this->assertTrue($auth->checkPass('admin', 'password')); 123f64dbc90SAndreas Gohr 124f64dbc90SAndreas Gohr // now with a hashed password 1255de3a6a5SAndreas Gohr $conf['plugin']['authpdo']['select-user'] = 'SELECT id AS uid, login AS user, name, pass AS hash, mail FROM user WHERE login = :user'; 126f64dbc90SAndreas Gohr $this->assertFalse($auth->checkPass('admin', 'password')); 127f64dbc90SAndreas Gohr $this->assertFalse($auth->checkPass('user', md5('password'))); 128f64dbc90SAndreas Gohr 12970a89417SAndreas Gohr // access user data 13070a89417SAndreas Gohr $info = $auth->getUserData('admin'); 13170a89417SAndreas Gohr $this->assertEquals('admin', $info['user']); 13270a89417SAndreas Gohr $this->assertEquals('The Admin', $info['name']); 13370a89417SAndreas Gohr $this->assertEquals('admin@example.com', $info['mail']); 13470a89417SAndreas Gohr $this->assertEquals(array('admin', 'user'), $info['grps']); 1355de3a6a5SAndreas Gohr 1365de3a6a5SAndreas Gohr // group retrieval 1375de3a6a5SAndreas Gohr $this->assertEquals(array('admin', 'user'), $auth->retrieveGroups()); 1385de3a6a5SAndreas Gohr $this->assertEquals(array('user'), $auth->retrieveGroups(1)); 1395de3a6a5SAndreas Gohr $this->assertEquals(array('admin'), $auth->retrieveGroups(0, 1)); 1405de3a6a5SAndreas Gohr 1415de3a6a5SAndreas Gohr // user creation 1425de3a6a5SAndreas Gohr $auth->createUser('test', 'password', 'A Test user', 'test@example.com', array('newgroup')); 1435de3a6a5SAndreas Gohr $info = $auth->getUserData('test'); 1445de3a6a5SAndreas Gohr $this->assertEquals('test', $info['user']); 1455de3a6a5SAndreas Gohr $this->assertEquals('A Test user', $info['name']); 1465de3a6a5SAndreas Gohr $this->assertEquals('test@example.com', $info['mail']); 1475de3a6a5SAndreas Gohr $this->assertEquals(array('newgroup', 'user'), $info['grps']); 1485de3a6a5SAndreas Gohr $this->assertEquals(array('admin', 'newgroup', 'user'), $auth->retrieveGroups()); 1494fb8dfabSAndreas Gohr 1504fb8dfabSAndreas Gohr // user modification 1514fb8dfabSAndreas Gohr $auth->modifyUser('test', array('user' => 'tester', 'name' => 'The Test User', 'pass' => 'secret')); 1524fb8dfabSAndreas Gohr $info = $auth->getUserData('tester'); 1534fb8dfabSAndreas Gohr $this->assertEquals('tester', $info['user']); 1544fb8dfabSAndreas Gohr $this->assertEquals('The Test User', $info['name']); 1554fb8dfabSAndreas Gohr $this->assertTrue($auth->checkPass('tester','secret')); 1564fb8dfabSAndreas Gohr 1574fb8dfabSAndreas Gohr // move user to different groups 1584fb8dfabSAndreas Gohr $auth->modifyUser('tester', array('grps' => array('user', 'admin', 'another'))); 1594fb8dfabSAndreas Gohr $info = $auth->getUserData('tester'); 1604fb8dfabSAndreas Gohr $this->assertEquals(array('admin', 'another', 'user'), $info['grps']); 161e19be516SAndreas Gohr 1623e2a8145SAndreas Gohr 1633e2a8145SAndreas Gohr $expect = array( 1643e2a8145SAndreas Gohr 'admin' => array( 1653e2a8145SAndreas Gohr 'user' => 'admin', 1663e2a8145SAndreas Gohr 'name' => 'The Admin', 1673e2a8145SAndreas Gohr 'mail' => 'admin@example.com', 1683e2a8145SAndreas Gohr 'uid' => '1', 1693e2a8145SAndreas Gohr 'grps' => array('admin', 'user') 1703e2a8145SAndreas Gohr ), 1713e2a8145SAndreas Gohr 'user' => array( 1723e2a8145SAndreas Gohr 'user' => 'user', 1733e2a8145SAndreas Gohr 'name' => 'A normal user', 1743e2a8145SAndreas Gohr 'mail' => 'user@example.com', 1753e2a8145SAndreas Gohr 'uid' => '2', 1763e2a8145SAndreas Gohr 'grps' => array('user') 1773e2a8145SAndreas Gohr ), 1783e2a8145SAndreas Gohr 'tester' => array( 1793e2a8145SAndreas Gohr 'user' => 'tester', 1803e2a8145SAndreas Gohr 'name' => 'The Test User', 1813e2a8145SAndreas Gohr 'mail' => 'test@example.com', 1823e2a8145SAndreas Gohr 'uid' => '3', 1833e2a8145SAndreas Gohr 'grps' => array('admin', 'another', 'user') 1843e2a8145SAndreas Gohr ) 1853e2a8145SAndreas Gohr ); 1863e2a8145SAndreas Gohr 1876459f496SAndreas Gohr // list users 1886459f496SAndreas Gohr $users = $auth->retrieveUsers(); 1893e2a8145SAndreas Gohr $this->assertEquals(array($expect['admin'], $expect['tester'], $expect['user']), $users); 1906459f496SAndreas Gohr 1916459f496SAndreas Gohr $users = $auth->retrieveUsers(1); // offset 1923e2a8145SAndreas Gohr $this->assertEquals(array($expect['tester'], $expect['user']), $users); 1936459f496SAndreas Gohr 1946459f496SAndreas Gohr $users = $auth->retrieveUsers(1, 1); // offset + limit 1953e2a8145SAndreas Gohr $this->assertEquals(array($expect['tester']), $users); 1966459f496SAndreas Gohr 1976459f496SAndreas Gohr $users = $auth->retrieveUsers(0, -1, array('group' => 'admin')); // full group 1983e2a8145SAndreas Gohr $this->assertEquals(array($expect['admin'], $expect['tester']), $users); 19912c7f5c3SAndreas Gohr $count = $auth->getUserCount(array('grps' => 'admin')); 200f3c1c207SAndreas Gohr $this->assertSame(2, $count); 2016459f496SAndreas Gohr 2026459f496SAndreas Gohr $users = $auth->retrieveUsers(0, -1, array('group' => 'dmi')); // substring 2033e2a8145SAndreas Gohr $this->assertEquals(array($expect['admin'], $expect['tester']), $users); 20412c7f5c3SAndreas Gohr $count = $auth->getUserCount(array('grps' => 'dmi')); 205f3c1c207SAndreas Gohr $this->assertSame(2, $count); 2066459f496SAndreas Gohr 2076459f496SAndreas Gohr $users = $auth->retrieveUsers(0, -1, array('user' => 'dmi')); // substring 2083e2a8145SAndreas Gohr $this->assertEquals(array($expect['admin']), $users); 2096459f496SAndreas Gohr $count = $auth->getUserCount(array('user' => 'dmi')); 210f3c1c207SAndreas Gohr $this->assertSame(1, $count); 2116459f496SAndreas Gohr 212e19be516SAndreas Gohr // delete user 213e19be516SAndreas Gohr $num = $auth->deleteUsers(array('tester', 'foobar')); 214f3c1c207SAndreas Gohr $this->assertSame(1, $num); 2156459f496SAndreas Gohr 216f64dbc90SAndreas Gohr } 2175de3a6a5SAndreas Gohr 218f64dbc90SAndreas Gohr} 219