From 763a3c33836632827279ddfd7620bfd3e90f5f38 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Mon, 22 Feb 2021 23:19:46 +0000 Subject: [PATCH] MDL-70977 mod_forum: fix empty equality check updating post content. Using the external method for updating posts, the check for empty subject/message content wasn't correct (disallowed the string '0'), in addition to being impossible to set a posts message format property to FORMAT_MOODLE (integer 0). --- mod/forum/externallib.php | 4 +- mod/forum/tests/externallib_test.php | 101 +++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/mod/forum/externallib.php b/mod/forum/externallib.php index 0df7c0f8ae4..fae24368f12 100644 --- a/mod/forum/externallib.php +++ b/mod/forum/externallib.php @@ -2694,11 +2694,11 @@ class mod_forum_external extends external_api { $updatepost->attachments = IGNORE_FILE_MERGE; // Prepare the post to be updated. - if (!empty($params['subject'])) { + if ($params['subject'] !== '') { $updatepost->subject = $params['subject']; } - if (!empty($params['message']) && !empty($params['messageformat'])) { + if ($params['message'] !== '' && isset($params['messageformat'])) { $updatepost->message = $params['message']; $updatepost->messageformat = $params['messageformat']; $updatepost->messagetrust = trusttext_trusted($modcontext); diff --git a/mod/forum/tests/externallib_test.php b/mod/forum/tests/externallib_test.php index 0e477478f15..81dc64a0691 100644 --- a/mod/forum/tests/externallib_test.php +++ b/mod/forum/tests/externallib_test.php @@ -3380,4 +3380,105 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { $this->expectExceptionMessage(get_string('cannotupdatepost', 'forum')); mod_forum_external::update_discussion_post($newpost->id, $subject, $message, $messageformat); } + + /** + * Test that we can update the subject of a post to the string '0' + */ + public function test_update_discussion_post_set_subject_to_zero(): void { + global $DB, $USER; + + $this->resetAfterTest(true); + $this->setAdminUser(); + + // Setup test data. + $course = $this->getDataGenerator()->create_course(); + $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]); + + $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion((object) [ + 'userid' => $USER->id, + 'course' => $course->id, + 'forum' => $forum->id, + 'name' => 'Test discussion subject', + ]); + + // Update discussion post subject. + $result = external_api::clean_returnvalue( + mod_forum_external::update_discussion_post_returns(), + mod_forum_external::update_discussion_post($discussion->firstpost, '0') + ); + $this->assertTrue($result['status']); + + // Get updated discussion post subject from DB. + $postsubject = $DB->get_field('forum_posts', 'subject', ['id' => $discussion->firstpost]); + $this->assertEquals('0', $postsubject); + } + + /** + * Test that we can update the message of a post to the string '0' + */ + public function test_update_discussion_post_set_message_to_zero(): void { + global $DB, $USER; + + $this->resetAfterTest(true); + $this->setAdminUser(); + + // Setup test data. + $course = $this->getDataGenerator()->create_course(); + $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]); + + $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion((object) [ + 'userid' => $USER->id, + 'course' => $course->id, + 'forum' => $forum->id, + 'message' => 'Test discussion message', + 'messageformat' => FORMAT_HTML, + ]); + + // Update discussion post message. + $result = external_api::clean_returnvalue( + mod_forum_external::update_discussion_post_returns(), + mod_forum_external::update_discussion_post($discussion->firstpost, '', '0', FORMAT_HTML) + ); + $this->assertTrue($result['status']); + + // Get updated discussion post subject from DB. + $postmessage = $DB->get_field('forum_posts', 'message', ['id' => $discussion->firstpost]); + $this->assertEquals('0', $postmessage); + } + + /** + * Test that we can update the message format of a post to {@see FORMAT_MOODLE} + */ + public function test_update_discussion_post_set_message_format_moodle(): void { + global $DB, $USER; + + $this->resetAfterTest(true); + $this->setAdminUser(); + + // Setup test data. + $course = $this->getDataGenerator()->create_course(); + $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]); + + $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion((object) [ + 'userid' => $USER->id, + 'course' => $course->id, + 'forum' => $forum->id, + 'message' => 'Test discussion message', + 'messageformat' => FORMAT_HTML, + ]); + + // Update discussion post message & messageformat. + $result = external_api::clean_returnvalue( + mod_forum_external::update_discussion_post_returns(), + mod_forum_external::update_discussion_post($discussion->firstpost, '', 'Update discussion message', FORMAT_MOODLE) + ); + $this->assertTrue($result['status']); + + // Get updated discussion post from DB. + $updatedpost = $DB->get_record('forum_posts', ['id' => $discussion->firstpost], 'message,messageformat'); + $this->assertEquals((object) [ + 'message' => 'Update discussion message', + 'messageformat' => FORMAT_MOODLE, + ], $updatedpost); + } }