Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
 */
1441 ariadna 30
final class matrix_user_manager_test extends \advanced_testcase {
1 efrain 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(
1441 ariadna 89
        ?string $servername,
1 efrain 90
        string $server,
91
        string $username,
92
        string $expecteduserid,
93
    ): void {
94
        $this->resetAfterTest();
1441 ariadna 95
        set_config('matrixhomeservername', $servername, 'communication_matrix');
1 efrain 96
        set_config('matrixhomeserverurl', $server, 'communication_matrix');
97
        $this->assertEquals(
98
            $expecteduserid,
99
            matrix_user_manager::get_formatted_matrix_userid($username),
100
        );
101
    }
102
 
103
    /**
104
     * Data provider for get_formatted_matrix_userid.
105
     *
106
     * @return array
107
     */
108
    public static function get_formatted_matrix_userid_provider(): array {
109
        return [
1441 ariadna 110
            'servername' => [
111
                'example.org',
112
                'https://matrix.example.org',
113
                'user',
114
                '@user:example.org',
115
            ],
116
            'servername empty string' => [
117
                '',
118
                'https://matrix.example.org',
119
                'user',
120
                '@user:matrix.example.org',
121
            ],
1 efrain 122
            'alphanumeric' => [
1441 ariadna 123
                null,
1 efrain 124
                'https://matrix.example.org',
125
                'alphabet1',
126
                '@alphabet1:matrix.example.org',
127
            ],
128
            'chara' => [
1441 ariadna 129
                null,
1 efrain 130
                'https://matrix.example.org',
131
                'asdf#$%^&*()+{}|<>?!,asdf',
132
                '@asdf.................asdf:matrix.example.org',
133
            ],
134
            'local server' => [
1441 ariadna 135
                null,
1 efrain 136
                'https://synapse',
137
                'colin.creavey',
138
                '@colin.creavey:synapse',
139
            ],
140
            'server with port' => [
1441 ariadna 141
                null,
1 efrain 142
                'https://matrix.example.org:8448',
143
                'colin.creavey',
144
                '@colin.creavey:matrix.example.org',
145
            ],
146
            'numeric username' => [
1441 ariadna 147
                null,
1 efrain 148
                'https://matrix.example.org',
149
                '123456',
150
                '@' . matrix_user_manager::MATRIX_USER_PREFIX . '123456:matrix.example.org',
151
            ],
152
        ];
153
    }
154
 
155
    /**
156
     * Data provider for set_matrix_userid_in_moodle.
157
     *
158
     * @return array
159
     */
160
    public static function set_matrix_userid_in_moodle_provider(): array {
161
        return array_combine(
162
            array_keys(self::get_formatted_matrix_userid_provider()),
163
            array_map(
164
                fn($value) => [$value[2]],
165
                self::get_formatted_matrix_userid_provider(),
166
            ),
167
        );
168
    }
169
 
170
    /**
171
     * Test setting of a user's matrix userid in Moodle.
172
     *
173
     * @dataProvider set_matrix_userid_in_moodle_provider
174
     * @param string $expectedusername
175
     */
176
    public function test_set_matrix_userid_in_moodle(
177
        string $expectedusername,
178
    ): void {
179
        $this->resetAfterTest();
180
 
181
        $user = $this->getDataGenerator()->create_user();
182
        matrix_user_manager::set_matrix_userid_in_moodle($user->id, $expectedusername);
183
 
184
        // Get created matrixuserid from moodle.
185
        $this->assertEquals(
186
            $expectedusername,
187
            matrix_user_manager::get_matrixid_from_moodle($user->id),
188
        );
189
    }
190
 
191
    /**
192
     * Test for getting a formatted matrix home server id.
193
     *
194
     * @dataProvider get_formatted_matrix_home_server_provider
195
     * @param string $input
196
     * @param string $expectedoutput
197
     */
198
    public function test_get_formatted_matrix_home_server(
199
        string $input,
200
        string $expectedoutput
201
    ): void {
202
        $this->resetAfterTest();
203
 
204
        set_config(
205
            'matrixhomeserverurl',
206
            $input,
207
            'communication_matrix',
208
        );
209
 
210
        $this->assertEquals(
211
            $expectedoutput,
212
            matrix_user_manager::get_formatted_matrix_home_server(),
213
        );
214
    }
215
 
216
    /**
217
     * Data provider for get_formatted_matrix_home_server.
218
     *
219
     * @return array
220
     */
221
    public static function get_formatted_matrix_home_server_provider(): array {
222
        return [
223
            'www is removed' => [
224
                'https://www.example.org',
225
                'example.org',
226
            ],
227
            'www is not removed if it is not at the beginning' => [
228
                'https://matrix.www.example.org',
229
                'matrix.www.example.org',
230
            ],
231
            'others are not removed' => [
232
                'https://matrix.example.org',
233
                'matrix.example.org',
234
            ],
235
        ];
236
    }
237
 
238
    /**
239
     * Test creation of matrix user profile fields.
240
     */
241
    public function test_create_matrix_user_profile_fields(): void {
242
        global $CFG;
243
        require_once("{$CFG->dirroot}/user/profile/lib.php");
244
 
245
        $this->resetAfterTest();
246
 
247
        $matrixprofilefield = get_config('communication_matrix', 'matrixuserid_field');
248
        $this->assertFalse($matrixprofilefield);
249
 
250
        $this->assertIsString(matrix_user_manager::create_matrix_user_profile_fields());
251
        $matrixprofilefield = get_config('communication_matrix', 'matrixuserid_field');
252
        $this->assertNotFalse($matrixprofilefield);
253
 
254
        $user = $this->getDataGenerator()->create_user();
255
        $this->assertObjectHasProperty($matrixprofilefield, profile_user_record($user->id));
256
    }
257
}