See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310]
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 * Core h5p external functions tests. 19 * 20 * @package core_h5p 21 * @category external 22 * @copyright 2019 Carlos Escobedo <carlos@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 * @since Moodle 3.8 25 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 global $CFG; 30 31 require_once($CFG->libdir . '/externallib.php'); 32 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 33 34 use core_h5p\external; 35 use core_h5p\file_storage; 36 use core_h5p\local\library\autoloader; 37 38 /** 39 * Core h5p external functions tests 40 * 41 * @package core_h5p 42 * @category external 43 * @copyright 2019 Carlos Escobedo <carlos@moodle.com> 44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 45 * @since Moodle 3.8 46 */ 47 class core_h5p_external_testcase extends externallib_advanced_testcase { 48 49 protected function setUp() { 50 parent::setUp(); 51 autoloader::register(); 52 } 53 54 /** 55 * test_get_trusted_h5p_file description 56 */ 57 public function test_get_trusted_h5p_file() { 58 $this->resetAfterTest(true); 59 $this->setAdminUser(); 60 61 // This is a valid .H5P file. 62 $filename = 'find-the-words.h5p'; 63 $syscontext = \context_system::instance(); 64 65 // Create a fake export H5P file with normal pluginfile call. 66 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); 67 $deployedfile = $generator->create_export_file($filename, 68 $syscontext->id, 69 \core_h5p\file_storage::COMPONENT, 70 \core_h5p\file_storage::EXPORT_FILEAREA, 71 $generator::PLUGINFILE); 72 73 // Make the URL to pass to the WS. 74 $url = \moodle_url::make_pluginfile_url( 75 $syscontext->id, 76 \core_h5p\file_storage::COMPONENT, 77 \core_h5p\file_storage::EXPORT_FILEAREA, 78 0, 79 '/', 80 $filename 81 ); 82 83 // Call the WS. 84 $result = external::get_trusted_h5p_file($url->out(false), 0, 0, 0, 0); 85 $result = external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result); 86 // Expected result: Just 1 record on files and none on warnings. 87 $this->assertCount(1, $result['files']); 88 $this->assertCount(0, $result['warnings']); 89 90 // Check info export file to compare with the ws's results. 91 $this->assertEquals($deployedfile['filepath'], $result['files'][0]['filepath']); 92 $this->assertEquals($deployedfile['mimetype'], $result['files'][0]['mimetype']); 93 $this->assertEquals($deployedfile['filesize'], $result['files'][0]['filesize']); 94 $this->assertEquals($deployedfile['timemodified'], $result['files'][0]['timemodified']); 95 $this->assertEquals($deployedfile['filename'], $result['files'][0]['filename']); 96 $this->assertEquals($deployedfile['fileurl'], $result['files'][0]['fileurl']); 97 } 98 99 /** 100 * test_h5p_invalid_url description 101 */ 102 public function test_h5p_invalid_url() { 103 $this->resetAfterTest(); 104 $this->setAdminUser(); 105 106 // Create an empty url. 107 $urlempty = ''; 108 $result = external::get_trusted_h5p_file($urlempty, 0, 0, 0, 0); 109 $result = external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result); 110 // Expected result: Just 1 record on warnings and none on files. 111 $this->assertCount(0, $result['files']); 112 $this->assertCount(1, $result['warnings']); 113 // Check the warnings to be sure that h5pinvalidurl is the message by moodle_exception. 114 $this->assertEquals($urlempty, $result['warnings'][0]['item']); 115 $this->assertEquals(get_string('h5pinvalidurl', 'core_h5p'), $result['warnings'][0]['message']); 116 117 // Create a non-local URL. 118 $urlnonlocal = 'http://www.google.com/pluginfile.php/644/block_html/content/arithmetic-quiz-1-1.h5p'; 119 $result = external::get_trusted_h5p_file($urlnonlocal, 0, 0, 0, 0); 120 $result = external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result); 121 // Expected result: Just 1 record on warnings and none on files. 122 $this->assertCount(0, $result['files']); 123 $this->assertCount(1, $result['warnings']); 124 // Check the warnings to be sure that h5pinvalidurl is the message by moodle_exception. 125 $this->assertEquals($urlnonlocal, $result['warnings'][0]['item']); 126 $this->assertEquals(get_string('h5pinvalidurl', 'core_h5p'), $result['warnings'][0]['message']); 127 } 128 129 /** 130 * test_h5p_file_not_found description 131 */ 132 public function test_h5p_file_not_found() { 133 $this->resetAfterTest(); 134 $this->setAdminUser(); 135 136 // Create a valid url with an h5pfile which doesn't exist in DB. 137 $syscontext = \context_system::instance(); 138 $filenotfoundurl = \moodle_url::make_pluginfile_url( 139 $syscontext->id, 140 \core_h5p\file_storage::COMPONENT, 141 'unittest', 142 0, 143 '/', 144 'notfound.h5p' 145 ); 146 // Call the ws. 147 $result = external::get_trusted_h5p_file($filenotfoundurl->out(), 0, 0, 0, 0); 148 $result = external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result); 149 // Expected result: Just 1 record on warnings and none on files. 150 $this->assertCount(0, $result['files']); 151 $this->assertCount(1, $result['warnings']); 152 // Check the warnings to be sure that h5pfilenotfound is the message by h5p error. 153 $this->assertEquals($filenotfoundurl->out(), $result['warnings'][0]['item']); 154 $this->assertEquals(get_string('h5pfilenotfound', 'core_h5p'), $result['warnings'][0]['message']); 155 } 156 157 /** 158 * Test the request to get_trusted_h5p_file 159 * using webservice/pluginfile.php as url param. 160 */ 161 public function test_allow_webservice_pluginfile_in_url_param() { 162 $this->resetAfterTest(true); 163 $this->setAdminUser(); 164 165 // This is a valid .H5P file. 166 $filename = 'find-the-words.h5p'; 167 $syscontext = \context_system::instance(); 168 169 // Create a fake export H5P file with webservice call. 170 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); 171 $deployedfile = $generator->create_export_file($filename, 172 $syscontext->id, 173 \core_h5p\file_storage::COMPONENT, 174 \core_h5p\file_storage::EXPORT_FILEAREA); 175 176 // Make the URL to pass to the WS. 177 $url = \moodle_url::make_webservice_pluginfile_url( 178 $syscontext->id, 179 \core_h5p\file_storage::COMPONENT, 180 \core_h5p\file_storage::EXPORT_FILEAREA, 181 0, 182 '/', 183 $filename 184 ); 185 186 // Call the WS. 187 $result = external::get_trusted_h5p_file($url->out(), 0, 0, 0, 0); 188 $result = external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result); 189 190 // Check info export file to compare with the ws's results. 191 $this->assertEquals($deployedfile['filepath'], $result['files'][0]['filepath']); 192 $this->assertEquals($deployedfile['mimetype'], $result['files'][0]['mimetype']); 193 $this->assertEquals($deployedfile['filesize'], $result['files'][0]['filesize']); 194 $this->assertEquals($deployedfile['timemodified'], $result['files'][0]['timemodified']); 195 $this->assertEquals($deployedfile['filename'], $result['files'][0]['filename']); 196 $this->assertEquals($deployedfile['fileurl'], $result['files'][0]['fileurl']); 197 } 198 199 /** 200 * Test the request to get_trusted_h5p_file 201 * using tokenpluginfile.php as url param. 202 */ 203 public function test_allow_tokenluginfile_in_url_param() { 204 $this->resetAfterTest(true); 205 $this->setAdminUser(); 206 207 // This is a valid .H5P file. 208 $filename = 'find-the-words.h5p'; 209 $syscontext = \context_system::instance(); 210 211 // Create a fake export H5P file with tokenfile call. 212 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); 213 $deployedfile = $generator->create_export_file($filename, 214 $syscontext->id, 215 \core_h5p\file_storage::COMPONENT, 216 \core_h5p\file_storage::EXPORT_FILEAREA, 217 $generator::TOKENPLUGINFILE); 218 219 // Make the URL to pass to the WS. 220 $url = \moodle_url::make_pluginfile_url( 221 $syscontext->id, 222 \core_h5p\file_storage::COMPONENT, 223 \core_h5p\file_storage::EXPORT_FILEAREA, 224 0, 225 '/', 226 $filename, 227 false, 228 true 229 ); 230 231 // Call the WS. 232 $result = external::get_trusted_h5p_file($url->out(false), 0, 0, 0, 0); 233 $result = external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result); 234 // Expected result: Just 1 record on files and none on warnings. 235 $this->assertCount(1, $result['files']); 236 $this->assertCount(0, $result['warnings']); 237 238 // Check info export file to compare with the ws's results. 239 $this->assertEquals($deployedfile['filepath'], $result['files'][0]['filepath']); 240 $this->assertEquals($deployedfile['mimetype'], $result['files'][0]['mimetype']); 241 $this->assertEquals($deployedfile['filesize'], $result['files'][0]['filesize']); 242 $this->assertEquals($deployedfile['timemodified'], $result['files'][0]['timemodified']); 243 $this->assertEquals($deployedfile['filename'], $result['files'][0]['filename']); 244 $this->assertEquals($deployedfile['fileurl'], $result['files'][0]['fileurl']); 245 } 246 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body