forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
framework_test.php
2336 lines (1896 loc) · 86.9 KB
/
framework_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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* Testing the H5PFrameworkInterface interface implementation.
*
* @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_collator;
use Moodle\H5PCore;
use Moodle\H5PDisplayOptionBehaviour;
/**
*
* Test class covering the H5PFrameworkInterface interface implementation.
*
* @package core_h5p
* @copyright 2019 Mihail Geshoski <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @runTestsInSeparateProcesses
*/
class framework_test extends \advanced_testcase {
/** @var \core_h5p\framework */
private $framework;
/**
* Set up function for tests.
*/
public function setUp(): void {
$factory = new \core_h5p\factory();
$this->framework = $factory->get_framework();
}
/**
* Test the behaviour of getPlatformInfo().
*/
public function test_getPlatformInfo() {
global $CFG;
$platforminfo = $this->framework->getPlatformInfo();
$expected = array(
'name' => 'Moodle',
'version' => $CFG->version,
'h5pVersion' => $CFG->version
);
$this->assertEquals($expected, $platforminfo);
}
/**
* Test the behaviour of fetchExternalData() when the store path is not defined.
*
* This test is intensive and requires downloading content of an external file,
* therefore it might take longer time to execute.
* In order to execute this test PHPUNIT_LONGTEST should be set to true in phpunit.xml or directly in config.php.
*/
public function test_fetchExternalData_no_path_defined() {
if (!PHPUNIT_LONGTEST) {
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
}
$this->resetAfterTest();
$library = 'H5P.Accordion';
// Provide a valid URL to an external H5P content.
$url = $this->getExternalTestFileUrl('/'.$library.'.h5p');
// Test fetching an external H5P content without defining a path to where the file should be stored.
$data = $this->framework->fetchExternalData($url, null, true);
// The response should not be empty and return true if the file was successfully downloaded.
$this->assertNotEmpty($data);
$this->assertTrue($data);
$h5pfolderpath = $this->framework->getUploadedH5pFolderPath();
// The uploaded file should exist on the filesystem.
$this->assertTrue(file_exists($h5pfolderpath . '.h5p'));
}
/**
* Test the behaviour of fetchExternalData() when the store path is defined.
*
* This test is intensive and requires downloading content of an external file,
* therefore it might take longer time to execute.
* In order to execute this test PHPUNIT_LONGTEST should be set to true in phpunit.xml or directly in config.php.
*/
public function test_fetchExternalData_path_defined() {
global $CFG;
if (!PHPUNIT_LONGTEST) {
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
}
$this->resetAfterTest();
$library = 'H5P.Accordion';
// Provide a valid URL to an external H5P content.
$url = $this->getExternalTestFileUrl('/'.$library.'.h5p');
$h5pfolderpath = $CFG->tempdir . uniqid('/h5p-');
$data = $this->framework->fetchExternalData($url, null, true, $h5pfolderpath . '.h5p');
// The response should not be empty and return true if the content has been successfully saved to a file.
$this->assertNotEmpty($data);
$this->assertTrue($data);
// The uploaded file should exist on the filesystem.
$this->assertTrue(file_exists($h5pfolderpath . '.h5p'));
}
/**
* Test the behaviour of fetchExternalData() when the URL is pointing to an external file that is
* not an h5p content.
*
* This test is intensive and requires downloading content of an external file,
* therefore it might take longer time to execute.
* In order to execute this test PHPUNIT_LONGTEST should be set to true in phpunit.xml or directly in config.php.
*/
public function test_fetchExternalData_url_not_h5p() {
if (!PHPUNIT_LONGTEST) {
// This test is intensive and requires downloading the content of an external file.
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
}
$this->resetAfterTest();
// Provide an URL to an external file that is not an H5P content file.
$url = $this->getExternalTestFileUrl('/h5pcontenttypes.json');
$data = $this->framework->fetchExternalData($url, null, true);
// The response should not be empty and return true if the content has been successfully saved to a file.
$this->assertNotEmpty($data);
$this->assertTrue($data);
// The uploaded file should exist on the filesystem with it's original extension.
// NOTE: The file would be later validated by the H5P Validator.
$h5pfolderpath = $this->framework->getUploadedH5pFolderPath();
$this->assertTrue(file_exists($h5pfolderpath . '.json'));
}
/**
* Test the behaviour of fetchExternalData() when the URL is invalid.
*/
public function test_fetchExternalData_url_invalid() {
// Provide an invalid URL to an external file.
$url = "someprotocol://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";
$data = $this->framework->fetchExternalData($url, null, true);
// The response should be empty.
$this->assertEmpty($data);
}
/**
* Test the behaviour of setLibraryTutorialUrl().
*/
public function test_setLibraryTutorialUrl() {
global $DB;
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
// Create several libraries records.
$lib1 = $generator->create_library_record('Library1', 'Lib1', 1, 0, 1, '', null, 'http://tutorial1.org',
'http://example.org');
$lib2 = $generator->create_library_record('Library2', 'Lib2', 2, 0, 1, '', null, 'http://tutorial2.org');
$lib3 = $generator->create_library_record('Library3', 'Lib3', 3, 0);
// Check only lib1 tutorial URL is updated.
$url = 'https://newtutorial.cat';
$this->framework->setLibraryTutorialUrl($lib1->machinename, $url);
$libraries = $DB->get_records('h5p_libraries');
$this->assertEquals($libraries[$lib1->id]->tutorial, $url);
$this->assertNotEquals($libraries[$lib2->id]->tutorial, $url);
// Check lib1 tutorial URL is set to null.
$this->framework->setLibraryTutorialUrl($lib1->machinename, null);
$libraries = $DB->get_records('h5p_libraries');
$this->assertCount(3, $libraries);
$this->assertNull($libraries[$lib1->id]->tutorial);
// Check no tutorial URL is set if library name doesn't exist.
$this->framework->setLibraryTutorialUrl('Unexisting library', $url);
$libraries = $DB->get_records('h5p_libraries');
$this->assertCount(3, $libraries);
$this->assertNull($libraries[$lib1->id]->tutorial);
$this->assertEquals($libraries[$lib2->id]->tutorial, 'http://tutorial2.org');
$this->assertNull($libraries[$lib3->id]->tutorial);
// Check tutorial is set as expected when it was null.
$this->framework->setLibraryTutorialUrl($lib3->machinename, $url);
$libraries = $DB->get_records('h5p_libraries');
$this->assertEquals($libraries[$lib3->id]->tutorial, $url);
$this->assertNull($libraries[$lib1->id]->tutorial);
$this->assertEquals($libraries[$lib2->id]->tutorial, 'http://tutorial2.org');
}
/**
* Test the behaviour of setErrorMessage().
*/
public function test_setErrorMessage() {
// Set an error message and an error code.
$message = "Error message";
$code = '404';
// Set an error message.
$this->framework->setErrorMessage($message, $code);
// Get the error messages.
$errormessages = $this->framework->getMessages('error');
$expected = new \stdClass();
$expected->code = 404;
$expected->message = 'Error message';
$this->assertEquals($expected, $errormessages[0]);
}
/**
* Test the behaviour of setInfoMessage().
*/
public function test_setInfoMessage() {
$message = "Info message";
// Set an info message.
$this->framework->setInfoMessage($message);
// Get the info messages.
$infomessages = $this->framework->getMessages('info');
$expected = 'Info message';
$this->assertEquals($expected, $infomessages[0]);
}
/**
* Test the behaviour of getMessages() when requesting the info messages.
*/
public function test_getMessages_info() {
// Set an info message.
$this->framework->setInfoMessage("Info message");
// Set an error message.
$this->framework->setErrorMessage("Error message 1", 404);
// Get the info messages.
$infomessages = $this->framework->getMessages('info');
$expected = 'Info message';
// Make sure that only the info message has been returned.
$this->assertCount(1, $infomessages);
$this->assertEquals($expected, $infomessages[0]);
$infomessages = $this->framework->getMessages('info');
// Make sure the info messages have now been removed.
$this->assertEmpty($infomessages);
}
/**
* Test the behaviour of getMessages() when requesting the error messages.
*/
public function test_getMessages_error() {
// Set an info message.
$this->framework->setInfoMessage("Info message");
// Set an error message.
$this->framework->setErrorMessage("Error message 1", 404);
// Set another error message.
$this->framework->setErrorMessage("Error message 2", 403);
// Get the error messages.
$errormessages = $this->framework->getMessages('error');
// Make sure that only the error messages are being returned.
$this->assertEquals(2, count($errormessages));
$expected1 = (object) [
'code' => 404,
'message' => 'Error message 1'
];
$expected2 = (object) [
'code' => 403,
'message' => 'Error message 2'
];
$this->assertEquals($expected1, $errormessages[0]);
$this->assertEquals($expected2, $errormessages[1]);
$errormessages = $this->framework->getMessages('error');
// Make sure the info messages have now been removed.
$this->assertEmpty($errormessages);
}
/**
* Test the behaviour of t() when translating existing string that does not require any arguments.
*/
public function test_t_existing_string_no_args() {
// Existing language string without passed arguments.
$translation = $this->framework->t('No copyright information available for this content.');
// Make sure the string translation has been returned.
$this->assertEquals('No copyright information available for this content.', $translation);
}
/**
* Test the behaviour of t() when translating existing string that does require parameters.
*/
public function test_t_existing_string_args() {
// Existing language string with passed arguments.
$translation = $this->framework->t('Illegal option %option in %library',
['%option' => 'example', '%library' => 'Test library']);
// Make sure the string translation has been returned.
$this->assertEquals('Illegal option example in Test library', $translation);
}
/**
* Test the behaviour of t() when translating non-existent string.
*/
public function test_t_non_existent_string() {
// Non-existing language string.
$message = 'Random message %option';
$translation = $this->framework->t($message);
// Make sure a debugging message is triggered.
$this->assertDebuggingCalled("String translation cannot be found. Please add a string definition for '" .
$message . "' in the core_h5p component.");
// As the string does not exist in the mapping array, make sure the passed message is returned.
$this->assertEquals($message, $translation);
}
/**
* Test the behaviour of getLibraryFileUrl() when requesting a file URL from an existing library and
* the folder name is parsable.
**/
public function test_getLibraryFileUrl() {
global $CFG;
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
// Create a library record.
$lib = $generator->create_library_record('Library', 'Lib', 1, 1);
$expected = "{$CFG->wwwroot}/pluginfile.php/1/core_h5p/libraries/{$lib->id}/Library-1.1/library.json";
// Get the URL of a file from an existing library. The provided folder name is parsable.
$actual = $this->framework->getLibraryFileUrl('Library-1.1', 'library.json');
// Make sure the expected URL is returned.
$this->assertEquals($expected, $actual);
}
/**
* Test the behaviour of getLibraryFileUrl() when requesting a file URL from a non-existent library and
* the folder name is parsable.
**/
public function test_getLibraryFileUrl_non_existent_library() {
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
// Create a library record.
$generator->create_library_record('Library', 'Lib', 1, 1);
// Get the URL of a file from a non-existent library. The provided folder name is parsable.
$actual = $this->framework->getLibraryFileUrl('Library2-1.1', 'library.json');
// Make sure a debugging message is triggered.
$this->assertDebuggingCalled('The library "Library2-1.1" does not exist.');
// Make sure that an URL is not returned.
$this->assertEquals(null, $actual);
}
/**
* Test the behaviour of getLibraryFileUrl() when requesting a file URL from an existing library and
* the folder name is not parsable.
**/
public function test_getLibraryFileUrl_not_parsable_folder_name() {
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
// Create a library record.
$generator->create_library_record('Library', 'Lib', 1, 1);
// Get the URL of a file from an existing library. The provided folder name is not parsable.
$actual = $this->framework->getLibraryFileUrl('Library1.1', 'library.json');
// Make sure a debugging message is triggered.
$this->assertDebuggingCalled(
'The provided string value "Library1.1" is not a valid name for a library folder.');
// Make sure that an URL is not returned.
$this->assertEquals(null, $actual);
}
/**
* Test the behaviour of getLibraryFileUrl() when requesting a file URL from a library that has multiple
* versions and the folder name is parsable.
**/
public function test_getLibraryFileUrl_library_has_multiple_versions() {
global $CFG;
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
// Create library records with a different minor version.
$lib1 = $generator->create_library_record('Library', 'Lib', 1, 1);
$lib2 = $generator->create_library_record('Library', 'Lib', 1, 3);
$expected = "{$CFG->wwwroot}/pluginfile.php/1/core_h5p/libraries/{$lib2->id}/Library-1.3/library.json";
// Get the URL of a file from an existing library (Library 1.3). The provided folder name is parsable.
$actual = $this->framework->getLibraryFileUrl('Library-1.3', 'library.json');
// Make sure the proper URL (from the requested library version) is returned.
$this->assertEquals($expected, $actual);
}
/**
* Test the behaviour of getLibraryFileUrl() when requesting a file URL from a library that has multiple
* patch versions and the folder name is parsable.
**/
public function test_getLibraryFileUrl_library_has_multiple_patch_versions() {
global $CFG;
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
// Create library records with a different patch version.
$lib1 = $generator->create_library_record('Library', 'Lib', 1, 1, 2);
$lib2 = $generator->create_library_record('Library', 'Lib', 1, 1, 4);
$lib3 = $generator->create_library_record('Library', 'Lib', 1, 1, 3);
$expected = "{$CFG->wwwroot}/pluginfile.php/1/core_h5p/libraries/{$lib2->id}/Library-1.1/library.json";
// Get the URL of a file from an existing library. The provided folder name is parsable.
$actual = $this->framework->getLibraryFileUrl('Library-1.1', 'library.json');
// Make sure the proper URL (from the latest library patch) is returned.
$this->assertEquals($expected, $actual);
}
/**
* Test the behaviour of getLibraryFileUrl() when requesting a file URL from a sub-folder
* of an existing library and the folder name is parsable.
**/
public function test_getLibraryFileUrl_library_subfolder() {
global $CFG;
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
// Create a library record.
$lib = $generator->create_library_record('Library', 'Lib', 1, 1);
$expected = "{$CFG->wwwroot}/pluginfile.php/1/core_h5p/libraries/{$lib->id}/Library-1.1/css/example.css";
// Get the URL of a file from a sub-folder from an existing library. The provided folder name is parsable.
$actual = $this->framework->getLibraryFileUrl('Library-1.1/css', 'example.css');
// Make sure the proper URL is returned.
$this->assertEquals($expected, $actual);
}
/**
* Test the behaviour of loadAddons().
*/
public function test_loadAddons() {
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
// Create a Library addon (1.1).
$generator->create_library_record('Library', 'Lib', 1, 1, 2,
'', '/regex1/');
// Create a Library addon (1.3).
$generator->create_library_record('Library', 'Lib', 1, 3, 2,
'', '/regex2/');
// Create a Library addon (1.2).
$generator->create_library_record('Library', 'Lib', 1, 2, 2,
'', '/regex3/');
// Create a Library1 addon (1.2)
$generator->create_library_record('Library1', 'Lib1', 1, 2, 2,
'', '/regex11/');
// Load the latest version of each addon.
$addons = $this->framework->loadAddons();
// The addons array should return 2 results (Library and Library1 addon).
$this->assertCount(2, $addons);
// Ensure the addons array is consistently ordered before asserting their contents.
core_collator::asort_array_of_arrays_by_key($addons, 'machineName');
[$addonone, $addontwo] = array_values($addons);
// Make sure the version 1.3 is the latest 'Library' addon version.
$this->assertEquals('Library', $addonone['machineName']);
$this->assertEquals(1, $addonone['majorVersion']);
$this->assertEquals(3, $addonone['minorVersion']);
// Make sure the version 1.2 is the latest 'Library1' addon version.
$this->assertEquals('Library1', $addontwo['machineName']);
$this->assertEquals(1, $addontwo['majorVersion']);
$this->assertEquals(2, $addontwo['minorVersion']);
}
/**
* Test the behaviour of loadLibraries().
*/
public function test_loadLibraries() {
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
// Generate h5p related data.
$generator->generate_h5p_data();
// Load all libraries.
$libraries = $this->framework->loadLibraries();
// Make sure all libraries are returned.
$this->assertNotEmpty($libraries);
$this->assertCount(6, $libraries);
$this->assertEquals('MainLibrary', $libraries['MainLibrary'][0]->machine_name);
$this->assertEquals('1', $libraries['MainLibrary'][0]->major_version);
$this->assertEquals('0', $libraries['MainLibrary'][0]->minor_version);
$this->assertEquals('1', $libraries['MainLibrary'][0]->patch_version);
}
/**
* Test the behaviour of test_getLibraryId() when requesting an existing machine name.
*/
public function test_getLibraryId_existing_machine_name() {
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
// Create a library.
$lib = $generator->create_library_record('Library', 'Lib', 1, 1, 2);
// Request the library ID of the library with machine name 'Library'.
$libraryid = $this->framework->getLibraryId('Library');
// Make sure the library ID is being returned.
$this->assertNotFalse($libraryid);
$this->assertEquals($lib->id, $libraryid);
}
/**
* Test the behaviour of test_getLibraryId() when requesting a non-existent machine name.
*/
public function test_getLibraryId_non_existent_machine_name() {
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
// Create a library.
$generator->create_library_record('Library', 'Lib', 1, 1, 2);
// Request the library ID of the library with machinename => 'TestLibrary' (non-existent).
$libraryid = $this->framework->getLibraryId('TestLibrary');
// Make sure the library ID not being returned.
$this->assertFalse($libraryid);
}
/**
* Test the behaviour of test_getLibraryId() when requesting a non-existent major version.
*/
public function test_getLibraryId_non_existent_major_version() {
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
// Create a library.
$generator->create_library_record('Library', 'Lib', 1, 1, 2);
// Request the library ID of the library with machine name => 'Library', majorversion => 2 (non-existent).
$libraryid = $this->framework->getLibraryId('Library', 2);
// Make sure the library ID not being returned.
$this->assertFalse($libraryid);
}
/**
* Test the behaviour of test_getLibraryId() when requesting a non-existent minor version.
*/
public function test_getLibraryId_non_existent_minor_version() {
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
// Create a library.
$generator->create_library_record('Library', 'Lib', 1, 1, 2);
// Request the library ID of the library with machine name => 'Library',
// majorversion => 1, minorversion => 2 (non-existent).
$libraryid = $this->framework->getLibraryId('Library', 1, 2);
// Make sure the library ID not being returned.
$this->assertFalse($libraryid);
}
/**
* Test the behaviour of isPatchedLibrary().
*
* @dataProvider test_isPatchedLibrary_provider
* @param array $libraryrecords Array containing data for the library creation
* @param array $testlibrary Array containing the test library data
* @param bool $expected The expectation whether the library is patched or not
**/
public function test_isPatchedLibrary(array $libraryrecords, array $testlibrary, bool $expected): void {
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
foreach ($libraryrecords as $library) {
call_user_func_array([$generator, 'create_library_record'], $library);
}
$this->assertEquals($expected, $this->framework->isPatchedLibrary($testlibrary));
}
/**
* Data provider for test_isPatchedLibrary().
*
* @return array
*/
public function test_isPatchedLibrary_provider(): array {
return [
'Unpatched library. No different versioning' => [
[
['TestLibrary', 'Test', 1, 1, 2],
],
[
'machineName' => 'TestLibrary',
'majorVersion' => 1,
'minorVersion' => 1,
'patchVersion' => 2
],
false,
],
'Major version identical; Minor version identical; Patch version newer' => [
[
['TestLibrary', 'Test', 1, 1, 2],
],
[
'machineName' => 'TestLibrary',
'majorVersion' => 1,
'minorVersion' => 1,
'patchVersion' => 3
],
true,
],
'Major version identical; Minor version newer; Patch version newer' => [
[
['TestLibrary', 'Test', 1, 1, 2],
],
[
'machineName' => 'TestLibrary',
'majorVersion' => 1,
'minorVersion' => 2,
'patchVersion' => 3
],
false,
],
'Major version identical; Minor version identical; Patch version older' => [
[
['TestLibrary', 'Test', 1, 1, 2],
],
[
'machineName' => 'TestLibrary',
'majorVersion' => 1,
'minorVersion' => 1,
'patchVersion' => 1
],
false,
],
'Major version identical; Minor version newer; Patch version older' => [
[
['TestLibrary', 'Test', 1, 1, 2],
],
[
'machineName' => 'TestLibrary',
'majorVersion' => 1,
'minorVersion' => 2,
'patchVersion' => 1
],
false,
],
'Major version newer; Minor version identical; Patch version older' => [
[
['TestLibrary', 'Test', 1, 1, 2],
],
[
'machineName' => 'TestLibrary',
'majorVersion' => 2,
'minorVersion' => 1,
'patchVersion' => 1
],
false,
],
'Major version newer; Minor version identical; Patch version newer' => [
[
['TestLibrary', 'Test', 1, 1, 2],
],
[
'machineName' => 'TestLibrary',
'majorVersion' => 2,
'minorVersion' => 1,
'patchVersion' => 3
],
false,
],
'Major version older; Minor version identical; Patch version older' => [
[
['TestLibrary', 'Test', 1, 1, 2],
],
[
'machineName' => 'TestLibrary',
'majorVersion' => 0,
'minorVersion' => 1,
'patchVersion' => 1
],
false,
],
'Major version older; Minor version identical; Patch version newer' => [
[
['TestLibrary', 'Test', 1, 1, 2],
],
[
'machineName' => 'TestLibrary',
'majorVersion' => 0,
'minorVersion' => 1,
'patchVersion' => 3
],
false,
],
];
}
/**
* Test the behaviour of isInDevMode().
*/
public function test_isInDevMode() {
$isdevmode = $this->framework->isInDevMode();
$this->assertFalse($isdevmode);
}
/**
* Test the behaviour of mayUpdateLibraries().
*/
public function test_mayUpdateLibraries(): void {
global $DB;
$this->resetAfterTest();
// Create some users.
$contextsys = \context_system::instance();
$user = $this->getDataGenerator()->create_user();
$admin = get_admin();
$managerrole = $DB->get_record('role', ['shortname' => 'manager'], '*', MUST_EXIST);
$studentrole = $DB->get_record('role', ['shortname' => 'student'], '*', MUST_EXIST);
$manager = $this->getDataGenerator()->create_user();
role_assign($managerrole->id, $manager->id, $contextsys);
// Create a course with a label and enrol the user.
$course = $this->getDataGenerator()->create_course();
$label = $this->getDataGenerator()->create_module('label', ['course' => $course->id]);
list(, $labelcm) = get_course_and_cm_from_instance($label->id, 'label');
$contextlabel = \context_module::instance($labelcm->id);
$this->getDataGenerator()->enrol_user($user->id, $course->id, 'student');
// Create the .h5p file.
$path = __DIR__ . '/fixtures/h5ptest.zip';
// Admin and manager should have permission to update libraries.
$file = helper::create_fake_stored_file_from_path($path, $admin->id, $contextsys);
$this->framework->set_file($file);
$mayupdatelib = $this->framework->mayUpdateLibraries();
$this->assertTrue($mayupdatelib);
$file = helper::create_fake_stored_file_from_path($path, $manager->id, $contextsys);
$this->framework->set_file($file);
$mayupdatelib = $this->framework->mayUpdateLibraries();
$this->assertTrue($mayupdatelib);
// By default, normal user hasn't permission to update libraries (in both contexts, system and module label).
$file = helper::create_fake_stored_file_from_path($path, $user->id, $contextsys);
$this->framework->set_file($file);
$mayupdatelib = $this->framework->mayUpdateLibraries();
$this->assertFalse($mayupdatelib);
$file = helper::create_fake_stored_file_from_path($path, $user->id, $contextlabel);
$this->framework->set_file($file);
$mayupdatelib = $this->framework->mayUpdateLibraries();
$this->assertFalse($mayupdatelib);
// If the current user (admin) can update libraries, the method should return true (even if the file userid hasn't the
// required capabilility in the file context).
$file = helper::create_fake_stored_file_from_path($path, $admin->id, $contextlabel);
$this->framework->set_file($file);
$mayupdatelib = $this->framework->mayUpdateLibraries();
$this->assertTrue($mayupdatelib);
// If the update capability is assigned to the user, they should be able to update the libraries (only in the context
// where the capability has been assigned).
$file = helper::create_fake_stored_file_from_path($path, $user->id, $contextlabel);
$this->framework->set_file($file);
$mayupdatelib = $this->framework->mayUpdateLibraries();
$this->assertFalse($mayupdatelib);
assign_capability('moodle/h5p:updatelibraries', CAP_ALLOW, $studentrole->id, $contextlabel);
$mayupdatelib = $this->framework->mayUpdateLibraries();
$this->assertTrue($mayupdatelib);
$file = helper::create_fake_stored_file_from_path($path, $user->id, $contextsys);
$this->framework->set_file($file);
$mayupdatelib = $this->framework->mayUpdateLibraries();
$this->assertFalse($mayupdatelib);
}
/**
* Test the behaviour of get_file() and set_file().
*/
public function test_get_file(): void {
$this->resetAfterTest();
// Create some users.
$contextsys = \context_system::instance();
$user = $this->getDataGenerator()->create_user();
// The H5P file.
$path = __DIR__ . '/fixtures/h5ptest.zip';
// An error should be raised when it's called before initialitzing it.
$this->expectException('coding_exception');
$this->expectExceptionMessage('Using get_file() before file is set');
$this->framework->get_file();
// Check the value when only path and user are set.
$file = helper::create_fake_stored_file_from_path($path, $user->id);
$this->framework->set_file($file);
$file = $this->framework->get_file();
$this->assertEquals($user->id, $$file->get_userid());
$this->assertEquals($contextsys->id, $file->get_contextid());
// Check the value when also the context is set.
$course = $this->getDataGenerator()->create_course();
$contextcourse = \context_course::instance($course->id);
$file = helper::create_fake_stored_file_from_path($path, $user->id, $contextcourse);
$this->framework->set_file($file);
$file = $this->framework->get_file();
$this->assertEquals($user->id, $$file->get_userid());
$this->assertEquals($contextcourse->id, $file->get_contextid());
}
/**
* Test the behaviour of saveLibraryData() when saving data for a new library.
*/
public function test_saveLibraryData_new_library() {
global $DB;
$this->resetAfterTest();
$librarydata = array(
'title' => 'Test',
'machineName' => 'TestLibrary',
'majorVersion' => '1',
'minorVersion' => '0',
'patchVersion' => '2',
'runnable' => 1,
'fullscreen' => 1,
'preloadedJs' => array(
array(
'path' => 'js/name.min.js'
)
),
'preloadedCss' => array(
array(
'path' => 'css/name.css'
)
),
'dropLibraryCss' => array(
array(
'machineName' => 'Name2'
)
)
);
// Create a new library.
$this->framework->saveLibraryData($librarydata);
$library = $DB->get_record('h5p_libraries', ['machinename' => $librarydata['machineName']]);
// Make sure the library data was properly saved.
$this->assertNotEmpty($library);
$this->assertNotEmpty($librarydata['libraryId']);
$this->assertEquals($librarydata['title'], $library->title);
$this->assertEquals($librarydata['machineName'], $library->machinename);
$this->assertEquals($librarydata['majorVersion'], $library->majorversion);
$this->assertEquals($librarydata['minorVersion'], $library->minorversion);
$this->assertEquals($librarydata['patchVersion'], $library->patchversion);
$this->assertEquals($librarydata['preloadedJs'][0]['path'], $library->preloadedjs);
$this->assertEquals($librarydata['preloadedCss'][0]['path'], $library->preloadedcss);
$this->assertEquals($librarydata['dropLibraryCss'][0]['machineName'], $library->droplibrarycss);
}
/**
* Test the behaviour of saveLibraryData() when saving (updating) data for an existing library.
*/
public function test_saveLibraryData_existing_library() {
global $DB;
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
// Create a library record.
$library = $generator->create_library_record('TestLibrary', 'Test', 1, 0, 2);
$librarydata = array(
'libraryId' => $library->id,
'title' => 'Test1',
'machineName' => 'TestLibrary',
'majorVersion' => '1',
'minorVersion' => '2',
'patchVersion' => '2',
'runnable' => 1,
'fullscreen' => 1,
'preloadedJs' => array(
array(
'path' => 'js/name.min.js'
)
),
'preloadedCss' => array(
array(
'path' => 'css/name.css'
)
),
'dropLibraryCss' => array(
array(
'machineName' => 'Name2'
)
)
);
// Update the library.
$this->framework->saveLibraryData($librarydata, false);
$library = $DB->get_record('h5p_libraries', ['machinename' => $librarydata['machineName']]);
// Make sure the library data was properly updated.
$this->assertNotEmpty($library);
$this->assertNotEmpty($librarydata['libraryId']);
$this->assertEquals($librarydata['title'], $library->title);
$this->assertEquals($librarydata['machineName'], $library->machinename);
$this->assertEquals($librarydata['majorVersion'], $library->majorversion);
$this->assertEquals($librarydata['minorVersion'], $library->minorversion);
$this->assertEquals($librarydata['patchVersion'], $library->patchversion);
$this->assertEquals($librarydata['preloadedJs'][0]['path'], $library->preloadedjs);
$this->assertEquals($librarydata['preloadedCss'][0]['path'], $library->preloadedcss);
$this->assertEquals($librarydata['dropLibraryCss'][0]['machineName'], $library->droplibrarycss);
}
/**