Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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\output\forum_post_email;
  20  
  21  /**
  22   * Tests for the forum output/email class.
  23   *
  24   * @package    mod_forum
  25   * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class output_email_test extends \advanced_testcase {
  29      /**
  30       * Data provider for the postdate function tests.
  31       */
  32      public function postdate_provider() {
  33          return array(
  34              'Timed discussions disabled, timestart unset' => array(
  35                  'globalconfig'      => array(
  36                      'forum_enabletimedposts' => 0,
  37                  ),
  38                  'forumconfig'       => array(
  39                  ),
  40                  'postconfig'        => array(
  41                      'modified'  => 1000,
  42                  ),
  43                  'discussionconfig'  => array(
  44                  ),
  45                  'expectation'       => 1000,
  46              ),
  47              'Timed discussions disabled, timestart set and newer' => array(
  48                  'globalconfig'      => array(
  49                      'forum_enabletimedposts' => 0,
  50                  ),
  51                  'forumconfig'       => array(
  52                  ),
  53                  'postconfig'        => array(
  54                      'modified'  => 1000,
  55                  ),
  56                  'discussionconfig'  => array(
  57                      'timestart' => 2000,
  58                  ),
  59                  'expectation'       => 1000,
  60              ),
  61              'Timed discussions disabled, timestart set but older' => array(
  62                  'globalconfig'      => array(
  63                      'forum_enabletimedposts' => 0,
  64                  ),
  65                  'forumconfig'       => array(
  66                  ),
  67                  'postconfig'        => array(
  68                      'modified'  => 1000,
  69                  ),
  70                  'discussionconfig'  => array(
  71                      'timestart' => 500,
  72                  ),
  73                  'expectation'       => 1000,
  74              ),
  75              'Timed discussions enabled, timestart unset' => array(
  76                  'globalconfig'      => array(
  77                      'forum_enabletimedposts' => 1,
  78                  ),
  79                  'forumconfig'       => array(
  80                  ),
  81                  'postconfig'        => array(
  82                      'modified'  => 1000,
  83                  ),
  84                  'discussionconfig'  => array(
  85                  ),
  86                  'expectation'       => 1000,
  87              ),
  88              'Timed discussions enabled, timestart set and newer' => array(
  89                  'globalconfig'      => array(
  90                      'forum_enabletimedposts' => 1,
  91                  ),
  92                  'forumconfig'       => array(
  93                  ),
  94                  'postconfig'        => array(
  95                      'modified'  => 1000,
  96                  ),
  97                  'discussionconfig'  => array(
  98                      'timestart' => 2000,
  99                  ),
 100                  'expectation'       => 2000,
 101              ),
 102              'Timed discussions enabled, timestart set but older' => array(
 103                  'globalconfig'      => array(
 104                      'forum_enabletimedposts' => 1,
 105                  ),
 106                  'forumconfig'       => array(
 107                  ),
 108                  'postconfig'        => array(
 109                      'modified'  => 1000,
 110                  ),
 111                  'discussionconfig'  => array(
 112                      'timestart' => 500,
 113                  ),
 114                  'expectation'       => 1000,
 115              ),
 116          );
 117      }
 118  
 119      /**
 120       * Test for the forum email renderable postdate.
 121       *
 122       * @dataProvider postdate_provider
 123       *
 124       * @param array  $globalconfig      The configuration to set on $CFG
 125       * @param array  $forumconfig       The configuration for this forum
 126       * @param array  $postconfig        The configuration for this post
 127       * @param array  $discussionconfig  The configuration for this discussion
 128       * @param string $expectation       The expected date
 129       */
 130      public function test_postdate($globalconfig, $forumconfig, $postconfig, $discussionconfig, $expectation) {
 131          global $CFG, $DB;
 132          $this->resetAfterTest(true);
 133  
 134          // Apply the global configuration.
 135          foreach ($globalconfig as $key => $value) {
 136              $CFG->$key = $value;
 137          }
 138  
 139          // Create the fixture.
 140          $user = $this->getDataGenerator()->create_user();
 141          $course = $this->getDataGenerator()->create_course();
 142          $forum = $this->getDataGenerator()->create_module('forum', (object) array('course' => $course->id));
 143          $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
 144  
 145          $this->getDataGenerator()->enrol_user($user->id, $course->id);
 146  
 147          // Create a new discussion.
 148          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion(
 149              (object) array_merge($discussionconfig, array(
 150                  'course'    => $course->id,
 151                  'forum'     => $forum->id,
 152                  'userid'    => $user->id,
 153              )));
 154  
 155          // Apply the discussion configuration.
 156          // Some settings are ignored by the generator and must be set manually.
 157          $discussion = $DB->get_record('forum_discussions', array('id' => $discussion->id));
 158          foreach ($discussionconfig as $key => $value) {
 159              $discussion->$key = $value;
 160          }
 161          $DB->update_record('forum_discussions', $discussion);
 162  
 163          // Apply the post configuration.
 164          // Some settings are ignored by the generator and must be set manually.
 165          $post = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
 166          foreach ($postconfig as $key => $value) {
 167              $post->$key = $value;
 168          }
 169          $DB->update_record('forum_posts', $post);
 170  
 171          // Create the renderable.
 172          $renderable = new forum_post_email(
 173                  $course,
 174                  $cm,
 175                  $forum,
 176                  $discussion,
 177                  $post,
 178                  $user,
 179                  $user,
 180                  true
 181              );
 182  
 183          // Check the postdate matches our expectations.
 184          $this->assertEquals(userdate($expectation, "", \core_date::get_user_timezone($user)), $renderable->get_postdate());
 185      }
 186  }