Completed
Push — authpdo ( 1600c7...3f4d15 )
by Andreas
05:51
created

mysql_plugin_authpdo_test::test_requirements()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 3
eloc 3
nc 2
nop 0
1
<?php
2
3
/**
4
 * MySQL tests for the authpdo plugin
5
 *
6
 * @group plugin_authpdo
7
 * @group plugins
8
 */
9
class mysql_plugin_authpdo_test extends DokuWikiTest {
10
11
    protected $host = '';
12
    protected $database = 'authpdo_testing';
13
    protected $user = '';
14
    protected $pass = '';
15
16
    public function setUp() {
17
        parent::setUp();
18
        $configuration = DOKU_UNITTEST . 'mysql.conf.php';
19
        if(!file_exists($configuration)) {
20
            return;
21
        }
22
        /** @var $conf array */
23
        include $configuration;
24
        $this->host = $conf['host'];
0 ignored issues
show
Bug introduced by
The variable $conf does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
25
        $this->user = $conf['user'];
26
        $this->pass = $conf['pass'];
27
    }
28
29
    /**
30
     * Check if database credentials exist
31
     */
32
    public function test_requirements() {
33
        if(!$this->host || !$this->user) {
34
            $this->markTestSkipped("Skipped mysql tests. Missing configuration");
35
        }
36
    }
37
38
    /**
39
     * create the database for testing
40
     */
41
    protected function createDatabase() {
42
        $pdo = new PDO(
43
            "mysql:dbname=;host={$this->host}", $this->user, $this->pass,
44
            array(
45
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
46
            )
47
        );
48
        $pdo->exec("DROP DATABASE IF EXISTS {$this->database}");
49
        $pdo->exec("CREATE DATABASE {$this->database}");
50
        $pdo = null;
0 ignored issues
show
Unused Code introduced by
$pdo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
    }
52
53
    /**
54
     * remove the database
55
     */
56
    protected function dropDatabase() {
57
        $pdo = new PDO(
58
            "mysql:dbname={$this->database};host={$this->host}", $this->user, $this->pass,
59
            array(
60
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
61
            )
62
        );
63
        $pdo->exec("DROP DATABASE IF EXISTS {$this->database}");
64
        $pdo = null;
0 ignored issues
show
Unused Code introduced by
$pdo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
65
    }
66
67
    /**
68
     * imports a database dump
69
     *
70
     * @param $file
71
     */
72
    protected function importDatabase($file) {
73
        // connect to database and import dump
74
        $pdo = null;
0 ignored issues
show
Unused Code introduced by
$pdo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
75
        $pdo = new PDO(
76
            "mysql:dbname={$this->database};host={$this->host}", $this->user, $this->pass,
77
            array(
78
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
79
            )
80
        );
81
        $sql = file_get_contents($file);
82
        $pdo->exec($sql);
83
        $pdo = null;
0 ignored issues
show
Unused Code introduced by
$pdo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
84
    }
85
86
    /**
87
     * run all the tests with the given user, depending on the capabilities
88
     *
89
     * @param auth_plugin_authpdo $auth
90
     * @param $user
91
     */
92
    protected function runTests(auth_plugin_authpdo $auth, $user) {
93
        global $conf;
94
        $info = 'testing ' . $user['user'];
95
        $this->assertTrue($auth->success, $info);
96
97
        // minimal setup
98
        $this->assertTrue($auth->checkPass($user['user'], $user['pass']), $info);
99
        $check = $auth->getUserData($user['user']);
100
        $this->assertEquals($user['user'], $check['user'], $info);
101
        $this->assertEquals($user['name'], $check['name'], $info);
102
        $this->assertEquals($user['mail'], $check['mail'], $info);
103
        $groups = array_merge($user['grps'], array($conf['defaultgroup']));
104
        $this->assertEquals($groups, $check['grps'], $info);
105
106
        // modPass
107
        if($auth->canDo('modPass')) {
108
            $newpass = 'foobar';
109
            $ok = $auth->modifyUser($user['user'], array('pass' => $newpass));
110
            $this->assertTrue($ok, $info);
111
            $this->assertTrue($auth->checkPass($user['user'], $newpass), $info);
112
        }
113
114
        // modMail
115
        if($auth->canDo('modMail')) {
116
            $newmail = '[email protected]';
117
            $ok = $auth->modifyUser($user['user'], array('mail' => $newmail));
118
            $this->assertTrue($ok, $info);
119
            $check = $auth->getUserData($user['user']);
120
            $this->assertEquals($newmail, $check['mail'], $info);
121
        }
122
123
        // modName
124
        if($auth->canDo('modName')) {
125
            $newname = 'FirstName Foobar';
126
            $ok = $auth->modifyUser($user['user'], array('name' => $newname));
127
            $this->assertTrue($ok, $info);
128
            $check = $auth->getUserData($user['user']);
129
            $this->assertEquals($newname, $check['name'], $info);
130
        }
131
132
        // modLogin
133
        if($auth->canDo('modLogin')) {
134
            $newuser = 'foobar'.$user['user'];
135
            $ok = $auth->modifyUser($user['user'], array('user' => $newuser));
136
            $this->assertTrue($ok, $info);
137
            $check = $auth->getUserData($newuser);
138
            $this->assertEquals($newuser, $check['user'], $info);
139
        }
140
141
    }
142
143
    /**
144
     * This triggers all the tests based on the dumps and configurations
145
     *
146
     * @depends test_requirements
147
     */
148
    public function test_mysql() {
149
        global $conf;
150
151
        $files = glob(__DIR__ . '/mysql/*.php');
152
        foreach($files as $file) {
153
            $dump = preg_replace('/\.php$/', '.sql', $file);
154
155
            $this->createDatabase();
156
            $this->importDatabase($dump);
157
158
            // Setup the configuration and initialize a new auth object
159
            /** @var $data array */
160
            include $file;
161
162
            $conf['plugin']['authpdo'] = array();
163
            $conf['plugin']['authpdo'] = $data['conf'];
0 ignored issues
show
Bug introduced by
The variable $data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
164
            $conf['plugin']['authpdo']['dsn'] = "mysql:dbname={$this->database};host={$this->host}";
165
            $conf['plugin']['authpdo']['user'] = $this->user;
166
            $conf['plugin']['authpdo']['pass'] = $this->pass;
167
            $conf['plugin']['authpdo']['debug'] = 1;
168
            if($data['passcrypt']) $conf['passcrypt'] = $data['passcrypt'];
169
            $auth = new auth_plugin_authpdo();
170
171
            foreach($data['users'] as $user) {
172
                $this->runTests($auth, $user);
173
            }
174
175
            $this->dropDatabase();
176
        }
177
    }
178
179
}
180