Merge branch 'MDL-60966-master' of https://github.com/snake/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2017-12-27 00:36:00 +01:00 committed by David Monllao
commit bb3e956ed9
3 changed files with 46 additions and 10 deletions

View File

@ -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']);
}
}

View File

@ -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);

View File

@ -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('<span class="filter_mathjaxloader_equation"><p><span class="nolink">$$ \pi $$</span></p>
</span>', 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('<p>$$ \pi $$</p>
', 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 = '<p><a id="test"></a><a href="#test">Text</a></p>';
$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 = '<p><a id="test"></a><a href="#test">Text</a></p>';
$testformat = FORMAT_HTML;
$correct = array('<p><a></a><a href="#test">Text</a></p>', 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 = '<p><a id="test"></a><a href="#test">Text</a></p>'."\n".'Newline';
$testformat = FORMAT_MOODLE;
$correct = array('<p><a id="test"></a><a href="#test">Text</a></p> 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 = '<p><a id="test"></a><a href="#test">Text</a></p>';
$testformat = FORMAT_MOODLE;
$correct = array('<div class="text_to_html">'.$test.'</div>', 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 = '<p><a id="test"></a><a href="#test">Text</a></p>';
$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 = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' .
'<script>hi</script> <h3>there</h3>!';
$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 = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' .
'<script>hi</script> <h3>there</h3>?';
$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 = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' .
'<script>hi</script> <h3>there</h3>@';
$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 = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' .
'<script>hi</script> <h3>there</h3>%';
$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]));