MDL-65569 mod_forum: Correct rules for SSD
authorAndrew Nicols <andrew@nicols.co.uk>
Tue, 14 May 2019 01:56:33 +0000 (09:56 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Tue, 14 May 2019 04:32:05 +0000 (12:32 +0800)
In a Single Simple Discussion forum, the first post can only be edited
by a user with the manageactivities capability, but all other posts
behave as normal.

mod/forum/classes/local/factories/url.php
mod/forum/classes/local/managers/capability.php
mod/forum/tests/managers_capability_test.php

index f7141b3..c66335f 100644 (file)
@@ -247,7 +247,7 @@ class url {
      * @return moodle_url
      */
     public function get_edit_post_url_from_post(forum_entity $forum, post_entity $post) : moodle_url {
-        if ($forum->get_type() == 'single') {
+        if ($forum->get_type() == 'single' && !$post->has_parent()) {
             return new moodle_url('/course/modedit.php', [
                 'update' => $forum->get_course_module_record()->id,
                 'sesskey' => sesskey(),
index 5709c71..76ba7c1 100644 (file)
@@ -420,7 +420,10 @@ class capability {
                 $ineditingtime = !$post->has_parent() && $discussion->has_started();
                 break;
             case 'single':
-                return $discussion->is_first_post($post) && has_capability('moodle/course:manageactivities', $context, $user);
+                if ($discussion->is_first_post($post)) {
+                    return has_capability('moodle/course:manageactivities', $context, $user);
+                }
+                break;
         }
 
         return ($ownpost && $ineditingtime) || has_capability('mod/forum:editanypost', $context, $user);
index 9c5ffa7..2f8309d 100644 (file)
@@ -689,11 +689,13 @@ class mod_forum_managers_capability_testcase extends advanced_testcase {
         $capabilitymanager = $this->managerfactory->get_capability_manager($forum);
 
         // Create a new post that definitely isn't the first post of the discussion.
+        // Only the author, and a user with editanypost can edit it.
         $post = $this->entityfactory->get_post_from_stdclass(
             (object) array_merge((array) $this->postrecord, ['id' => $post->get_id() + 100])
         );
-
-        $this->assertFalse($capabilitymanager->can_edit_post($user, $discussion, $post));
+        $this->give_capability('mod/forum:editanypost');
+        $this->assertTrue($capabilitymanager->can_edit_post($user, $discussion, $post));
+        $this->assertFalse($capabilitymanager->can_edit_post($otheruser, $discussion, $post));
 
         $post = $this->post;
         // Set the first post of the discussion to our post.