13f4d1534SAndreas Gohr<?php 23f4d1534SAndreas Gohr 33f4d1534SAndreas Gohr/** 432a211c7SAndreas 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 1132a211c7SAndreas Gohr protected $driver = 'mysql'; 123f4d1534SAndreas Gohr protected $host = ''; 133f4d1534SAndreas Gohr protected $database = 'authpdo_testing'; 143f4d1534SAndreas Gohr protected $user = ''; 153f4d1534SAndreas Gohr protected $pass = ''; 1632a211c7SAndreas Gohr protected $port = ''; 173f4d1534SAndreas Gohr 183f4d1534SAndreas Gohr public function setUp() { 193f4d1534SAndreas Gohr parent::setUp(); 2032a211c7SAndreas 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']; 2932a211c7SAndreas Gohr $this->port = $conf['port']; 303f4d1534SAndreas Gohr } 313f4d1534SAndreas Gohr 323f4d1534SAndreas Gohr /** 3332a211c7SAndreas Gohr * try to remove the last set up database 3432a211c7SAndreas Gohr * 3532a211c7SAndreas Gohr * it might still be there if something went wrong 3632a211c7SAndreas Gohr */ 3732a211c7SAndreas Gohr public function tearDown() { 3832a211c7SAndreas Gohr parent::tearDown(); 3932a211c7SAndreas Gohr $this->dropDatabase(); 4032a211c7SAndreas Gohr } 4132a211c7SAndreas Gohr 4232a211c7SAndreas Gohr /** 4332a211c7SAndreas Gohr * Check if database credentials and extensions exist 443f4d1534SAndreas Gohr */ 453f4d1534SAndreas Gohr public function test_requirements() { 463f4d1534SAndreas Gohr if(!$this->host || !$this->user) { 4732a211c7SAndreas Gohr $this->markTestSkipped("Skipped {$this->driver} tests. Missing configuration"); 4832a211c7SAndreas Gohr } 4932a211c7SAndreas Gohr if(!class_exists('PDO')) { 5032a211c7SAndreas Gohr $this->markTestSkipped("Skipped {$this->driver} tests. Missing PDO extension"); 5132a211c7SAndreas Gohr } 5232a211c7SAndreas Gohr if(!in_array($this->driver, pdo_drivers())) { 5332a211c7SAndreas 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( 6232a211c7SAndreas 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( 7732a211c7SAndreas 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 ); 8232a211c7SAndreas Gohr try { 833f4d1534SAndreas Gohr $pdo->exec("DROP DATABASE IF EXISTS {$this->database}"); 8432a211c7SAndreas Gohr } catch (PDOException $e) { 8532a211c7SAndreas Gohr // ignore - sometimes this fails even though the database was deleted 8632a211c7SAndreas 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( 9932a211c7SAndreas 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 /** 254*c3f4c777SAndreas Gohr * prepares the individual configurations for testing 2553f4d1534SAndreas Gohr * 256*c3f4c777SAndreas Gohr * @return array 2573f4d1534SAndreas Gohr */ 258*c3f4c777SAndreas Gohr public function data_provider() { 259*c3f4c777SAndreas Gohr $testdata = array(); 2603f4d1534SAndreas Gohr 26132a211c7SAndreas Gohr $files = glob(__DIR__ . "/{$this->driver}/*.php"); 2623f4d1534SAndreas Gohr foreach($files as $file) { 2633f4d1534SAndreas Gohr $dump = preg_replace('/\.php$/', '.sql', $file); 264*c3f4c777SAndreas Gohr $dbname = 'authpdo_testing_' . basename($file, '.php'); 265*c3f4c777SAndreas Gohr 266*c3f4c777SAndreas Gohr /** @var $data array */ 267*c3f4c777SAndreas Gohr include $file; 268*c3f4c777SAndreas Gohr 269*c3f4c777SAndreas Gohr $testdata[] = array($dbname, $dump, $data); 270*c3f4c777SAndreas Gohr } 271*c3f4c777SAndreas Gohr 272*c3f4c777SAndreas Gohr return $testdata; 273*c3f4c777SAndreas Gohr } 274*c3f4c777SAndreas Gohr 275*c3f4c777SAndreas Gohr /** 276*c3f4c777SAndreas Gohr * This triggers all the tests based on the dumps and configurations 277*c3f4c777SAndreas Gohr * 278*c3f4c777SAndreas Gohr * @dataProvider data_provider 279*c3f4c777SAndreas Gohr * @depends test_requirements 280*c3f4c777SAndreas Gohr * @param string $dbname Name of the database to use 281*c3f4c777SAndreas Gohr * @param string $dump The path to the dump file to import 282*c3f4c777SAndreas Gohr * @param array|string $data config and test user setup. When a string is passed, test is skipped with that msg 283*c3f4c777SAndreas Gohr */ 284*c3f4c777SAndreas Gohr public function test_database($dbname, $dump, $data){ 285*c3f4c777SAndreas Gohr global $conf; 286*c3f4c777SAndreas Gohr 287*c3f4c777SAndreas Gohr if(!is_array($data)) { 288*c3f4c777SAndreas Gohr $this->markTestSkipped($data); 289*c3f4c777SAndreas Gohr return; 290*c3f4c777SAndreas Gohr } 291*c3f4c777SAndreas Gohr 292*c3f4c777SAndreas Gohr $this->database = $dbname; 2933f4d1534SAndreas Gohr 2943f4d1534SAndreas Gohr $this->createDatabase(); 2953f4d1534SAndreas Gohr $this->importDatabase($dump); 2963f4d1534SAndreas Gohr 2973f4d1534SAndreas Gohr // Setup the configuration and initialize a new auth object 2983f4d1534SAndreas Gohr $conf['plugin']['authpdo'] = array(); 2993f4d1534SAndreas Gohr $conf['plugin']['authpdo'] = $data['conf']; 30032a211c7SAndreas Gohr $conf['plugin']['authpdo']['dsn'] = "{$this->driver}:dbname={$this->database};host={$this->host};port={$this->port}"; 3013f4d1534SAndreas Gohr $conf['plugin']['authpdo']['user'] = $this->user; 3023f4d1534SAndreas Gohr $conf['plugin']['authpdo']['pass'] = $this->pass; 3033f4d1534SAndreas Gohr $conf['plugin']['authpdo']['debug'] = 1; 3043f4d1534SAndreas Gohr if($data['passcrypt']) $conf['passcrypt'] = $data['passcrypt']; 3053f4d1534SAndreas Gohr $auth = new auth_plugin_authpdo(); 3063f4d1534SAndreas Gohr 307964d95c6SAndreas Gohr $this->runGeneralTests($auth, $data['users']); 3083f4d1534SAndreas Gohr foreach($data['users'] as $user) { 309964d95c6SAndreas Gohr $this->runUserTests($auth, $user); 3103f4d1534SAndreas Gohr } 3113f4d1534SAndreas Gohr 3123f4d1534SAndreas Gohr $this->dropDatabase(); 3133f4d1534SAndreas Gohr } 3143f4d1534SAndreas Gohr 3153f4d1534SAndreas Gohr} 316