Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace communication_matrix;
18
 
19
use moodle_exception;
20
 
21
/**
22
 * Class matrix_user_manager_test to test the matrix user manager.
23
 *
24
 * @package    communication_matrix
25
 * @category   test
26
 * @copyright  2023 Stevani Andolo <stevani.andolo@moodle.com>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @covers \communication_matrix\matrix_user_manager
29
 */
30
class matrix_user_manager_test extends \advanced_testcase {
31
    /**
32
     * Test fetcihing a users matrix userid from Moodle.
33
     */
34
    public function test_get_matrixid_from_moodle_without_field(): void {
35
        $user = get_admin();
36
 
37
        $this->assertNull(matrix_user_manager::get_matrixid_from_moodle($user->id));
38
    }
39
 
40
    /**
41
     * Test fetching a user's matrix userid from Moodle.
42
     */
43
    public function test_get_matrixid_from_moodle(): void {
44
        $this->resetAfterTest();
45
 
46
        $user1 = $this->getDataGenerator()->create_user();
47
        $user2 = $this->getDataGenerator()->create_user();
48
 
49
        // Add user ids to both users.
50
        matrix_user_manager::set_matrix_userid_in_moodle(
51
            $user1->id,
52
            '@someexampleuser:matrix.moodle.org',
53
        );
54
 
55
        matrix_user_manager::set_matrix_userid_in_moodle(
56
            $user2->id,
57
            '@someotherexampleuser:matrix.moodle.org',
58
        );
59
 
60
        // And confirm that they're fetched back.
61
        $this->assertEquals(
62
            '@someexampleuser:matrix.moodle.org',
63
            matrix_user_manager::get_matrixid_from_moodle($user1->id),
64
        );
65
        $this->assertEquals(
66
            '@someotherexampleuser:matrix.moodle.org',
67
            matrix_user_manager::get_matrixid_from_moodle($user2->id),
68
        );
69
    }
70
 
71
    /**
72
     * Test fetching a formatted matrix userid from Moodle when no server is set.
73
     */
74
    public function test_get_formatted_matrix_userid_unset(): void {
75
        $this->expectException(moodle_exception::class);
76
 
77
        matrix_user_manager::get_formatted_matrix_userid('No value');
78
    }
79
 
80
    /**
81
     * Test fetch of a formatted matrix userid.
82
     *
83
     * @dataProvider get_formatted_matrix_userid_provider
84
     * @param string $server
85
     * @param string $username The moodle username to turn into a Matrix username
86
     * @param string $expecteduserid The expected matrix user id
87
     */
88
    public function test_get_formatted_matrix_userid(
89
        string $server,
90
        string $username,
91
        string $expecteduserid,
92
    ): void {
93
        $this->resetAfterTest();
94
 
95
        set_config('matrixhomeserverurl', $server, 'communication_matrix');
96
        $this->assertEquals(
97
            $expecteduserid,
98
            matrix_user_manager::get_formatted_matrix_userid($username),
99
        );
100
    }
101
 
102
    /**
103
     * Data provider for get_formatted_matrix_userid.
104
     *
105
     * @return array
106
     */
107
    public static function get_formatted_matrix_userid_provider(): array {
108
        return [
109
            'alphanumeric' => [
110
                'https://matrix.example.org',
111
                'alphabet1',
112
                '@alphabet1:matrix.example.org',
113
            ],
114
            'chara' => [
115
                'https://matrix.example.org',
116
                'asdf#$%^&*()+{}|<>?!,asdf',
117
                '@asdf.................asdf:matrix.example.org',
118
            ],
119
            'local server' => [
120
                'https://synapse',
121
                'colin.creavey',
122
                '@colin.creavey:synapse',
123
            ],
124
            'server with port' => [
125
                'https://matrix.example.org:8448',
126
                'colin.creavey',
127
                '@colin.creavey:matrix.example.org',
128
            ],
129
            'numeric username' => [
130
                'https://matrix.example.org',
131
                '123456',
132
                '@' . matrix_user_manager::MATRIX_USER_PREFIX . '123456:matrix.example.org',
133
            ],
134
        ];
135
    }
136
 
137
    /**
138
     * Data provider for set_matrix_userid_in_moodle.
139
     *
140
     * @return array
141
     */
142
    public static function set_matrix_userid_in_moodle_provider(): array {
143
        return array_combine(
144
            array_keys(self::get_formatted_matrix_userid_provider()),
145
            array_map(
146
                fn($value) => [$value[2]],
147
                self::get_formatted_matrix_userid_provider(),
148
            ),
149
        );
150
    }
151
 
152
    /**
153
     * Test setting of a user's matrix userid in Moodle.
154
     *
155
     * @dataProvider set_matrix_userid_in_moodle_provider
156
     * @param string $expectedusername
157
     */
158
    public function test_set_matrix_userid_in_moodle(
159
        string $expectedusername,
160
    ): void {
161
        $this->resetAfterTest();
162
 
163
        $user = $this->getDataGenerator()->create_user();
164
        matrix_user_manager::set_matrix_userid_in_moodle($user->id, $expectedusername);
165
 
166
        // Get created matrixuserid from moodle.
167
        $this->assertEquals(
168
            $expectedusername,
169
            matrix_user_manager::get_matrixid_from_moodle($user->id),
170
        );
171
    }
172
 
173
    /**
174
     * Test for getting a formatted matrix home server id.
175
     *
176
     * @dataProvider get_formatted_matrix_home_server_provider
177
     * @param string $input
178
     * @param string $expectedoutput
179
     */
180
    public function test_get_formatted_matrix_home_server(
181
        string $input,
182
        string $expectedoutput
183
    ): void {
184
        $this->resetAfterTest();
185
 
186
        set_config(
187
            'matrixhomeserverurl',
188
            $input,
189
            'communication_matrix',
190
        );
191
 
192
        $this->assertEquals(
193
            $expectedoutput,
194
            matrix_user_manager::get_formatted_matrix_home_server(),
195
        );
196
    }
197
 
198
    /**
199
     * Data provider for get_formatted_matrix_home_server.
200
     *
201
     * @return array
202
     */
203
    public static function get_formatted_matrix_home_server_provider(): array {
204
        return [
205
            'www is removed' => [
206
                'https://www.example.org',
207
                'example.org',
208
            ],
209
            'www is not removed if it is not at the beginning' => [
210
                'https://matrix.www.example.org',
211
                'matrix.www.example.org',
212
            ],
213
            'others are not removed' => [
214
                'https://matrix.example.org',
215
                'matrix.example.org',
216
            ],
217
        ];
218
    }
219
 
220
    /**
221
     * Test creation of matrix user profile fields.
222
     */
223
    public function test_create_matrix_user_profile_fields(): void {
224
        global $CFG;
225
        require_once("{$CFG->dirroot}/user/profile/lib.php");
226
 
227
        $this->resetAfterTest();
228
 
229
        $matrixprofilefield = get_config('communication_matrix', 'matrixuserid_field');
230
        $this->assertFalse($matrixprofilefield);
231
 
232
        $this->assertIsString(matrix_user_manager::create_matrix_user_profile_fields());
233
        $matrixprofilefield = get_config('communication_matrix', 'matrixuserid_field');
234
        $this->assertNotFalse($matrixprofilefield);
235
 
236
        $user = $this->getDataGenerator()->create_user();
237
        $this->assertObjectHasProperty($matrixprofilefield, profile_user_record($user->id));
238
    }
239
}