diff --git a/lib/externallib.php b/lib/externallib.php index 867059151d5..af7cf9f9b61 100644 --- a/lib/externallib.php +++ b/lib/externallib.php @@ -800,7 +800,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 02d64774e6a..e854382c7ee 100644 --- a/lib/tests/externallib_test.php +++ b/lib/tests/externallib_test.php @@ -72,6 +72,68 @@ class core_externallib_testcase extends advanced_testcase { $this->assertSame('aaa', $result['text']); } + 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); + $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct); + + $settings->set_raw(false); + $settings->set_filter(true); + + $test = '$$ \pi $$'; + $testformat = FORMAT_MARKDOWN; + $correct = array('

$$ \pi $$

+
', 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); + } + /** * Test for clean_returnvalue(). */