From 0377e78e36c1b2aef83d4f248ecaa034c5627d7a Mon Sep 17 00:00:00 2001 From: Ryan Wyllie Date: Fri, 24 Nov 2017 06:36:09 +0000 Subject: [PATCH] MDL-60966 external: contexts can be passed to external format funcs --- lib/classes/external/exporter.php | 6 +++--- lib/externallib.php | 26 +++++++++++++++++++------- lib/tests/externallib_test.php | 24 ++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/lib/classes/external/exporter.php b/lib/classes/external/exporter.php index 9a262815918..38c195a5964 100644 --- a/lib/classes/external/exporter.php +++ b/lib/classes/external/exporter.php @@ -157,7 +157,7 @@ abstract class exporter { $formatparams = $this->get_format_parameters($property); $format = $record->$propertyformat; - list($text, $format) = external_format_text($data->$property, $format, $formatparams['context']->id, + list($text, $format) = external_format_text($data->$property, $format, $formatparams['context'], $formatparams['component'], $formatparams['filearea'], $formatparams['itemid'], $formatparams['options']); $data->$property = $text; @@ -168,11 +168,11 @@ abstract class exporter { if (!empty($definition['multiple'])) { foreach ($data->$property as $key => $value) { - $data->{$property}[$key] = external_format_string($value, $formatparams['context']->id, + $data->{$property}[$key] = external_format_string($value, $formatparams['context'], $formatparams['striplinks'], $formatparams['options']); } } else { - $data->$property = external_format_string($data->$property, $formatparams['context']->id, + $data->$property = external_format_string($data->$property, $formatparams['context'], $formatparams['striplinks'], $formatparams['options']); } } diff --git a/lib/externallib.php b/lib/externallib.php index e49aaf27c82..ec42975cab1 100644 --- a/lib/externallib.php +++ b/lib/externallib.php @@ -896,21 +896,25 @@ function external_validate_format($format) { * @param string $str The string to be filtered. Should be plain text, expect * possibly for multilang tags. * @param boolean $striplinks To strip any link in the result text. Moodle 1.8 default changed from false to true! MDL-8713 - * @param int $contextid The id of the context for the string (affects filters). + * @param context|int $contextorid The id of the context for the string or the context (affects filters). * @param array $options options array/object or courseid * @return string text * @since Moodle 3.0 */ -function external_format_string($str, $contextid, $striplinks = true, $options = array()) { +function external_format_string($str, $contextorid, $striplinks = true, $options = array()) { // Get settings (singleton). $settings = external_settings::get_instance(); - if (empty($contextid)) { + if (empty($contextorid)) { throw new coding_exception('contextid is required'); } if (!$settings->get_raw()) { - $context = context::instance_by_id($contextid); + if (is_object($contextorid) && is_a($contextorid, 'context')) { + $context = $contextorid; + } else { + $context = context::instance_by_id($contextorid); + } $options['context'] = $context; $options['filter'] = isset($options['filter']) && !$options['filter'] ? false : $settings->get_filter(); $str = format_string($str, $striplinks, $options); @@ -944,7 +948,7 @@ function external_format_string($str, $contextid, $striplinks = true, $options = * * @param string $text The content that may contain ULRs in need of rewriting. * @param int $textformat The text format. - * @param int $contextid This parameter and the next two identify the file area to use. + * @param context|int $contextorid This parameter and the next two identify the file area to use. * @param string $component * @param string $filearea helps identify the file area. * @param int $itemid helps identify the file area. @@ -953,13 +957,21 @@ function external_format_string($str, $contextid, $striplinks = true, $options = * @since Moodle 2.3 * @since Moodle 3.2 component, filearea and itemid are optional parameters */ -function external_format_text($text, $textformat, $contextid, $component = null, $filearea = null, $itemid = null, +function external_format_text($text, $textformat, $contextorid, $component = null, $filearea = null, $itemid = null, $options = null) { global $CFG; // Get settings (singleton). $settings = external_settings::get_instance(); + if (is_object($contextorid) && is_a($contextorid, 'context')) { + $context = $contextorid; + $contextid = $context->id; + } else { + $context = null; + $contextid = $contextorid; + } + if ($component and $filearea and $settings->get_fileurl()) { require_once($CFG->libdir . "/filelib.php"); $text = file_rewrite_pluginfile_urls($text, $settings->get_file(), $contextid, $component, $filearea, $itemid); @@ -979,7 +991,7 @@ function external_format_text($text, $textformat, $contextid, $component = null, $options['filter'] = isset($options['filter']) && !$options['filter'] ? false : $settings->get_filter(); $options['para'] = isset($options['para']) ? $options['para'] : false; - $options['context'] = context::instance_by_id($contextid); + $options['context'] = !is_null($context) ? $context : context::instance_by_id($contextid); $options['allowid'] = isset($options['allowid']) ? $options['allowid'] : true; $text = format_text($text, $textformat, $options); diff --git a/lib/tests/externallib_test.php b/lib/tests/externallib_test.php index 5d449da04d0..2f1aaf640ab 100644 --- a/lib/tests/externallib_test.php +++ b/lib/tests/externallib_test.php @@ -128,7 +128,9 @@ class core_externallib_testcase extends advanced_testcase { $test = '$$ \pi $$'; $testformat = FORMAT_MARKDOWN; $correct = array($test, $testformat); + // Function external_format_text should work with context id or context instance. $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct); + $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0), $correct); $settings->set_raw(false); $settings->set_filter(true); @@ -137,48 +139,62 @@ class core_externallib_testcase extends advanced_testcase { $testformat = FORMAT_MARKDOWN; $correct = array('

$$ \pi $$

', FORMAT_HTML); + // Function external_format_text should work with context id or context instance. $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct); + $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0), $correct); // Filters can be opted out from by the developer. $test = '$$ \pi $$'; $testformat = FORMAT_MARKDOWN; $correct = array('

$$ \pi $$

', FORMAT_HTML); + // Function external_format_text should work with context id or context instance. $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, ['filter' => false]), $correct); + $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, ['filter' => false]), $correct); $test = '

Text

'; $testformat = FORMAT_HTML; $correct = array($test, FORMAT_HTML); $options = array('allowid' => true); + // Function external_format_text should work with context id or context instance. $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct); + $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct); $test = '

Text

'; $testformat = FORMAT_HTML; $correct = array('

Text

', FORMAT_HTML); $options = new StdClass(); $options->allowid = false; + // Function external_format_text should work with context id or context instance. $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct); + $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct); $test = '

Text

'."\n".'Newline'; $testformat = FORMAT_MOODLE; $correct = array('

Text

Newline', FORMAT_HTML); $options = new StdClass(); $options->newlines = false; + // Function external_format_text should work with context id or context instance. $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct); + $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct); $test = '

Text

'; $testformat = FORMAT_MOODLE; $correct = array('
'.$test.'
', FORMAT_HTML); $options = new StdClass(); $options->para = true; + // Function external_format_text should work with context id or context instance. $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct); + $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct); $test = '

Text

'; $testformat = FORMAT_MOODLE; $correct = array($test, FORMAT_HTML); $options = new StdClass(); $options->context = $context; + // Function external_format_text should work with context id or context instance. $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct); + $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct); $settings->set_raw($currentraw); $settings->set_filter($currentfilter); @@ -203,7 +219,9 @@ class core_externallib_testcase extends advanced_testcase { $test = 'ENFR ' . '

there

!'; $correct = $test; + // Function external_format_string should work with context id or context instance. $this->assertSame($correct, external_format_string($test, $context->id)); + $this->assertSame($correct, external_format_string($test, $context)); $settings->set_raw(false); $settings->set_filter(false); @@ -211,20 +229,26 @@ class core_externallib_testcase extends advanced_testcase { $test = 'ENFR ' . '

there

?'; $correct = 'ENFR hi there?'; + // Function external_format_string should work with context id or context instance. $this->assertSame($correct, external_format_string($test, $context->id)); + $this->assertSame($correct, external_format_string($test, $context)); $settings->set_filter(true); $test = 'ENFR ' . '

there

@'; $correct = 'EN hi there@'; + // Function external_format_string should work with context id or context instance. $this->assertSame($correct, external_format_string($test, $context->id)); + $this->assertSame($correct, external_format_string($test, $context)); // Filters can be opted out. $test = 'ENFR ' . '

there

%'; $correct = 'ENFR hi there%'; + // Function external_format_string should work with context id or context instance. $this->assertSame($correct, external_format_string($test, $context->id, false, ['filter' => false])); + $this->assertSame($correct, external_format_string($test, $context, false, ['filter' => false])); $this->assertSame("& < > \" '", format_string("& < > \" '", true, ['escape' => false]));