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 discussion entity tests.
  19   *
  20   * @package    mod_forum
  21   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use mod_forum\local\entities\discussion as discussion_entity;
  28  use mod_forum\local\entities\post as post_entity;
  29  
  30  /**
  31   * The discussion entity tests.
  32   *
  33   * @package    mod_forum
  34   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class mod_forum_entities_discussion_testcase extends advanced_testcase {
  38      /**
  39       * Test the entity returns expected values.
  40       */
  41      public function test_entity() {
  42          $this->resetAfterTest();
  43  
  44          // In the past to ensure the time started is true.
  45          $time = time() + 10;
  46          $discussion = new discussion_entity(
  47              1,
  48              2,
  49              3,
  50              'test discussion',
  51              4,
  52              5,
  53              6,
  54              false,
  55              $time,
  56              $time,
  57              0,
  58              0,
  59              false,
  60              0
  61          );
  62          $firstpost = new post_entity(
  63              4,
  64              1,
  65              0,
  66              1,
  67              time(),
  68              time(),
  69              true,
  70              'post subject',
  71              'post message',
  72              1,
  73              true,
  74              false,
  75              0,
  76              false,
  77              false,
  78              false,
  79              null,
  80              null
  81          );
  82          $notfirstpost = new post_entity(
  83              1,
  84              1,
  85              0,
  86              1,
  87              time(),
  88              time(),
  89              true,
  90              'post subject',
  91              'post message',
  92              1,
  93              true,
  94              false,
  95              0,
  96              false,
  97              false,
  98              false,
  99              null,
 100              null
 101          );
 102  
 103          $this->assertEquals(1, $discussion->get_id());
 104          $this->assertEquals(2, $discussion->get_course_id());
 105          $this->assertEquals(3, $discussion->get_forum_id());
 106          $this->assertEquals('test discussion', $discussion->get_name());
 107          $this->assertEquals(4, $discussion->get_first_post_id());
 108          $this->assertEquals(5, $discussion->get_user_id());
 109          $this->assertEquals(6, $discussion->get_group_id());
 110          $this->assertEquals(false, $discussion->is_assessed());
 111          $this->assertEquals($time, $discussion->get_time_modified());
 112          $this->assertEquals($time, $discussion->get_user_modified());
 113          $this->assertEquals(0, $discussion->get_time_start());
 114          $this->assertEquals(0, $discussion->get_time_end());
 115          $this->assertEquals(false, $discussion->is_pinned());
 116          $this->assertEquals(true, $discussion->is_first_post($firstpost));
 117          $this->assertEquals(false, $discussion->is_first_post($notfirstpost));
 118          $this->assertEquals(true, $discussion->has_started());
 119          $this->assertEquals(true, $discussion->has_group());
 120      }
 121  
 122      /**
 123       * Test the display period settings for discussions.
 124       * This covers each individual date function as well as the combination of the 2.
 125       *
 126       * @dataProvider test_diplay_period_options_provider
 127       * @param string $testdescription A basic description of the base assertions.
 128       * @param int $startoffset Start time offset with current time in seconds.
 129       * @param int $endoffset End time offset with current time in seconds.
 130       * @param bool $timestartresult Expected result from the has_started function
 131       * @param bool $timeendresult Expected result from the has_ended function
 132       * @param bool $isvisible Expected result from the is_timed_discussion_visible function
 133       */
 134      public function test_display_period_settings($testdescription, $startoffset, $endoffset,
 135                                                   $timestartresult, $timeendresult, $isvisible) {
 136          global $CFG;
 137          $this->resetAfterTest();
 138  
 139          $basetime = time();
 140          $starttime = $startoffset != 0 ? $basetime + $startoffset : 0;
 141          $endtime = $endoffset != 0 ? $basetime + $endoffset : 0;
 142          $discussion = new discussion_entity(
 143              1,
 144              2,
 145              3,
 146              'test discussion',
 147              4,
 148              5,
 149              6,
 150              false,
 151              $basetime,
 152              $basetime,
 153              $starttime,
 154              $endtime,
 155              false,
 156              0
 157          );
 158          $CFG->forum_enabletimedposts = true;
 159  
 160          $this->assertEquals($timestartresult, $discussion->has_started(), $testdescription);
 161          $this->assertEquals($timeendresult, $discussion->has_ended(), $testdescription);
 162          $this->assertEquals($isvisible, $discussion->is_timed_discussion_visible(), $testdescription);
 163      }
 164  
 165      /**
 166       * Data provider for test_display_period_settings().
 167       *
 168       * @return array start/end time offsets and the expected results.
 169       */
 170      public function test_diplay_period_options_provider() {
 171          return array(
 172              ["No dates set", 0, 0, true, false, true],
 173              ["Only started date in the future", 100, 0, false, false, false],
 174              ["Only started date in the past", -100, 0, true, false, true],
 175              ["Only end date in the future", 0, 100, true, false, true],
 176              ["Only end date in the past", 0, -100, true, true, false],
 177              ["Start date in the past, end date in the future", -100, 100, true, false, true],
 178              ["Both dates in the past", -100, -50, true, true, false],
 179              ["Both dates in the future", 100, 150, false, false, false],
 180          );
 181      }
 182  }