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.
   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 select report filter
  27   *
  28   * @package     core_reportbuilder
  29   * @covers      \core_reportbuilder\local\filters\base
  30   * @covers      \core_reportbuilder\local\filters\select
  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 select_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              [select::ANY_VALUE, null, true],
  44              [select::EQUAL_TO, 'starwars', true],
  45              [select::EQUAL_TO, 'mandalorian', false],
  46              [select::NOT_EQUAL_TO, 'starwars', false],
  47              [select::NOT_EQUAL_TO, 'mandalorian', true],
  48          ];
  49      }
  50  
  51      /**
  52       * Test getting filter SQL
  53       *
  54       * @param int $operator
  55       * @param string|null $value
  56       * @param bool $expectmatch
  57       *
  58       * @dataProvider get_sql_filter_simple_provider
  59       */
  60      public function test_get_sql_filter_simple(int $operator, ?string $value, bool $expectmatch): void {
  61          global $DB;
  62  
  63          $this->resetAfterTest();
  64  
  65          $course1 = $this->getDataGenerator()->create_course([
  66              'fullname' => "May the course be with you",
  67              'shortname' => 'starwars',
  68          ]);
  69          $course2 = $this->getDataGenerator()->create_course([
  70              'fullname' => "This is the course",
  71              'shortname' => 'mandalorian',
  72          ]);
  73  
  74          $filter = (new filter(
  75              select::class,
  76              'test',
  77              new lang_string('course'),
  78              'testentity',
  79              'shortname'
  80          ))->set_options([
  81              $course1->shortname => $course1->fullname,
  82              $course2->shortname => $course2->fullname,
  83          ]);
  84  
  85          // Create instance of our filter, passing given operator.
  86          [$select, $params] = select::create($filter)->get_sql_filter([
  87              $filter->get_unique_identifier() . '_operator' => $operator,
  88              $filter->get_unique_identifier() . '_value' => $value,
  89          ]);
  90  
  91          $fullnames = $DB->get_fieldset_select('course', 'fullname', $select, $params);
  92          if ($expectmatch) {
  93              $this->assertContains($course1->fullname, $fullnames);
  94          } else {
  95              $this->assertNotContains($course1->fullname, $fullnames);
  96          }
  97      }
  98  }