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 addGroup($group) { 18 return parent::addGroup($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 test_pdo_sqlite_support() { 33 if(!class_exists('PDO') || !in_array('sqlite',PDO::getAvailableDrivers())) { 34 $this->markTestSkipped('skipping all authpdo tests for sqlite. Need PDO_sqlite extension'); 35 } 36 } 37 38 public function setUp() { 39 parent::setUp(); 40 $this->dbfile = tempnam('/tmp/', 'pluginpdo_test_'); 41 copy(__DIR__ . '/test.sqlite3', $this->dbfile); 42 43 global $conf; 44 45 $conf['plugin']['authpdo']['debug'] = 1; 46 $conf['plugin']['authpdo']['dsn'] = 'sqlite:' . $this->dbfile; 47 $conf['plugin']['authpdo']['user'] = ''; 48 $conf['plugin']['authpdo']['pass'] = ''; 49 50 $conf['plugin']['authpdo']['select-user'] = 'SELECT id AS uid, login AS user, name, pass AS clear, mail FROM user WHERE login = :user'; 51 $conf['plugin']['authpdo']['select-user-groups'] = 'SELECT * FROM member AS m, "group" AS g WHERE m.gid = g.id AND m.uid = :uid'; 52 $conf['plugin']['authpdo']['select-groups'] = 'SELECT id AS gid, "group" FROM "group"'; 53 54 $conf['plugin']['authpdo']['insert-user'] = 'INSERT INTO user (login, pass, name, mail) VALUES (:user, :hash, :name, :mail)'; 55 $conf['plugin']['authpdo']['delete-user'] = 'DELETE FROM user WHERE id = :uid'; 56 57 $conf['plugin']['authpdo']['list-users'] = 'SELECT DISTINCT login as user 58 FROM user U, member M, "group" G 59 WHERE U.id = M.uid 60 AND M.gid = G.id 61 AND G."group" LIKE :group 62 AND U.login LIKE :user 63 AND U.name LIKE :name 64 AND U.mail LIKE :mail 65 ORDER BY login 66 LIMIT :start,:limit'; 67 68 $conf['plugin']['authpdo']['count-users'] = 'SELECT COUNT(DISTINCT login) as count 69 FROM user U, member M, "group" G 70 WHERE U.id = M.uid 71 AND M.gid = G.id 72 AND G."group" LIKE :group 73 AND U.login LIKE :user 74 AND U.name LIKE :name 75 AND U.mail LIKE :mail'; 76 77 78 $conf['plugin']['authpdo']['update-user-login'] = 'UPDATE user SET login = :newlogin WHERE id = :uid'; 79 $conf['plugin']['authpdo']['update-user-info'] = 'UPDATE user SET name = :name, mail = :mail WHERE id = :uid'; 80 $conf['plugin']['authpdo']['update-user-pass'] = 'UPDATE user SET pass = :hash WHERE id = :uid'; 81 82 $conf['plugin']['authpdo']['insert-group'] = 'INSERT INTO "group" ("group") VALUES (:group)'; 83 $conf['plugin']['authpdo']['join-group'] = 'INSERT INTO member (uid, gid) VALUES (:uid, :gid)'; 84 $conf['plugin']['authpdo']['leave-group'] = 'DELETE FROM member WHERE uid = :uid AND gid = :gid'; 85 } 86 87 public function tearDown() { 88 parent::tearDown(); 89 unlink($this->dbfile); 90 } 91 92 /** 93 * @depends test_pdo_sqlite_support 94 */ 95 public function test_internals() { 96 $auth = new testable_auth_plugin_authpdo(); 97 98 $groups = $auth->_selectGroups(); 99 $this->assertArrayHasKey('user', $groups); 100 $this->assertEquals(1, $groups['user']['gid']); 101 $this->assertArrayHasKey('admin', $groups); 102 $this->assertEquals(2, $groups['admin']['gid']); 103 104 $ok = $auth->addGroup('test'); 105 $this->assertTrue($ok); 106 $groups = $auth->_selectGroups(); 107 $this->assertArrayHasKey('test', $groups); 108 $this->assertEquals(3, $groups['test']['gid']); 109 } 110 111 /** 112 * @depends test_pdo_sqlite_support 113 */ 114 public function test_userinfo() { 115 global $conf; 116 $auth = new auth_plugin_authpdo(); 117 118 // clear text pasword (with default config above 119 $this->assertFalse($auth->checkPass('nobody', 'nope')); 120 $this->assertFalse($auth->checkPass('admin', 'nope')); 121 $this->assertTrue($auth->checkPass('admin', 'password')); 122 123 // now with a hashed password 124 $conf['plugin']['authpdo']['select-user'] = 'SELECT id AS uid, login AS user, name, pass AS hash, mail FROM user WHERE login = :user'; 125 $this->assertFalse($auth->checkPass('admin', 'password')); 126 $this->assertFalse($auth->checkPass('user', md5('password'))); 127 128 // access user data 129 $info = $auth->getUserData('admin'); 130 $this->assertEquals('admin', $info['user']); 131 $this->assertEquals('The Admin', $info['name']); 132 $this->assertEquals('admin@example.com', $info['mail']); 133 $this->assertEquals(array('admin', 'user'), $info['grps']); 134 135 // group retrieval 136 $this->assertEquals(array('admin', 'user'), $auth->retrieveGroups()); 137 $this->assertEquals(array('user'), $auth->retrieveGroups(1)); 138 $this->assertEquals(array('admin'), $auth->retrieveGroups(0, 1)); 139 140 // user creation 141 $auth->createUser('test', 'password', 'A Test user', 'test@example.com', array('newgroup')); 142 $info = $auth->getUserData('test'); 143 $this->assertEquals('test', $info['user']); 144 $this->assertEquals('A Test user', $info['name']); 145 $this->assertEquals('test@example.com', $info['mail']); 146 $this->assertEquals(array('newgroup', 'user'), $info['grps']); 147 $this->assertEquals(array('admin', 'newgroup', 'user'), $auth->retrieveGroups()); 148 149 // user modification 150 $auth->modifyUser('test', array('user' => 'tester', 'name' => 'The Test User', 'pass' => 'secret')); 151 $info = $auth->getUserData('tester'); 152 $this->assertEquals('tester', $info['user']); 153 $this->assertEquals('The Test User', $info['name']); 154 $this->assertTrue($auth->checkPass('tester','secret')); 155 156 // move user to different groups 157 $auth->modifyUser('tester', array('grps' => array('user', 'admin', 'another'))); 158 $info = $auth->getUserData('tester'); 159 $this->assertEquals(array('admin', 'another', 'user'), $info['grps']); 160 161 162 $expect = array( 163 'admin' => array( 164 'user' => 'admin', 165 'name' => 'The Admin', 166 'mail' => 'admin@example.com', 167 'uid' => '1', 168 'grps' => array('admin', 'user') 169 ), 170 'user' => array( 171 'user' => 'user', 172 'name' => 'A normal user', 173 'mail' => 'user@example.com', 174 'uid' => '2', 175 'grps' => array('user') 176 ), 177 'tester' => array( 178 'user' => 'tester', 179 'name' => 'The Test User', 180 'mail' => 'test@example.com', 181 'uid' => '3', 182 'grps' => array('admin', 'another', 'user') 183 ) 184 ); 185 186 // list users 187 $users = $auth->retrieveUsers(); 188 $this->assertEquals(array($expect['admin'], $expect['tester'], $expect['user']), $users); 189 190 $users = $auth->retrieveUsers(1); // offset 191 $this->assertEquals(array($expect['tester'], $expect['user']), $users); 192 193 $users = $auth->retrieveUsers(1, 1); // offset + limit 194 $this->assertEquals(array($expect['tester']), $users); 195 196 $users = $auth->retrieveUsers(0, -1, array('group' => 'admin')); // full group 197 $this->assertEquals(array($expect['admin'], $expect['tester']), $users); 198 $count = $auth->getUserCount(array('grps' => 'admin')); 199 $this->assertSame(2, $count); 200 201 $users = $auth->retrieveUsers(0, -1, array('group' => 'dmi')); // substring 202 $this->assertEquals(array($expect['admin'], $expect['tester']), $users); 203 $count = $auth->getUserCount(array('grps' => 'dmi')); 204 $this->assertSame(2, $count); 205 206 $users = $auth->retrieveUsers(0, -1, array('user' => 'dmi')); // substring 207 $this->assertEquals(array($expect['admin']), $users); 208 $count = $auth->getUserCount(array('user' => 'dmi')); 209 $this->assertSame(1, $count); 210 211 // delete user 212 $num = $auth->deleteUsers(array('tester', 'foobar')); 213 $this->assertSame(1, $num); 214 215 } 216 217} 218