Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Tests for private reply functionality. 19 * 20 * @package mod_forum 21 * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 global $CFG; 28 require_once($CFG->dirroot . '/mod/forum/lib.php'); 29 require_once($CFG->dirroot . '/mod/forum/locallib.php'); 30 require_once (__DIR__ . '/generator_trait.php'); 31 32 /** 33 * Tests for private reply functionality. 34 * 35 * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk> 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class private_replies extends advanced_testcase { 39 40 use mod_forum_tests_generator_trait; 41 42 /** 43 * Setup before tests. 44 */ 45 public function setUp(): void { 46 // We must clear the subscription caches. This has to be done both before each test, and after in case of other 47 // tests using these functions. 48 \mod_forum\subscriptions::reset_forum_cache(); 49 } 50 51 /** 52 * Tear down after tests. 53 */ 54 public function tearDown(): void { 55 // We must clear the subscription caches. This has to be done both before each test, and after in case of other 56 // tests using these functions. 57 \mod_forum\subscriptions::reset_forum_cache(); 58 } 59 60 /** 61 * Ensure that the forum_post_is_visible_privately function reports that a post is visible to a user when another 62 * user wrote the post, and it is not private. 63 */ 64 public function test_forum_post_is_visible_privately_not_private() { 65 $this->resetAfterTest(); 66 67 $course = $this->getDataGenerator()->create_course(); 68 $forum = $this->getDataGenerator()->create_module('forum', [ 69 'course' => $course->id, 70 ]); 71 72 [$student] = $this->helper_create_users($course, 1, 'student'); 73 [$teacher] = $this->helper_create_users($course, 1, 'teacher'); 74 [$discussion] = $this->helper_post_to_forum($forum, $teacher); 75 $post = $this->helper_post_to_discussion($forum, $discussion, $teacher); 76 77 $this->setUser($student); 78 $cm = get_coursemodule_from_instance('forum', $forum->id); 79 $this->assertTrue(forum_post_is_visible_privately($post, $cm)); 80 } 81 82 /** 83 * Ensure that the forum_post_is_visible_privately function reports that a post is visible to a user when another 84 * user wrote the post, and the user under test is the intended recipient. 85 */ 86 public function test_forum_post_is_visible_privately_private_to_user() { 87 $this->resetAfterTest(); 88 89 $course = $this->getDataGenerator()->create_course(); 90 $forum = $this->getDataGenerator()->create_module('forum', [ 91 'course' => $course->id, 92 ]); 93 94 [$student] = $this->helper_create_users($course, 1, 'student'); 95 [$teacher] = $this->helper_create_users($course, 1, 'teacher'); 96 [$discussion] = $this->helper_post_to_forum($forum, $teacher); 97 $post = $this->helper_post_to_discussion($forum, $discussion, $teacher, [ 98 'privatereplyto' => $student->id, 99 ]); 100 101 $this->setUser($student); 102 $cm = get_coursemodule_from_instance('forum', $forum->id); 103 $this->assertTrue(forum_post_is_visible_privately($post, $cm)); 104 } 105 106 /** 107 * Ensure that the forum_post_is_visible_privately function reports that a post is visible to a user when another 108 * user wrote the post, and the user under test is a role with the view capability. 109 */ 110 public function test_forum_post_is_visible_privately_private_to_user_view_as_teacher() { 111 $this->resetAfterTest(); 112 113 $course = $this->getDataGenerator()->create_course(); 114 $forum = $this->getDataGenerator()->create_module('forum', [ 115 'course' => $course->id, 116 ]); 117 118 [$student] = $this->helper_create_users($course, 1, 'student'); 119 [$teacher, $otherteacher] = $this->helper_create_users($course, 2, 'teacher'); 120 [$discussion] = $this->helper_post_to_forum($forum, $teacher); 121 $post = $this->helper_post_to_discussion($forum, $discussion, $teacher, [ 122 'privatereplyto' => $student->id, 123 ]); 124 125 $this->setUser($otherteacher); 126 $cm = get_coursemodule_from_instance('forum', $forum->id); 127 $this->assertTrue(forum_post_is_visible_privately($post, $cm)); 128 } 129 130 /** 131 * Ensure that the forum_post_is_visible_privately function reports that a post is not visible to a user when 132 * another user wrote the post, and the user under test is a role without the view capability. 133 */ 134 public function test_forum_post_is_visible_privately_private_to_user_view_as_other_student() { 135 $this->resetAfterTest(); 136 137 $course = $this->getDataGenerator()->create_course(); 138 $forum = $this->getDataGenerator()->create_module('forum', [ 139 'course' => $course->id, 140 ]); 141 142 [$student, $otherstudent] = $this->helper_create_users($course, 2, 'student'); 143 [$teacher] = $this->helper_create_users($course, 1, 'teacher'); 144 [$discussion] = $this->helper_post_to_forum($forum, $teacher); 145 $post = $this->helper_post_to_discussion($forum, $discussion, $teacher, [ 146 'privatereplyto' => $student->id, 147 ]); 148 149 $this->setUser($otherstudent); 150 $cm = get_coursemodule_from_instance('forum', $forum->id); 151 $this->assertFalse(forum_post_is_visible_privately($post, $cm)); 152 } 153 154 /** 155 * Ensure that the forum_post_is_visible_privately function reports that a post is visible to a user who wrote a 156 * private reply, but not longer holds the view capability. 157 */ 158 public function test_forum_post_is_visible_privately_private_to_user_view_as_author() { 159 $this->resetAfterTest(); 160 161 $course = $this->getDataGenerator()->create_course(); 162 $forum = $this->getDataGenerator()->create_module('forum', [ 163 'course' => $course->id, 164 ]); 165 166 [$student] = $this->helper_create_users($course, 1, 'student'); 167 [$teacher] = $this->helper_create_users($course, 1, 'teacher'); 168 [$discussion] = $this->helper_post_to_forum($forum, $teacher); 169 $post = $this->helper_post_to_discussion($forum, $discussion, $teacher, [ 170 'privatereplyto' => $student->id, 171 ]); 172 173 unassign_capability('mod/forum:readprivatereplies', $this->get_role_id('teacher')); 174 175 $this->setUser($teacher); 176 $cm = get_coursemodule_from_instance('forum', $forum->id); 177 $this->assertTrue(forum_post_is_visible_privately($post, $cm)); 178 } 179 180 /** 181 * Ensure that the forum_user_can_reply_privately returns true for a teacher replying to a forum post. 182 */ 183 public function test_forum_user_can_reply_privately_as_teacher() { 184 $this->resetAfterTest(); 185 186 $course = $this->getDataGenerator()->create_course(); 187 $forum = $this->getDataGenerator()->create_module('forum', [ 188 'course' => $course->id, 189 ]); 190 191 [$student] = $this->helper_create_users($course, 1, 'student'); 192 [$teacher] = $this->helper_create_users($course, 1, 'teacher'); 193 [, $post] = $this->helper_post_to_forum($forum, $student); 194 195 $this->setUser($teacher); 196 $cm = get_coursemodule_from_instance('forum', $forum->id); 197 $context = \context_module::instance($cm->id); 198 $this->assertTrue(forum_user_can_reply_privately($context, $post)); 199 } 200 201 /** 202 * Ensure that the forum_user_can_reply_privately returns true for a teacher replying to a forum post. 203 */ 204 public function test_forum_user_can_reply_privately_as_student() { 205 $this->resetAfterTest(); 206 207 $course = $this->getDataGenerator()->create_course(); 208 $forum = $this->getDataGenerator()->create_module('forum', [ 209 'course' => $course->id, 210 ]); 211 212 [$student, $otherstudent] = $this->helper_create_users($course, 2, 'student'); 213 [, $post] = $this->helper_post_to_forum($forum, $student); 214 215 $this->setUser($otherstudent); 216 $cm = get_coursemodule_from_instance('forum', $forum->id); 217 $context = \context_module::instance($cm->id); 218 $this->assertFalse(forum_user_can_reply_privately($context, $post)); 219 } 220 221 /** 222 * Ensure that the forum_user_can_reply_privately returns false where the parent post is already a private reply. 223 */ 224 public function test_forum_user_can_reply_privately_parent_is_already_private() { 225 $this->resetAfterTest(); 226 227 $course = $this->getDataGenerator()->create_course(); 228 $forum = $this->getDataGenerator()->create_module('forum', [ 229 'course' => $course->id, 230 ]); 231 232 [$student] = $this->helper_create_users($course, 1, 'student'); 233 [$teacher] = $this->helper_create_users($course, 1, 'teacher'); 234 [$discussion] = $this->helper_post_to_forum($forum, $student); 235 $post = $this->helper_post_to_discussion($forum, $discussion, $teacher, ['privatereplyto' => $student->id]); 236 237 $this->setUser($teacher); 238 $cm = get_coursemodule_from_instance('forum', $forum->id); 239 $context = \context_module::instance($cm->id); 240 $this->assertFalse(forum_user_can_reply_privately($context, $post)); 241 } 242 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body