Completed
Push — authpdo ( 3f4d15...964d95 )
by Andreas
05:10
created

mysql_plugin_authpdo_test::runUserTests()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 69
Code Lines 47

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 69
rs 6.9081
cc 7
eloc 47
nc 64
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 general tests on all users
88
     *
89
     * @param auth_plugin_authpdo $auth
90
     * @param array $users
91
     */
92
    protected function runGeneralTests(auth_plugin_authpdo $auth, $users) {
93
        $this->assertTrue($auth->success, 'intialize auth');
94
95
        if($auth->canDo('getUsers')) {
96
            $list = $auth->retrieveUsers();
97
            $this->assertGreaterThanOrEqual(count($users), count($list));
98
        }
99
100
        if($auth->canDo('getUserCount')) {
101
            $count = $auth->getUserCount();
102
            $this->assertGreaterThanOrEqual(count($users), $count);
103
        }
104
    }
105
106
    /**
107
     * run all the tests with the given user, depending on the capabilities
108
     *
109
     * @param auth_plugin_authpdo $auth
110
     * @param $user
111
     */
112
    protected function runUserTests(auth_plugin_authpdo $auth, $user) {
113
        global $conf;
114
        $info = 'testing ' . $user['user'];
115
116
        // minimal setup
117
        $this->assertTrue($auth->checkPass($user['user'], $user['pass']), $info);
118
        $check = $auth->getUserData($user['user']);
119
        $this->assertEquals($user['user'], $check['user'], $info);
120
        $this->assertEquals($user['name'], $check['name'], $info);
121
        $this->assertEquals($user['mail'], $check['mail'], $info);
122
        $groups = array_merge($user['grps'], array($conf['defaultgroup']));
123
        $this->assertEquals($groups, $check['grps'], $info);
124
125
        // getUsers
126
        if($auth->canDo('getUsers')) {
127
            $list = $auth->retrieveUsers(0, -1, array('user' => $user['user']));
128
            $this->assertGreaterThanOrEqual(1, count($list));
129
            $list = $auth->retrieveUsers(0, -1, array('name' => $user['name']));
130
            $this->assertGreaterThanOrEqual(1, count($list));
131
            $list = $auth->retrieveUsers(0, -1, array('mail' => $user['mail']));
132
            $this->assertGreaterThanOrEqual(1, count($list));
133
        }
134
135
        // getUserCount
136
        if($auth->canDo('getUserCount')) {
137
            $count = $auth->getUserCount(array('user' => $user['user']));
138
            $this->assertGreaterThanOrEqual(1, $count);
139
            $count = $auth->getUserCount(array('name' => $user['name']));
140
            $this->assertGreaterThanOrEqual(1, $count);
141
            $count = $auth->getUserCount(array('mail' => $user['mail']));
142
            $this->assertGreaterThanOrEqual(1, $count);
143
        }
144
145
        // modPass
146
        if($auth->canDo('modPass')) {
147
            $newpass = 'foobar';
148
            $ok = $auth->modifyUser($user['user'], array('pass' => $newpass));
149
            $this->assertTrue($ok, $info);
150
            $this->assertTrue($auth->checkPass($user['user'], $newpass), $info);
151
        }
152
153
        // modMail
154
        if($auth->canDo('modMail')) {
155
            $newmail = '[email protected]';
156
            $ok = $auth->modifyUser($user['user'], array('mail' => $newmail));
157
            $this->assertTrue($ok, $info);
158
            $check = $auth->getUserData($user['user']);
159
            $this->assertEquals($newmail, $check['mail'], $info);
160
        }
161
162
        // modName
163
        if($auth->canDo('modName')) {
164
            $newname = 'FirstName Foobar';
165
            $ok = $auth->modifyUser($user['user'], array('name' => $newname));
166
            $this->assertTrue($ok, $info);
167
            $check = $auth->getUserData($user['user']);
168
            $this->assertEquals($newname, $check['name'], $info);
169
        }
170
171
        // modLogin
172
        if($auth->canDo('modLogin')) {
173
            $newuser = 'foobar' . $user['user'];
174
            $ok = $auth->modifyUser($user['user'], array('user' => $newuser));
175
            $this->assertTrue($ok, $info);
176
            $check = $auth->getUserData($newuser);
177
            $this->assertEquals($newuser, $check['user'], $info);
178
        }
179
180
    }
181
182
    /**
183
     * This triggers all the tests based on the dumps and configurations
184
     *
185
     * @depends test_requirements
186
     */
187
    public function test_mysql() {
188
        global $conf;
189
190
        $files = glob(__DIR__ . '/mysql/*.php');
191
        foreach($files as $file) {
192
            $dump = preg_replace('/\.php$/', '.sql', $file);
193
194
            $this->createDatabase();
195
            $this->importDatabase($dump);
196
197
            // Setup the configuration and initialize a new auth object
198
            /** @var $data array */
199
            include $file;
200
201
            $conf['plugin']['authpdo'] = array();
202
            $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...
203
            $conf['plugin']['authpdo']['dsn'] = "mysql:dbname={$this->database};host={$this->host}";
204
            $conf['plugin']['authpdo']['user'] = $this->user;
205
            $conf['plugin']['authpdo']['pass'] = $this->pass;
206
            $conf['plugin']['authpdo']['debug'] = 1;
207
            if($data['passcrypt']) $conf['passcrypt'] = $data['passcrypt'];
208
            $auth = new auth_plugin_authpdo();
209
210
            $this->runGeneralTests($auth, $data['users']);
211
            foreach($data['users'] as $user) {
212
                $this->runUserTests($auth, $user);
213
            }
214
215
            $this->dropDatabase();
216
        }
217
    }
218
219
}
220