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

SaveUserTest::action()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
require_once __DIR__.'/V2TestCase.php';
5
require_once __DIR__.'/../../../../vendor/autoload.php';
6
7
/**
8
 * Class SaveUserTest
9
 *
10
 * SAVE_USER webservice unit tests
11
 */
12
class SaveUserTest extends V2TestCase
13
{
14
    public function action()
15
    {
16
        return 'save_user';
17
    }
18
19
    /**
20
     * creates a minimal test user
21
     * asserts that it was created with the supplied data
22
     *
23
     * @throws Exception if it cannot delete the created test user
24
     */
25
    public function testCreateAMinimalUser()
26
    {
27
        // call the web service with minimal information
28
        $loginName = 'testUser'.time();
29
        $email = 'testUser@local';
30
        $status = 5;
31
        $userId = $this->integer(
32
            [
33
                'loginname' => $loginName,
34
                'firstname' => 'Małgorzata',
35
                'lastname' => 'Summer',
36
                'original_user_id_name' => 'external_user_id',
37
                'original_user_id_value' => $loginName,
38
                'email' => $email,
39
                'status' => $status,
40
                'password' => 'test',
41
            ]
42
        );
43
44
        // assert the user was saved and given the returned user id
45
        $user = UserManager::getManager()->find($userId);
46
        $this->assertNotNull($user, 'the returned userId does not point to an user');
47
48
        // assert each field was filled with provided information
49
        $this->assertSame($loginName, $user->getUserName());
50
        $this->assertSame($email, $user->getEmail());
51
        $this->assertSame($status, $user->getStatus());
52
53
        // clean up
54
        UserManager::delete_user($userId);
55
    }
56
57
    /**
58
     * Creates a test user with an extra field
59
     * asserts that the extra field values were saved
60
     *
61
     * @throws Exception if it cannot delete the created test user
62
     */
63
    public function testCreateAUserWithExtraFields()
64
    {
65
        // call the web service
66
        $extraFieldName = 'age';
67
        $extraFieldOriginalValue = '29';
68
        $loginName = 'testUser'.time();
69
        $userId = $this->integer(
70
            [
71
                'loginname' => $loginName,
72
                'email' => 'testUser@local',
73
                'original_user_id_name' => 'external_user_id',
74
                'original_user_id_value' => $loginName,
75
                'status' => 5,
76
                'password' => 'test',
77
                'firstname' => 'Małgorzata',
78
                'lastname' => 'Summer',
79
                'extra' => [
80
                    ['field_name' => $extraFieldName, 'field_value' => $extraFieldOriginalValue],
81
                ],
82
            ]
83
        );
84
85
        // assert user extra field value was saved
86
        $savedValue = (new ExtraFieldValue('user'))->get_values_by_handler_and_field_variable($userId, $extraFieldName);
87
        $this->assertNotFalse($savedValue);
88
        $this->assertSame($extraFieldOriginalValue, $savedValue['value']);
89
90
        // clean up
91
        UserManager::delete_user($userId);
92
    }
93
}
94