Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

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