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 tags report filter
  27   *
  28   * @package     core_reportbuilder
  29   * @covers      \core_reportbuilder\local\filters\base
  30   * @covers      \core_reportbuilder\local\filters\tags
  31   * @copyright   2022 Paul Holden <paulh@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class tags_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 value' => [tags::ANY_VALUE, null, ['course01', 'course01', 'course02', 'course03']],
  44              'Not empty' => [tags::NOT_EMPTY, null, ['course01', 'course01', 'course02']],
  45              'Empty' => [tags::EMPTY, null, ['course03']],
  46              'Equal to unselected' => [tags::EQUAL_TO, null, ['course01', 'course01', 'course02', 'course03']],
  47              'Equal to selected tag' => [tags::EQUAL_TO, 'cat', ['course01']],
  48              'Not equal to unselected' => [tags::NOT_EQUAL_TO, null, ['course01', 'course01', 'course02', 'course03']],
  49              'Not equal to selected tag' => [tags::NOT_EQUAL_TO, 'fish', ['course01', 'course01', 'course03']],
  50          ];
  51      }
  52  
  53      /**
  54       * Test getting filter SQL
  55       *
  56       * @param int $operator
  57       * @param string|null $tagname
  58       * @param array $expectedcoursenames
  59       *
  60       * @dataProvider get_sql_filter_provider
  61       */
  62      public function test_get_sql_filter(int $operator, ?string $tagname, array $expectedcoursenames): void {
  63          global $DB;
  64  
  65          $this->resetAfterTest();
  66  
  67          $this->getDataGenerator()->create_course(['fullname' => 'course01', 'tags' => ['cat', 'dog']]);
  68          $this->getDataGenerator()->create_course(['fullname' => 'course02', 'tags' => ['fish']]);
  69          $this->getDataGenerator()->create_course(['fullname' => 'course03']);
  70  
  71          $filter = (new filter(
  72              tags::class,
  73              'tags',
  74              new lang_string('tags'),
  75              'testentity',
  76              't.id'
  77          ));
  78  
  79          // Create instance of our filter, passing ID of the tag if specified.
  80          if ($tagname !== null) {
  81              $tagid = $DB->get_field('tag', 'id', ['name' => $tagname], MUST_EXIST);
  82              $value = [$tagid];
  83          } else {
  84              $value = null;
  85          }
  86  
  87          [$select, $params] = tags::create($filter)->get_sql_filter([
  88              $filter->get_unique_identifier() . '_operator' => $operator,
  89              $filter->get_unique_identifier() . '_value' => $value,
  90          ]);
  91  
  92          $sql = 'SELECT c.fullname
  93                    FROM {course} c
  94               LEFT JOIN {tag_instance} ti ON ti.itemid = c.id
  95               LEFT JOIN {tag} t ON t.id = ti.tagid
  96                   WHERE c.id != ' . SITEID;
  97  
  98          if ($select) {
  99              $sql .= " AND {$select}";
 100          }
 101  
 102          $courses = $DB->get_fieldset_sql($sql, $params);
 103          $this->assertEqualsCanonicalizing($expectedcoursenames, $courses);
 104      }
 105  }