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 } 3629a1d291SAndreas Gohr } 3729a1d291SAndreas Gohr 38f64dbc90SAndreas Gohr public function setUp() { 39f64dbc90SAndreas Gohr parent::setUp(); 40f64dbc90SAndreas Gohr $this->dbfile = tempnam('/tmp/', 'pluginpdo_test_'); 41f64dbc90SAndreas Gohr copy(__DIR__ . '/test.sqlite3', $this->dbfile); 42f64dbc90SAndreas Gohr 43f64dbc90SAndreas Gohr global $conf; 44f64dbc90SAndreas Gohr 45f64dbc90SAndreas Gohr $conf['plugin']['authpdo']['debug'] = 1; 46f64dbc90SAndreas Gohr $conf['plugin']['authpdo']['dsn'] = 'sqlite:' . $this->dbfile; 47f64dbc90SAndreas Gohr $conf['plugin']['authpdo']['user'] = ''; 48f64dbc90SAndreas Gohr $conf['plugin']['authpdo']['pass'] = ''; 49f64dbc90SAndreas Gohr 505de3a6a5SAndreas Gohr $conf['plugin']['authpdo']['select-user'] = 'SELECT id AS uid, login AS user, name, pass AS clear, mail FROM user WHERE login = :user'; 5170a89417SAndreas Gohr $conf['plugin']['authpdo']['select-user-groups'] = 'SELECT * FROM member AS m, "group" AS g WHERE m.gid = g.id AND m.uid = :uid'; 525de3a6a5SAndreas Gohr $conf['plugin']['authpdo']['select-groups'] = 'SELECT id AS gid, "group" FROM "group"'; 53e19be516SAndreas Gohr 545de3a6a5SAndreas Gohr $conf['plugin']['authpdo']['insert-user'] = 'INSERT INTO user (login, pass, name, mail) VALUES (:user, :hash, :name, :mail)'; 55e19be516SAndreas Gohr $conf['plugin']['authpdo']['delete-user'] = 'DELETE FROM user WHERE id = :uid'; 564fb8dfabSAndreas Gohr 576459f496SAndreas Gohr $conf['plugin']['authpdo']['list-users'] = 'SELECT DISTINCT login as user 586459f496SAndreas Gohr FROM user U, member M, "group" G 596459f496SAndreas Gohr WHERE U.id = M.uid 606459f496SAndreas Gohr AND M.gid = G.id 616459f496SAndreas Gohr AND G."group" LIKE :group 626459f496SAndreas Gohr AND U.login LIKE :user 636459f496SAndreas Gohr AND U.name LIKE :name 646459f496SAndreas Gohr AND U.mail LIKE :mail 656459f496SAndreas Gohr ORDER BY login 666459f496SAndreas Gohr LIMIT :start,:limit'; 676459f496SAndreas Gohr 686459f496SAndreas Gohr $conf['plugin']['authpdo']['count-users'] = 'SELECT COUNT(DISTINCT login) as count 696459f496SAndreas Gohr FROM user U, member M, "group" G 706459f496SAndreas Gohr WHERE U.id = M.uid 716459f496SAndreas Gohr AND M.gid = G.id 726459f496SAndreas Gohr AND G."group" LIKE :group 736459f496SAndreas Gohr AND U.login LIKE :user 746459f496SAndreas Gohr AND U.name LIKE :name 756459f496SAndreas Gohr AND U.mail LIKE :mail'; 766459f496SAndreas Gohr 776459f496SAndreas Gohr 784fb8dfabSAndreas Gohr $conf['plugin']['authpdo']['update-user-login'] = 'UPDATE user SET login = :newlogin WHERE id = :uid'; 794fb8dfabSAndreas Gohr $conf['plugin']['authpdo']['update-user-info'] = 'UPDATE user SET name = :name, mail = :mail WHERE id = :uid'; 804fb8dfabSAndreas Gohr $conf['plugin']['authpdo']['update-user-pass'] = 'UPDATE user SET pass = :hash WHERE id = :uid'; 814fb8dfabSAndreas Gohr 825de3a6a5SAndreas Gohr $conf['plugin']['authpdo']['insert-group'] = 'INSERT INTO "group" ("group") VALUES (:group)'; 835de3a6a5SAndreas Gohr $conf['plugin']['authpdo']['join-group'] = 'INSERT INTO member (uid, gid) VALUES (:uid, :gid)'; 844fb8dfabSAndreas Gohr $conf['plugin']['authpdo']['leave-group'] = 'DELETE FROM member WHERE uid = :uid AND gid = :gid'; 85f64dbc90SAndreas Gohr } 86f64dbc90SAndreas Gohr 87f64dbc90SAndreas Gohr public function tearDown() { 88f64dbc90SAndreas Gohr parent::tearDown(); 89f64dbc90SAndreas Gohr unlink($this->dbfile); 90f64dbc90SAndreas Gohr } 91f64dbc90SAndreas Gohr 9229a1d291SAndreas Gohr /** 9329a1d291SAndreas Gohr * @depends test_pdo_sqlite_support 9429a1d291SAndreas Gohr */ 955de3a6a5SAndreas Gohr public function test_internals() { 965de3a6a5SAndreas Gohr $auth = new testable_auth_plugin_authpdo(); 975de3a6a5SAndreas Gohr 985de3a6a5SAndreas Gohr $groups = $auth->_selectGroups(); 995de3a6a5SAndreas Gohr $this->assertArrayHasKey('user', $groups); 1005de3a6a5SAndreas Gohr $this->assertEquals(1, $groups['user']['gid']); 1015de3a6a5SAndreas Gohr $this->assertArrayHasKey('admin', $groups); 1025de3a6a5SAndreas Gohr $this->assertEquals(2, $groups['admin']['gid']); 1035de3a6a5SAndreas Gohr 1046459f496SAndreas Gohr $ok = $auth->addGroup('test'); 1055de3a6a5SAndreas Gohr $this->assertTrue($ok); 1065de3a6a5SAndreas Gohr $groups = $auth->_selectGroups(); 1075de3a6a5SAndreas Gohr $this->assertArrayHasKey('test', $groups); 1085de3a6a5SAndreas Gohr $this->assertEquals(3, $groups['test']['gid']); 1095de3a6a5SAndreas Gohr } 1105de3a6a5SAndreas Gohr 11129a1d291SAndreas Gohr /** 11229a1d291SAndreas Gohr * @depends test_pdo_sqlite_support 11329a1d291SAndreas Gohr */ 114f64dbc90SAndreas Gohr public function test_userinfo() { 115f64dbc90SAndreas Gohr global $conf; 116f64dbc90SAndreas Gohr $auth = new auth_plugin_authpdo(); 117f64dbc90SAndreas Gohr 118f64dbc90SAndreas Gohr // clear text pasword (with default config above 119f64dbc90SAndreas Gohr $this->assertFalse($auth->checkPass('nobody', 'nope')); 120f64dbc90SAndreas Gohr $this->assertFalse($auth->checkPass('admin', 'nope')); 121f64dbc90SAndreas Gohr $this->assertTrue($auth->checkPass('admin', 'password')); 122f64dbc90SAndreas Gohr 123f64dbc90SAndreas Gohr // now with a hashed password 1245de3a6a5SAndreas Gohr $conf['plugin']['authpdo']['select-user'] = 'SELECT id AS uid, login AS user, name, pass AS hash, mail FROM user WHERE login = :user'; 125f64dbc90SAndreas Gohr $this->assertFalse($auth->checkPass('admin', 'password')); 126f64dbc90SAndreas Gohr $this->assertFalse($auth->checkPass('user', md5('password'))); 127f64dbc90SAndreas Gohr 12870a89417SAndreas Gohr // access user data 12970a89417SAndreas Gohr $info = $auth->getUserData('admin'); 13070a89417SAndreas Gohr $this->assertEquals('admin', $info['user']); 13170a89417SAndreas Gohr $this->assertEquals('The Admin', $info['name']); 13270a89417SAndreas Gohr $this->assertEquals('admin@example.com', $info['mail']); 13370a89417SAndreas Gohr $this->assertEquals(array('admin', 'user'), $info['grps']); 1345de3a6a5SAndreas Gohr 1355de3a6a5SAndreas Gohr // group retrieval 1365de3a6a5SAndreas Gohr $this->assertEquals(array('admin', 'user'), $auth->retrieveGroups()); 1375de3a6a5SAndreas Gohr $this->assertEquals(array('user'), $auth->retrieveGroups(1)); 1385de3a6a5SAndreas Gohr $this->assertEquals(array('admin'), $auth->retrieveGroups(0, 1)); 1395de3a6a5SAndreas Gohr 1405de3a6a5SAndreas Gohr // user creation 1415de3a6a5SAndreas Gohr $auth->createUser('test', 'password', 'A Test user', 'test@example.com', array('newgroup')); 1425de3a6a5SAndreas Gohr $info = $auth->getUserData('test'); 1435de3a6a5SAndreas Gohr $this->assertEquals('test', $info['user']); 1445de3a6a5SAndreas Gohr $this->assertEquals('A Test user', $info['name']); 1455de3a6a5SAndreas Gohr $this->assertEquals('test@example.com', $info['mail']); 1465de3a6a5SAndreas Gohr $this->assertEquals(array('newgroup', 'user'), $info['grps']); 1475de3a6a5SAndreas Gohr $this->assertEquals(array('admin', 'newgroup', 'user'), $auth->retrieveGroups()); 1484fb8dfabSAndreas Gohr 1494fb8dfabSAndreas Gohr // user modification 1504fb8dfabSAndreas Gohr $auth->modifyUser('test', array('user' => 'tester', 'name' => 'The Test User', 'pass' => 'secret')); 1514fb8dfabSAndreas Gohr $info = $auth->getUserData('tester'); 1524fb8dfabSAndreas Gohr $this->assertEquals('tester', $info['user']); 1534fb8dfabSAndreas Gohr $this->assertEquals('The Test User', $info['name']); 1544fb8dfabSAndreas Gohr $this->assertTrue($auth->checkPass('tester','secret')); 1554fb8dfabSAndreas Gohr 1564fb8dfabSAndreas Gohr // move user to different groups 1574fb8dfabSAndreas Gohr $auth->modifyUser('tester', array('grps' => array('user', 'admin', 'another'))); 1584fb8dfabSAndreas Gohr $info = $auth->getUserData('tester'); 1594fb8dfabSAndreas Gohr $this->assertEquals(array('admin', 'another', 'user'), $info['grps']); 160e19be516SAndreas Gohr 1613e2a8145SAndreas Gohr 1623e2a8145SAndreas Gohr $expect = array( 1633e2a8145SAndreas Gohr 'admin' => array( 1643e2a8145SAndreas Gohr 'user' => 'admin', 1653e2a8145SAndreas Gohr 'name' => 'The Admin', 1663e2a8145SAndreas Gohr 'mail' => 'admin@example.com', 1673e2a8145SAndreas Gohr 'uid' => '1', 1683e2a8145SAndreas Gohr 'grps' => array('admin', 'user') 1693e2a8145SAndreas Gohr ), 1703e2a8145SAndreas Gohr 'user' => array( 1713e2a8145SAndreas Gohr 'user' => 'user', 1723e2a8145SAndreas Gohr 'name' => 'A normal user', 1733e2a8145SAndreas Gohr 'mail' => 'user@example.com', 1743e2a8145SAndreas Gohr 'uid' => '2', 1753e2a8145SAndreas Gohr 'grps' => array('user') 1763e2a8145SAndreas Gohr ), 1773e2a8145SAndreas Gohr 'tester' => array( 1783e2a8145SAndreas Gohr 'user' => 'tester', 1793e2a8145SAndreas Gohr 'name' => 'The Test User', 1803e2a8145SAndreas Gohr 'mail' => 'test@example.com', 1813e2a8145SAndreas Gohr 'uid' => '3', 1823e2a8145SAndreas Gohr 'grps' => array('admin', 'another', 'user') 1833e2a8145SAndreas Gohr ) 1843e2a8145SAndreas Gohr ); 1853e2a8145SAndreas Gohr 1866459f496SAndreas Gohr // list users 1876459f496SAndreas Gohr $users = $auth->retrieveUsers(); 1883e2a8145SAndreas Gohr $this->assertEquals(array($expect['admin'], $expect['tester'], $expect['user']), $users); 1896459f496SAndreas Gohr 1906459f496SAndreas Gohr $users = $auth->retrieveUsers(1); // offset 1913e2a8145SAndreas Gohr $this->assertEquals(array($expect['tester'], $expect['user']), $users); 1926459f496SAndreas Gohr 1936459f496SAndreas Gohr $users = $auth->retrieveUsers(1, 1); // offset + limit 1943e2a8145SAndreas Gohr $this->assertEquals(array($expect['tester']), $users); 1956459f496SAndreas Gohr 1966459f496SAndreas Gohr $users = $auth->retrieveUsers(0, -1, array('group' => 'admin')); // full group 1973e2a8145SAndreas Gohr $this->assertEquals(array($expect['admin'], $expect['tester']), $users); 198*12c7f5c3SAndreas Gohr $count = $auth->getUserCount(array('grps' => 'admin')); 199f3c1c207SAndreas Gohr $this->assertSame(2, $count); 2006459f496SAndreas Gohr 2016459f496SAndreas Gohr $users = $auth->retrieveUsers(0, -1, array('group' => 'dmi')); // substring 2023e2a8145SAndreas Gohr $this->assertEquals(array($expect['admin'], $expect['tester']), $users); 203*12c7f5c3SAndreas Gohr $count = $auth->getUserCount(array('grps' => 'dmi')); 204f3c1c207SAndreas Gohr $this->assertSame(2, $count); 2056459f496SAndreas Gohr 2066459f496SAndreas Gohr $users = $auth->retrieveUsers(0, -1, array('user' => 'dmi')); // substring 2073e2a8145SAndreas Gohr $this->assertEquals(array($expect['admin']), $users); 2086459f496SAndreas Gohr $count = $auth->getUserCount(array('user' => 'dmi')); 209f3c1c207SAndreas Gohr $this->assertSame(1, $count); 2106459f496SAndreas Gohr 211e19be516SAndreas Gohr // delete user 212e19be516SAndreas Gohr $num = $auth->deleteUsers(array('tester', 'foobar')); 213f3c1c207SAndreas Gohr $this->assertSame(1, $num); 2146459f496SAndreas Gohr 215f64dbc90SAndreas Gohr } 2165de3a6a5SAndreas Gohr 217f64dbc90SAndreas Gohr} 218