Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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 number report filter
  27   *
  28   * @package     core_reportbuilder
  29   * @covers      \core_reportbuilder\local\filters\base
  30   * @covers      \core_reportbuilder\local\filters\number
  31   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class number_test extends advanced_testcase {
  35  
  36      /**
  37       * Data provider for {@see test_get_sql_filter_simple}
  38       *
  39       * @return array[]
  40       */
  41      public function get_sql_filter_simple_provider(): array {
  42          return [
  43              [number::ANY_VALUE, null, null, true],
  44              [number::IS_NOT_EMPTY, null, null, true],
  45              [number::IS_EMPTY, null, null, false],
  46              [number::LESS_THAN, 1, null, false],
  47              [number::LESS_THAN, 123, null, false],
  48              [number::LESS_THAN, 124, null, true],
  49              [number::GREATER_THAN, 1, null, true],
  50              [number::GREATER_THAN, 123, null, false],
  51              [number::GREATER_THAN, 124, null, false],
  52              [number::EQUAL_TO, 123, null, true],
  53              [number::EQUAL_TO, 124, null, false],
  54              [number::EQUAL_OR_LESS_THAN, 124, null, true],
  55              [number::EQUAL_OR_LESS_THAN, 123, null, true],
  56              [number::EQUAL_OR_LESS_THAN, 122, null, false],
  57              [number::EQUAL_OR_GREATER_THAN, 122, null, true],
  58              [number::EQUAL_OR_GREATER_THAN, 123, null, true],
  59              [number::EQUAL_OR_GREATER_THAN, 124, null, false],
  60              [number::RANGE, 122, 124, true],
  61              [number::RANGE, 124, 125, false],
  62              [number::RANGE, 122, 123, true],
  63              [number::RANGE, 123, 124, true],
  64          ];
  65      }
  66  
  67      /**
  68       * Test getting filter SQL
  69       *
  70       * @param int $operator
  71       * @param int|null $value1
  72       * @param int|null $value2
  73       * @param bool $expectmatch
  74       *
  75       * @dataProvider get_sql_filter_simple_provider
  76       */
  77      public function test_get_sql_filter_simple(int $operator, ?int $value1, ?int $value2, bool $expectmatch): void {
  78          global $DB;
  79  
  80          $this->resetAfterTest();
  81  
  82          $course = $this->getDataGenerator()->create_course([
  83              'timecreated' => 123,
  84          ]);
  85  
  86          $filter = new filter(
  87              number::class,
  88              'test',
  89              new lang_string('course'),
  90              'testentity',
  91              'timecreated'
  92          );
  93  
  94          // Create instance of our filter, passing given operator.
  95          [$select, $params] = number::create($filter)->get_sql_filter([
  96              $filter->get_unique_identifier() . '_value1' => $value1,
  97              $filter->get_unique_identifier() . '_value2' => $value2,
  98              $filter->get_unique_identifier() . '_operator' => $operator,
  99          ]);
 100  
 101          $fullnames = $DB->get_fieldset_select('course', 'fullname', $select, $params);
 102          if ($expectmatch) {
 103              $this->assertContains($course->fullname, $fullnames);
 104          } else {
 105              $this->assertNotContains($course->fullname, $fullnames);
 106          }
 107      }
 108  
 109      /**
 110       * Data provider for {@see test_get_sql_filter_invalid}
 111       *
 112       * @return array[]
 113       */
 114      public function get_sql_filter_invalid_provider(): array {
 115          return [
 116              [number::LESS_THAN],
 117              [number::GREATER_THAN],
 118              [number::EQUAL_TO],
 119              [number::EQUAL_OR_LESS_THAN],
 120              [number::EQUAL_OR_GREATER_THAN],
 121              [number::RANGE],
 122          ];
 123      }
 124  
 125      /**
 126       * Test getting filter SQL for operators that require values
 127       *
 128       * @param int $operator
 129       *
 130       * @dataProvider get_sql_filter_invalid_provider
 131       */
 132      public function test_get_sql_filter_invalid(int $operator): void {
 133          $filter = new filter(
 134              number::class,
 135              'test',
 136              new lang_string('course'),
 137              'testentity',
 138              'timecreated'
 139          );
 140  
 141          [$select, $params] = number::create($filter)->get_sql_filter([
 142              $filter->get_unique_identifier() . '_operator' => $operator,
 143          ]);
 144  
 145          $this->assertEquals('', $select);
 146          $this->assertEquals([], $params);
 147      }
 148  }