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 dml_exception;
24
use EduSharingApiClient\CurlResult;
25
use EduSharingApiClient\EduSharingAuthHelper;
26
use EduSharingApiClient\EduSharingHelperBase;
27
use EduSharingApiClient\EduSharingNodeHelper;
28
use EduSharingApiClient\EduSharingNodeHelperConfig;
29
use EduSharingApiClient\UrlHandling;
30
use SimpleXMLElement;
31
use testUtils\FakeConfig;
32
 
33
// phpcs:ignore -- no Moodle internal check needed.
34
global $CFG;
35
require_once($CFG->dirroot . '/mod/edusharing/eduSharingAutoloader.php');
36
 
37
/**
38
 * Class MetadataLogicTest
39
 *
40
 * @author Marian Ziegler <ziegler@edu-sharing.net>
41
 * @package mod_edusharing
42
 * @covers \mod_edusharing\MetadataLogic
43
 */
44
class metadata_logic_test extends advanced_testcase {
45
    /**
46
     * Function test_if_import_metadata_sets_all_config_entries_on_success
47
     *
48
     * @return void
49
     * @throws EduSharingUserException
50
     * @throws dml_exception
51
     */
52
    public function test_if_import_metadata_sets_all_config_entries_on_success(): void {
53
        $this->resetAfterTest();
54
        global $_SERVER, $CFG;
55
        require_once($CFG->dirroot . '/mod/edusharing/tests/testUtils/FakeConfig.php');
56
        $_SERVER['SERVER_NAME'] = 'testServer';
57
        $metadataurl            = 'test.de';
58
        $metadataxml            = file_get_contents(__DIR__ . '/metadataTest.xml');
59
        $basehelper             = new EduSharingHelperBase('www.url.de', 'pkey123', 'appid123');
60
        $authhelper             = new EduSharingAuthHelper($basehelper);
61
        $nodeconfig             = new EduSharingNodeHelperConfig(new UrlHandling(true));
62
        $nodehelper             = new EduSharingNodeHelper($basehelper, $nodeconfig);
63
        $fakeconfig             = new FakeConfig();
64
        $fakeconfig->set_entries([
65
            'application_appid' => 'app123',
66
        ]);
67
        $utils       = new UtilityFunctions($fakeconfig);
68
        $servicemock = $this->getMockBuilder(EduSharingService::class)
69
            ->onlyMethods(['import_metadata'])
70
            ->setConstructorArgs([$authhelper, $nodehelper])
71
            ->getMock();
72
        $servicemock->expects($this->once())
73
            ->method('import_metadata')
74
            ->with($metadataurl)
75
            ->will($this->returnValue(new CurlResult($metadataxml, 0, [])));
76
        $logic = new MetadataLogic($servicemock, $utils);
77
        $logic->import_metadata($metadataurl);
78
        $this->assertEquals('http', $fakeconfig->get('repository_clientprotocol'));
79
        $this->assertEquals('http://test.de/edu-sharing/services/authbyapp',
80
            $fakeconfig->get('repository_authenticationwebservice'));
81
        $this->assertEquals('http://test.de/edu-sharing/services/usage2', $fakeconfig->get('repository_usagewebservice'));
82
        $this->assertEquals('publicKeyTest', $fakeconfig->get('repository_public_key'));
83
        $this->assertEquals('http://test.de/esrender/application/esmain/index.php', $fakeconfig->get('repository_contenturl'));
84
        $this->assertEquals('local', $fakeconfig->get('repository_appcaption'));
85
        $this->assertEquals('8100', $fakeconfig->get('repository_clientport'));
86
        $this->assertEquals('8080', $fakeconfig->get('repository_port'));
87
        $this->assertEquals('test.de', $fakeconfig->get('repository_domain'));
88
        $this->assertEquals('http://test.de/edu-sharing/services/authbyapp?wsdl',
89
            $fakeconfig->get('repository_authenticationwebservice_wsdl'));
90
        $this->assertEquals('REPOSITORY', $fakeconfig->get('repository_type'));
91
        $this->assertEquals('enterprise-docker-maven-fixes-8-0', $fakeconfig->get('repository_appid'));
92
        $this->assertEquals('http:/test.de/edu-sharing/services/usage2?wsdl', $fakeconfig->get('repository_usagewebservice_wsdl'));
93
        $this->assertEquals('http', $fakeconfig->get('repository_protocol'));
94
        $this->assertEquals('repository-service', $fakeconfig->get('repository_host'));
95
    }
96
 
97
    /**
98
     * Function test_if_import_metadata_generates_new_app_id_if_none_present
99
     *
100
     * @return void
101
     * @throws EduSharingUserException
102
     * @throws dml_exception
103
     */
104
    public function test_if_import_metadata_generates_new_app_id_if_none_present(): void {
105
        $this->resetAfterTest();
106
        global $_SERVER, $CFG;
107
        require_once($CFG->dirroot . '/mod/edusharing/tests/testUtils/FakeConfig.php');
108
        $_SERVER['SERVER_NAME'] = 'testServer';
109
        $fakeconfig             = new FakeConfig();
110
        $metadataurl            = 'test.de';
111
        $metadataxml            = file_get_contents(__DIR__ . '/metadataTest.xml');
112
        $basehelper             = new EduSharingHelperBase('www.url.de', 'pkey123', 'appid123');
113
        $authhelper             = new EduSharingAuthHelper($basehelper);
114
        $nodeconfig             = new EduSharingNodeHelperConfig(new UrlHandling(true));
115
        $nodehelper             = new EduSharingNodeHelper($basehelper, $nodeconfig);
116
        $utils                  = new UtilityFunctions($fakeconfig);
117
        $servicemock            = $this->getMockBuilder(EduSharingService::class)
118
            ->onlyMethods(['import_metadata'])
119
            ->setConstructorArgs([$authhelper, $nodehelper])
120
            ->getMock();
121
        $servicemock->expects($this->once())
122
            ->method('import_metadata')
123
            ->with($metadataurl)
124
            ->will($this->returnValue(new CurlResult($metadataxml, 0, [])));
125
        $logic = new MetadataLogic($servicemock, $utils);
126
        $logic->import_metadata($metadataurl);
127
        $this->assertTrue(is_string($fakeconfig->get('application_appid')), 'application_appid was not set');
128
        $this->assertTrue(str_contains($fakeconfig->get('application_appid'), 'moodle_'),
129
            'application_appid does not contain moodle prefix');
130
    }
131
 
132
    /**
133
     * Function test_if_import_metadata_uses_configured_app_id_if_found
134
     *
135
     * @return void
136
     * @throws EduSharingUserException
137
     * @throws dml_exception
138
     */
139
    public function test_if_import_metadata_uses_configured_app_id_if_found(): void {
140
        $this->resetAfterTest();
141
        global $_SERVER, $CFG;
142
        require_once($CFG->dirroot . '/mod/edusharing/tests/testUtils/FakeConfig.php');
143
        $_SERVER['SERVER_NAME'] = 'testServer';
144
        $fakeconfig             = new FakeConfig();
145
        $fakeconfig->set_entries([
146
            'application_appid' => 'testId',
147
        ]);
148
        $metadataurl = 'test.de';
149
        $metadataxml = file_get_contents(__DIR__ . '/metadataTest.xml');
150
        $basehelper  = new EduSharingHelperBase('www.url.de', 'pkey123', 'appid123');
151
        $authhelper  = new EduSharingAuthHelper($basehelper);
152
        $nodeconfig  = new EduSharingNodeHelperConfig(new UrlHandling(true));
153
        $nodehelper  = new EduSharingNodeHelper($basehelper, $nodeconfig);
154
        $servicemock = $this->getMockBuilder(EduSharingService::class)
155
            ->onlyMethods(['import_metadata'])
156
            ->setConstructorArgs([$authhelper, $nodehelper])
157
            ->getMock();
158
        $servicemock->expects($this->once())
159
            ->method('import_metadata')
160
            ->with($metadataurl)
161
            ->will($this->returnValue(new CurlResult($metadataxml, 0, [])));
162
        $utils = new UtilityFunctions($fakeconfig);
163
        $logic = new MetadataLogic($servicemock, $utils);
164
        $logic->import_metadata($metadataurl);
165
        $this->assertEquals('testId', $fakeconfig->get('application_appid'));
166
    }
167
 
168
    /**
169
     * Function test_if_import_metadata_uses_app_id_class_variable_if_set
170
     *
171
     * @return void
172
     * @throws EduSharingUserException
173
     * @throws dml_exception
174
     */
175
    public function test_if_import_metadata_uses_app_id_class_variable_if_set(): void {
176
        $this->resetAfterTest();
177
        global $_SERVER, $CFG;
178
        require_once($CFG->dirroot . '/mod/edusharing/tests/testUtils/FakeConfig.php');
179
        $_SERVER['SERVER_NAME'] = 'testServer';
180
        $metadataurl            = 'test.de';
181
        $metadataxml            = file_get_contents(__DIR__ . '/metadataTest.xml');
182
        $basehelper             = new EduSharingHelperBase('www.url.de', 'pkey123', 'appid123');
183
        $authhelper             = new EduSharingAuthHelper($basehelper);
184
        $nodeconfig             = new EduSharingNodeHelperConfig(new UrlHandling(true));
185
        $nodehelper             = new EduSharingNodeHelper($basehelper, $nodeconfig);
186
        $servicemock            = $this->getMockBuilder(EduSharingService::class)
187
            ->onlyMethods(['import_metadata'])
188
            ->setConstructorArgs([$authhelper, $nodehelper])
189
            ->getMock();
190
        $servicemock->expects($this->once())
191
            ->method('import_metadata')
192
            ->with($metadataurl)
193
            ->will($this->returnValue(new CurlResult($metadataxml, 0, [])));
194
        $fakeconfig = new FakeConfig();
195
        $utils      = new UtilityFunctions($fakeconfig);
196
        $logic      = new MetadataLogic($servicemock, $utils);
197
        $logic->set_app_id('testId');
198
        $logic->import_metadata($metadataurl);
199
        $this->assertEquals('testId', $fakeconfig->get('application_appid'));
200
    }
201
 
202
    /**
203
     * Function test_if_import_metadata_does_not_set_host_aliases_if_none_are_set
204
     *
205
     * @return void
206
     * @throws EduSharingUserException
207
     * @throws dml_exception
208
     */
209
    public function test_if_import_metadata_does_not_set_host_aliases_if_none_are_set(): void {
210
        $this->resetAfterTest();
211
        global $_SERVER, $CFG;
212
        require_once($CFG->dirroot . '/mod/edusharing/tests/testUtils/FakeConfig.php');
213
        $_SERVER['SERVER_NAME'] = 'testServer';
214
        $metadataurl            = 'test.de';
215
        $metadataxml            = file_get_contents(__DIR__ . '/metadataTest.xml');
216
        $basehelper             = new EduSharingHelperBase('www.url.de', 'pkey123', 'appid123');
217
        $authhelper             = new EduSharingAuthHelper($basehelper);
218
        $nodeconfig             = new EduSharingNodeHelperConfig(new UrlHandling(true));
219
        $nodehelper             = new EduSharingNodeHelper($basehelper, $nodeconfig);
220
        $servicemock            = $this->getMockBuilder(EduSharingService::class)
221
            ->onlyMethods(['import_metadata'])
222
            ->setConstructorArgs([$authhelper, $nodehelper])
223
            ->getMock();
224
        $servicemock->expects($this->once())
225
            ->method('import_metadata')
226
            ->with($metadataurl)
227
            ->will($this->returnValue(new CurlResult($metadataxml, 0, [])));
228
        $fakeconfig = new FakeConfig();
229
        $fakeconfig->set_entries([
230
            'application_appid' => 'testId',
231
        ]);
232
        $utils = new UtilityFunctions($fakeconfig);
233
        $logic = new MetadataLogic($servicemock, $utils);
234
        $logic->import_metadata($metadataurl);
235
        $this->assertFalse($fakeconfig->get('application_host_aliases'));
236
    }
237
 
238
    /**
239
     * Function test_if_import_metadata_sets_host_aliases_if_set_as_class_variables
240
     *
241
     * @return void
242
     * @throws EduSharingUserException
243
     * @throws dml_exception
244
     */
245
    public function test_if_import_metadata_sets_host_aliases_if_set_as_class_variables(): void {
246
        $this->resetAfterTest();
247
        global $_SERVER, $CFG;
248
        require_once($CFG->dirroot . '/mod/edusharing/tests/testUtils/FakeConfig.php');
249
        $_SERVER['SERVER_NAME'] = 'testServer';
250
        $metadataurl            = 'test.de';
251
        $metadataxml            = file_get_contents(__DIR__ . '/metadataTest.xml');
252
        $basehelper             = new EduSharingHelperBase('www.url.de', 'pkey123', 'appid123');
253
        $authhelper             = new EduSharingAuthHelper($basehelper);
254
        $nodeconfig             = new EduSharingNodeHelperConfig(new UrlHandling(true));
255
        $nodehelper             = new EduSharingNodeHelper($basehelper, $nodeconfig);
256
        $servicemock            = $this->getMockBuilder(EduSharingService::class)
257
            ->onlyMethods(['import_metadata'])
258
            ->setConstructorArgs([$authhelper, $nodehelper])
259
            ->getMock();
260
        $servicemock->expects($this->once())
261
            ->method('import_metadata')
262
            ->with($metadataurl)
263
            ->will($this->returnValue(new CurlResult($metadataxml, 0, [])));
264
        $fakeconfig = new FakeConfig();
265
        $fakeconfig->set_entries([
266
            'application_appid' => 'testId',
267
        ]);
268
        $utils = new UtilityFunctions($fakeconfig);
269
        $logic = new MetadataLogic($servicemock, $utils);
270
        $logic->set_host_aliases('hostAliasesTest');
271
        $logic->import_metadata($metadataurl);
272
        $this->assertEquals('hostAliasesTest', $fakeconfig->get('application_host_aliases'));
273
    }
274
 
275
    /**
276
     * Function test_if_import_metadata_does_not_set_wlo_guest_user_if_none_provided
277
     *
278
     * @return void
279
     * @throws EduSharingUserException
280
     * @throws dml_exception
281
     **/
282
    public function test_if_import_metadata_does_not_set_wlo_guest_user_if_none_provided(): void {
283
        $this->resetAfterTest();
284
        global $_SERVER, $CFG;
285
        require_once($CFG->dirroot . '/mod/edusharing/tests/testUtils/FakeConfig.php');
286
        $_SERVER['SERVER_NAME'] = 'testServer';
287
        $metadataurl            = 'test.de';
288
        $metadataxml            = file_get_contents(__DIR__ . '/metadataTest.xml');
289
        $basehelper             = new EduSharingHelperBase('www.url.de', 'pkey123', 'appid123');
290
        $authhelper             = new EduSharingAuthHelper($basehelper);
291
        $nodeconfig             = new EduSharingNodeHelperConfig(new UrlHandling(true));
292
        $nodehelper             = new EduSharingNodeHelper($basehelper, $nodeconfig);
293
        $servicemock            = $this->getMockBuilder(EduSharingService::class)
294
            ->onlyMethods(['import_metadata'])
295
            ->setConstructorArgs([$authhelper, $nodehelper])
296
            ->getMock();
297
        $servicemock->expects($this->once())
298
            ->method('import_metadata')
299
            ->with($metadataurl)
300
            ->will($this->returnValue(new CurlResult($metadataxml, 0, [])));
301
        $fakeconfig = new FakeConfig();
302
        $fakeconfig->set_entries([
303
            'application_appid' => 'testId',
304
        ]);
305
        $utils = new UtilityFunctions($fakeconfig);
306
        $logic = new MetadataLogic($servicemock, $utils);
307
        $logic->import_metadata($metadataurl);
308
        $this->assertFalse($fakeconfig->get('edu_guest_guest_id'));
309
        $this->assertFalse($fakeconfig->get('wlo_guest_option'));
310
    }
311
 
312
    /**
313
     * Function test_if_import_metadata_does_set_wlo_guest_user_if_class_variable_is_set
314
     *
315
     * @return void
316
     * @throws EduSharingUserException
317
     * @throws dml_exception
318
     **/
319
    public function test_if_import_metadata_does_set_wlo_guest_user_if_class_variable_is_set(): void {
320
        $this->resetAfterTest();
321
        global $_SERVER, $CFG;
322
        require_once($CFG->dirroot . '/mod/edusharing/tests/testUtils/FakeConfig.php');
323
        $_SERVER['SERVER_NAME'] = 'testServer';
324
        $metadataurl            = 'test.de';
325
        $metadataxml            = file_get_contents(__DIR__ . '/metadataTest.xml');
326
        $basehelper             = new EduSharingHelperBase('www.url.de', 'pkey123', 'appid123');
327
        $authhelper             = new EduSharingAuthHelper($basehelper);
328
        $nodeconfig             = new EduSharingNodeHelperConfig(new UrlHandling(true));
329
        $nodehelper             = new EduSharingNodeHelper($basehelper, $nodeconfig);
330
        $servicemock            = $this->getMockBuilder(EduSharingService::class)
331
            ->onlyMethods(['import_metadata'])
332
            ->setConstructorArgs([$authhelper, $nodehelper])
333
            ->getMock();
334
        $servicemock->expects($this->once())
335
            ->method('import_metadata')
336
            ->with($metadataurl)
337
            ->will($this->returnValue(new CurlResult($metadataxml, 0, [])));
338
        $fakeconfig = new FakeConfig();
339
        $fakeconfig->set_entries([
340
            'application_appid' => 'testId',
341
        ]);
342
        $utils = new UtilityFunctions($fakeconfig);
343
        $logic = new MetadataLogic($servicemock, $utils);
344
        $logic->set_wlo_guest_user('wloGuestTest');
345
        $logic->import_metadata($metadataurl);
346
        $this->assertEquals('wloGuestTest', $fakeconfig->get('edu_guest_guest_id'));
347
        $this->assertEquals('1', $fakeconfig->get('wlo_guest_option'));
348
    }
349
 
350
    /**
351
     * Function test_if_import_metadata_generates_new_key_pair_if_none_found
352
     *
353
     * @return void
354
     * @throws EduSharingUserException
355
     * @throws dml_exception
356
     **/
357
    public function test_if_import_metadata_generates_new_key_pair_if_none_found(): void {
358
        $this->resetAfterTest();
359
        global $_SERVER, $CFG;
360
        require_once($CFG->dirroot . '/mod/edusharing/tests/testUtils/FakeConfig.php');
361
        $_SERVER['SERVER_NAME'] = 'testServer';
362
        $metadataurl            = 'test.de';
363
        $metadataxml            = file_get_contents(__DIR__ . '/metadataTestWithoutKey.xml');
364
        $basehelper             = new EduSharingHelperBase('www.url.de', 'pkey123', 'appid123');
365
        $authhelper             = new EduSharingAuthHelper($basehelper);
366
        $nodeconfig             = new EduSharingNodeHelperConfig(new UrlHandling(true));
367
        $nodehelper             = new EduSharingNodeHelper($basehelper, $nodeconfig);
368
        $servicemock            = $this->getMockBuilder(EduSharingService::class)
369
            ->onlyMethods(['import_metadata'])
370
            ->setConstructorArgs([$authhelper, $nodehelper])
371
            ->getMock();
372
        $servicemock->expects($this->once())
373
            ->method('import_metadata')
374
            ->with($metadataurl)
375
            ->will($this->returnValue(new CurlResult($metadataxml, 0, [])));
376
        $fakeconfig = new FakeConfig();
377
        $fakeconfig->set_entries([
378
            'application_appid' => 'testId',
379
        ]);
380
        $utils = new UtilityFunctions($fakeconfig);
381
        $logic = new MetadataLogic($servicemock, $utils);
382
        $logic->set_wlo_guest_user('wloGuestTest');
383
        $logic->import_metadata($metadataurl);
384
        $this->assertNotEmpty($fakeconfig->get('application_private_key'));
385
        $this->assertNotEmpty($fakeconfig->get('application_public_key'));
386
    }
387
 
388
    /**
389
     * Function test_if_create_xml_metadata_creates_xml_with_all_needed_entries
390
     *
391
     * @return void
392
     *
393
     * @throws dml_exception
394
     */
395
    public function test_if_create_xml_metadata_creates_xml_with_all_needed_entries(): void {
396
        $this->resetAfterTest();
397
        global $CFG;
398
        require_once($CFG->dirroot . '/mod/edusharing/tests/testUtils/FakeConfig.php');
399
        $CFG->wwwroot = 'https://www.example.com/moodle';
400
        $fakeconfig   = new FakeConfig();
401
        $fakeconfig->set_entries([
402
            'application_appid'         => 'testAppId',
403
            'application_type'          => 'testType',
404
            'application_host'          => 'testHost',
405
            'application_host_aliases'  => 'testHostAliases',
406
            'application_public_key'    => 'testPublicKey',
407
            'EDU_AUTH_AFFILIATION_NAME' => 'testAffiliationName',
408
            'edu_guest_guest_id'        => 'testGuestId',
409
            'wlo_guest_option'          => '1',
410
        ]);
411
        $basehelper = new EduSharingHelperBase('www.url.de', 'testPublicKey', 'testAppId');
412
        $authhelper = new EduSharingAuthHelper($basehelper);
413
        $nodeconfig = new EduSharingNodeHelperConfig(new UrlHandling(true));
414
        $nodehelper = new EduSharingNodeHelper($basehelper, $nodeconfig);
415
        $logic      = new MetadataLogic(new EduSharingService($authhelper, $nodehelper), new UtilityFunctions($fakeconfig));
416
        $xmlstring  = $logic->create_xml_metadata();
417
        $xml        = new SimpleXMLElement($xmlstring);
418
        $this->assertEquals(11, $xml->count());
419
        $this->assertEquals('testAppId', $xml->xpath('entry[@key="appid"]')[0]);
420
        $this->assertEquals('testType', $xml->xpath('entry[@key="type"]')[0]);
421
        $this->assertEquals('moodle', $xml->xpath('entry[@key="subtype"]')[0]);
422
        $this->assertEquals('www.example.com', $xml->xpath('entry[@key="domain"]')[0]);
423
        $this->assertEquals('testHost', $xml->xpath('entry[@key="host"]')[0]);
424
        $this->assertEquals('testHostAliases', $xml->xpath('entry[@key="host_aliases"]')[0]);
425
        $this->assertEquals('true', $xml->xpath('entry[@key="trustedclient"]')[0]);
426
        $this->assertEquals('moodle:course/update', $xml->xpath('entry[@key="hasTeachingPermission"]')[0]);
427
        $this->assertEquals('testPublicKey', $xml->xpath('entry[@key="public_key"]')[0]);
428
        $this->assertEquals('testAffiliationName', $xml->xpath('entry[@key="appcaption"]')[0]);
429
        $this->assertEquals('testGuestId', $xml->xpath('entry[@key="auth_by_app_user_whitelist"]')[0]);
430
    }
431
}