Completed
Push — master ( a73e85...13dd2e )
by Julito
08:28
created

SaveUserJsonTest::testCreateAUserWithExtraFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 19
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 30
rs 9.6333
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 SaveUserJsonTest
9
 *
10
 * SAVE_USER_JSON webservice unit tests
11
 */
12
class SaveUserJsonTest extends V2TestCase
13
{
14
    public function action()
15
    {
16
        return 'save_user_json';
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
        $json = json_encode(
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
        $userId = $this->integer([ 'json' => $json ]);
44
45
        // assert the user was saved and given the returned user id
46
        $user = UserManager::getManager()->find($userId);
47
        $this->assertNotNull($user, 'the returned userId does not point to an user');
48
49
        // assert each field was filled with provided information
50
        $this->assertSame($loginName, $user->getUserName());
51
        $this->assertSame($email, $user->getEmail());
52
        $this->assertSame($status, $user->getStatus());
53
54
        // clean up
55
        UserManager::delete_user($userId);
56
    }
57
58
    /**
59
     * Creates a test user with an extra field 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
        $json = json_encode(
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
        $userId = $this->integer(['json' => $json]);
85
86
        // assert user extra field value was saved
87
        $savedValue = (new ExtraFieldValue('user'))->get_values_by_handler_and_field_variable($userId, $extraFieldName);
88
        $this->assertNotFalse($savedValue);
89
        $this->assertSame($extraFieldOriginalValue, $savedValue['value']);
90
91
        // clean up
92
        UserManager::delete_user($userId);
93
    }
94
}
95