Skip to content

Commit

Permalink
Merge branch 'MDL-70977-39' of git://github.com/paulholden/moodle int…
Browse files Browse the repository at this point in the history
…o MOODLE_39_STABLE
  • Loading branch information
abgreeve committed Mar 18, 2021
2 parents 841fd94 + 9b3a8f7 commit 6f5266c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mod/forum/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2681,11 +2681,11 @@ public static function update_discussion_post($postid, $subject = '', $message =
$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);
Expand Down
101 changes: 101 additions & 0 deletions mod/forum/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3372,4 +3372,105 @@ public function test_update_discussion_post_other_user_post() {
$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);
}
}

0 comments on commit 6f5266c

Please sign in to comment.