2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Testing the Moodle local class for managing the H5P Editor.
22 * @copyright 2020 Victor Deniz <victor@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 use advanced_testcase;
31 use core_h5p\local\library\autoloader;
33 use page_requirements_manager;
37 * Test class covering the editor class.
40 * @copyright 2020 Victor Deniz <victor@moodle.com>
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 * @runTestsInSeparateProcesses
45 class editor_testcase extends advanced_testcase {
48 * Form object to be used in test case.
50 protected function get_test_form() {
53 require_once($CFG->libdir . '/formslib.php');
55 return new class extends \moodleform {
59 public function definition(): void {
60 // No definition required.
64 * Returns form reference.
66 * @return MoodleQuickForm
68 public function getform() {
69 $mform = $this->_form;
76 * Test that existing content is properly set.
78 public function test_set_content() {
79 $this->resetAfterTest();
81 autoloader::register();
84 // This is a valid .H5P file.
85 $filename = 'find-the-words.h5p';
86 $path = __DIR__ . '/fixtures/' . $filename;
87 $syscontext = \context_system::instance();
89 'contextid' => $syscontext->id,
90 'component' => \core_h5p\file_storage::COMPONENT,
91 'filearea' => 'unittest',
94 'filename' => $filename,
96 // Load the h5p file into DB.
97 $fs = get_file_storage();
98 $file = $fs->create_file_from_pathname($filerecord, $path);
99 // Make the URL to pass to the WS.
100 $url = \moodle_url::make_pluginfile_url(
102 \core_h5p\file_storage::COMPONENT,
108 $config = new \stdClass();
110 $h5pplayer = new player($url->out(), $config);
112 // Call the method. We need the id of the new H5P content.
113 $rc = new \ReflectionClass(player::class);
114 $rcp = $rc->getProperty('h5pid');
115 $rcp->setAccessible(true);
116 $h5pid = $rcp->getValue($h5pplayer);
118 $editor = new editor();
119 $editor->set_content($h5pid);
121 // Check we get the H5P content.
122 $rc = new \ReflectionClass(editor::class);
123 $rcp = $rc->getProperty('oldcontent');
124 $rcp->setAccessible(true);
125 $oldcontent = $rcp->getValue($editor);
127 $core = (new factory)->get_core();
128 $this->assertSame($core->loadContent($h5pid), $oldcontent);
130 // Check we get the file of the H5P content.
131 $rcp = $rc->getProperty('oldfile');
132 $rcp->setAccessible(true);
133 $oldfile = $rcp->getValue($editor);
135 $this->assertSame($file->get_contenthash(), $oldfile->get_contenthash());
139 * Tests that library and file area are properly set.
141 public function test_set_library() {
144 $library = 'H5P.Accordion 1.5';
146 $filearea = 'unittest';
147 $filename = 'export.h5p';
150 $editor = new editor();
151 $editor->set_library($library, $contextid, file_storage::COMPONENT, $filearea, 0, '/', $filename);
153 // Check that the library has the right value.
154 $rc = new \ReflectionClass(editor::class);
155 $rcp = $rc->getProperty('library');
156 $rcp->setAccessible(true);
157 $actual = $rcp->getValue($editor);
159 $this->assertSame($library, $actual);
161 // Check that the file area has the right value.
163 'contextid' => $contextid,
164 'component' => file_storage::COMPONENT,
165 'filearea' => $filearea,
168 'filename' => $filename,
169 'userid' => $USER->id
172 $rcp = $rc->getProperty('filearea');
173 $rcp->setAccessible(true);
174 $actual = $rcp->getValue($editor);
176 $this->assertEquals($expected, $actual);
180 * Test that required assets (js and css) and form will be loaded in page.
182 public function test_add_editor_to_form() {
186 $form = $this->get_test_form();
187 $mform = $form->getform();
190 $editor = new editor();
191 $editor->add_editor_to_form($mform);
193 // Check $PAGE has the expected css and js scripts.
194 $rc = new \ReflectionClass(page_requirements_manager::class);
195 $rcp = $rc->getProperty('cssurls');
196 $rcp2 = $rc->getProperty('jsincludes');
197 $rcp->setAccessible(true);
198 $rcp2->setAccessible(true);
199 $actualcss = array_keys($rcp->getValue($PAGE->requires));
200 $actualjs = array_keys($rcp2->getValue($PAGE->requires)['head']);
201 $cachebuster = helper::get_cache_buster();
203 $h5pcorepath = autoloader::get_h5p_core_library_url()->out();
205 $expectedcss = \H5PCore::$styles;
206 $expectedjs = \H5PCore::$scripts;
208 array_walk($expectedcss, function(&$item, $key) use ($h5pcorepath, $cachebuster) {
209 $item = $h5pcorepath . $item. $cachebuster;
213 array_walk($expectedjs, function(&$item, $key) use ($h5pcorepath, $cachebuster) {
214 $item = $h5pcorepath . $item . $cachebuster;
217 // Add translation script.
218 $language = framework::get_language();
219 $languagescript = "language/{$language}.js";
221 if (!file_exists($CFG->dirroot . autoloader::get_h5p_editor_library_base($languagescript))) {
222 $languagescript = 'language/en.js';
224 $expectedjs[] = autoloader::get_h5p_editor_library_url($languagescript . $cachebuster)->out();
226 $expectedjs[] = (new \moodle_url('/h5p/js/h5p_overrides.js' . $cachebuster))->out();
227 $expectedjs[] = autoloader::get_h5p_editor_library_url('scripts/h5peditor-editor.js' . $cachebuster)->out();
228 $expectedjs[] = autoloader::get_h5p_editor_library_url('scripts/h5peditor-init.js' . $cachebuster)->out();
230 // Sort arrays before comparison.
236 $this->assertSame($expectedcss, $actualcss);
237 $this->assertSame($expectedjs, $actualjs);
239 // H5P Editor expected form fields.
240 $this->assertTrue($mform->elementExists('h5pparams'));
241 $this->assertTrue($mform->elementExists('h5plibrary'));
242 $this->assertTrue($mform->elementExists('h5paction'));
246 * Test new content creation.
248 public function test_save_content() {
251 $this->resetAfterTest();
253 // Fake form data sent during creation.
254 $data = new \stdClass();
255 $data->h5plibrary = "H5P.ArithmeticQuiz 1.1";
256 $data->h5pparams = '{"params":{"quizType":"arithmetic","arithmeticType":"addition","UI":{"score":"Score:","time":"Time: @time"},
257 "intro":"This is a content for testing"},"metadata":{"defaultLanguage":"en","title":"Testing content"}}';
260 $library = 'H5P.ArithmeticQuiz 1.1';
261 $machinename = 'H5P.ArithmeticQuiz';
263 $filearea = 'unittest';
264 $filename = 'export.h5p';
266 // Fake installed library for the H5P content.
267 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
269 $semantics = json_encode([['type' => 'text', 'name' => 'text', 'label' => 'Plain text', 'description' => 'Some text']]);
270 $generator->create_library_record($machinename, $title, 1, 1, 2, $semantics);
272 $editor = new editor();
273 $editor->set_library($library, $contextid, file_storage::COMPONENT, $filearea, 0, '/', $filename);
274 $newcontentid = $editor->save_content($data);
276 // Check the H5P content file was created where expected.
277 $fs = get_file_storage();
278 $out = $fs->get_file($contextid, file_storage::COMPONENT, $filearea, 0, '/', $filename);
279 $this->assertNotEmpty($out);