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
// This program 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
// This program 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
declare(strict_types = 1);
18
 
19
// Namespace does not match PSR. But Moodle likes it this way.
20
namespace mod_edusharing;
21
 
22
use advanced_testcase;
23
use cached_cm_info;
24
use core\moodle_database_for_testing;
25
use dml_exception;
26
use Exception;
27
use stdClass;
28
use testUtils\FakeConfig;
29
use testUtils\TestStringGenerator;
30
 
31
/**
32
 * Class UtilityFunctionsTest
33
 *
34
 * @author Marian Ziegler <ziegler@edu-sharing.net>
35
 * @package mod_edusharing
36
 * @covers \mod_edusharing\UtilityFunctions
37
 */
38
class utility_functions_test extends advanced_testcase {
39
    /**
40
     * Function test_if_get_object_id_from_url_returns_proper_path_if_url_is_ok
41
     *
42
     * @return void
43
     */
44
    public function test_if_get_object_id_from_url_returns_proper_path_if_url_is_ok(): void {
45
        $utils = new UtilityFunctions();
46
        $this->assertEquals('hallo', $utils->get_object_id_from_url('http://test.com/hallo/'));
47
    }
48
 
49
    /**
50
     * Function test_if_get_object_id_from_url_triggers_warning_if_url_is_malformed
51
     *
52
     * @return void
53
     */
54
    public function test_if_get_object_id_from_url_triggers_warning_if_url_is_malformed(): void {
55
        $utils = new UtilityFunctions();
56
        $this->expectWarning();
57
        $utils->get_object_id_from_url('http://test.com:-80/hallo/');
58
    }
59
 
60
    /**
61
     * Function test_if_get_repository_id_from_url_returns_host_if_url_is_ok
62
     *
63
     * @return void
64
     * @throws Exception
65
     */
66
    public function test_if_get_repository_id_from_url_returns_host_if_url_is_ok(): void {
67
        $utils = new UtilityFunctions();
68
        $this->assertEquals('test.com', $utils->get_repository_id_from_url('http://test.com/hallo/'));
69
    }
70
 
71
    /**
72
     * Function test_if_get_repository_throws_exception_if_url_is_malformed
73
     *
74
     * @return void
75
     * @throws Exception
76
     */
77
    public function test_if_get_repository_throws_exception_if_url_is_malformed(): void {
78
        $utils = new UtilityFunctions();
79
        $this->expectException(Exception::class);
80
        $utils->get_repository_id_from_url('http://test.com:-80/hallo/');
81
    }
82
 
83
    /**
84
     * Function test_if_get_auth_key_returns_user_id_if_sso_is_active
85
     *
86
     * @return void
87
     *
88
     * @throws dml_exception
89
     */
90
    public function test_if_get_auth_key_returns_user_id_if_sso_is_active_and_obfuscation_is_active(): void {
91
        $this->resetAfterTest();
92
        global $SESSION, $CFG;
93
        require_once($CFG->dirroot . '/mod/edusharing/tests/testUtils/FakeConfig.php');
94
        $fakeconfig = new FakeConfig();
95
        $fakeconfig->set_entries([
96
            'EDU_AUTH_PARAM_NAME_USERID' => 'test',
97
            'obfuscate_auth_param' => '1',
98
        ]);
99
        $utils                   = new UtilityFunctions($fakeconfig);
100
        $SESSION->edusharing_sso = ['test' => 'expectedId'];
101
        $this->assertEquals('expectedId', $utils->get_auth_key());
102
    }
103
 
104
    /**
105
     * Function test_get_auth_key_returns_guest_id_if_guest_option_is_active
106
     *
107
     * @return void
108
     *
109
     * @throws dml_exception
110
     */
111
    public function test_get_auth_key_returns_guest_id_if_guest_option_is_active(): void {
112
        $this->resetAfterTest();
113
        global $SESSION, $CFG;
114
        require_once($CFG->dirroot . '/mod/edusharing/tests/testUtils/FakeConfig.php');
115
        unset($SESSION->edusharing_sso);
116
        $fakeconfig = new FakeConfig();
117
        $fakeconfig->set_entries([
118
            'edu_guest_option'   => '1',
119
            'edu_guest_guest_id' => 'expectedId',
120
        ]);
121
        $utils = new UtilityFunctions($fakeconfig);
122
        $this->assertEquals('expectedId', $utils->get_auth_key());
123
    }
124
 
125
    /**
126
     * Function test_get_auth_key_returns_configured_auth_key_if_set
127
     *
128
     * @return void
129
     *
130
     * @throws dml_exception
131
     */
132
    public function test_get_auth_key_returns_configured_auth_key_if_set(): void {
133
        $this->resetAfterTest();
134
        global $SESSION, $USER, $CFG;
135
        require_once($CFG->dirroot . '/mod/edusharing/tests/testUtils/FakeConfig.php');
136
        unset($SESSION->edusharing_sso);
137
        $fakeconfig = new FakeConfig();
138
        $fakeconfig->set_entries([
139
            'EDU_AUTH_KEY' => 'email',
140
        ]);
141
        $utils       = new UtilityFunctions($fakeconfig);
142
        $USER->email = 'expected@expected.org';
143
        $this->assertEquals('expected@expected.org', $utils->get_auth_key());
144
    }
145
 
146
    /**
147
     * Function test_get_auth_key_returns_auth_key_in_profile_if_all_previous_are_not_met
148
     *
149
     * @return void
150
     *
151
     * @throws dml_exception
152
     */
153
    public function test_get_auth_key_returns_auth_key_in_profile_if_all_previous_are_not_met(): void {
154
        $this->resetAfterTest();
155
        global $SESSION, $USER, $CFG;
156
        unset($SESSION->edusharing_sso);
157
        require_once($CFG->dirroot . '/mod/edusharing/tests/testUtils/FakeConfig.php');
158
        $fakeconfig = new FakeConfig();
159
        $fakeconfig->set_entries([
160
            'EDU_AUTH_KEY' => 'nonsense',
161
        ]);
162
        $utils                     = new UtilityFunctions($fakeconfig);
163
        $USER->profile['nonsense'] = 'expectedId';
164
        $this->assertEquals('expectedId', $utils->get_auth_key());
165
    }
166
 
167
    /**
168
     * Function test_get_auth_key_returns_user_name_as_last_resort
169
     *
170
     * @return void
171
     *
172
     * @throws dml_exception
173
     */
174
    public function test_get_auth_key_returns_user_name_as_last_resort(): void {
175
        $this->resetAfterTest();
176
        global $SESSION, $USER, $CFG;
177
        require_once($CFG->dirroot . '/mod/edusharing/tests/testUtils/FakeConfig.php');
178
        unset($SESSION->edusharing_sso);
179
        $fakeconfig = new FakeConfig();
180
        $fakeconfig->set_entries([
181
            'EDU_AUTH_KEY' => 'nonsense',
182
        ]);
183
        $utils          = new UtilityFunctions($fakeconfig);
184
        $USER->username = 'expectedName';
185
        $this->assertEquals('expectedName', $utils->get_auth_key());
186
    }
187
 
188
    /**
189
     * Function test_if_set_moodle_ids_in_edusharing_entries_does_not_set_anything_if_no_matches
190
     *
191
     * @return void
192
     *
193
     */
194
    public function test_if_set_moodle_ids_in_edusharing_entries_does_not_set_anything_if_no_matches(): void {
195
        $this->resetAfterTest();
196
        require_once('lib/dml/tests/dml_test.php');
197
        $dbmock = $this->getMockBuilder(moodle_database_for_testing::class)
198
            ->onlyMethods(['get_record', 'update_record'])
199
            ->getMock();
200
        $dbmock->expects($this->never())->method('get_record');
201
        $dbmock->expects($this->never())->method('update_record');
202
 
203
    }
204
 
205
    /**
206
     * Function test_if_set_moodle_ids_in_edusharing_entries_sets_found_resource_ids_to_db
207
     *
208
     * @return void
209
     *
210
     */
211
    public function test_if_set_moodle_ids_in_edusharing_entries_sets_found_resource_ids_to_db(): void {
212
        $this->resetAfterTest();
213
        require_once('lib/dml/tests/dml_test.php');
214
        $dbmock = $this->getMockBuilder(moodle_database_for_testing::class)
215
            ->onlyMethods(['get_record', 'update_record'])
216
            ->getMock();
217
        $edusharing1 = new stdClass();
218
        $edusharing1->id = 1;
219
        $edusharing2 = new stdClass();
220
        $edusharing2->id = 2;
221
        $dbmock->expects($this->exactly(2))
222
            ->method('get_record')
223
            ->withConsecutive(
224
                ['edusharing', ['id' => 'resourceID1'], '*', MUST_EXIST],
225
                ['edusharing', ['id' => 'resourceID2'], '*', MUST_EXIST]
226
            )->willReturnOnConsecutiveCalls($edusharing1, $edusharing2);
227
        $sectionid = 4;
228
        $moduleid = 5;
229
        $edusharing3 = clone $edusharing1;
230
        $edusharing3->section_id = $sectionid;
231
        $edusharing3->module_id = $moduleid;
232
        $edusharing4 = clone $edusharing2;
233
        $edusharing4->section_id = $sectionid;
234
        $edusharing4->module_id = $moduleid;
235
        $dbmock->expects($this->exactly(2))
236
            ->method('update_record')
237
            ->withConsecutive(
238
                ['edusharing', $edusharing3],
239
                ['edusharing', $edusharing4]
240
            );
241
        // phpcs:ignore -- GLOBALS is supposed to be all caps.
242
        $GLOBALS['DB'] = $dbmock;
243
        $text = '<img resourceId=resourceID1& class="as_edusharing_atto_asda">';
244
        $text .= '<a resourceId="resourceID2&" class="dsfg_edusharing_atto_afdd">text</a>';
245
        $utils = new UtilityFunctions();
246
        $utils->set_moodle_ids_in_edusharing_entries($text, $sectionid, $moduleid);
247
    }
248
 
249
    /**
250
     * Function test_if_set_module_in_db_only_sets_section_id_if_no_module_id_provided
251
     *
252
     * @return void
253
     *
254
     */
255
    public function test_if_set_module_in_db_only_sets_section_id_if_no_module_id_provided(): void {
256
        $this->resetAfterTest();
257
        require_once('lib/dml/tests/dml_test.php');
258
        $dbmock = $this->getMockBuilder(moodle_database_for_testing::class)
259
            ->onlyMethods(['get_record', 'update_record'])
260
            ->getMock();
261
        $edusharing1 = new stdClass();
262
        $edusharing1->id = 1;
263
        $edusharing2 = new stdClass();
264
        $edusharing2->id = 2;
265
        $dbmock->expects($this->exactly(2))
266
            ->method('get_record')
267
            ->withConsecutive(
268
                ['edusharing', ['id' => 'resourceID1'], '*', MUST_EXIST],
269
                ['edusharing', ['id' => 'resourceID2'], '*', MUST_EXIST]
270
            )->willReturnOnConsecutiveCalls($edusharing1, $edusharing2);
271
        $sectionid = 4;
272
        $edusharing3 = clone $edusharing1;
273
        $edusharing3->section_id = $sectionid;
274
        $edusharing4 = clone $edusharing2;
275
        $edusharing4->section_id = $sectionid;
276
        $dbmock->expects($this->exactly(2))
277
            ->method('update_record')
278
            ->withConsecutive(
279
                ['edusharing', $edusharing3],
280
                ['edusharing', $edusharing4]
281
            );
282
        // phpcs:ignore -- GLOBALS is supposed to be all caps.
283
        $GLOBALS['DB'] = $dbmock;
284
        $text = '<img resourceId=resourceID1& class="as_edusharing_atto_asda">';
285
        $text .= '<a resourceId="resourceID2&" class="dsfg_edusharing_atto_afdd">text</a>';
286
        $utils = new UtilityFunctions();
287
        $utils->set_moodle_ids_in_edusharing_entries($text, $sectionid);
288
    }
289
 
290
    /**
291
     * Function test_if_set_module_in_db_does_not_set_anything_to_db_if_no_matches_found
292
     *
293
     * @return void
294
     */
295
    public function test_if_set_module_in_db_does_not_set_anything_to_db_if_no_matches_found(): void {
296
        $this->resetAfterTest();
297
        require_once('lib/dml/tests/dml_test.php');
298
        $utils  = new UtilityFunctions();
299
        $dbmock = $this->getMockBuilder(moodle_database_for_testing::class)
300
            ->onlyMethods(['set_field'])
301
            ->getMock();
302
        $dbmock->expects($this->never())->method('set_field');
303
        // phpcs:ignore -- GLOBALS is supposed to be all caps.
304
        $GLOBALS['DB'] = $dbmock;
305
        $utils->set_moodle_ids_in_edusharing_entries('NothingHere', 1, 2);
306
    }
307
 
308
    /**
309
     * Function test_if_get_course_module_info_returns_proper_info_if_data_found_in_db
310
     *
311
     * @return void
312
     */
313
    public function test_if_get_course_module_info_returns_proper_info_if_data_found_in_db(): void {
314
        $this->resetAfterTest();
315
        require_once('lib/dml/tests/dml_test.php');
316
        $this->resetAfterTest();
317
        $utils                   = new UtilityFunctions();
318
        $module                  = new stdClass();
319
        $module->instance        = 'instanceId';
320
        $module->showdescription = false;
321
        $module->id              = 2;
322
        $returnone               = new stdClass();
323
        $returnone->intro        = "myIntro";
324
        $returnone->introFormat  = '2';
325
        $returntwo               = new stdClass();
326
        $returntwo->popup_window = '1';
327
        $dbmock                  = $this->getMockBuilder(moodle_database_for_testing::class)
328
            ->onlyMethods(['get_record'])
329
            ->getMock();
330
        $dbmock->expects($this->exactly(2))
331
            ->method('get_record')
332
            ->withConsecutive(
333
                [],
334
                ['edusharing', ['id' => 'instanceId'], '*', MUST_EXIST])
335
            ->willReturnOnConsecutiveCalls($returnone, $returntwo);
336
        // phpcs:ignore -- GLOBALS is supposed to be all caps.
337
        $GLOBALS['DB'] = $dbmock;
338
        $result        = $utils->get_course_module_info($module);
339
        $this->assertTrue($result instanceof cached_cm_info);
340
        $this->assertEquals('this.target=\'_blank\';', $result->onclick);
341
    }
342
 
343
    /**
344
     * Function test_if_get_course_module_info_returns_false_if_no_record_found
345
     *
346
     * @return void
347
     */
348
    public function test_if_get_course_module_info_returns_false_if_no_record_found(): void {
349
        $this->resetAfterTest();
350
        require_once('lib/dml/tests/dml_test.php');
351
        $utils = new UtilityFunctions();
352
        $this->resetAfterTest();
353
        $module           = new stdClass();
354
        $module->instance = 'instanceId';
355
        $module->id       = 2;
356
        $dbmock           = $this->getMockBuilder(moodle_database_for_testing::class)
357
            ->onlyMethods(['get_record'])
358
            ->getMock();
359
        $dbmock->expects($this->once())
360
            ->method('get_record')
361
            ->with('edusharing', ['id' => 'instanceId'], 'id, name, intro, introformat', MUST_EXIST)
362
            ->willThrowException(new Exception());
363
        // phpcs:ignore -- GLOBALS is supposed to be all caps.
364
        $GLOBALS['DB'] = $dbmock;
365
        $this->assertEquals(false, $utils->get_course_module_info($module));
366
    }
367
 
368
    /**
369
     * Function test_get_inline_object_matches_returns_only_atto_matches_from_input
370
     *
371
     * @return void
372
     */
373
    public function test_get_inline_object_matches_returns_only_atto_matches_from_input(): void {
374
        global $CFG;
375
        require_once($CFG->dirroot . '/mod/edusharing/tests/testUtils/TestStringGenerator.php');
376
        $text   = TestStringGenerator::getattoteststring();
377
        $utils  = new UtilityFunctions();
378
        $result = $utils->get_inline_object_matches($text);
379
        $this->assertTrue(count($result) === 4);
380
        $this->assertTrue(count(array_filter($result, fn($value) => str_contains($value, '</a>'))) === 2);
381
        $this->assertTrue(count(array_filter($result, fn($value) => str_contains($value, '<img'))) === 2);
382
    }
383
}