From 328b4a73fbdafd3ba45c291e1c53f48f67d861d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Wed, 30 Mar 2016 18:45:27 +0200 Subject: [PATCH 1/2] MDL-52263 libraries: Add options to external_format_text --- lib/externallib.php | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/externallib.php b/lib/externallib.php index 7fd7e52abe8..019ddd26175 100644 --- a/lib/externallib.php +++ b/lib/externallib.php @@ -792,16 +792,34 @@ function external_format_string($str, $contextid, $striplinks = true, $options = * The caller can change the format (raw, filter, file, fileurl) with the external_settings singleton * All web service servers must set this singleton when parsing the $_GET and $_POST. * + *
+ * Options are the same that in {@link format_text()} with some changes in defaults to provide backwards compatibility:
+ *      trusted     :   If true the string won't be cleaned. Default false.
+ *      noclean     :   If true the string won't be cleaned only if trusted is also true. Default false.
+ *      nocache     :   If true the string will not be cached and will be formatted every call. Default false.
+ *      filter      :   If true the string will be run through applicable filters as well. Default (different from format_text)
+ *                      got form settings.
+ *      para        :   If true then the returned string will be wrapped in div tags. Default (different from format_text) false.
+ *                      Default changed because div tags are not commonly needed.
+ *      newlines    :   If true then lines newline breaks will be converted to HTML newline breaks. Default true.
+ *      context     :   Not used! Using contextid parameter instead.
+ *      overflowdiv :   If set to true the formatted text will be encased in a div with the class no-overflow before being
+ *                      returned. Default false.
+ *      allowid     :   If true then id attributes will not be removed, even when using htmlpurifier. Default (different from
+ *                      format_text) true. Default changed id attributes are commonly needed.
+ * 
+ * * @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 string $component * @param string $filearea helps identify the file area. * @param int $itemid helps identify the file area. + * @param object/array $options text formatting options * @return array text + textformat * @since Moodle 2.3 */ -function external_format_text($text, $textformat, $contextid, $component, $filearea, $itemid) { +function external_format_text($text, $textformat, $contextid, $component, $filearea, $itemid, $options = null) { global $CFG; // Get settings (singleton). @@ -813,8 +831,23 @@ function external_format_text($text, $textformat, $contextid, $component, $filea } if (!$settings->get_raw()) { - $context = context::instance_by_id($contextid); - $text = format_text($text, $textformat, array('para' => false, 'filter' => $settings->get_filter(), 'context' => $context)); + $options = (array)$options; + + // If context is passed in options, check that is the same to show a debug message. + if (isset($options['context'])) { + if ((is_object($options['context']) && $options['context']->id != $contextid) + || $options['context'] != $contextid) { + debugging('Different contexts found in external_format_text parameters. $options[\'context\'] not allowed. + Using $contextid parameter...', DEBUG_DEVELOPER); + } + } + + $options['filter'] = isset($options['filter']) ? $options['filter'] : $settings->get_filter(); + $options['para'] = isset($options['para']) ? $options['para'] : false; + $options['context'] = context::instance_by_id($contextid); + $options['allowid'] = isset($options['allowid']) ? $options['allowid'] : true; + + $text = format_text($text, $textformat, $options); $textformat = FORMAT_HTML; // Once converted to html (from markdown, plain... lets inform consumer this is already HTML). } From d830d4601d732e1d9f5572b4554c1ebec1d86660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Tue, 12 Apr 2016 14:36:07 +0200 Subject: [PATCH 2/2] MDL-52263 libraries: Add test cases to external_format_text options --- lib/externallib.php | 2 +- lib/tests/externallib_test.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/externallib.php b/lib/externallib.php index 019ddd26175..6c52c811bdf 100644 --- a/lib/externallib.php +++ b/lib/externallib.php @@ -836,7 +836,7 @@ function external_format_text($text, $textformat, $contextid, $component, $filea // If context is passed in options, check that is the same to show a debug message. if (isset($options['context'])) { if ((is_object($options['context']) && $options['context']->id != $contextid) - || $options['context'] != $contextid) { + || (!is_object($options['context']) && $options['context'] != $contextid)) { debugging('Different contexts found in external_format_text parameters. $options[\'context\'] not allowed. Using $contextid parameter...', DEBUG_DEVELOPER); } diff --git a/lib/tests/externallib_test.php b/lib/tests/externallib_test.php index f67a1205c60..14d9164b011 100644 --- a/lib/tests/externallib_test.php +++ b/lib/tests/externallib_test.php @@ -96,6 +96,40 @@ class core_externallib_testcase extends advanced_testcase { ', FORMAT_HTML); $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct); + $test = '

Text

'; + $testformat = FORMAT_HTML; + $correct = array($test, FORMAT_HTML); + $options = array('allowid' => true); + $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct); + + $test = '

Text

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

Text

', FORMAT_HTML); + $options = new StdClass(); + $options->allowid = false; + $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct); + + $test = '

Text

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

Text

Newline', FORMAT_HTML); + $options = new StdClass(); + $options->newlines = false; + $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct); + + $test = '

Text

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

Text

'; + $testformat = FORMAT_MOODLE; + $correct = array($test, FORMAT_HTML); + $options = new StdClass(); + $options->context = $context; + $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct); + $settings->set_raw($currentraw); $settings->set_filter($currentfilter); }