13f4d1534SAndreas Gohr<?php 23f4d1534SAndreas Gohr 33f4d1534SAndreas Gohr/** 4*32a211c7SAndreas Gohr * mysql tests for the authpdo plugin 53f4d1534SAndreas Gohr * 63f4d1534SAndreas Gohr * @group plugin_authpdo 73f4d1534SAndreas Gohr * @group plugins 83f4d1534SAndreas Gohr */ 93f4d1534SAndreas Gohrclass mysql_plugin_authpdo_test extends DokuWikiTest { 103f4d1534SAndreas Gohr 11*32a211c7SAndreas Gohr protected $driver = 'mysql'; 123f4d1534SAndreas Gohr protected $host = ''; 133f4d1534SAndreas Gohr protected $database = 'authpdo_testing'; 143f4d1534SAndreas Gohr protected $user = ''; 153f4d1534SAndreas Gohr protected $pass = ''; 16*32a211c7SAndreas Gohr protected $port = ''; 173f4d1534SAndreas Gohr 183f4d1534SAndreas Gohr public function setUp() { 193f4d1534SAndreas Gohr parent::setUp(); 20*32a211c7SAndreas Gohr $configuration = DOKU_UNITTEST . "{$this->driver}.conf.php"; 213f4d1534SAndreas Gohr if(!file_exists($configuration)) { 223f4d1534SAndreas Gohr return; 233f4d1534SAndreas Gohr } 243f4d1534SAndreas Gohr /** @var $conf array */ 253f4d1534SAndreas Gohr include $configuration; 263f4d1534SAndreas Gohr $this->host = $conf['host']; 273f4d1534SAndreas Gohr $this->user = $conf['user']; 283f4d1534SAndreas Gohr $this->pass = $conf['pass']; 29*32a211c7SAndreas Gohr $this->port = $conf['port']; 303f4d1534SAndreas Gohr } 313f4d1534SAndreas Gohr 323f4d1534SAndreas Gohr /** 33*32a211c7SAndreas Gohr * try to remove the last set up database 34*32a211c7SAndreas Gohr * 35*32a211c7SAndreas Gohr * it might still be there if something went wrong 36*32a211c7SAndreas Gohr */ 37*32a211c7SAndreas Gohr public function tearDown() { 38*32a211c7SAndreas Gohr parent::tearDown(); 39*32a211c7SAndreas Gohr $this->dropDatabase(); 40*32a211c7SAndreas Gohr } 41*32a211c7SAndreas Gohr 42*32a211c7SAndreas Gohr /** 43*32a211c7SAndreas Gohr * Check if database credentials and extensions exist 443f4d1534SAndreas Gohr */ 453f4d1534SAndreas Gohr public function test_requirements() { 463f4d1534SAndreas Gohr if(!$this->host || !$this->user) { 47*32a211c7SAndreas Gohr $this->markTestSkipped("Skipped {$this->driver} tests. Missing configuration"); 48*32a211c7SAndreas Gohr } 49*32a211c7SAndreas Gohr if(!class_exists('PDO')) { 50*32a211c7SAndreas Gohr $this->markTestSkipped("Skipped {$this->driver} tests. Missing PDO extension"); 51*32a211c7SAndreas Gohr } 52*32a211c7SAndreas Gohr if(!in_array($this->driver, pdo_drivers())) { 53*32a211c7SAndreas Gohr $this->markTestSkipped("Skipped {$this->driver} tests. Missing pdo_{$this->driver} extension"); 543f4d1534SAndreas Gohr } 553f4d1534SAndreas Gohr } 563f4d1534SAndreas Gohr 573f4d1534SAndreas Gohr /** 583f4d1534SAndreas Gohr * create the database for testing 593f4d1534SAndreas Gohr */ 603f4d1534SAndreas Gohr protected function createDatabase() { 613f4d1534SAndreas Gohr $pdo = new PDO( 62*32a211c7SAndreas Gohr "{$this->driver}:host={$this->host};port={$this->port}", $this->user, $this->pass, 633f4d1534SAndreas Gohr array( 643f4d1534SAndreas Gohr PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes 653f4d1534SAndreas Gohr ) 663f4d1534SAndreas Gohr ); 673f4d1534SAndreas Gohr $pdo->exec("DROP DATABASE IF EXISTS {$this->database}"); 683f4d1534SAndreas Gohr $pdo->exec("CREATE DATABASE {$this->database}"); 693f4d1534SAndreas Gohr $pdo = null; 703f4d1534SAndreas Gohr } 713f4d1534SAndreas Gohr 723f4d1534SAndreas Gohr /** 733f4d1534SAndreas Gohr * remove the database 743f4d1534SAndreas Gohr */ 753f4d1534SAndreas Gohr protected function dropDatabase() { 763f4d1534SAndreas Gohr $pdo = new PDO( 77*32a211c7SAndreas Gohr "{$this->driver}:host={$this->host};port={$this->port}", $this->user, $this->pass, 783f4d1534SAndreas Gohr array( 793f4d1534SAndreas Gohr PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes 803f4d1534SAndreas Gohr ) 813f4d1534SAndreas Gohr ); 82*32a211c7SAndreas Gohr try { 833f4d1534SAndreas Gohr $pdo->exec("DROP DATABASE IF EXISTS {$this->database}"); 84*32a211c7SAndreas Gohr } catch (PDOException $e) { 85*32a211c7SAndreas Gohr // ignore - sometimes this fails even though the database was deleted 86*32a211c7SAndreas Gohr } 873f4d1534SAndreas Gohr $pdo = null; 883f4d1534SAndreas Gohr } 893f4d1534SAndreas Gohr 903f4d1534SAndreas Gohr /** 913f4d1534SAndreas Gohr * imports a database dump 923f4d1534SAndreas Gohr * 933f4d1534SAndreas Gohr * @param $file 943f4d1534SAndreas Gohr */ 953f4d1534SAndreas Gohr protected function importDatabase($file) { 963f4d1534SAndreas Gohr // connect to database and import dump 973f4d1534SAndreas Gohr $pdo = null; 983f4d1534SAndreas Gohr $pdo = new PDO( 99*32a211c7SAndreas Gohr "{$this->driver}:dbname={$this->database};host={$this->host};port={$this->port}", $this->user, $this->pass, 1003f4d1534SAndreas Gohr array( 1013f4d1534SAndreas Gohr PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes 1023f4d1534SAndreas Gohr ) 1033f4d1534SAndreas Gohr ); 1043f4d1534SAndreas Gohr $sql = file_get_contents($file); 1053f4d1534SAndreas Gohr $pdo->exec($sql); 1063f4d1534SAndreas Gohr $pdo = null; 1073f4d1534SAndreas Gohr } 1083f4d1534SAndreas Gohr 1093f4d1534SAndreas Gohr /** 110964d95c6SAndreas Gohr * Run general tests on all users 111964d95c6SAndreas Gohr * 112964d95c6SAndreas Gohr * @param auth_plugin_authpdo $auth 113964d95c6SAndreas Gohr * @param array $users 114964d95c6SAndreas Gohr */ 115964d95c6SAndreas Gohr protected function runGeneralTests(auth_plugin_authpdo $auth, $users) { 116df01249eSAndreas Gohr global $conf; 117df01249eSAndreas Gohr $info = 'DSN: ' . $auth->getConf('dsn'); 118df01249eSAndreas Gohr $this->assertTrue($auth->success, $info); 119964d95c6SAndreas Gohr 120964d95c6SAndreas Gohr if($auth->canDo('getUsers')) { 121964d95c6SAndreas Gohr $list = $auth->retrieveUsers(); 122df01249eSAndreas Gohr $this->assertGreaterThanOrEqual(count($users), count($list), $info); 123df01249eSAndreas Gohr } 124df01249eSAndreas Gohr 125df01249eSAndreas Gohr if($auth->canDo('getGroups')) { 126df01249eSAndreas Gohr $list = $auth->retrieveGroups(); 127df01249eSAndreas Gohr $this->assertGreaterThanOrEqual(1, $list, $info); 128964d95c6SAndreas Gohr } 129964d95c6SAndreas Gohr 130964d95c6SAndreas Gohr if($auth->canDo('getUserCount')) { 131964d95c6SAndreas Gohr $count = $auth->getUserCount(); 132964d95c6SAndreas Gohr $this->assertGreaterThanOrEqual(count($users), $count); 133964d95c6SAndreas Gohr } 134df01249eSAndreas Gohr 135df01249eSAndreas Gohr if($auth->canDo('addUser')) { 136df01249eSAndreas Gohr $newuser = array( 137df01249eSAndreas Gohr 'user' => 'newuserfoobar', 138df01249eSAndreas Gohr 'name' => 'First LastFoobar', 139df01249eSAndreas Gohr 'pass' => 'password', 140df01249eSAndreas Gohr 'mail' => 'newuserfoobar@example.com', 141df01249eSAndreas Gohr 'grps' => array('acompletelynewgroup') 142df01249eSAndreas Gohr ); 143df01249eSAndreas Gohr $ok = $auth->createUser( 144df01249eSAndreas Gohr $newuser['user'], 145df01249eSAndreas Gohr $newuser['pass'], 146df01249eSAndreas Gohr $newuser['name'], 147df01249eSAndreas Gohr $newuser['mail'], 148df01249eSAndreas Gohr $newuser['grps'] 149df01249eSAndreas Gohr ); 150df01249eSAndreas Gohr $this->assertTrue($ok, $info); 151df01249eSAndreas Gohr $check = $auth->getUserData($newuser['user']); 152df01249eSAndreas Gohr $this->assertEquals($newuser['user'], $check['user'], $info); 153df01249eSAndreas Gohr $this->assertEquals($newuser['mail'], $check['mail'], $info); 154df01249eSAndreas Gohr $groups = array_merge($newuser['grps'], array($conf['defaultgroup'])); 155df01249eSAndreas Gohr $this->assertEquals($groups, $check['grps'], $info); 156df01249eSAndreas Gohr } 157964d95c6SAndreas Gohr } 158964d95c6SAndreas Gohr 159964d95c6SAndreas Gohr /** 1603f4d1534SAndreas Gohr * run all the tests with the given user, depending on the capabilities 1613f4d1534SAndreas Gohr * 1623f4d1534SAndreas Gohr * @param auth_plugin_authpdo $auth 1633f4d1534SAndreas Gohr * @param $user 1643f4d1534SAndreas Gohr */ 165964d95c6SAndreas Gohr protected function runUserTests(auth_plugin_authpdo $auth, $user) { 1663f4d1534SAndreas Gohr global $conf; 167df01249eSAndreas Gohr $info = 'DSN: ' . $auth->getConf('dsn') . ' User:' . $user['user']; 1683f4d1534SAndreas Gohr 1693f4d1534SAndreas Gohr // minimal setup 1703f4d1534SAndreas Gohr $this->assertTrue($auth->checkPass($user['user'], $user['pass']), $info); 1713f4d1534SAndreas Gohr $check = $auth->getUserData($user['user']); 1723f4d1534SAndreas Gohr $this->assertEquals($user['user'], $check['user'], $info); 1733f4d1534SAndreas Gohr $this->assertEquals($user['name'], $check['name'], $info); 1743f4d1534SAndreas Gohr $this->assertEquals($user['mail'], $check['mail'], $info); 1753f4d1534SAndreas Gohr $groups = array_merge($user['grps'], array($conf['defaultgroup'])); 1763f4d1534SAndreas Gohr $this->assertEquals($groups, $check['grps'], $info); 1773f4d1534SAndreas Gohr 178964d95c6SAndreas Gohr // getUsers 179964d95c6SAndreas Gohr if($auth->canDo('getUsers')) { 180964d95c6SAndreas Gohr $list = $auth->retrieveUsers(0, -1, array('user' => $user['user'])); 181964d95c6SAndreas Gohr $this->assertGreaterThanOrEqual(1, count($list)); 182964d95c6SAndreas Gohr $list = $auth->retrieveUsers(0, -1, array('name' => $user['name'])); 183964d95c6SAndreas Gohr $this->assertGreaterThanOrEqual(1, count($list)); 184964d95c6SAndreas Gohr $list = $auth->retrieveUsers(0, -1, array('mail' => $user['mail'])); 185964d95c6SAndreas Gohr $this->assertGreaterThanOrEqual(1, count($list)); 186964d95c6SAndreas Gohr } 187964d95c6SAndreas Gohr 188964d95c6SAndreas Gohr // getUserCount 189964d95c6SAndreas Gohr if($auth->canDo('getUserCount')) { 190964d95c6SAndreas Gohr $count = $auth->getUserCount(array('user' => $user['user'])); 191964d95c6SAndreas Gohr $this->assertGreaterThanOrEqual(1, $count); 192964d95c6SAndreas Gohr $count = $auth->getUserCount(array('name' => $user['name'])); 193964d95c6SAndreas Gohr $this->assertGreaterThanOrEqual(1, $count); 194964d95c6SAndreas Gohr $count = $auth->getUserCount(array('mail' => $user['mail'])); 195964d95c6SAndreas Gohr $this->assertGreaterThanOrEqual(1, $count); 196964d95c6SAndreas Gohr } 197964d95c6SAndreas Gohr 198df01249eSAndreas Gohr // modGroups 199df01249eSAndreas Gohr if($auth->canDo('modGroups')) { 200df01249eSAndreas Gohr $newgroup = 'foobar'; 201df01249eSAndreas Gohr $ok = $auth->modifyUser($user['user'], array('grps' => array($newgroup))); 202df01249eSAndreas Gohr $this->assertTrue($ok, $info); 203df01249eSAndreas Gohr $check = $auth->getUserData($user['user']); 204df01249eSAndreas Gohr $this->assertTrue(in_array($newgroup, $check['grps']), $info); 205df01249eSAndreas Gohr } 206df01249eSAndreas Gohr 2073f4d1534SAndreas Gohr // modPass 2083f4d1534SAndreas Gohr if($auth->canDo('modPass')) { 2093f4d1534SAndreas Gohr $newpass = 'foobar'; 2103f4d1534SAndreas Gohr $ok = $auth->modifyUser($user['user'], array('pass' => $newpass)); 2113f4d1534SAndreas Gohr $this->assertTrue($ok, $info); 2123f4d1534SAndreas Gohr $this->assertTrue($auth->checkPass($user['user'], $newpass), $info); 2133f4d1534SAndreas Gohr } 2143f4d1534SAndreas Gohr 2153f4d1534SAndreas Gohr // modMail 2163f4d1534SAndreas Gohr if($auth->canDo('modMail')) { 2173f4d1534SAndreas Gohr $newmail = 'foobar@example.com'; 2183f4d1534SAndreas Gohr $ok = $auth->modifyUser($user['user'], array('mail' => $newmail)); 2193f4d1534SAndreas Gohr $this->assertTrue($ok, $info); 2203f4d1534SAndreas Gohr $check = $auth->getUserData($user['user']); 2213f4d1534SAndreas Gohr $this->assertEquals($newmail, $check['mail'], $info); 2223f4d1534SAndreas Gohr } 2233f4d1534SAndreas Gohr 2243f4d1534SAndreas Gohr // modName 2253f4d1534SAndreas Gohr if($auth->canDo('modName')) { 2263f4d1534SAndreas Gohr $newname = 'FirstName Foobar'; 2273f4d1534SAndreas Gohr $ok = $auth->modifyUser($user['user'], array('name' => $newname)); 2283f4d1534SAndreas Gohr $this->assertTrue($ok, $info); 2293f4d1534SAndreas Gohr $check = $auth->getUserData($user['user']); 2303f4d1534SAndreas Gohr $this->assertEquals($newname, $check['name'], $info); 2313f4d1534SAndreas Gohr } 2323f4d1534SAndreas Gohr 2333f4d1534SAndreas Gohr // modLogin 2343f4d1534SAndreas Gohr if($auth->canDo('modLogin')) { 2353f4d1534SAndreas Gohr $newuser = 'foobar' . $user['user']; 2363f4d1534SAndreas Gohr $ok = $auth->modifyUser($user['user'], array('user' => $newuser)); 2373f4d1534SAndreas Gohr $this->assertTrue($ok, $info); 2383f4d1534SAndreas Gohr $check = $auth->getUserData($newuser); 2393f4d1534SAndreas Gohr $this->assertEquals($newuser, $check['user'], $info); 240df01249eSAndreas Gohr // rename back 241df01249eSAndreas Gohr $ok = $auth->modifyUser($newuser, array('user' => $user['user'])); 242df01249eSAndreas Gohr $this->assertTrue($ok, $info); 2433f4d1534SAndreas Gohr } 2443f4d1534SAndreas Gohr 245df01249eSAndreas Gohr // delUser 246df01249eSAndreas Gohr if($auth->canDo('delUser')) { 247df01249eSAndreas Gohr $num = $auth->deleteUsers(array($user['user'])); 248df01249eSAndreas Gohr $this->assertEquals(1, $num, $info); 249df01249eSAndreas Gohr $this->assertFalse($auth->getUserData($user['user']), $info); 250df01249eSAndreas Gohr } 2513f4d1534SAndreas Gohr } 2523f4d1534SAndreas Gohr 2533f4d1534SAndreas Gohr /** 2543f4d1534SAndreas Gohr * This triggers all the tests based on the dumps and configurations 2553f4d1534SAndreas Gohr * 2563f4d1534SAndreas Gohr * @depends test_requirements 2573f4d1534SAndreas Gohr */ 258*32a211c7SAndreas Gohr public function test_pgsql() { 2593f4d1534SAndreas Gohr global $conf; 2603f4d1534SAndreas Gohr 261*32a211c7SAndreas Gohr $files = glob(__DIR__ . "/{$this->driver}/*.php"); 2623f4d1534SAndreas Gohr foreach($files as $file) { 2633f4d1534SAndreas Gohr $dump = preg_replace('/\.php$/', '.sql', $file); 264df01249eSAndreas Gohr $this->database = 'authpdo_testing_' . basename($file, '.php'); 2653f4d1534SAndreas Gohr 2663f4d1534SAndreas Gohr $this->createDatabase(); 2673f4d1534SAndreas Gohr $this->importDatabase($dump); 2683f4d1534SAndreas Gohr 2693f4d1534SAndreas Gohr // Setup the configuration and initialize a new auth object 2703f4d1534SAndreas Gohr /** @var $data array */ 2713f4d1534SAndreas Gohr include $file; 2723f4d1534SAndreas Gohr 2733f4d1534SAndreas Gohr $conf['plugin']['authpdo'] = array(); 2743f4d1534SAndreas Gohr $conf['plugin']['authpdo'] = $data['conf']; 275*32a211c7SAndreas Gohr $conf['plugin']['authpdo']['dsn'] = "{$this->driver}:dbname={$this->database};host={$this->host};port={$this->port}"; 2763f4d1534SAndreas Gohr $conf['plugin']['authpdo']['user'] = $this->user; 2773f4d1534SAndreas Gohr $conf['plugin']['authpdo']['pass'] = $this->pass; 2783f4d1534SAndreas Gohr $conf['plugin']['authpdo']['debug'] = 1; 2793f4d1534SAndreas Gohr if($data['passcrypt']) $conf['passcrypt'] = $data['passcrypt']; 2803f4d1534SAndreas Gohr $auth = new auth_plugin_authpdo(); 2813f4d1534SAndreas Gohr 282964d95c6SAndreas Gohr $this->runGeneralTests($auth, $data['users']); 2833f4d1534SAndreas Gohr foreach($data['users'] as $user) { 284964d95c6SAndreas Gohr $this->runUserTests($auth, $user); 2853f4d1534SAndreas Gohr } 2863f4d1534SAndreas Gohr 2873f4d1534SAndreas Gohr $this->dropDatabase(); 2883f4d1534SAndreas Gohr } 2893f4d1534SAndreas Gohr } 2903f4d1534SAndreas Gohr 2913f4d1534SAndreas Gohr} 292