Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

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