Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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  declare(strict_types=1);
  18  
  19  namespace core_reportbuilder\local\filters;
  20  
  21  use advanced_testcase;
  22  use lang_string;
  23  use core_reportbuilder\local\report\filter;
  24  
  25  /**
  26   * Unit tests for duration report filter
  27   *
  28   * @package     core_reportbuilder
  29   * @covers      \core_reportbuilder\local\filters\base
  30   * @covers      \core_reportbuilder\local\filters\duration
  31   * @copyright   2021 Paul Holden <paulh@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class duration_test extends advanced_testcase {
  35  
  36      /**
  37       * Data provider for {@see test_get_sql_filter}
  38       *
  39       * @return array
  40       */
  41      public function get_sql_filter_provider(): array {
  42          return [
  43              'Any duration' =>
  44                  [duration::DURATION_ANY, true],
  45  
  46              // Maximum operator.
  47              'Maximum seconds non-match' =>
  48                  [duration::DURATION_MAXIMUM, false, HOURSECS, 1],
  49              'Maximum seconds match' =>
  50                  [duration::DURATION_MAXIMUM, true, HOURSECS * 3, 1],
  51              'Maximum minutes non-match' =>
  52                  [duration::DURATION_MAXIMUM, false, 60, MINSECS],
  53              'Maximum minutes match' =>
  54                  [duration::DURATION_MAXIMUM, true, 150, MINSECS],
  55              'Maximum hours non-match (float)' =>
  56                  [duration::DURATION_MAXIMUM, false, 0.5, HOURSECS],
  57              'Maximum hours non-match' =>
  58                  [duration::DURATION_MAXIMUM, false, 1, HOURSECS],
  59              'Maximum hours match (float)' =>
  60                  [duration::DURATION_MAXIMUM, true, 2.5, HOURSECS],
  61              'Maximum hours match' =>
  62                  [duration::DURATION_MAXIMUM, true, 3, HOURSECS],
  63  
  64              // Minimum operator.
  65              'Minimum seconds match' =>
  66                  [duration::DURATION_MINIMUM, true, HOURSECS, 1],
  67              'Minimum seconds non-match' =>
  68                  [duration::DURATION_MINIMUM, false, HOURSECS * 3, 1],
  69              'Minimum minutes match' =>
  70                  [duration::DURATION_MINIMUM, true, 60, MINSECS],
  71              'Minimum minutes non-match' =>
  72                  [duration::DURATION_MINIMUM, false, 150, MINSECS],
  73              'Minimum hours match (float)' =>
  74                  [duration::DURATION_MINIMUM, true, 0.5, HOURSECS],
  75              'Minimum hours match' =>
  76                  [duration::DURATION_MINIMUM, true, 1, HOURSECS],
  77              'Minimum hours non-match (float)' =>
  78                  [duration::DURATION_MINIMUM, false, 2.5, HOURSECS],
  79              'Minimum hours non-match' =>
  80                  [duration::DURATION_MINIMUM, false, 3, HOURSECS],
  81          ];
  82      }
  83  
  84      /**
  85       * Test getting filter SQL
  86       *
  87       * @param int $operator
  88       * @param bool $expectuser
  89       * @param float $value
  90       * @param int $unit
  91       *
  92       * @dataProvider get_sql_filter_provider
  93       */
  94      public function test_get_sql_filter(int $operator, bool $expectuser, float $value = 0, int $unit = MINSECS): void {
  95          global $DB;
  96  
  97          $this->resetAfterTest();
  98  
  99          // We are going to enrol our student from now, with a duration of two hours (timeend is two hours later).
 100          $timestart = time();
 101          $timeend = $timestart + (HOURSECS * 2);
 102  
 103          $course = $this->getDataGenerator()->create_course();
 104          $user = $this->getDataGenerator()->create_and_enrol($course, 'student', null, 'manual', $timestart, $timeend);
 105  
 106          $filter = new filter(
 107              duration::class,
 108              'test',
 109              new lang_string('yes'),
 110              'testentity',
 111              'timeend - timestart'
 112          );
 113  
 114          // Create instance of our filter, passing given values.
 115          [$select, $params] = duration::create($filter)->get_sql_filter([
 116              $filter->get_unique_identifier() . '_operator' => $operator,
 117              $filter->get_unique_identifier() . '_value' => $value,
 118              $filter->get_unique_identifier() . '_unit' => $unit,
 119          ]);
 120  
 121          $useridfield = $DB->get_field_select('user_enrolments', 'userid', $select, $params);
 122          if ($expectuser) {
 123              $this->assertEquals($useridfield, $user->id);
 124          } else {
 125              $this->assertFalse($useridfield);
 126          }
 127      }
 128  }