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 |
/**
|
|
|
18 |
* Testing the Moodle local class for managing the H5P Editor.
|
|
|
19 |
*
|
|
|
20 |
* @package core_h5p
|
|
|
21 |
* @category test
|
|
|
22 |
* @copyright 2020 Victor Deniz <victor@moodle.com>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
namespace core_h5p;
|
|
|
27 |
|
|
|
28 |
defined('MOODLE_INTERNAL') || die();
|
|
|
29 |
|
|
|
30 |
use advanced_testcase;
|
|
|
31 |
use core_h5p\local\library\autoloader;
|
|
|
32 |
use MoodleQuickForm;
|
|
|
33 |
use page_requirements_manager;
|
|
|
34 |
use Moodle\H5PCore;
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
*
|
|
|
38 |
* Test class covering the editor class.
|
|
|
39 |
*
|
|
|
40 |
* @package core_h5p
|
|
|
41 |
* @copyright 2020 Victor Deniz <victor@moodle.com>
|
|
|
42 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
43 |
*
|
|
|
44 |
* @runTestsInSeparateProcesses
|
|
|
45 |
*/
|
|
|
46 |
class editor_test extends advanced_testcase {
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Form object to be used in test case.
|
|
|
50 |
*/
|
|
|
51 |
protected function get_test_form() {
|
|
|
52 |
global $CFG;
|
|
|
53 |
|
|
|
54 |
require_once($CFG->libdir . '/formslib.php');
|
|
|
55 |
|
|
|
56 |
return new class extends \moodleform {
|
|
|
57 |
/**
|
|
|
58 |
* Form definition.
|
|
|
59 |
*/
|
|
|
60 |
public function definition(): void {
|
|
|
61 |
// No definition required.
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Returns form reference.
|
|
|
66 |
*
|
|
|
67 |
* @return MoodleQuickForm
|
|
|
68 |
*/
|
|
|
69 |
public function getform() {
|
|
|
70 |
$mform = $this->_form;
|
|
|
71 |
return $mform;
|
|
|
72 |
}
|
|
|
73 |
};
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Test that existing content is properly set.
|
|
|
78 |
*/
|
11 |
efrain |
79 |
public function test_set_content(): void {
|
1 |
efrain |
80 |
$this->resetAfterTest();
|
|
|
81 |
|
|
|
82 |
autoloader::register();
|
|
|
83 |
|
|
|
84 |
// Add H5P content.
|
|
|
85 |
// This is a valid .H5P file.
|
|
|
86 |
$filename = 'find-the-words.h5p';
|
|
|
87 |
$path = __DIR__ . '/fixtures/' . $filename;
|
|
|
88 |
$syscontext = \context_system::instance();
|
|
|
89 |
$filerecord = [
|
|
|
90 |
'contextid' => $syscontext->id,
|
|
|
91 |
'component' => \core_h5p\file_storage::COMPONENT,
|
|
|
92 |
'filearea' => 'unittest',
|
|
|
93 |
'itemid' => 0,
|
|
|
94 |
'filepath' => '/',
|
|
|
95 |
'filename' => $filename,
|
|
|
96 |
];
|
|
|
97 |
// Load the h5p file into DB.
|
|
|
98 |
$fs = get_file_storage();
|
|
|
99 |
$file = $fs->create_file_from_pathname($filerecord, $path);
|
|
|
100 |
// Make the URL to pass to the WS.
|
|
|
101 |
$url = \moodle_url::make_pluginfile_url(
|
|
|
102 |
$syscontext->id,
|
|
|
103 |
\core_h5p\file_storage::COMPONENT,
|
|
|
104 |
'unittest',
|
|
|
105 |
0,
|
|
|
106 |
'/',
|
|
|
107 |
$filename
|
|
|
108 |
);
|
|
|
109 |
$config = new \stdClass();
|
|
|
110 |
|
|
|
111 |
$h5pplayer = new player($url->out(), $config);
|
|
|
112 |
|
|
|
113 |
// Call the method. We need the id of the new H5P content.
|
|
|
114 |
$rc = new \ReflectionClass(player::class);
|
|
|
115 |
$rcp = $rc->getProperty('h5pid');
|
|
|
116 |
$h5pid = $rcp->getValue($h5pplayer);
|
|
|
117 |
|
|
|
118 |
$editor = new editor();
|
|
|
119 |
$editor->set_content($h5pid);
|
|
|
120 |
|
|
|
121 |
// Check we get the H5P content.
|
|
|
122 |
$rc = new \ReflectionClass(editor::class);
|
|
|
123 |
$rcp = $rc->getProperty('oldcontent');
|
|
|
124 |
$oldcontent = $rcp->getValue($editor);
|
|
|
125 |
|
|
|
126 |
$core = (new factory)->get_core();
|
|
|
127 |
$this->assertSame($core->loadContent($h5pid), $oldcontent);
|
|
|
128 |
|
|
|
129 |
// Check we get the file of the H5P content.
|
|
|
130 |
$rcp = $rc->getProperty('oldfile');
|
|
|
131 |
$oldfile = $rcp->getValue($editor);
|
|
|
132 |
|
|
|
133 |
$this->assertSame($file->get_contenthash(), $oldfile->get_contenthash());
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Tests that library and file area are properly set.
|
|
|
138 |
*/
|
11 |
efrain |
139 |
public function test_set_library(): void {
|
1 |
efrain |
140 |
global $USER;
|
|
|
141 |
|
|
|
142 |
$library = 'H5P.Accordion 1.5';
|
|
|
143 |
$contextid = 1;
|
|
|
144 |
$filearea = 'unittest';
|
|
|
145 |
$filename = 'export.h5p';
|
|
|
146 |
|
|
|
147 |
// Call method.
|
|
|
148 |
$editor = new editor();
|
|
|
149 |
$editor->set_library($library, $contextid, file_storage::COMPONENT, $filearea, 0, '/', $filename);
|
|
|
150 |
|
|
|
151 |
// Check that the library has the right value.
|
|
|
152 |
$rc = new \ReflectionClass(editor::class);
|
|
|
153 |
$rcp = $rc->getProperty('library');
|
|
|
154 |
$actual = $rcp->getValue($editor);
|
|
|
155 |
|
|
|
156 |
$this->assertSame($library, $actual);
|
|
|
157 |
|
|
|
158 |
// Check that the file area has the right value.
|
|
|
159 |
$expected = [
|
|
|
160 |
'contextid' => $contextid,
|
|
|
161 |
'component' => file_storage::COMPONENT,
|
|
|
162 |
'filearea' => $filearea,
|
|
|
163 |
'itemid' => 0,
|
|
|
164 |
'filepath' => '/',
|
|
|
165 |
'filename' => $filename,
|
|
|
166 |
'userid' => $USER->id
|
|
|
167 |
];
|
|
|
168 |
|
|
|
169 |
$rcp = $rc->getProperty('filearea');
|
|
|
170 |
$actual = $rcp->getValue($editor);
|
|
|
171 |
|
|
|
172 |
$this->assertEquals($expected, $actual);
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
/**
|
|
|
176 |
* Test that required assets (js and css) and form will be loaded in page.
|
|
|
177 |
*/
|
11 |
efrain |
178 |
public function test_add_editor_to_form(): void {
|
1 |
efrain |
179 |
global $PAGE, $CFG;
|
|
|
180 |
|
|
|
181 |
$this->resetAfterTest();
|
|
|
182 |
$this->setAdminUser();
|
|
|
183 |
|
|
|
184 |
// Get form data.
|
|
|
185 |
$form = $this->get_test_form();
|
|
|
186 |
$mform = $form->getform();
|
|
|
187 |
|
|
|
188 |
// Call method.
|
|
|
189 |
$editor = new editor();
|
|
|
190 |
$editor->add_editor_to_form($mform);
|
|
|
191 |
|
|
|
192 |
// Check $PAGE has the expected css and js scripts.
|
|
|
193 |
$rc = new \ReflectionClass(page_requirements_manager::class);
|
|
|
194 |
$rcp = $rc->getProperty('cssurls');
|
|
|
195 |
$rcp2 = $rc->getProperty('jsincludes');
|
|
|
196 |
$actualcss = array_keys($rcp->getValue($PAGE->requires));
|
|
|
197 |
$actualjs = array_keys($rcp2->getValue($PAGE->requires)['head']);
|
|
|
198 |
$cachebuster = helper::get_cache_buster();
|
|
|
199 |
|
|
|
200 |
$h5pcorepath = autoloader::get_h5p_core_library_url()->out();
|
|
|
201 |
|
|
|
202 |
$expectedcss = H5PCore::$styles;
|
|
|
203 |
$expectedjs = H5PCore::$scripts;
|
|
|
204 |
|
|
|
205 |
array_walk($expectedcss, function(&$item, $key) use ($h5pcorepath, $cachebuster) {
|
|
|
206 |
$item = $h5pcorepath . $item. $cachebuster;
|
|
|
207 |
|
|
|
208 |
});
|
|
|
209 |
|
|
|
210 |
array_walk($expectedjs, function(&$item, $key) use ($h5pcorepath, $cachebuster) {
|
|
|
211 |
$item = $h5pcorepath . $item . $cachebuster;
|
|
|
212 |
});
|
|
|
213 |
|
|
|
214 |
$expectedjs[] = (new \moodle_url('/h5p/js/h5p_overrides.js' . $cachebuster))->out();
|
|
|
215 |
$expectedjs[] = autoloader::get_h5p_editor_library_url('scripts/h5peditor-editor.js' . $cachebuster)->out();
|
|
|
216 |
$expectedjs[] = autoloader::get_h5p_editor_library_url('scripts/h5peditor-init.js' . $cachebuster)->out();
|
|
|
217 |
|
|
|
218 |
// Sort arrays before comparison.
|
|
|
219 |
sort($actualcss);
|
|
|
220 |
sort($actualjs);
|
|
|
221 |
sort($expectedcss);
|
|
|
222 |
sort($expectedjs);
|
|
|
223 |
|
|
|
224 |
$this->assertSame($expectedcss, $actualcss);
|
|
|
225 |
$this->assertSame($expectedjs, $actualjs);
|
|
|
226 |
|
|
|
227 |
// H5P Editor expected form fields.
|
|
|
228 |
$this->assertTrue($mform->elementExists('h5pparams'));
|
|
|
229 |
$this->assertTrue($mform->elementExists('h5plibrary'));
|
|
|
230 |
$this->assertTrue($mform->elementExists('h5paction'));
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
/**
|
|
|
234 |
* Test new content creation.
|
|
|
235 |
*/
|
11 |
efrain |
236 |
public function test_save_content(): void {
|
1 |
efrain |
237 |
global $DB;
|
|
|
238 |
|
|
|
239 |
$this->resetAfterTest();
|
|
|
240 |
|
|
|
241 |
// Fake form data sent during creation.
|
|
|
242 |
$data = new \stdClass();
|
|
|
243 |
$data->h5plibrary = "H5P.ArithmeticQuiz 1.1";
|
|
|
244 |
$data->h5pparams = '{"params":{"quizType":"arithmetic","arithmeticType":"addition","UI":{"score":"Score:","time":"Time: @time"},
|
|
|
245 |
"intro":"This is a content for testing"},"metadata":{"defaultLanguage":"en","title":"Testing content"}}';
|
|
|
246 |
|
|
|
247 |
$title = 'libtest';
|
|
|
248 |
$library = 'H5P.ArithmeticQuiz 1.1';
|
|
|
249 |
$machinename = 'H5P.ArithmeticQuiz';
|
|
|
250 |
$contextid = 1;
|
|
|
251 |
$filearea = 'unittest';
|
|
|
252 |
$filename = 'export.h5p';
|
|
|
253 |
|
|
|
254 |
// Fake installed library for the H5P content.
|
|
|
255 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
|
|
|
256 |
|
|
|
257 |
$semantics = json_encode([['type' => 'text', 'name' => 'text', 'label' => 'Plain text', 'description' => 'Some text']]);
|
|
|
258 |
$generator->create_library_record($machinename, $title, 1, 1, 2, $semantics);
|
|
|
259 |
|
|
|
260 |
$editor = new editor();
|
|
|
261 |
$editor->set_library($library, $contextid, file_storage::COMPONENT, $filearea, 0, '/', $filename);
|
|
|
262 |
$newcontentid = $editor->save_content($data);
|
|
|
263 |
|
|
|
264 |
// Check the H5P content file was created where expected.
|
|
|
265 |
$fs = get_file_storage();
|
|
|
266 |
$out = $fs->get_file($contextid, file_storage::COMPONENT, $filearea, 0, '/', $filename);
|
|
|
267 |
$this->assertNotEmpty($out);
|
|
|
268 |
}
|
|
|
269 |
}
|