Merge branch 'MDL-67174' of https://github.com/paulholden/moodle
[moodle.git] / h5p / tests / helper_test.php
blob932375720afe5af22e788d8fdcf3d30d5c652bd4
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.
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/>.
17 /**
18 * Testing the H5P helper.
20 * @package core_h5p
21 * @category test
22 * @copyright 2019 Sara Arjona <sara@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 declare(strict_types = 1);
28 namespace core_h5p;
30 use advanced_testcase;
32 /**
33 * Test class covering the H5P helper.
35 * @package core_h5p
36 * @copyright 2019 Sara Arjona <sara@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class helper_testcase extends \advanced_testcase {
41 /**
42 * Test the behaviour of get_display_options().
44 * @dataProvider get_display_options_provider
45 * @param bool $frame Whether the frame should be displayed or not
46 * @param bool $export Whether the export action button should be displayed or not
47 * @param bool $embed Whether the embed action button should be displayed or not
48 * @param bool $copyright Whether the copyright action button should be displayed or not
49 * @param int $expected The expectation with the displayoptions value
51 public function test_get_display_options(bool $frame, bool $export, bool $embed, bool $copyright, int $expected): void {
52 $this->setRunTestInSeparateProcess(true);
53 $this->resetAfterTest();
55 $factory = new \core_h5p\factory();
56 $core = $factory->get_core();
57 $config = (object)[
58 'frame' => $frame,
59 'export' => $export,
60 'embed' => $embed,
61 'copyright' => $copyright,
63 $displayoptions = helper::get_display_options($core, $config);
65 $this->assertEquals($expected, $displayoptions);
68 /**
69 * Data provider for test_get_display_options().
71 * @return array
73 public function get_display_options_provider(): array {
74 return [
75 'All display options disabled' => [
76 false,
77 false,
78 false,
79 false,
80 15,
82 'All display options enabled' => [
83 true,
84 true,
85 true,
86 true,
89 'Frame disabled and the rest enabled' => [
90 false,
91 true,
92 true,
93 true,
96 'Only export enabled' => [
97 false,
98 true,
99 false,
100 false,
103 'Only embed enabled' => [
104 false,
105 false,
106 true,
107 false,
110 'Only copyright enabled' => [
111 false,
112 false,
113 false,
114 true,
121 * Test the behaviour of save_h5p() when there are some missing libraries in the system.
122 * @runInSeparateProcess
124 public function test_save_h5p_missing_libraries(): void {
125 $this->resetAfterTest();
126 $factory = new \core_h5p\factory();
128 // Create a user.
129 $user = $this->getDataGenerator()->create_user();
130 $this->setUser($user);
132 // This is a valid .H5P file.
133 $path = __DIR__ . '/fixtures/greeting-card-887.h5p';
134 $file = helper::create_fake_stored_file_from_path($path, (int)$user->id);
135 $factory->get_framework()->set_file($file);
137 $config = (object)[
138 'frame' => 1,
139 'export' => 1,
140 'embed' => 0,
141 'copyright' => 0,
144 // There are some missing libraries in the system, so an error should be returned.
145 $h5pid = helper::save_h5p($factory, $file, $config);
146 $this->assertFalse($h5pid);
147 $errors = $factory->get_framework()->getMessages('error');
148 $this->assertCount(1, $errors);
149 $error = reset($errors);
150 $this->assertEquals('missing-required-library', $error->code);
151 $this->assertEquals('Missing required library H5P.GreetingCard 1.0', $error->message);
155 * Test the behaviour of save_h5p() when the libraries exist in the system.
156 * @runInSeparateProcess
158 public function test_save_h5p_existing_libraries(): void {
159 global $DB;
161 $this->resetAfterTest();
162 $factory = new \core_h5p\factory();
164 // Create a user.
165 $user = $this->getDataGenerator()->create_user();
166 $this->setUser($user);
168 // This is a valid .H5P file.
169 $path = __DIR__ . '/fixtures/greeting-card-887.h5p';
170 $file = helper::create_fake_stored_file_from_path($path, (int)$user->id);
171 $factory->get_framework()->set_file($file);
173 $config = (object)[
174 'frame' => 1,
175 'export' => 1,
176 'embed' => 0,
177 'copyright' => 0,
179 // The required libraries exist in the system before saving the .h5p file.
180 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
181 $lib = $generator->create_library_record('H5P.GreetingCard', 'GreetingCard', 1, 0);
182 $h5pid = helper::save_h5p($factory, $file, $config);
183 $this->assertNotEmpty($h5pid);
185 // No errors are raised.
186 $errors = $factory->get_framework()->getMessages('error');
187 $this->assertCount(0, $errors);
189 // And the content in the .h5p file has been saved as expected.
190 $h5p = $DB->get_record('h5p', ['id' => $h5pid]);
191 $this->assertEquals($lib->id, $h5p->mainlibraryid);
192 $this->assertEquals(helper::get_display_options($factory->get_core(), $config), $h5p->displayoptions);
193 $this->assertContains('Hello world!', $h5p->jsoncontent);
197 * Test the behaviour of save_h5p() when the .h5p file is invalid.
198 * @runInSeparateProcess
200 public function test_save_h5p_invalid_file(): void {
201 $this->resetAfterTest();
202 $factory = new \core_h5p\factory();
204 // Create a user.
205 $user = $this->getDataGenerator()->create_user();
206 $this->setUser($user);
208 // Prepare an invalid .H5P file.
209 $path = __DIR__ . '/fixtures/h5ptest.zip';
210 $file = helper::create_fake_stored_file_from_path($path, (int)$user->id);
211 $factory->get_framework()->set_file($file);
212 $config = (object)[
213 'frame' => 1,
214 'export' => 1,
215 'embed' => 0,
216 'copyright' => 0,
219 // When saving an invalid .h5p file, an error should be raised.
220 $h5pid = helper::save_h5p($factory, $file, $config);
221 $this->assertFalse($h5pid);
222 $errors = $factory->get_framework()->getMessages('error');
223 $this->assertCount(2, $errors);
225 $expectederrorcodes = ['invalid-content-folder', 'invalid-h5p-json-file'];
226 foreach ($errors as $error) {
227 $this->assertContains($error->code, $expectederrorcodes);
232 * Test the behaviour of can_deploy_package().
234 public function test_can_deploy_package(): void {
235 $this->resetAfterTest();
236 $factory = new \core_h5p\factory();
238 // Create a user.
239 $user = $this->getDataGenerator()->create_user();
240 $admin = get_admin();
242 // Prepare a valid .H5P file.
243 $path = __DIR__ . '/fixtures/greeting-card-887.h5p';
245 // Files created by users can't be deployed.
246 $file = helper::create_fake_stored_file_from_path($path, (int)$user->id);
247 $factory->get_framework()->set_file($file);
248 $candeploy = helper::can_deploy_package($file);
249 $this->assertFalse($candeploy);
251 // Files created by admins can be deployed, even when the current user is not the admin.
252 $this->setUser($user);
253 $file = helper::create_fake_stored_file_from_path($path, (int)$admin->id);
254 $factory->get_framework()->set_file($file);
255 $candeploy = helper::can_deploy_package($file);
256 $this->assertTrue($candeploy);
260 * Test the behaviour of can_update_library().
262 public function can_update_library(): void {
263 $this->resetAfterTest();
264 $factory = new \core_h5p\factory();
266 // Create a user.
267 $user = $this->getDataGenerator()->create_user();
268 $admin = get_admin();
270 // Prepare a valid .H5P file.
271 $path = __DIR__ . '/fixtures/greeting-card-887.h5p';
273 // Libraries can't be updated when the file has been created by users.
274 $file = helper::create_fake_stored_file_from_path($path, (int)$user->id);
275 $factory->get_framework()->set_file($file);
276 $candeploy = helper::can_update_library($file);
277 $this->assertFalse($candeploy);
279 // Libraries can be updated when the file has been created by admin, even when the current user is not the admin.
280 $this->setUser($user);
281 $file = helper::create_fake_stored_file_from_path($path, (int)$admin->id);
282 $factory->get_framework()->set_file($file);
283 $candeploy = helper::can_update_library($file);
284 $this->assertTrue($candeploy);