PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/h5p/tests/external/external_test.php

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