Passed
Push — master ( 482b77...45d699 )
by Julito
09:12
created

UsernameExistTest::testUsernameWhichDoesExist()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 22
rs 9.8666
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
require_once __DIR__.'/V2TestCase.php';
6
require_once __DIR__.'/../../../../vendor/autoload.php';
7
8
/**
9
 * Class UsernameExistTest
10
 *
11
 * USERNAME_EXIST webservice unit tests
12
 */
13
class UsernameExistTest extends V2TestCase
14
{
15
    public function action()
16
    {
17
        return 'username_exist';
18
    }
19
20
    /**
21
     * test nonexistence of a username which does not exist
22
     * assert that the webservice returns false
23
     */
24
    public function testUsernameWhichDoesNotExist()
25
    {
26
        // generate a random name which does not exist in the database
27
        do {
28
            $loginName = rand();
29
        } while (UserManager::get_user_id_from_username($loginName));
30
31
        // expect the web service to return false
32
        $this->assertFalse($this->boolean(['loginname' => $loginName]));
33
    }
34
35
    /**
36
     * test existence of a username which does exist
37
     * assert that the webservice returns true
38
     */
39
    public function testUsernameWhichDoesExist()
40
    {
41
        // generate a random name which does not exist in the database
42
        do {
43
            $loginName = rand();
44
        } while (UserManager::get_user_id_from_username($loginName));
45
46
        // create a test user with this login name
47
        $userId = UserManager::create_user(
48
            $loginName,
49
            $loginName,
50
            STUDENT,
51
            $loginName.'@local',
52
            $loginName,
53
            $loginName
54
        );
55
56
        // expect the web service to return true
57
        $this->assertTrue($this->boolean(['loginname' => $loginName]));
58
59
        // clean up
60
        UserManager::delete_users([$userId]);
61
    }
62
}
63