| Conditions | 1 |
| Paths | 1 |
| Total Lines | 77 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 114 | public function test_userinfo() { |
||
| 115 | global $conf; |
||
| 116 | $auth = new auth_plugin_authpdo(); |
||
| 117 | |||
| 118 | // clear text pasword (with default config above |
||
| 119 | $this->assertFalse($auth->checkPass('nobody', 'nope')); |
||
| 120 | $this->assertFalse($auth->checkPass('admin', 'nope')); |
||
| 121 | $this->assertTrue($auth->checkPass('admin', 'password')); |
||
| 122 | |||
| 123 | // now with a hashed password |
||
| 124 | $conf['plugin']['authpdo']['select-user'] = 'SELECT id AS uid, login AS user, name, pass AS hash, mail FROM user WHERE login = :user'; |
||
| 125 | $this->assertFalse($auth->checkPass('admin', 'password')); |
||
| 126 | $this->assertFalse($auth->checkPass('user', md5('password'))); |
||
| 127 | |||
| 128 | // access user data |
||
| 129 | $info = $auth->getUserData('admin'); |
||
| 130 | $this->assertEquals('admin', $info['user']); |
||
| 131 | $this->assertEquals('The Admin', $info['name']); |
||
| 132 | $this->assertEquals('[email protected]', $info['mail']); |
||
| 133 | $this->assertEquals(array('admin', 'user'), $info['grps']); |
||
| 134 | |||
| 135 | // group retrieval |
||
| 136 | $this->assertEquals(array('admin', 'user'), $auth->retrieveGroups()); |
||
| 137 | $this->assertEquals(array('user'), $auth->retrieveGroups(1)); |
||
| 138 | $this->assertEquals(array('admin'), $auth->retrieveGroups(0, 1)); |
||
| 139 | |||
| 140 | // user creation |
||
| 141 | $auth->createUser('test', 'password', 'A Test user', '[email protected]', array('newgroup')); |
||
| 142 | $info = $auth->getUserData('test'); |
||
| 143 | $this->assertEquals('test', $info['user']); |
||
| 144 | $this->assertEquals('A Test user', $info['name']); |
||
| 145 | $this->assertEquals('[email protected]', $info['mail']); |
||
| 146 | $this->assertEquals(array('newgroup', 'user'), $info['grps']); |
||
| 147 | $this->assertEquals(array('admin', 'newgroup', 'user'), $auth->retrieveGroups()); |
||
| 148 | |||
| 149 | // user modification |
||
| 150 | $auth->modifyUser('test', array('user' => 'tester', 'name' => 'The Test User', 'pass' => 'secret')); |
||
| 151 | $info = $auth->getUserData('tester'); |
||
| 152 | $this->assertEquals('tester', $info['user']); |
||
| 153 | $this->assertEquals('The Test User', $info['name']); |
||
| 154 | $this->assertTrue($auth->checkPass('tester','secret')); |
||
| 155 | |||
| 156 | // move user to different groups |
||
| 157 | $auth->modifyUser('tester', array('grps' => array('user', 'admin', 'another'))); |
||
| 158 | $info = $auth->getUserData('tester'); |
||
| 159 | $this->assertEquals(array('admin', 'another', 'user'), $info['grps']); |
||
| 160 | |||
| 161 | // list users |
||
| 162 | $users = $auth->retrieveUsers(); |
||
| 163 | $this->assertEquals(array('admin', 'tester', 'user'), $users); |
||
| 164 | |||
| 165 | $users = $auth->retrieveUsers(1); // offset |
||
| 166 | $this->assertEquals(array('tester', 'user'), $users); |
||
| 167 | |||
| 168 | $users = $auth->retrieveUsers(1, 1); // offset + limit |
||
| 169 | $this->assertEquals(array('tester'), $users); |
||
| 170 | |||
| 171 | $users = $auth->retrieveUsers(0, -1, array('group' => 'admin')); // full group |
||
| 172 | $this->assertEquals(array('admin', 'tester'), $users); |
||
| 173 | $count = $auth->getUserCount(array('group' => 'admin')); |
||
| 174 | $this->assertEquals(2, $count); |
||
| 175 | |||
| 176 | $users = $auth->retrieveUsers(0, -1, array('group' => 'dmi')); // substring |
||
| 177 | $this->assertEquals(array('admin', 'tester'), $users); |
||
| 178 | $count = $auth->getUserCount(array('group' => 'dmi')); |
||
| 179 | $this->assertEquals(2, $count); |
||
| 180 | |||
| 181 | $users = $auth->retrieveUsers(0, -1, array('user' => 'dmi')); // substring |
||
| 182 | $this->assertEquals(array('admin'), $users); |
||
| 183 | $count = $auth->getUserCount(array('user' => 'dmi')); |
||
| 184 | $this->assertEquals(1, $count); |
||
| 185 | |||
| 186 | // delete user |
||
| 187 | $num = $auth->deleteUsers(array('tester', 'foobar')); |
||
| 188 | $this->assertEquals(1, $num); |
||
| 189 | |||
| 190 | } |
||
| 191 | |||
| 193 |