Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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   * The forum module mail generation tests for groups.
  19   *
  20   * @package    mod_forum
  21   * @copyright  2013 Andrew Nicols
  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 (__DIR__ . '/cron_trait.php');
  30  require_once (__DIR__ . '/generator_trait.php');
  31  
  32  /**
  33   * The forum module mail generation tests for groups.
  34   *
  35   * @copyright  2013 Andrew Nicols
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class mod_forum_mail_group_testcase extends advanced_testcase {
  39      // Make use of the cron tester trait.
  40      use mod_forum_tests_cron_trait;
  41  
  42      // Make use of the test generator trait.
  43      use mod_forum_tests_generator_trait;
  44  
  45      /**
  46       * @var \phpunit_message_sink
  47       */
  48      protected $messagesink;
  49  
  50      /**
  51       * @var \phpunit_mailer_sink
  52       */
  53      protected $mailsink;
  54  
  55      public function setUp(): void {
  56          global $CFG;
  57  
  58          // We must clear the subscription caches. This has to be done both before each test, and after in case of other
  59          // tests using these functions.
  60          \mod_forum\subscriptions::reset_forum_cache();
  61          \mod_forum\subscriptions::reset_discussion_cache();
  62  
  63          // Messaging is not compatible with transactions...
  64          $this->preventResetByRollback();
  65  
  66          // Catch all messages.
  67          $this->messagesink = $this->redirectMessages();
  68          $this->mailsink = $this->redirectEmails();
  69  
  70          // Forcibly reduce the maxeditingtime to a second in the past to
  71          // ensure that messages are sent out.
  72          $CFG->maxeditingtime = -1;
  73      }
  74  
  75      public function tearDown(): void {
  76          // We must clear the subscription caches. This has to be done both before each test, and after in case of other
  77          // tests using these functions.
  78          \mod_forum\subscriptions::reset_forum_cache();
  79  
  80          $this->messagesink->clear();
  81          $this->messagesink->close();
  82          unset($this->messagesink);
  83  
  84          $this->mailsink->clear();
  85          $this->mailsink->close();
  86          unset($this->mailsink);
  87      }
  88  
  89      /**
  90       * Ensure that posts written in a forum marked for separate groups includes notifications for the members of that
  91       * group, and any user with accessallgroups.
  92       */
  93      public function test_separate_group() {
  94          global $CFG, $DB;
  95  
  96          $this->resetAfterTest(true);
  97  
  98          // Create a course, with a forum.
  99          $course = $this->getDataGenerator()->create_course();
 100  
 101          $forum = $this->getDataGenerator()->create_module('forum', [
 102              'course' => $course->id,
 103              'forcesubscribe' => FORUM_INITIALSUBSCRIBE,
 104              'groupmode' => SEPARATEGROUPS,
 105          ]);
 106  
 107          // Create three students:
 108          // - author, enrolled in group A; and
 109          // - recipient, enrolled in group B; and
 110          // - other, enrolled in the course, but no groups.
 111          list($author, $recipient, $otheruser) = $this->helper_create_users($course, 3);
 112  
 113          // Create one teacher, not in any group and no accessallgroups capability.
 114          list($teacher) = $this->helper_create_users($course, 1, 'teacher');
 115  
 116          // Create one editing teacher, not in any group but with accessallgroups capability.
 117          list($editingteacher) = $this->helper_create_users($course, 1, 'editingteacher');
 118  
 119          $groupa = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
 120          $groupb = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
 121          $this->getDataGenerator()->create_group_member([
 122              'groupid' => $groupa->id,
 123              'userid' => $author->id,
 124          ]);
 125          $this->getDataGenerator()->create_group_member([
 126              'groupid' => $groupb->id,
 127              'userid' => $recipient->id,
 128          ]);
 129  
 130          // Post a discussion to the forum.
 131          list($discussion, $post) = $this->helper_post_to_forum($forum, $author, [
 132              'groupid' => $groupa->id,
 133          ]);
 134  
 135          // Only the author should receive.
 136          $expect = [
 137              'author' => (object) [
 138                  'userid' => $author->id,
 139                  'messages' => 1,
 140              ],
 141              'recipient' => (object) [
 142                  'userid' => $recipient->id,
 143                  'messages' => 0,
 144              ],
 145              'otheruser' => (object) [
 146                  'userid' => $otheruser->id,
 147                  'messages' => 0,
 148              ],
 149              'teacher' => (object) [
 150                  'userid' => $teacher->id,
 151                  'messages' => 0,
 152              ],
 153              'editingteacher' => (object) [
 154                  'userid' => $editingteacher->id,
 155                  'messages' => 1,
 156              ],
 157          ];
 158          $this->queue_tasks_and_assert($expect);
 159  
 160          // No notifications should be queued.
 161          $this->send_notifications_and_assert($author, [$post]);
 162          $this->send_notifications_and_assert($recipient, []);
 163          $this->send_notifications_and_assert($otheruser, []);
 164          $this->send_notifications_and_assert($teacher, []);
 165          $this->send_notifications_and_assert($editingteacher, [$post]);
 166      }
 167  
 168      /**
 169       * Ensure that posts written in a forum marked for visible groups includes notifications for the members of that
 170       * group, and any user with accessallgroups.
 171       */
 172      public function test_visible_group() {
 173          global $CFG, $DB;
 174  
 175          $this->resetAfterTest(true);
 176  
 177          // Create a course, with a forum.
 178          $course = $this->getDataGenerator()->create_course();
 179  
 180          $forum = $this->getDataGenerator()->create_module('forum', [
 181              'course' => $course->id,
 182              'forcesubscribe' => FORUM_INITIALSUBSCRIBE,
 183              'groupmode' => VISIBLEGROUPS,
 184          ]);
 185  
 186          // Create three students:
 187          // - author, enrolled in group A; and
 188          // - recipient, enrolled in group B; and
 189          // - other, enrolled in the course, but no groups.
 190          list($author, $recipient, $otheruser) = $this->helper_create_users($course, 3);
 191  
 192          // Create one teacher, not in any group and no accessallgroups capability.
 193          list($teacher) = $this->helper_create_users($course, 1, 'teacher');
 194  
 195          // Create one editing teacher, not in any group but with accessallgroups capability.
 196          list($editingteacher) = $this->helper_create_users($course, 1, 'editingteacher');
 197  
 198          $groupa = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
 199          $groupb = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
 200          $this->getDataGenerator()->create_group_member([
 201              'groupid' => $groupa->id,
 202              'userid' => $author->id,
 203          ]);
 204          $this->getDataGenerator()->create_group_member([
 205              'groupid' => $groupb->id,
 206              'userid' => $recipient->id,
 207          ]);
 208  
 209          // Post a discussion to the forum.
 210          list($discussion, $post) = $this->helper_post_to_forum($forum, $author, [
 211              'groupid' => $groupa->id,
 212          ]);
 213  
 214          // Only the author should receive.
 215          $expect = [
 216              'author' => (object) [
 217                  'userid' => $author->id,
 218                  'messages' => 1,
 219              ],
 220              'recipient' => (object) [
 221                  'userid' => $recipient->id,
 222                  'messages' => 0,
 223              ],
 224              'otheruser' => (object) [
 225                  'userid' => $otheruser->id,
 226                  'messages' => 0,
 227              ],
 228              'teacher' => (object) [
 229                  'userid' => $teacher->id,
 230                  'messages' => 0,
 231              ],
 232              'editingteacher' => (object) [
 233                  'userid' => $editingteacher->id,
 234                  'messages' => 1,
 235              ],
 236          ];
 237          $this->queue_tasks_and_assert($expect);
 238  
 239          // No notifications should be queued.
 240          $this->send_notifications_and_assert($author, [$post]);
 241          $this->send_notifications_and_assert($recipient, []);
 242          $this->send_notifications_and_assert($otheruser, []);
 243          $this->send_notifications_and_assert($teacher, []);
 244          $this->send_notifications_and_assert($editingteacher, [$post]);
 245      }
 246  }