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   * @package    mod_forum
  33   * @copyright  2013 Andrew Nicols
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class qanda_test extends \advanced_testcase {
  37      // Make use of the cron tester trait.
  38      use mod_forum_tests_cron_trait;
  39  
  40      // Make use of the test generator trait.
  41      use mod_forum_tests_generator_trait;
  42  
  43      /**
  44       * @var \phpunit_message_sink
  45       */
  46      protected $messagesink;
  47  
  48      /**
  49       * @var \phpunit_mailer_sink
  50       */
  51      protected $mailsink;
  52  
  53      public function setUp(): void {
  54          global $CFG;
  55  
  56          // We must clear the subscription caches. This has to be done both before each test, and after in case of other
  57          // tests using these functions.
  58          \mod_forum\subscriptions::reset_forum_cache();
  59          \mod_forum\subscriptions::reset_discussion_cache();
  60  
  61          // Messaging is not compatible with transactions...
  62          $this->preventResetByRollback();
  63  
  64          // Catch all messages.
  65          $this->messagesink = $this->redirectMessages();
  66          $this->mailsink = $this->redirectEmails();
  67  
  68          // Forcibly reduce the maxeditingtime to a second in the past to
  69          // ensure that messages are sent out.
  70          $CFG->maxeditingtime = -1;
  71      }
  72  
  73      public function tearDown(): void {
  74          // We must clear the subscription caches. This has to be done both before each test, and after in case of other
  75          // tests using these functions.
  76          \mod_forum\subscriptions::reset_forum_cache();
  77  
  78          $this->messagesink->clear();
  79          $this->messagesink->close();
  80          unset($this->messagesink);
  81  
  82          $this->mailsink->clear();
  83          $this->mailsink->close();
  84          unset($this->mailsink);
  85      }
  86  
  87      /**
  88       * Test that a user who has not posted in a q&a forum does not receive
  89       * notificatinos.
  90       */
  91      public function test_user_has_not_posted() {
  92          global $CFG, $DB;
  93  
  94          $this->resetAfterTest(true);
  95  
  96          // Create a course, with a forum.
  97          $course = $this->getDataGenerator()->create_course();
  98  
  99          $forum = $this->getDataGenerator()->create_module('forum', [
 100              'course' => $course->id,
 101              'forcesubscribe' => FORUM_INITIALSUBSCRIBE,
 102              'groupmode' => SEPARATEGROUPS,
 103              'type' => 'qanda',
 104          ]);
 105  
 106          // Create three students:
 107          // - author, enrolled in group A; and
 108          // - recipient, enrolled in group B; and
 109          // - other, enrolled in the course, but no groups.
 110          list($author, $recipient, $otheruser) = $this->helper_create_users($course, 3);
 111  
 112          // Create one editing teacher, not in any group but with accessallgroups capability.
 113          list($editingteacher) = $this->helper_create_users($course, 1, 'editingteacher');
 114  
 115          // Post a discussion to the forum.
 116          list($discussion, $post) = $this->helper_post_to_forum($forum, $editingteacher);
 117          $reply = $this->helper_reply_to_post($post, $author);
 118          $otherreply = $this->helper_reply_to_post($post, $recipient);
 119          $DB->execute("UPDATE {forum_posts} SET modified = modified - 1");
 120          $DB->execute("UPDATE {forum_posts} SET created = created - 1");
 121          $DB->execute("UPDATE {forum_discussions} SET timemodified = timemodified - 1");
 122  
 123          // Only the author, recipient, and teachers should receive.
 124          $expect = [
 125              'author' => (object) [
 126                  'userid' => $author->id,
 127                  'messages' => 3,
 128              ],
 129              'recipient' => (object) [
 130                  'userid' => $recipient->id,
 131                  'messages' => 3,
 132              ],
 133              'otheruser' => (object) [
 134                  'userid' => $otheruser->id,
 135                  'messages' => 3,
 136              ],
 137              'editingteacher' => (object) [
 138                  'userid' => $editingteacher->id,
 139                  'messages' => 3,
 140              ],
 141          ];
 142          $this->queue_tasks_and_assert($expect);
 143          $posts = [$post, $reply, $otherreply];
 144  
 145          // No notifications should be queued.
 146          $this->send_notifications_and_assert($author, $posts);
 147          $this->send_notifications_and_assert($recipient, $posts);
 148          $this->send_notifications_and_assert($otheruser, [$post]);
 149          $this->send_notifications_and_assert($editingteacher, $posts);
 150      }
 151  }