1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace contenttype_h5p;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Test for H5P content bank plugin.
|
|
|
21 |
*
|
|
|
22 |
* @package contenttype_h5p
|
|
|
23 |
* @category test
|
|
|
24 |
* @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
* @coversDefaultClass \contenttype_h5p\content
|
|
|
27 |
*/
|
1441 |
ariadna |
28 |
final class content_h5p_test extends \advanced_testcase {
|
1 |
efrain |
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Tests for uploaded file.
|
|
|
32 |
*
|
|
|
33 |
* @covers ::get_file
|
|
|
34 |
*/
|
11 |
efrain |
35 |
public function test_upload_file(): void {
|
1 |
efrain |
36 |
$this->resetAfterTest();
|
|
|
37 |
|
|
|
38 |
// Create content.
|
|
|
39 |
$record = new \stdClass();
|
|
|
40 |
$record->name = 'Test content';
|
|
|
41 |
$record->configdata = '';
|
|
|
42 |
$contenttype = new \contenttype_h5p\contenttype(\context_system::instance());
|
|
|
43 |
$content = $contenttype->create_content($record);
|
|
|
44 |
|
|
|
45 |
// Create a dummy file.
|
|
|
46 |
$filename = 'content.h5p';
|
|
|
47 |
$dummy = [
|
|
|
48 |
'contextid' => \context_system::instance()->id,
|
|
|
49 |
'component' => 'contentbank',
|
|
|
50 |
'filearea' => 'public',
|
|
|
51 |
'itemid' => $content->get_id(),
|
|
|
52 |
'filepath' => '/',
|
|
|
53 |
'filename' => $filename
|
|
|
54 |
];
|
|
|
55 |
$fs = get_file_storage();
|
|
|
56 |
$fs->create_file_from_string($dummy, 'dummy content');
|
|
|
57 |
|
|
|
58 |
$file = $content->get_file();
|
|
|
59 |
$this->assertInstanceOf(\stored_file::class, $file);
|
|
|
60 |
$this->assertEquals($filename, $file->get_filename());
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Tests for is view allowed content.
|
|
|
65 |
*
|
|
|
66 |
* @covers ::is_view_allowed
|
|
|
67 |
* @dataProvider is_view_allowed_provider
|
|
|
68 |
*
|
|
|
69 |
* @param string $role User role to use for create and view contents.
|
|
|
70 |
* @param array $disabledlibraries Library names to disable.
|
|
|
71 |
* @param array $expected Array with the expected values for the contents in the following order:
|
|
|
72 |
* ['H5P.Blanks deployed', 'H5P.Accordion deployed', 'H5P.Accordion undeployed', 'Invalid content'].
|
|
|
73 |
*/
|
|
|
74 |
public function test_is_view_allowed(string $role, array $disabledlibraries, array $expected): void {
|
|
|
75 |
global $CFG, $USER, $DB;
|
|
|
76 |
|
|
|
77 |
$this->resetAfterTest();
|
|
|
78 |
|
|
|
79 |
// Create a course.
|
|
|
80 |
$course = $this->getDataGenerator()->create_course();
|
|
|
81 |
$coursecontext = \context_course::instance($course->id);
|
|
|
82 |
|
|
|
83 |
// Set user.
|
|
|
84 |
if ($role == 'admin') {
|
|
|
85 |
$this->setAdminUser();
|
|
|
86 |
} else {
|
|
|
87 |
// Enrol user to the course.
|
|
|
88 |
$user = $this->getDataGenerator()->create_and_enrol($course, $role);
|
|
|
89 |
$this->setUser($user);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
// Add contents to the content bank.
|
|
|
93 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
|
|
|
94 |
$filepath = $CFG->dirroot . '/h5p/tests/fixtures/filltheblanks.h5p';
|
|
|
95 |
$contents = $generator->generate_contentbank_data('contenttype_h5p', 1, $USER->id, $coursecontext, true, $filepath);
|
|
|
96 |
$filltheblanks = array_shift($contents);
|
|
|
97 |
$filepath = $CFG->dirroot . '/h5p/tests/fixtures/ipsums.h5p';
|
|
|
98 |
$contents = $generator->generate_contentbank_data('contenttype_h5p', 2, $USER->id, $coursecontext, true, $filepath);
|
|
|
99 |
$accordion1 = array_shift($contents);
|
|
|
100 |
$accordion2 = array_shift($contents);
|
|
|
101 |
$filepath = $CFG->dirroot . '/h5p/tests/fixtures/invalid.zip';
|
|
|
102 |
$contents = $generator->generate_contentbank_data('contenttype_h5p', 1, $USER->id, $coursecontext, true, $filepath);
|
|
|
103 |
$invalid = array_shift($contents);
|
|
|
104 |
|
|
|
105 |
// Load some of these H5P files though the player to create the H5P DB entries.
|
|
|
106 |
$h5pplayer = new \core_h5p\player($filltheblanks->get_file_url(), new \stdClass(), true);
|
|
|
107 |
$h5pplayer = new \core_h5p\player($accordion1->get_file_url(), new \stdClass(), true);
|
|
|
108 |
|
|
|
109 |
// Check the expected H5P content has been created.
|
|
|
110 |
$this->assertEquals(2, $DB->count_records('h5p'));
|
|
|
111 |
$this->assertEquals(4, $DB->count_records('contentbank_content'));
|
|
|
112 |
|
|
|
113 |
// Disable libraries.
|
|
|
114 |
foreach ($disabledlibraries as $libraryname) {
|
|
|
115 |
$libraryid = $DB->get_field('h5p_libraries', 'id', ['machinename' => $libraryname]);
|
|
|
116 |
\core_h5p\api::set_library_enabled((int) $libraryid, false);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
$this->assertEquals($expected[0], $filltheblanks->is_view_allowed());
|
|
|
120 |
$this->assertEquals($expected[1], $accordion1->is_view_allowed());
|
|
|
121 |
$this->assertEquals($expected[2], $accordion2->is_view_allowed());
|
|
|
122 |
$this->assertEquals($expected[3], $invalid->is_view_allowed());
|
|
|
123 |
|
|
|
124 |
// Check that after enabling libraries again, all the content return true (but the invalid package).
|
|
|
125 |
foreach ($disabledlibraries as $libraryname) {
|
|
|
126 |
$libraryid = $DB->get_field('h5p_libraries', 'id', ['machinename' => $libraryname]);
|
|
|
127 |
\core_h5p\api::set_library_enabled((int) $libraryid, true);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
$this->assertEquals(true, $filltheblanks->is_view_allowed());
|
|
|
131 |
$this->assertEquals(true, $accordion1->is_view_allowed());
|
|
|
132 |
$this->assertEquals(true, $accordion2->is_view_allowed()); // It will be deployed, so now it will always return true.
|
|
|
133 |
$this->assertEquals($expected[3], $invalid->is_view_allowed());
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Data provider for test_is_view_allowed.
|
|
|
138 |
*
|
|
|
139 |
* @return array
|
|
|
140 |
*/
|
1441 |
ariadna |
141 |
public static function is_view_allowed_provider(): array {
|
1 |
efrain |
142 |
return [
|
|
|
143 |
'Editing teacher with all libraries enabled' => [
|
|
|
144 |
'role' => 'editingteacher',
|
|
|
145 |
'disabledlibraries' => [],
|
|
|
146 |
'expected' => [true, true, true, false],
|
|
|
147 |
],
|
|
|
148 |
'Manager with all libraries enabled' => [
|
|
|
149 |
'role' => 'manager',
|
|
|
150 |
'disabledlibraries' => [],
|
|
|
151 |
'expected' => [true, true, true, true],
|
|
|
152 |
],
|
|
|
153 |
'Admin with all libraries enabled' => [
|
|
|
154 |
'role' => 'admin',
|
|
|
155 |
'disabledlibraries' => [],
|
|
|
156 |
'expected' => [true, true, true, true],
|
|
|
157 |
],
|
|
|
158 |
'Editing teacher with H5P.Accordion disabled' => [
|
|
|
159 |
'role' => 'editingteacher',
|
|
|
160 |
'disabledlibraries' => ['H5P.Accordion'],
|
|
|
161 |
'expected' => [true, false, false, false],
|
|
|
162 |
],
|
|
|
163 |
'Manager with H5P.Accordion disabled' => [
|
|
|
164 |
'role' => 'manager',
|
|
|
165 |
'disabledlibraries' => ['H5P.Accordion'],
|
|
|
166 |
'expected' => [true, false, true, true],
|
|
|
167 |
],
|
|
|
168 |
'Admin with H5P.Accordion disabled' => [
|
|
|
169 |
'role' => 'admin',
|
|
|
170 |
'disabledlibraries' => ['H5P.Accordion'],
|
|
|
171 |
'expected' => [true, false, true, true],
|
|
|
172 |
],
|
|
|
173 |
'Editing teacher with all libraries disabled' => [
|
|
|
174 |
'role' => 'editingteacher',
|
|
|
175 |
'disabledlibraries' => ['H5P.Accordion', 'H5P.Blanks'],
|
|
|
176 |
'expected' => [false, false, false, false],
|
|
|
177 |
],
|
|
|
178 |
'Manager with all libraries disabled' => [
|
|
|
179 |
'role' => 'manager',
|
|
|
180 |
'disabledlibraries' => ['H5P.Accordion', 'H5P.Blanks'],
|
|
|
181 |
'expected' => [false, false, true, true],
|
|
|
182 |
],
|
|
|
183 |
'Admin with all libraries disabled' => [
|
|
|
184 |
'role' => 'admin',
|
|
|
185 |
'disabledlibraries' => ['H5P.Accordion', 'H5P.Blanks'],
|
|
|
186 |
'expected' => [false, false, true, true],
|
|
|
187 |
],
|
|
|
188 |
];
|
|
|
189 |
}
|
|
|
190 |
}
|