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).
This commit is contained in:
Paul Holden
2021-03-15 12:49:12 +00:00
parent 8ee6f49384
commit 9b3a8f7bd1
2 changed files with 103 additions and 2 deletions
+2 -2
View File
@@ -2681,11 +2681,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);
+101
View File
@@ -3372,4 +3372,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);
}
}