forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator_test.php
572 lines (505 loc) · 19.2 KB
/
generator_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Test class covering the h5p data generator class.
*
* @package core_h5p
* @category test
* @copyright 2019 Mihail Geshoski <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_h5p;
use core_h5p\local\library\autoloader;
defined('MOODLE_INTERNAL') || die();
/**
* Generator testcase for the core_grading generator.
*
* @package core_h5p
* @category test
* @copyright 2019 Mihail Geshoski <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @runTestsInSeparateProcesses
*/
class generator_testcase extends \advanced_testcase {
/**
* Tests set up.
*/
protected function setUp(): void {
parent::setUp();
autoloader::register();
}
/**
* Test the returned data of generate_h5p_data() when the method is called without requesting
* creation of library files.
*/
public function test_generate_h5p_data_no_files_created_return_data() {
global $DB;
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
$data = $generator->generate_h5p_data();
$mainlib = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']);
$lib1 = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']);
$lib2 = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']);
$lib3 = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']);
$lib4 = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']);
$lib5 = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']);
$h5p = $DB->get_record('h5p', ['mainlibraryid' => $mainlib->id]);
$expected = (object) [
'h5pcontent' => (object) array(
'h5pid' => $h5p->id,
'contentdependencies' => array($mainlib, $lib1, $lib2, $lib3, $lib4)
),
'mainlib' => (object) array(
'data' => $mainlib,
'dependencies' => array($lib1, $lib2, $lib3)
),
'lib1' => (object) array(
'data' => $lib1,
'dependencies' => array($lib2, $lib3, $lib4)
),
'lib2' => (object) array(
'data' => $lib2,
'dependencies' => array()
),
'lib3' => (object) array(
'data' => $lib3,
'dependencies' => array($lib5)
),
'lib4' => (object) array(
'data' => $lib4,
'dependencies' => array()
),
'lib5' => (object) array(
'data' => $lib5,
'dependencies' => array()
),
];
$this->assertEquals($expected, $data);
}
/**
* Test the returned data of generate_h5p_data() when the method requests
* creation of library files.
*/
public function test_generate_h5p_data_files_created_return_data() {
global $DB;
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
$data = $generator->generate_h5p_data(true);
$mainlib = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']);
$lib1 = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']);
$lib2 = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']);
$lib3 = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']);
$lib4 = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']);
$lib5 = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']);
$h5p = $DB->get_record('h5p', ['mainlibraryid' => $mainlib->id]);
$expected = (object) [
'h5pcontent' => (object) array(
'h5pid' => $h5p->id,
'contentdependencies' => array($mainlib, $lib1, $lib2, $lib3, $lib4)
),
'mainlib' => (object) array(
'data' => $mainlib,
'dependencies' => array($lib1, $lib2, $lib3)
),
'lib1' => (object) array(
'data' => $lib1,
'dependencies' => array($lib2, $lib3, $lib4)
),
'lib2' => (object) array(
'data' => $lib2,
'dependencies' => array()
),
'lib3' => (object) array(
'data' => $lib3,
'dependencies' => array($lib5)
),
'lib4' => (object) array(
'data' => $lib4,
'dependencies' => array()
),
'lib5' => (object) array(
'data' => $lib5,
'dependencies' => array()
),
];
$this->assertEquals($expected, $data);
}
/**
* Test the behaviour of generate_h5p_data(). Test whether library files are created or not
* on filesystem depending what the method defines.
*
* @dataProvider test_generate_h5p_data_files_creation_provider
* @param bool $createlibraryfiles Whether to create library files on the filesystem
* @param bool $expected The expectation whether the files have been created or not
**/
public function test_generate_h5p_data_files_creation(bool $createlibraryfiles, bool $expected) {
global $DB;
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
$generator->generate_h5p_data($createlibraryfiles);
$libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']);
$libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']);
$libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']);
$libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']);
$libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']);
$libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']);
foreach($libraries as $lib) {
// Return the created library files.
$libraryfiles = $DB->get_records('files',
array(
'component' => \core_h5p\file_storage::COMPONENT,
'filearea' => \core_h5p\file_storage::LIBRARY_FILEAREA,
'itemid' => $lib->id
)
);
$haslibraryfiles = !empty($libraryfiles);
$this->assertEquals($expected, $haslibraryfiles);
}
}
/**
* Data provider for test_generate_h5p_data_files_creation().
*
* @return array
*/
public function test_generate_h5p_data_files_creation_provider(): array {
return [
'Do not create library related files on the filesystem' => [
false,
false
],
'Create library related files on the filesystem' => [
true,
true
]
];
}
/**
* Test the behaviour of create_library_record(). Test whether the library data is properly
* saved in the database.
*/
public function test_create_library_record() {
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
$data = $generator->create_library_record(
'Library', 'Lib', 1, 2, 3, 'Semantics example', '/regex11/', 'http://tutorial.org/', 'http://example.org/'
);
unset($data->id);
$expected = (object) [
'machinename' => 'Library',
'title' => 'Lib',
'majorversion' => '1',
'minorversion' => '2',
'patchversion' => '3',
'runnable' => '1',
'fullscreen' => '1',
'embedtypes' => '',
'preloadedjs' => 'js/example.js',
'preloadedcss' => 'css/example.css',
'droplibrarycss' => '',
'semantics' => 'Semantics example',
'addto' => '/regex11/',
'tutorial' => 'http://tutorial.org/',
'example' => 'http://example.org/',
'coremajor' => null,
'coreminor' => null,
'metadatasettings' => null,
'enabled' => 1,
];
$this->assertEquals($expected, $data);
}
/**
* Test the behaviour of create_h5p_record(). Test whather the h5p content data is
* properly saved in the database.
*
* @dataProvider test_create_h5p_record_provider
* @param array $h5pdata The h5p content data
* @param \stdClass $expected The expected saved data
**/
public function test_create_h5p_record(array $h5pdata, \stdClass $expected) {
global $DB;
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
$h5pid = call_user_func_array([$generator, 'create_h5p_record'], $h5pdata);
$data = $DB->get_record('h5p', ['id' => $h5pid]);
unset($data->id);
unset($data->timecreated);
unset($data->timemodified);
$this->assertEquals($data, $expected);
}
/**
* Data provider for test_create_h5p_record().
*
* @return array
*/
public function test_create_h5p_record_provider(): array {
$createdjsoncontent = json_encode(
array(
'text' => '<p>Created dummy text<\/p>\n',
'questions' => '<p>Test created question<\/p>\n'
)
);
$defaultjsoncontent = json_encode(
array(
'text' => '<p>Dummy text<\/p>\n',
'questions' => '<p>Test question<\/p>\n'
)
);
$createdfilteredcontent = json_encode(
array(
'text' => 'Created dummy text',
'questions' => 'Test created question'
)
);
$defaultfilteredcontent = json_encode(
array(
'text' => 'Dummy text',
'questions' => 'Test question'
)
);
return [
'Create h5p content record with set json content and set filtered content' => [
[
1,
$createdjsoncontent,
$createdfilteredcontent
],
(object) array(
'jsoncontent' => $createdjsoncontent,
'mainlibraryid' => '1',
'displayoptions' => '8',
'pathnamehash' => sha1('pathname'),
'contenthash' => sha1('content'),
'filtered' => $createdfilteredcontent,
)
],
'Create h5p content record with set json content and default filtered content' => [
[
1,
$createdjsoncontent,
null
],
(object) array(
'jsoncontent' => $createdjsoncontent,
'mainlibraryid' => '1',
'displayoptions' => '8',
'pathnamehash' => sha1('pathname'),
'contenthash' => sha1('content'),
'filtered' => $defaultfilteredcontent,
)
],
'Create h5p content record with default json content and set filtered content' => [
[
1,
null,
$createdfilteredcontent
],
(object) array(
'jsoncontent' => $defaultjsoncontent,
'mainlibraryid' => '1',
'displayoptions' => '8',
'pathnamehash' => sha1('pathname'),
'contenthash' => sha1('content'),
'filtered' => $createdfilteredcontent,
)
],
'Create h5p content record with default json content and default filtered content' => [
[
1,
null,
null
],
(object) array(
'jsoncontent' => $defaultjsoncontent,
'mainlibraryid' => '1',
'displayoptions' => '8',
'pathnamehash' => sha1('pathname'),
'contenthash' => sha1('content'),
'filtered' => $defaultfilteredcontent,
)
]
];
}
/**
* Test the behaviour of create_contents_libraries_record(). Test whether the contents libraries
* are properly saved in the database.
*
* @dataProvider test_create_contents_libraries_record_provider
* @param array $contentslibrariestdata The h5p contents libraries data.
* @param \stdClass $expected The expected saved data.
**/
public function test_create_contents_libraries_record(array $contentslibrariestdata, \stdClass $expected) {
global $DB;
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
$contentlibid = call_user_func_array([$generator, 'create_contents_libraries_record'], $contentslibrariestdata);
$data = $DB->get_record('h5p_contents_libraries', ['id' => $contentlibid]);
unset($data->id);
$this->assertEquals($data, $expected);
}
/**
* Data provider for test_create_contents_libraries_record().
*
* @return array
*/
public function test_create_contents_libraries_record_provider(): array {
return [
'Create h5p content library with set dependency type' => [
[
1,
1,
'dynamic'
],
(object) array(
'h5pid' => '1',
'libraryid' => '1',
'dependencytype' => 'dynamic',
'dropcss' => '0',
'weight' => '1'
)
],
'Create h5p content library with a default dependency type' => [
[
1,
1
],
(object) array(
'h5pid' => '1',
'libraryid' => '1',
'dependencytype' => 'preloaded',
'dropcss' => '0',
'weight' => '1'
)
]
];
}
/**
* Test the behaviour of create_library_dependency_record(). Test whether the contents libraries
* are properly saved in the database.
*
* @dataProvider test_create_library_dependency_record_provider
* @param array $librarydependencydata The library dependency data.
* @param \stdClass $expected The expected saved data.
**/
public function test_create_library_dependency_record(array $librarydependencydata, \stdClass $expected) {
global $DB;
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
$contentlibid = call_user_func_array([$generator, 'create_library_dependency_record'], $librarydependencydata);
$data = $DB->get_record('h5p_library_dependencies', ['id' => $contentlibid]);
unset($data->id);
$this->assertEquals($data, $expected);
}
/**
* Data provider for test_create_library_dependency_record().
*
* @return array
*/
public function test_create_library_dependency_record_provider(): array {
return [
'Create h5p library dependency with set dependency type' => [
[
1,
1,
'dynamic'
],
(object) array(
'libraryid' => '1',
'requiredlibraryid' => '1',
'dependencytype' => 'dynamic'
)
],
'Create h5p library dependency with default dependency type' => [
[
1,
1
],
(object) array(
'libraryid' => '1',
'requiredlibraryid' => '1',
'dependencytype' => 'preloaded'
)
]
];
}
/**
* Test the behaviour of create_content_file(). Test whether a file belonging to a content is created.
*
* @dataProvider test_create_content_file_provider
* @param array $filedata Data from the file to be created.
* @param array $expecteddata Data expected.Data from the file to be created.
*/
public function test_create_content_file($filedata, $expecteddata): void {
$this->resetAfterTest();
$generator = self::getDataGenerator()->get_plugin_generator('core_h5p');
if ($expecteddata[1] === 'exception') {
$this->expectException('coding_exception');
}
call_user_func_array([$generator, 'create_content_file'], $filedata);
$systemcontext = \context_system::instance();
$filearea = $filedata[1];
$filepath = '/'. dirname($filedata[0]). '/';
$filename = basename($filedata[0]);
$itemid = $expecteddata[0];
$fs = new \file_storage();
$exists = $fs->file_exists($systemcontext->id, file_storage::COMPONENT, $filearea, $itemid, $filepath,
$filename);
if ($expecteddata[1] === true) {
$this->assertTrue($exists);
} else if ($expecteddata[1] === false) {
$this->assertFalse($exists);
}
}
/**
* Data provider for test_create_content_file(). Data from different files to be created.
*
* @return array
**/
public function test_create_content_file_provider(): array {
return [
'Create file in content with id 4' => [
[
'images/img1.png',
'content',
4
],
[
4,
true
]
],
'Create file in the editor' => [
[
'images/img1.png',
'editor'
],
[
0,
true
]
],
'Create file in content without id' => [
[
'images/img1.png',
'content'
],
[
0,
'exception'
]
]
];
}
}