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_qanda_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       * Test that a user who has not posted in a q&a forum does not receive
  91       * notificatinos.
  92       */
  93      public function test_user_has_not_posted() {
  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              'type' => 'qanda',
 106          ]);
 107  
 108          // Create three students:
 109          // - author, enrolled in group A; and
 110          // - recipient, enrolled in group B; and
 111          // - other, enrolled in the course, but no groups.
 112          list($author, $recipient, $otheruser) = $this->helper_create_users($course, 3);
 113  
 114          // Create one editing teacher, not in any group but with accessallgroups capability.
 115          list($editingteacher) = $this->helper_create_users($course, 1, 'editingteacher');
 116  
 117          // Post a discussion to the forum.
 118          list($discussion, $post) = $this->helper_post_to_forum($forum, $editingteacher);
 119          $reply = $this->helper_reply_to_post($post, $author);
 120          $otherreply = $this->helper_reply_to_post($post, $recipient);
 121          $DB->execute("UPDATE {forum_posts} SET modified = modified - 1");
 122          $DB->execute("UPDATE {forum_posts} SET created = created - 1");
 123          $DB->execute("UPDATE {forum_discussions} SET timemodified = timemodified - 1");
 124  
 125          // Only the author, recipient, and teachers should receive.
 126          $expect = [
 127              'author' => (object) [
 128                  'userid' => $author->id,
 129                  'messages' => 3,
 130              ],
 131              'recipient' => (object) [
 132                  'userid' => $recipient->id,
 133                  'messages' => 3,
 134              ],
 135              'otheruser' => (object) [
 136                  'userid' => $otheruser->id,
 137                  'messages' => 3,
 138              ],
 139              'editingteacher' => (object) [
 140                  'userid' => $editingteacher->id,
 141                  'messages' => 3,
 142              ],
 143          ];
 144          $this->queue_tasks_and_assert($expect);
 145          $posts = [$post, $reply, $otherreply];
 146  
 147          // No notifications should be queued.
 148          $this->send_notifications_and_assert($author, $posts);
 149          $this->send_notifications_and_assert($recipient, $posts);
 150          $this->send_notifications_and_assert($otheruser, [$post]);
 151          $this->send_notifications_and_assert($editingteacher, $posts);
 152      }
 153  }