From 3b85322940a0815dc79313203cf6da3beb73c5fa Mon Sep 17 00:00:00 2001 From: cescobedo Date: Thu, 7 Nov 2019 23:05:41 +0100 Subject: [PATCH] MDL-67173 core_h5p: Translate non-localURL in WS to invalidurl The WebService should return invalidurl when the coding error is "Coding error detected, it must be fixed by a programmer: out_al_local_url called on a non-local URL" So, we translate the error to be used by Mobile App. --- h5p/classes/external.php | 17 +++++++++++++---- h5p/tests/external_test.php | 11 +++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/h5p/classes/external.php b/h5p/classes/external.php index d1e5d25e5ae..837f6283928 100644 --- a/h5p/classes/external.php +++ b/h5p/classes/external.php @@ -63,9 +63,9 @@ class external extends external_api { /** * Return the H5P file trusted. * - * The Mobile App needs to work with an H5P package which can trust. - * And this H5P package is only trusted by the Mobile App once it's been processed - * by the core checking the right caps, validating the H5P package + * The Mobile App needs to work with an H5P package which can trust. + * And this H5P package is only trusted by the Mobile App once it's been processed + * by the core checking the right caps, validating the H5P package * and doing any clean-up process involved. * * @since Moodle 3.8 @@ -99,9 +99,18 @@ class external extends external_api { $messages = $h5pplayer->get_messages(); } catch (\moodle_exception $e) { $messages = (object) [ - 'exception' => $e->getMessage(), 'code' => $e->getCode(), ]; + // To mantain the coherence between web coding error and Mobile coding errors. + // We need to return the same message error to Mobile. + // The 'out_al_local_url called on a non-local URL' error is provided by the \moodle_exception. + // We have to translate to h5pinvalidurl which is the same coding error showed in web. + if ($e->errorcode === 'codingerror' && + $e->a === 'out_as_local_url called on a non-local URL') { + $messages->exception = get_string('h5pinvalidurl', 'core_h5p'); + } else { + $messages->exception = $e->getMessage(); + } } if (empty($messages->error) && empty($messages->exception)) { diff --git a/h5p/tests/external_test.php b/h5p/tests/external_test.php index 70f613a56cd..9d02199a8d8 100644 --- a/h5p/tests/external_test.php +++ b/h5p/tests/external_test.php @@ -124,6 +124,17 @@ class core_h5p_external_testcase extends externallib_advanced_testcase { // Check the warnings to be sure that h5pinvalidurl is the message by moodle_exception. $this->assertEquals($urlempty, $result['warnings'][0]['item']); $this->assertEquals(get_string('h5pinvalidurl', 'core_h5p'), $result['warnings'][0]['message']); + + // Create a non-local URL. + $urlnonlocal = 'http://www.google.com/pluginfile.php/644/block_html/content/arithmetic-quiz-1-1.h5p'; + $result = external::get_trusted_h5p_file($urlnonlocal, 0, 0, 0, 0); + $result = external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result); + // Expected result: Just 1 record on warnings and none on files. + $this->assertCount(0, $result['files']); + $this->assertCount(1, $result['warnings']); + // Check the warnings to be sure that h5pinvalidurl is the message by moodle_exception. + $this->assertEquals($urlnonlocal, $result['warnings'][0]['item']); + $this->assertEquals(get_string('h5pinvalidurl', 'core_h5p'), $result['warnings'][0]['message']); } /**