From 106f23730a6b18b2bd326f81be17c7b6350db0dd Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20Mudr=C3=A1k?= Date: Tue, 17 Jan 2017 19:27:57 +0100 Subject: [PATCH] MDL-57677 mod_forum: Fix wrong user displayed as the last post's author In the recent issue MDL-56225, we started to record the current user as the usermodified in the forum_discussions table when updating a forum post. It made sense but it was a mistake. Even if the current user really modifies the discussion by updating the post, the field usermodified has actually been always interpreted and displayed as the last post' author. Not as the last user who touched the discussion. This patch reverts that particular change to the previous behaviour and adds explicit unit test for it. --- mod/forum/lib.php | 2 +- mod/forum/tests/lib_test.php | 54 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 79168f557cd..d482fa53955 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -4439,7 +4439,7 @@ function forum_update_post($newpost, $mform, $unused = null) { // Last post modified tracking. $discussion->timemodified = $post->modified; - $discussion->usermodified = $USER->id; + $discussion->usermodified = $post->userid; if (!$post->parent) { // Post is a discussion starter - update discussion title and times too $discussion->name = $post->subject; diff --git a/mod/forum/tests/lib_test.php b/mod/forum/tests/lib_test.php index ef135e5eac3..82c5ffbf57a 100644 --- a/mod/forum/tests/lib_test.php +++ b/mod/forum/tests/lib_test.php @@ -3260,4 +3260,58 @@ class mod_forum_lib_testcase extends advanced_testcase { ], ]; } + + /** + * Test that {@link forum_update_post()} keeps correct forum_discussions usermodified. + */ + public function test_forum_update_post_keeps_discussions_usermodified() { + global $DB; + + $this->resetAfterTest(); + + // Let there be light. + $teacher = self::getDataGenerator()->create_user(); + $student = self::getDataGenerator()->create_user(); + $course = self::getDataGenerator()->create_course(); + + $forum = self::getDataGenerator()->create_module('forum', (object)[ + 'course' => $course->id, + ]); + + $generator = self::getDataGenerator()->get_plugin_generator('mod_forum'); + + // Let the teacher start a discussion. + $discussion = $generator->create_discussion((object)[ + 'course' => $course->id, + 'userid' => $teacher->id, + 'forum' => $forum->id, + ]); + + // On this freshly created discussion, the teacher is the author of the last post. + $this->assertEquals($teacher->id, $DB->get_field('forum_discussions', 'usermodified', ['id' => $discussion->id])); + + // Let the student reply to the teacher's post. + $reply = $generator->create_post((object)[ + 'course' => $course->id, + 'userid' => $student->id, + 'forum' => $forum->id, + 'discussion' => $discussion->id, + 'parent' => $discussion->firstpost, + ]); + + // The student should now be the last post's author. + $this->assertEquals($student->id, $DB->get_field('forum_discussions', 'usermodified', ['id' => $discussion->id])); + + // Let the teacher edit the student's reply. + $this->setUser($teacher->id); + $newpost = (object)[ + 'id' => $reply->id, + 'itemid' => 0, + 'subject' => 'Amended subject', + ]; + forum_update_post($newpost, null); + + // The student should be still the last post's author. + $this->assertEquals($student->id, $DB->get_field('forum_discussions', 'usermodified', ['id' => $discussion->id])); + } } -- 2.43.0