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