MDL-56531 tool_usertours: Run step title/content through filters

This commit is contained in:
Andrew Nicols 2016-10-21 13:23:18 +08:00
parent 919b9dfabd
commit 3f41a803e7
6 changed files with 77 additions and 125 deletions

View File

@ -355,6 +355,8 @@ class helper {
* @return string
*/
public static function render_stepname_inplace_editable(step $step) {
$title = format_text(step::get_string_from_input($step->get_title()), FORMAT_HTML);
return new \core\output\inplace_editable(
'tool_usertours',
'stepname',
@ -362,9 +364,9 @@ class helper {
true,
\html_writer::link(
$step->get_edit_link(),
$step->get_title()
$title
),
$step->get_title(false)
$step->get_title()
);
}

View File

@ -96,7 +96,7 @@ class step_list extends \flexible_table {
* @return string
*/
protected function col_content(step $step) {
return $step->get_content(false);
return format_text(step::get_string_from_input($step->get_content()), FORMAT_HTML);
}
/**

View File

@ -63,26 +63,16 @@ class step implements \renderable {
$result = (object) [
'stepid' => $step->get_id(),
'title' => external_format_text(
static::get_string_from_input($step->get_title()),
stepsource::get_string_from_input($step->get_title()),
FORMAT_HTML,
$PAGE->context->id,
'tool_usertours',
null,
null,
[
'filter' => true,
]
'tool_usertours'
)[0],
'content' => external_format_text(
static::get_string_from_input($step->get_content()),
stepsource::get_string_from_input($step->get_content()),
FORMAT_HTML,
$PAGE->context->id,
'tool_usertours',
null,
null,
[
'filter' => true,
]
'tool_usertours'
)[0],
'element' => $step->get_target()->convert_to_css(),
];
@ -95,27 +85,4 @@ class step implements \renderable {
return $result;
}
/**
* Attempt to fetch any matching langstring if the string is in the
* format identifier,component.
*
* @param string $string
* @return string
*/
protected static function get_string_from_input($string) {
$string = trim($string);
if (preg_match('|^([a-zA-Z][a-zA-Z0-9\.:/_-]*),([a-zA-Z][a-zA-Z0-9\.:/_-]*)$|', $string, $matches)) {
if ($matches[2] === 'moodle') {
$matches[2] = 'core';
}
if (get_string_manager()->string_exists($matches[1], $matches[2])) {
$string = get_string($matches[1], $matches[2]);
}
}
return $string;
}
}

View File

@ -216,7 +216,7 @@ class step {
* @return $this
*/
public function set_title($value) {
$this->title = clean_param($value, PARAM_TEXT);
$this->title = clean_text($value);
$this->dirty = true;
return $this;
@ -630,4 +630,27 @@ class step {
return $this;
}
/**
* Attempt to fetch any matching langstring if the string is in the
* format identifier,component.
*
* @param string $string
* @return string
*/
public static function get_string_from_input($string) {
$string = trim($string);
if (preg_match('|^([a-zA-Z][a-zA-Z0-9\.:/_-]*),([a-zA-Z][a-zA-Z0-9\.:/_-]*)$|', $string, $matches)) {
if ($matches[2] === 'moodle') {
$matches[2] = 'core';
}
if (get_string_manager()->string_exists($matches[1], $matches[2])) {
$string = get_string($matches[1], $matches[2]);
}
}
return $string;
}
}

View File

@ -1,84 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Tests for step.
*
* @package tool_usertours
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Tests for step.
*
* @package tool_usertours
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class step_output_testcase extends advanced_testcase {
/**
* Data Provider for get_string_from_inpu.
*
* @return array
*/
public function get_string_from_input_provider() {
return [
'Text' => [
'example',
'example',
],
'Text which looks like a langstring' => [
'example,fakecomponent',
'example,fakecomponent',
],
'Text which is a langstring' => [
'administration,core',
'Administration',
],
'Text which is a langstring but uses "moodle" instead of "core"' => [
'administration,moodle',
'Administration',
],
'Text which is a langstring, but with extra whitespace' => [
' administration,moodle ',
'Administration',
],
'Looks like a langstring, but has incorrect space around comma' => [
'administration , moodle',
'administration , moodle',
],
];
}
/**
* Ensure that the get_string_from_input function returns translated
* strings correctly.
*
* @dataProvider get_string_from_input_provider
* @param string $string The string to test
* @param string $expected The expected result
*/
public function test_get_string_from_input($string, $expected) {
$rc = new ReflectionClass('\\tool_usertours\\output\\step');
$rcm = $rc->getMethod('get_string_from_input');
$rcm->setAccessible(true);
$this->assertEquals($expected, $rcm->invoke(null, $string));
}
}

View File

@ -803,4 +803,48 @@ class step_testcase extends advanced_testcase {
$this->assertEquals($value, $step->$getter());
}
/**
* Data Provider for get_string_from_input.
*
* @return array
*/
public function get_string_from_input_provider() {
return [
'Text' => [
'example',
'example',
],
'Text which looks like a langstring' => [
'example,fakecomponent',
'example,fakecomponent',
],
'Text which is a langstring' => [
'administration,core',
'Administration',
],
'Text which is a langstring but uses "moodle" instead of "core"' => [
'administration,moodle',
'Administration',
],
'Text which is a langstring, but with extra whitespace' => [
' administration,moodle ',
'Administration',
],
'Looks like a langstring, but has incorrect space around comma' => [
'administration , moodle',
'administration , moodle',
],
];
}
/**
* Ensure that the get_string_from_input function returns langstring strings correctly.
*
* @dataProvider get_string_from_input_provider
* @param string $string The string to test
* @param string $expected The expected result
*/
public function test_get_string_from_input($string, $expected) {
$this->assertEquals($expected, \tool_usertours\step::get_string_from_input($string));
}
}