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 311 and 402] [Versions 311 and 403] [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  /**
  18   * The forum module cron trait.
  19   *
  20   * @package    mod_forum
  21   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  trait mod_forum_tests_cron_trait {
  28      /**
  29       * Run the main cron task to queue all tasks, and ensure that posts
  30       * were sent to the correct users.
  31       *
  32       * @param   \stdClass[] $expectations The list of users, along with their expected count of messages and digests.
  33       */
  34      protected function queue_tasks_and_assert($expectations = []) {
  35          global $DB;
  36  
  37          // Note, we cannot use expectOutputRegex because it only allows for a single RegExp.
  38          ob_start();
  39          cron_setup_user();
  40          $cron = new \mod_forum\task\cron_task();
  41          $cron->execute();
  42          $output = ob_get_contents();
  43          ob_end_clean();
  44  
  45          $uniqueusers = 0;
  46          foreach ($expectations as $expect) {
  47              $expect->digests = isset($expect->digests) ? $expect->digests : 0;
  48              $expect->messages = isset($expect->messages) ? $expect->messages : 0;
  49              $expect->mentioned = isset($expect->mentioned) ? $expect->mentioned : false;
  50              if ($expect->digests || $expect->messages) {
  51                  $expect->mentioned = true;
  52              }
  53              if (!$expect->mentioned) {
  54                  $this->assertDoesNotMatchRegularExpression("/Queued 0 for {$expect->userid}/", $output);
  55              } else {
  56                  $uniqueusers++;
  57                  $this->assertMatchesRegularExpression(
  58                          "/Queued {$expect->digests} digests and {$expect->messages} messages for {$expect->userid}/",
  59                          $output
  60                      );
  61              }
  62          }
  63  
  64          if (empty($expectations)) {
  65              $this->assertMatchesRegularExpression("/No posts found./", $output);
  66          } else {
  67              $this->assertMatchesRegularExpression("/Unique users: {$uniqueusers}/", $output);
  68          }
  69  
  70          // Update the forum queue for digests.
  71          $DB->execute("UPDATE {forum_queue} SET timemodified = timemodified - 1");
  72      }
  73  
  74      /**
  75       * Run any send_user_notifications tasks for the specified user, and
  76       * ensure that the posts specified were sent.
  77       *
  78       * @param   \stdClass   $user
  79       * @param   \stdClass[] $posts
  80       * @param   bool        $ignoreemptyposts
  81       */
  82      protected function send_notifications_and_assert($user, $posts = [], $ignoreemptyposts = false) {
  83          ob_start();
  84          $this->runAdhocTasks(\mod_forum\task\send_user_notifications::class, $user->id);
  85          $output = ob_get_contents();
  86          ob_end_clean();
  87  
  88          if (empty($posts) && !$ignoreemptyposts) {
  89              $this->assertEquals('', $output);
  90          } else {
  91              $this->assertMatchesRegularExpression("/Sending messages to {$user->username}/", $output);
  92              foreach ($posts as $post) {
  93                  $this->assertMatchesRegularExpression("/Post {$post->id} sent/", $output);
  94              }
  95              $count = count($posts);
  96              $this->assertMatchesRegularExpression("/Sent {$count} messages with 0 failures/", $output);
  97          }
  98      }
  99  
 100      /**
 101       * Run any send_user_digests tasks for the specified user, and
 102       * ensure that the posts specified were sent.
 103       *
 104       * @param   \stdClass   $user
 105       * @param   \stdClass[] $fullposts
 106       * @param   \stdClass[] $shortposts
 107       */
 108      protected function send_digests_and_assert($user, $fullposts = [], $shortposts = []) {
 109          ob_start();
 110          $this->runAdhocTasks(\mod_forum\task\send_user_digests::class, $user->id);
 111          $output = ob_get_contents();
 112          ob_end_clean();
 113  
 114          if (empty($shortposts) && empty($fullposts)) {
 115              $this->assertEquals('', $output);
 116              $this->assertMatchesRegularExpression("/Digest sent with 0 messages./", $output);
 117          } else {
 118              $this->assertMatchesRegularExpression("/Sending forum digests for {$user->username}/", $output);
 119              foreach ($fullposts as $post) {
 120                  $this->assertMatchesRegularExpression("/Adding post {$post->id} in format 1/", $output);
 121              }
 122              foreach ($shortposts as $post) {
 123                  $this->assertMatchesRegularExpression("/Adding post {$post->id} in format 2/", $output);
 124              }
 125              $count = count($fullposts) + count($shortposts);
 126              $this->assertMatchesRegularExpression("/Digest sent with {$count} messages./", $output);
 127          }
 128      }
 129  }