From 907db7f06b21421f3e2eee0213308e698f805825 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Wed, 30 Oct 2019 13:42:34 +0100 Subject: [PATCH 1/2] MDL-66877 mod: Fix WebServices returning unfiltered activity names --- mod/assign/externallib.php | 4 ++-- mod/assign/tests/externallib_test.php | 15 +++++++++++---- mod/data/classes/external.php | 4 +++- mod/data/tests/externallib_test.php | 14 +++++++++++++- mod/lesson/classes/external.php | 4 +++- mod/lesson/tests/external_test.php | 13 ++++++++++++- 6 files changed, 44 insertions(+), 10 deletions(-) diff --git a/mod/assign/externallib.php b/mod/assign/externallib.php index 492dfc52096..140f3fb7a16 100644 --- a/mod/assign/externallib.php +++ b/mod/assign/externallib.php @@ -432,7 +432,7 @@ class mod_assign_external extends external_api { 'id' => $module->assignmentid, 'cmid' => $module->id, 'course' => $module->course, - 'name' => $module->name, + 'name' => external_format_string($module->name, $context), 'nosubmissions' => $module->nosubmissions, 'submissiondrafts' => $module->submissiondrafts, 'sendnotifications' => $module->sendnotifications, @@ -528,7 +528,7 @@ class mod_assign_external extends external_api { 'id' => new external_value(PARAM_INT, 'assignment id'), 'cmid' => new external_value(PARAM_INT, 'course module id'), 'course' => new external_value(PARAM_INT, 'course id'), - 'name' => new external_value(PARAM_TEXT, 'assignment name'), + 'name' => new external_value(PARAM_RAW, 'assignment name'), 'nosubmissions' => new external_value(PARAM_INT, 'no submissions'), 'submissiondrafts' => new external_value(PARAM_INT, 'submissions drafts'), 'sendnotifications' => new external_value(PARAM_INT, 'send notifications'), diff --git a/mod/assign/tests/externallib_test.php b/mod/assign/tests/externallib_test.php index 8994bdfd7eb..4f832131f93 100644 --- a/mod/assign/tests/externallib_test.php +++ b/mod/assign/tests/externallib_test.php @@ -140,6 +140,13 @@ class mod_assign_external_testcase extends externallib_advanced_testcase { $this->resetAfterTest(true); + // Enable multilang filter to on content and heading. + filter_set_global_state('multilang', TEXTFILTER_ON); + filter_set_applies_to_strings('multilang', 1); + // Set WS filtering. + $wssettings = external_settings::get_instance(); + $wssettings->set_filter(true); + $category = self::getDataGenerator()->create_category(array( 'name' => 'Test category' )); @@ -166,7 +173,7 @@ class mod_assign_external_testcase extends externallib_advanced_testcase { // Create the assignment module with links to a filerecord. $assign1 = self::getDataGenerator()->create_module('assign', array( 'course' => $course1->id, - 'name' => 'lightwork assignment', + 'name' => 'EnglishEspañol', 'intro' => 'the assignment intro text here link', 'introformat' => FORMAT_HTML, 'markingworkflow' => 1, @@ -221,7 +228,7 @@ class mod_assign_external_testcase extends externallib_advanced_testcase { $assignment = $course['assignments'][0]; $this->assertEquals($assign1->id, $assignment['id']); $this->assertEquals($course1->id, $assignment['course']); - $this->assertEquals('lightwork assignment', $assignment['name']); + $this->assertEquals('English', $assignment['name']); $this->assertContains('the assignment intro text here', $assignment['intro']); $this->assertNotEmpty($assignment['configs']); // Check the url of the file attatched. @@ -249,7 +256,7 @@ class mod_assign_external_testcase extends externallib_advanced_testcase { $assignment = $course['assignments'][0]; $this->assertEquals($assign1->id, $assignment['id']); $this->assertEquals($course1->id, $assignment['course']); - $this->assertEquals('lightwork assignment', $assignment['name']); + $this->assertEquals('English', $assignment['name']); $this->assertArrayNotHasKey('intro', $assignment); $this->assertArrayNotHasKey('introattachments', $assignment); $this->assertEquals(1, $assignment['markingworkflow']); @@ -287,7 +294,7 @@ class mod_assign_external_testcase extends externallib_advanced_testcase { $assignment = $course['assignments'][0]; $this->assertEquals($assign1->id, $assignment['id']); $this->assertEquals($course1->id, $assignment['course']); - $this->assertEquals('lightwork assignment', $assignment['name']); + $this->assertEquals('English', $assignment['name']); $this->assertArrayNotHasKey('intro', $assignment); $this->assertArrayNotHasKey('introattachments', $assignment); $this->assertEquals(1, $assignment['markingworkflow']); diff --git a/mod/data/classes/external.php b/mod/data/classes/external.php index 649a6336a49..4811a6c5445 100644 --- a/mod/data/classes/external.php +++ b/mod/data/classes/external.php @@ -125,7 +125,9 @@ class mod_data_external extends external_api { } } $exporter = new database_summary_exporter($database, array('context' => $context)); - $arrdatabases[] = $exporter->export($PAGE->get_renderer('core')); + $data = $exporter->export($PAGE->get_renderer('core')); + $data->name = external_format_string($data->name, $context); + $arrdatabases[] = $data; } } diff --git a/mod/data/tests/externallib_test.php b/mod/data/tests/externallib_test.php index d2314aae9b6..c49c9be96fb 100644 --- a/mod/data/tests/externallib_test.php +++ b/mod/data/tests/externallib_test.php @@ -122,7 +122,8 @@ class mod_data_external_testcase extends externallib_advanced_testcase { * Test get databases by courses */ public function test_mod_data_get_databases_by_courses() { - global $DB; + global $DB, $CFG; + require_once($CFG->libdir . '/externallib.php'); $this->resetAfterTest(true); @@ -141,6 +142,8 @@ class mod_data_external_testcase extends externallib_advanced_testcase { $record = new stdClass(); $record->introformat = FORMAT_HTML; $record->course = $course1->id; + // Set multilang text to check that is properly filtered to "en" only. + $record->name = 'EnglishEspañol'; $record->intro = ''; $database1 = self::getDataGenerator()->create_module('data', $record); @@ -168,6 +171,14 @@ class mod_data_external_testcase extends externallib_advanced_testcase { } $enrol->enrol_user($instance2, $student->id, $studentrole->id); + // Enable multilang filter to on content and heading. + filter_manager::reset_caches(); + filter_set_global_state('multilang', TEXTFILTER_ON); + filter_set_applies_to_strings('multilang', true); + // Set WS filtering. + $wssettings = external_settings::get_instance(); + $wssettings->set_filter(true); + // Create what we expect to be returned when querying the two courses. // First for the student user. $expectedfields = array('id', 'coursemodule', 'course', 'name', 'comments', 'timeavailablefrom', @@ -192,6 +203,7 @@ class mod_data_external_testcase extends externallib_advanced_testcase { $expected1[$field] = $database1->{$field}; $expected2[$field] = $database2->{$field}; } + $expected1['name'] = 'English'; // Lang filtered expected. $expected1['comments'] = (bool) $expected1['comments']; $expected2['comments'] = (bool) $expected2['comments']; diff --git a/mod/lesson/classes/external.php b/mod/lesson/classes/external.php index d3ff0190b56..fc99b71d0f0 100644 --- a/mod/lesson/classes/external.php +++ b/mod/lesson/classes/external.php @@ -143,7 +143,9 @@ class mod_lesson_external extends external_api { $lessonrecord = self::get_lesson_summary_for_exporter($lessonrecord); $exporter = new lesson_summary_exporter($lessonrecord, array('context' => $context)); - $returnedlessons[] = $exporter->export($PAGE->get_renderer('core')); + $lesson = $exporter->export($PAGE->get_renderer('core')); + $lesson->name = external_format_string($lesson->name, $context); + $returnedlessons[] = $lesson; } } $result = array(); diff --git a/mod/lesson/tests/external_test.php b/mod/lesson/tests/external_test.php index 27ae4ae8ea6..95246491516 100644 --- a/mod/lesson/tests/external_test.php +++ b/mod/lesson/tests/external_test.php @@ -99,7 +99,8 @@ class mod_lesson_external_testcase extends externallib_advanced_testcase { * Test test_mod_lesson_get_lessons_by_courses */ public function test_mod_lesson_get_lessons_by_courses() { - global $DB; + global $DB, $CFG; + require_once($CFG->libdir . '/externallib.php'); // Create additional course. $course2 = self::getDataGenerator()->create_course(); @@ -107,6 +108,7 @@ class mod_lesson_external_testcase extends externallib_advanced_testcase { // Second lesson. $record = new stdClass(); $record->course = $course2->id; + $record->name = 'EnglishEspañol'; $lesson2 = self::getDataGenerator()->create_module('lesson', $record); // Execute real Moodle enrolment as we'll call unenrol() method on the instance later. @@ -122,6 +124,14 @@ class mod_lesson_external_testcase extends externallib_advanced_testcase { self::setUser($this->student); + // Enable multilang filter to on content and heading. + filter_manager::reset_caches(); + filter_set_global_state('multilang', TEXTFILTER_ON); + filter_set_applies_to_strings('multilang', true); + // Set WS filtering. + $wssettings = external_settings::get_instance(); + $wssettings->set_filter(true); + $returndescription = mod_lesson_external::get_lessons_by_courses_returns(); // Create what we expect to be returned when querying the two courses. @@ -157,6 +167,7 @@ class mod_lesson_external_testcase extends externallib_advanced_testcase { $expected2[$field] = $lesson2->{$field}; } + $expected2['name'] = 'English'; // Lang filtered expected. $expectedlessons = array($expected2, $expected1); // Call the external function passing course ids. From 27d6bd5de1fef87ac5e85a34d845f2c182764f36 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Wed, 30 Oct 2019 14:12:15 +0100 Subject: [PATCH 2/2] MDL-66877 mod: Fix unfiltered activity names in page headings --- mod/book/edit.php | 2 +- mod/book/tool/importhtml/index.php | 4 ++-- mod/choice/report.php | 2 +- mod/survey/report.php | 2 +- mod/survey/save.php | 2 +- mod/survey/view.php | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/mod/book/edit.php b/mod/book/edit.php index 202665260e3..367bbe7966d 100644 --- a/mod/book/edit.php +++ b/mod/book/edit.php @@ -137,7 +137,7 @@ if ($chapters = book_preload_chapters($book)) { } echo $OUTPUT->header(); -echo $OUTPUT->heading($book->name); +echo $OUTPUT->heading(format_string($book->name)); $mform->display(); diff --git a/mod/book/tool/importhtml/index.php b/mod/book/tool/importhtml/index.php index df186dd9e1a..b0e0d24419a 100644 --- a/mod/book/tool/importhtml/index.php +++ b/mod/book/tool/importhtml/index.php @@ -67,7 +67,7 @@ if ($mform->is_cancelled()) { } else if ($data = $mform->get_data()) { echo $OUTPUT->header(); - echo $OUTPUT->heading($book->name); + echo $OUTPUT->heading(format_string($book->name)); echo $OUTPUT->heading(get_string('importingchapters', 'booktool_importhtml'), 3); // this is a bloody hack - children do not try this at home! @@ -85,7 +85,7 @@ if ($mform->is_cancelled()) { } echo $OUTPUT->header(); -echo $OUTPUT->heading($book->name); +echo $OUTPUT->heading(format_string($book->name)); $mform->display(); diff --git a/mod/choice/report.php b/mod/choice/report.php index 5c3a5f0a6bd..7810b338c9e 100644 --- a/mod/choice/report.php +++ b/mod/choice/report.php @@ -68,7 +68,7 @@ $PAGE->set_title(format_string($choice->name).": $strresponses"); $PAGE->set_heading($course->fullname); echo $OUTPUT->header(); - echo $OUTPUT->heading($choice->name, 2, null); + echo $OUTPUT->heading(format_string($choice->name), 2, null); /// Check to see if groups are being used in this choice $groupmode = groups_get_activity_groupmode($cm); if ($groupmode) { diff --git a/mod/survey/report.php b/mod/survey/report.php index 7c147cd29e1..a3ad94541c7 100644 --- a/mod/survey/report.php +++ b/mod/survey/report.php @@ -118,7 +118,7 @@ $PAGE->set_title("$course->shortname: ".format_string($survey->name)); $PAGE->set_heading($course->fullname); echo $OUTPUT->header(); - echo $OUTPUT->heading($survey->name); + echo $OUTPUT->heading(format_string($survey->name)); /// Check to see if groups are being used in this survey if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used diff --git a/mod/survey/save.php b/mod/survey/save.php index afa25b20ecd..5ea69c97b85 100644 --- a/mod/survey/save.php +++ b/mod/survey/save.php @@ -59,7 +59,7 @@ $PAGE->set_title($strsurveysaved); $PAGE->set_heading($course->fullname); echo $OUTPUT->header(); - echo $OUTPUT->heading($survey->name); + echo $OUTPUT->heading(format_string($survey->name)); if (survey_already_done($survey->id, $USER->id)) { notice(get_string("alreadysubmitted", "survey"), get_local_referer(false)); diff --git a/mod/survey/view.php b/mod/survey/view.php index 884feb6c8e1..97a528357c6 100644 --- a/mod/survey/view.php +++ b/mod/survey/view.php @@ -69,7 +69,7 @@ $strsurvey = get_string("modulename", "survey"); $PAGE->set_title($survey->name); $PAGE->set_heading($course->fullname); echo $OUTPUT->header(); -echo $OUTPUT->heading($survey->name); +echo $OUTPUT->heading(format_string($survey->name)); // Check to see if groups are being used in this survey. if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used.