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 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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   * Contains unit tests for mod_forum\dates.
  19   *
  20   * @package   mod_forum
  21   * @category  test
  22   * @copyright 2021 Shamim Rezaie <shamim@moodle.com>
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  declare(strict_types=1);
  27  
  28  namespace mod_forum;
  29  
  30  use advanced_testcase;
  31  use cm_info;
  32  use core\activity_dates;
  33  
  34  /**
  35   * Class for unit testing mod_forum\dates.
  36   *
  37   * @copyright 2021 Shamim Rezaie <shamim@moodle.com>
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class dates_test extends advanced_testcase {
  41  
  42      /**
  43       * Data provider for get_dates_for_module().
  44       * @return array[]
  45       */
  46      public function get_dates_for_module_provider(): array {
  47          $now = time();
  48          $before = $now - DAYSECS;
  49          $after = $now + DAYSECS;
  50  
  51          return [
  52              'without any dates' => [
  53                  null, []
  54              ],
  55              'future due date' => [
  56                  $after, [
  57                      ['label' => 'Due:', 'timestamp' => $after],
  58                  ]
  59              ],
  60              'due date is past' => [
  61                  $before, [
  62                      ['label' => 'Due:', 'timestamp' => $before],
  63                  ]
  64              ],
  65          ];
  66      }
  67  
  68      /**
  69       * Test for get_dates_for_module().
  70       *
  71       * @dataProvider get_dates_for_module_provider
  72       * @param int|null $duedate Forum's due date.
  73       * @param array $expected The expected value of calling get_dates_for_module()
  74       */
  75      public function test_get_dates_for_module(?int $duedate, array $expected) {
  76          $this->resetAfterTest();
  77  
  78          $course = $this->getDataGenerator()->create_course();
  79          $user = $this->getDataGenerator()->create_user();
  80          $this->getDataGenerator()->enrol_user($user->id, $course->id);
  81  
  82          $data = ['course' => $course->id];
  83          if ($duedate) {
  84              $data['duedate'] = $duedate;
  85          }
  86  
  87          $this->setAdminUser();
  88          $forum = $this->getDataGenerator()->create_module('forum', $data);
  89  
  90          $this->setUser($user);
  91  
  92          $cm = get_coursemodule_from_instance('forum', $forum->id);
  93          // Make sure we're using a cm_info object.
  94          $cm = cm_info::create($cm);
  95  
  96          $dates = activity_dates::get_dates_for_module($cm, (int) $user->id);
  97  
  98          $this->assertEquals($expected, $dates);
  99      }
 100  }