MDL-76583 core_external: Migrate string formatting functions to util

This commit is contained in:
Andrew Nicols
2023-01-19 07:33:35 +08:00
parent 7f5fb6a592
commit caddb452f0
5 changed files with 370 additions and 233 deletions
+9 -3
View File
@@ -173,8 +173,10 @@ class externallib_test extends externallib_advanced_testcase {
/**
* Test get_config
*
* @covers \tool_mobile\external::get_config
*/
public function test_get_config() {
public function test_get_config(): void {
global $CFG, $SITE;
require_once($CFG->dirroot . '/course/format/lib.php');
@@ -188,7 +190,11 @@ class externallib_test extends externallib_advanced_testcase {
$result = \external_api::clean_returnvalue(external::get_config_returns(), $result);
// SITE summary is null in phpunit which gets transformed to an empty string by format_text.
list($sitesummary, $unused) = external_format_text($SITE->summary, $SITE->summaryformat, \context_system::instance()->id);
[$sitesummary, $summaryformat] = external_format_text(
$SITE->summary,
$SITE->summaryformat,
\context_system::instance()->id
);
// Test default values.
$context = \context_system::instance();
@@ -196,7 +202,7 @@ class externallib_test extends externallib_advanced_testcase {
array('name' => 'fullname', 'value' => $SITE->fullname),
array('name' => 'shortname', 'value' => $SITE->shortname),
array('name' => 'summary', 'value' => $sitesummary),
array('name' => 'summaryformat', 'value' => FORMAT_HTML),
array('name' => 'summaryformat', 'value' => $summaryformat),
array('name' => 'frontpage', 'value' => $CFG->frontpage),
array('name' => 'frontpageloggedin', 'value' => $CFG->frontpageloggedin),
array('name' => 'maxcategorydepth', 'value' => $CFG->maxcategorydepth),
+171 -1
View File
@@ -169,7 +169,7 @@ class util {
int $tokentype,
stdClass $service,
int $userid,
context $contextorid,
context $context,
int $validuntil = 0,
string $iprestriction = ''
): string {
@@ -416,4 +416,174 @@ class util {
}
return $token;
}
/**
* Format the string to be returned properly as requested by the either the web service server,
* either by an internally call.
* The caller can change the format (raw) with the settings singleton
* All web service servers must set this singleton when parsing the $_GET and $_POST.
*
* <pre>
* Options are the same that in {@link format_string()} with some changes:
* filter : Can be set to false to force filters off, else observes {@link settings}.
* </pre>
*
* @param string|null $content 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 context $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
*/
public static function format_string(
$content,
$context,
$striplinks = true,
$options = []
) {
if ($content === null || $content === '') {
// Nothing to return.
// Note: It's common for the DB to return null, so we allow format_string to take a null,
// even though it is counter-intuitive.
return '';
}
// Get settings (singleton).
$settings = external_settings::get_instance();
if (!$settings->get_raw()) {
$options['context'] = $context;
$options['filter'] = isset($options['filter']) && !$options['filter'] ? false : $settings->get_filter();
return format_string($content, $striplinks, $options);
}
return $content;
}
/**
* Format the text to be returned properly as requested by the either the web service server,
* either by an internally call.
* The caller can change the format (raw, filter, file, fileurl) with the \core_external\settings singleton
* All web service servers must set this singleton when parsing the $_GET and $_POST.
*
* <pre>
* 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 : Can be set to false to force filters off, else observes {@link \core_external\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.
* blanktarget : If true all <a> tags will have target="_blank" added unless target is explicitly specified.
* </pre>
*
* @param string|null $text The content that may contain ULRs in need of rewriting.
* @param string|int|null $textformat The text format.
* @param context $context This parameter and the next two identify the file area to use.
* @param string|null $component
* @param string|null $filearea helps identify the file area.
* @param int|string|null $itemid helps identify the file area.
* @param array|stdClass|null $options text formatting options
* @return array text + textformat
*/
public static function format_text(
$text,
$textformat,
$context,
$component = null,
$filearea = null,
$itemid = null,
$options = null
) {
global $CFG;
if ($text === null || $text === '') {
// Nothing to return.
// Note: It's common for the DB to return null, so we allow format_string to take nulls,
// even though it is counter-intuitive.
return ['', $textformat ?? FORMAT_MOODLE];
}
if (empty($itemid)) {
$itemid = null;
}
// Get settings (singleton).
$settings = external_settings::get_instance();
if ($component && $filearea && $settings->get_fileurl()) {
require_once($CFG->libdir . "/filelib.php");
$text = file_rewrite_pluginfile_urls($text, $settings->get_file(), $context->id, $component, $filearea, $itemid);
}
// Note that $CFG->forceclean does not apply here if the client requests for the raw database content.
// This is consistent with web clients that are still able to load non-cleaned text into editors, too.
if (!$settings->get_raw()) {
$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_int($options['context'])) {
if ($options['context'] != $context->id) {
debugging(
'Different contexts found in external_format_text parameters. $options[\'context\'] not allowed. ' .
'Using $contextid parameter...',
DEBUG_DEVELOPER
);
}
} else if ($options['context'] instanceof context) {
if ($options['context']->id != $context->id) {
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'] ? false : $settings->get_filter();
$options['para'] = isset($options['para']) ? $options['para'] : false;
$options['context'] = $context;
$options['allowid'] = isset($options['allowid']) ? $options['allowid'] : true;
$text = format_text($text, $textformat, $options);
// Once converted to html (from markdown, plain... lets inform consumer this is already HTML).
$textformat = FORMAT_HTML;
}
// Note: The formats defined in weblib are strings.
return [$text, $textformat];
}
/**
* Validate text field format against known FORMAT_XXX
*
* @param array $format the format to validate
* @return the validated format
* @throws coding_exception
* @since Moodle 2.3
*/
public static function validate_format($format) {
$allowedformats = array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN);
if (!in_array($format, $allowedformats)) {
throw new moodle_exception(
'formatnotsupported',
'webservice',
'',
null,
'The format with value=' . $format . ' is not supported by this Moodle site'
);
}
return $format;
}
}
+177 -2
View File
@@ -33,8 +33,12 @@ class util_test extends \advanced_testcase {
* Store the global DB for restore between tests.
*/
public function setUp(): void {
global $DB;
global $CFG, $DB;
$this->db = $DB;
external_settings::reset();
// Note: This is retained for testing of the old functions.
require_once("{$CFG->libdir}/externallib.php");
}
/**
@@ -45,6 +49,7 @@ class util_test extends \advanced_testcase {
if ($this->db !== null) {
$DB = $this->db;
}
external_settings::reset();
}
/**
@@ -219,8 +224,178 @@ class util_test extends \advanced_testcase {
// Change token default time.
$this->setUser($user2);
set_config('tokenduration', DAYSECS);
$token = util::external_generate_token_for_current_user($service);
$token = util::generate_token_for_current_user($service);
$timenow = time();
$this->assertLessThanOrEqual($timenow + DAYSECS, $token->validuntil);
}
/**
* Test the format_text function.
*
* @covers \core_external\util::format_text
*/
public function test_format_text(): void {
$settings = external_settings::get_instance();
$currentraw = $settings->get_raw();
$currentfilter = $settings->get_filter();
$settings->set_raw(true);
$settings->set_filter(false);
$context = \context_system::instance();
$test = '$$ \pi $$';
$testformat = FORMAT_MARKDOWN;
$correct = [$test, $testformat];
$this->assertSame($correct, util::format_text($test, $testformat, $context, 'core', '', 0));
// 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);
$test = '$$ \pi $$';
$testformat = FORMAT_MARKDOWN;
$correct = ['<span class="filter_mathjaxloader_equation"><p><span class="nolink">$$ \pi $$</span></p>
</span>', FORMAT_HTML,
];
$this->assertSame(util::format_text($test, $testformat, $context, 'core', '', 0), $correct);
// 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 = ['<p>$$ \pi $$</p>
', FORMAT_HTML,
];
$this->assertSame(util::format_text($test, $testformat, $context, 'core', '', 0, ['filter' => false]), $correct);
// 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 = [$test, FORMAT_HTML];
$options = ['allowid' => true];
$this->assertSame(util::format_text($test, $testformat, $context, 'core', '', 0, $options), $correct);
// 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 = ['<p><a></a><a href="#test">Text</a></p>', FORMAT_HTML];
$options = new \stdClass();
$options->allowid = false;
$this->assertSame(util::format_text($test, $testformat, $context, 'core', '', 0, $options), $correct);
// 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 = ['<p><a id="test"></a><a href="#test">Text</a></p> Newline', FORMAT_HTML];
$options = new \stdClass();
$options->newlines = false;
$this->assertSame(util::format_text($test, $testformat, $context, 'core', '', 0, $options), $correct);
// 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 = ['<div class="text_to_html">'.$test.'</div>', FORMAT_HTML];
$options = new \stdClass();
$options->para = true;
$this->assertSame(util::format_text($test, $testformat, $context, 'core', '', 0, $options), $correct);
// 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 = [$test, FORMAT_HTML];
$options = new \stdClass();
$options->context = $context;
$this->assertSame(util::format_text($test, $testformat, $context, 'core', '', 0, $options), $correct);
// 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);
}
/**
* Teset the format_string function.
*
* @covers \core_external\util::format_string
*/
public function test_external_format_string(): void {
$this->resetAfterTest();
$settings = external_settings::get_instance();
$currentraw = $settings->get_raw();
$currentfilter = $settings->get_filter();
// Enable multilang filter to on content and heading.
filter_set_global_state('multilang', TEXTFILTER_ON);
filter_set_applies_to_strings('multilang', 1);
$filtermanager = \filter_manager::instance();
$filtermanager->reset_caches();
$settings->set_raw(true);
$settings->set_filter(true);
$context = \context_system::instance();
$test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ';
$test .= '<script>hi</script> <h3>there</h3>!';
$correct = $test;
$this->assertSame($correct, util::format_string($test, $context));
// Function external_format_string should work with context id or context instance.
$this->assertSame($correct, external_format_string($test, $context));
$this->assertSame($correct, external_format_string($test, $context->id));
$settings->set_raw(false);
$settings->set_filter(false);
$test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ';
$test .= '<script>hi</script> <h3>there</h3>?';
$correct = 'ENFR hi there?';
$this->assertSame($correct, util::format_string($test, $context));
// Function external_format_string should work with context id or context instance.
$this->assertSame($correct, external_format_string($test, $context));
$this->assertSame($correct, external_format_string($test, $context->id));
$settings->set_filter(true);
$test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ';
$test .= '<script>hi</script> <h3>there</h3>@';
$correct = 'EN hi there@';
$this->assertSame($correct, util::format_string($test, $context));
// Function external_format_string should work with context id or context instance.
$this->assertSame($correct, external_format_string($test, $context));
$this->assertSame($correct, external_format_string($test, $context->id));
// Filters can be opted out.
$test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ';
$test .= '<script>hi</script> <h3>there</h3>%';
$correct = 'ENFR hi there%';
$this->assertSame($correct, util::format_string($test, $context, false, ['filter' => false]));
// 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]));
}
}
+11 -70
View File
@@ -14,7 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Support for external API
*
@@ -23,7 +22,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use core_external\util;
class_alias(\core_external\external_api::class, 'external_api');
class_alias(\core_external\restricted_context_exception::class, 'restricted_context_exception');
@@ -126,12 +125,7 @@ function external_delete_descriptions($component) {
* @since Moodle 2.3
*/
function external_validate_format($format) {
$allowedformats = array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN);
if (!in_array($format, $allowedformats)) {
throw new moodle_exception('formatnotsupported', 'webservice', '' , null,
'The format with value=' . $format . ' is not supported by this Moodle site');
}
return $format;
return util::validate_format($format);
}
/**
@@ -153,26 +147,12 @@ function external_validate_format($format) {
* @return string text
* @since Moodle 3.0
*/
function external_format_string($str, $contextorid, $striplinks = true, $options = array()) {
// Get settings (singleton).
$settings = external_settings::get_instance();
if (empty($contextorid)) {
throw new coding_exception('contextid is required');
function external_format_string($str, $context, $striplinks = true, $options = []) {
if (!$context instanceof context) {
$context = context::instance_by_id($context);
}
if (!$settings->get_raw()) {
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);
}
return $str;
return util::format_string($str, $context, $striplinks, $options);
}
/**
@@ -200,7 +180,7 @@ function external_format_string($str, $contextorid, $striplinks = true, $options
*
* @param string $text The content that may contain ULRs in need of rewriting.
* @param int $textformat The text format.
* @param context|int $contextorid This parameter and the next two identify the file area to use.
* @param context|int $context 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.
@@ -209,51 +189,12 @@ function external_format_string($str, $contextorid, $striplinks = true, $options
* @since Moodle 2.3
* @since Moodle 3.2 component, filearea and itemid are optional parameters
*/
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;
function external_format_text($text, $textformat, $context, $component = null, $filearea = null, $itemid = null, $options = null) {
if (!$context instanceof context) {
$context = context::instance_by_id($context);
}
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);
}
// Note that $CFG->forceclean does not apply here if the client requests for the raw database content.
// This is consistent with web clients that are still able to load non-cleaned text into editors, too.
if (!$settings->get_raw()) {
$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)
|| (!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);
}
}
$options['filter'] = isset($options['filter']) && !$options['filter'] ? false : $settings->get_filter();
$options['para'] = isset($options['para']) ? $options['para'] : false;
$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);
$textformat = FORMAT_HTML; // Once converted to html (from markdown, plain... lets inform consumer this is already HTML).
}
return array($text, $textformat);
return util::format_text($text, $textformat, $context, $component, $filearea, $itemid, $options);
}
/**
+2 -157
View File
@@ -16,167 +16,12 @@
namespace core;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/externallib.php');
/**
* Unit tests for /lib/externallib.php.
* Just a wrapper to access protected apis for testing.
*
* @package core
* @subpackage phpunit
* @copyright 2009 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class externallib_test extends \advanced_testcase {
public function test_external_format_text() {
$settings = \external_settings::get_instance();
$currentraw = $settings->get_raw();
$currentfilter = $settings->get_filter();
$settings->set_raw(true);
$settings->set_filter(false);
$context = \context_system::instance();
$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);
$test = '$$ \pi $$';
$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);
}
public function test_external_format_string() {
$this->resetAfterTest();
$settings = \external_settings::get_instance();
$currentraw = $settings->get_raw();
$currentfilter = $settings->get_filter();
// Enable multilang filter to on content and heading.
filter_set_global_state('multilang', TEXTFILTER_ON);
filter_set_applies_to_strings('multilang', 1);
$filtermanager = \filter_manager::instance();
$filtermanager->reset_caches();
$settings->set_raw(true);
$settings->set_filter(true);
$context = \context_system::instance();
$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);
$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]));
$settings->set_raw($currentraw);
$settings->set_filter($currentfilter);
}
}
/*
* Just a wrapper to access protected apis for testing
* Note: This is deprecated. Please use Reflection instead.
*/
class test_exernal_api extends \core_external\external_api {
public static function get_context_wrapper($params) {
return self::get_context_from_params($params);
}