xref: /dokuwiki/lib/plugins/authpdo/_test/mysql.test.php (revision 3f4d15349d786af7992b9e7377fcd5298eb6eb4b)
1*3f4d1534SAndreas Gohr<?php
2*3f4d1534SAndreas Gohr
3*3f4d1534SAndreas Gohr/**
4*3f4d1534SAndreas Gohr * MySQL tests for the authpdo plugin
5*3f4d1534SAndreas Gohr *
6*3f4d1534SAndreas Gohr * @group plugin_authpdo
7*3f4d1534SAndreas Gohr * @group plugins
8*3f4d1534SAndreas Gohr */
9*3f4d1534SAndreas Gohrclass mysql_plugin_authpdo_test extends DokuWikiTest {
10*3f4d1534SAndreas Gohr
11*3f4d1534SAndreas Gohr    protected $host = '';
12*3f4d1534SAndreas Gohr    protected $database = 'authpdo_testing';
13*3f4d1534SAndreas Gohr    protected $user = '';
14*3f4d1534SAndreas Gohr    protected $pass = '';
15*3f4d1534SAndreas Gohr
16*3f4d1534SAndreas Gohr    public function setUp() {
17*3f4d1534SAndreas Gohr        parent::setUp();
18*3f4d1534SAndreas Gohr        $configuration = DOKU_UNITTEST . 'mysql.conf.php';
19*3f4d1534SAndreas Gohr        if(!file_exists($configuration)) {
20*3f4d1534SAndreas Gohr            return;
21*3f4d1534SAndreas Gohr        }
22*3f4d1534SAndreas Gohr        /** @var $conf array */
23*3f4d1534SAndreas Gohr        include $configuration;
24*3f4d1534SAndreas Gohr        $this->host = $conf['host'];
25*3f4d1534SAndreas Gohr        $this->user = $conf['user'];
26*3f4d1534SAndreas Gohr        $this->pass = $conf['pass'];
27*3f4d1534SAndreas Gohr    }
28*3f4d1534SAndreas Gohr
29*3f4d1534SAndreas Gohr    /**
30*3f4d1534SAndreas Gohr     * Check if database credentials exist
31*3f4d1534SAndreas Gohr     */
32*3f4d1534SAndreas Gohr    public function test_requirements() {
33*3f4d1534SAndreas Gohr        if(!$this->host || !$this->user) {
34*3f4d1534SAndreas Gohr            $this->markTestSkipped("Skipped mysql tests. Missing configuration");
35*3f4d1534SAndreas Gohr        }
36*3f4d1534SAndreas Gohr    }
37*3f4d1534SAndreas Gohr
38*3f4d1534SAndreas Gohr    /**
39*3f4d1534SAndreas Gohr     * create the database for testing
40*3f4d1534SAndreas Gohr     */
41*3f4d1534SAndreas Gohr    protected function createDatabase() {
42*3f4d1534SAndreas Gohr        $pdo = new PDO(
43*3f4d1534SAndreas Gohr            "mysql:dbname=;host={$this->host}", $this->user, $this->pass,
44*3f4d1534SAndreas Gohr            array(
45*3f4d1534SAndreas Gohr                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
46*3f4d1534SAndreas Gohr            )
47*3f4d1534SAndreas Gohr        );
48*3f4d1534SAndreas Gohr        $pdo->exec("DROP DATABASE IF EXISTS {$this->database}");
49*3f4d1534SAndreas Gohr        $pdo->exec("CREATE DATABASE {$this->database}");
50*3f4d1534SAndreas Gohr        $pdo = null;
51*3f4d1534SAndreas Gohr    }
52*3f4d1534SAndreas Gohr
53*3f4d1534SAndreas Gohr    /**
54*3f4d1534SAndreas Gohr     * remove the database
55*3f4d1534SAndreas Gohr     */
56*3f4d1534SAndreas Gohr    protected function dropDatabase() {
57*3f4d1534SAndreas Gohr        $pdo = new PDO(
58*3f4d1534SAndreas Gohr            "mysql:dbname={$this->database};host={$this->host}", $this->user, $this->pass,
59*3f4d1534SAndreas Gohr            array(
60*3f4d1534SAndreas Gohr                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
61*3f4d1534SAndreas Gohr            )
62*3f4d1534SAndreas Gohr        );
63*3f4d1534SAndreas Gohr        $pdo->exec("DROP DATABASE IF EXISTS {$this->database}");
64*3f4d1534SAndreas Gohr        $pdo = null;
65*3f4d1534SAndreas Gohr    }
66*3f4d1534SAndreas Gohr
67*3f4d1534SAndreas Gohr    /**
68*3f4d1534SAndreas Gohr     * imports a database dump
69*3f4d1534SAndreas Gohr     *
70*3f4d1534SAndreas Gohr     * @param $file
71*3f4d1534SAndreas Gohr     */
72*3f4d1534SAndreas Gohr    protected function importDatabase($file) {
73*3f4d1534SAndreas Gohr        // connect to database and import dump
74*3f4d1534SAndreas Gohr        $pdo = null;
75*3f4d1534SAndreas Gohr        $pdo = new PDO(
76*3f4d1534SAndreas Gohr            "mysql:dbname={$this->database};host={$this->host}", $this->user, $this->pass,
77*3f4d1534SAndreas Gohr            array(
78*3f4d1534SAndreas Gohr                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
79*3f4d1534SAndreas Gohr            )
80*3f4d1534SAndreas Gohr        );
81*3f4d1534SAndreas Gohr        $sql = file_get_contents($file);
82*3f4d1534SAndreas Gohr        $pdo->exec($sql);
83*3f4d1534SAndreas Gohr        $pdo = null;
84*3f4d1534SAndreas Gohr    }
85*3f4d1534SAndreas Gohr
86*3f4d1534SAndreas Gohr    /**
87*3f4d1534SAndreas Gohr     * run all the tests with the given user, depending on the capabilities
88*3f4d1534SAndreas Gohr     *
89*3f4d1534SAndreas Gohr     * @param auth_plugin_authpdo $auth
90*3f4d1534SAndreas Gohr     * @param $user
91*3f4d1534SAndreas Gohr     */
92*3f4d1534SAndreas Gohr    protected function runTests(auth_plugin_authpdo $auth, $user) {
93*3f4d1534SAndreas Gohr        global $conf;
94*3f4d1534SAndreas Gohr        $info = 'testing ' . $user['user'];
95*3f4d1534SAndreas Gohr        $this->assertTrue($auth->success, $info);
96*3f4d1534SAndreas Gohr
97*3f4d1534SAndreas Gohr        // minimal setup
98*3f4d1534SAndreas Gohr        $this->assertTrue($auth->checkPass($user['user'], $user['pass']), $info);
99*3f4d1534SAndreas Gohr        $check = $auth->getUserData($user['user']);
100*3f4d1534SAndreas Gohr        $this->assertEquals($user['user'], $check['user'], $info);
101*3f4d1534SAndreas Gohr        $this->assertEquals($user['name'], $check['name'], $info);
102*3f4d1534SAndreas Gohr        $this->assertEquals($user['mail'], $check['mail'], $info);
103*3f4d1534SAndreas Gohr        $groups = array_merge($user['grps'], array($conf['defaultgroup']));
104*3f4d1534SAndreas Gohr        $this->assertEquals($groups, $check['grps'], $info);
105*3f4d1534SAndreas Gohr
106*3f4d1534SAndreas Gohr        // modPass
107*3f4d1534SAndreas Gohr        if($auth->canDo('modPass')) {
108*3f4d1534SAndreas Gohr            $newpass = 'foobar';
109*3f4d1534SAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('pass' => $newpass));
110*3f4d1534SAndreas Gohr            $this->assertTrue($ok, $info);
111*3f4d1534SAndreas Gohr            $this->assertTrue($auth->checkPass($user['user'], $newpass), $info);
112*3f4d1534SAndreas Gohr        }
113*3f4d1534SAndreas Gohr
114*3f4d1534SAndreas Gohr        // modMail
115*3f4d1534SAndreas Gohr        if($auth->canDo('modMail')) {
116*3f4d1534SAndreas Gohr            $newmail = 'foobar@example.com';
117*3f4d1534SAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('mail' => $newmail));
118*3f4d1534SAndreas Gohr            $this->assertTrue($ok, $info);
119*3f4d1534SAndreas Gohr            $check = $auth->getUserData($user['user']);
120*3f4d1534SAndreas Gohr            $this->assertEquals($newmail, $check['mail'], $info);
121*3f4d1534SAndreas Gohr        }
122*3f4d1534SAndreas Gohr
123*3f4d1534SAndreas Gohr        // modName
124*3f4d1534SAndreas Gohr        if($auth->canDo('modName')) {
125*3f4d1534SAndreas Gohr            $newname = 'FirstName Foobar';
126*3f4d1534SAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('name' => $newname));
127*3f4d1534SAndreas Gohr            $this->assertTrue($ok, $info);
128*3f4d1534SAndreas Gohr            $check = $auth->getUserData($user['user']);
129*3f4d1534SAndreas Gohr            $this->assertEquals($newname, $check['name'], $info);
130*3f4d1534SAndreas Gohr        }
131*3f4d1534SAndreas Gohr
132*3f4d1534SAndreas Gohr        // modLogin
133*3f4d1534SAndreas Gohr        if($auth->canDo('modLogin')) {
134*3f4d1534SAndreas Gohr            $newuser = 'foobar'.$user['user'];
135*3f4d1534SAndreas Gohr            $ok = $auth->modifyUser($user['user'], array('user' => $newuser));
136*3f4d1534SAndreas Gohr            $this->assertTrue($ok, $info);
137*3f4d1534SAndreas Gohr            $check = $auth->getUserData($newuser);
138*3f4d1534SAndreas Gohr            $this->assertEquals($newuser, $check['user'], $info);
139*3f4d1534SAndreas Gohr        }
140*3f4d1534SAndreas Gohr
141*3f4d1534SAndreas Gohr    }
142*3f4d1534SAndreas Gohr
143*3f4d1534SAndreas Gohr    /**
144*3f4d1534SAndreas Gohr     * This triggers all the tests based on the dumps and configurations
145*3f4d1534SAndreas Gohr     *
146*3f4d1534SAndreas Gohr     * @depends test_requirements
147*3f4d1534SAndreas Gohr     */
148*3f4d1534SAndreas Gohr    public function test_mysql() {
149*3f4d1534SAndreas Gohr        global $conf;
150*3f4d1534SAndreas Gohr
151*3f4d1534SAndreas Gohr        $files = glob(__DIR__ . '/mysql/*.php');
152*3f4d1534SAndreas Gohr        foreach($files as $file) {
153*3f4d1534SAndreas Gohr            $dump = preg_replace('/\.php$/', '.sql', $file);
154*3f4d1534SAndreas Gohr
155*3f4d1534SAndreas Gohr            $this->createDatabase();
156*3f4d1534SAndreas Gohr            $this->importDatabase($dump);
157*3f4d1534SAndreas Gohr
158*3f4d1534SAndreas Gohr            // Setup the configuration and initialize a new auth object
159*3f4d1534SAndreas Gohr            /** @var $data array */
160*3f4d1534SAndreas Gohr            include $file;
161*3f4d1534SAndreas Gohr
162*3f4d1534SAndreas Gohr            $conf['plugin']['authpdo'] = array();
163*3f4d1534SAndreas Gohr            $conf['plugin']['authpdo'] = $data['conf'];
164*3f4d1534SAndreas Gohr            $conf['plugin']['authpdo']['dsn'] = "mysql:dbname={$this->database};host={$this->host}";
165*3f4d1534SAndreas Gohr            $conf['plugin']['authpdo']['user'] = $this->user;
166*3f4d1534SAndreas Gohr            $conf['plugin']['authpdo']['pass'] = $this->pass;
167*3f4d1534SAndreas Gohr            $conf['plugin']['authpdo']['debug'] = 1;
168*3f4d1534SAndreas Gohr            if($data['passcrypt']) $conf['passcrypt'] = $data['passcrypt'];
169*3f4d1534SAndreas Gohr            $auth = new auth_plugin_authpdo();
170*3f4d1534SAndreas Gohr
171*3f4d1534SAndreas Gohr            foreach($data['users'] as $user) {
172*3f4d1534SAndreas Gohr                $this->runTests($auth, $user);
173*3f4d1534SAndreas Gohr            }
174*3f4d1534SAndreas Gohr
175*3f4d1534SAndreas Gohr            $this->dropDatabase();
176*3f4d1534SAndreas Gohr        }
177*3f4d1534SAndreas Gohr    }
178*3f4d1534SAndreas Gohr
179*3f4d1534SAndreas Gohr}
180