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.

Differences Between: [Versions 401 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  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\helpers\database;
  24  use core_reportbuilder\local\report\filter;
  25  
  26  /**
  27   * Unit tests for course category report filter
  28   *
  29   * @package     core_reportbuilder
  30   * @covers      \core_reportbuilder\local\filters\category
  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 category_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              ['One', false, ['One']],
  44              ['One', true, ['One', 'Two', 'Three']],
  45              ['Two', true, ['Two', 'Three']],
  46              ['Three', true, ['Three']],
  47              [null, false, ['Category 1', 'One', 'Two', 'Three']],
  48          ];
  49      }
  50  
  51      /**
  52       * Test getting filter SQL
  53       *
  54       * @param string|null $categoryname
  55       * @param bool $subcategories
  56       * @param string[] $expectedcategories
  57       *
  58       * @dataProvider get_sql_filter_provider
  59       */
  60      public function test_get_sql_filter(?string $categoryname, bool $subcategories, array $expectedcategories): void {
  61          global $DB;
  62  
  63          $this->resetAfterTest();
  64  
  65          $category1 = $this->getDataGenerator()->create_category(['name' => 'One']);
  66          $category2 = $this->getDataGenerator()->create_category(['name' => 'Two', 'parent' => $category1->id]);
  67          $category3 = $this->getDataGenerator()->create_category(['name' => 'Three', 'parent' => $category2->id]);
  68  
  69          if ($categoryname !== null) {
  70              $categoryid = $DB->get_field('course_categories', 'id', ['name' => $categoryname], MUST_EXIST);
  71          } else {
  72              $categoryid = null;
  73          }
  74  
  75          $filter = new filter(
  76              category::class,
  77              'test',
  78              new lang_string('yes'),
  79              'testentity',
  80              'id'
  81          );
  82  
  83          // Create instance of our filter, passing given operator.
  84          [$select, $params] = category::create($filter)->get_sql_filter([
  85              $filter->get_unique_identifier() . '_value' => $categoryid,
  86              $filter->get_unique_identifier() . '_subcategories' => $subcategories,
  87          ]);
  88  
  89          $categories = $DB->get_fieldset_select('course_categories', 'name', $select, $params);
  90          $this->assertEqualsCanonicalizing($expectedcategories, $categories);
  91      }
  92  
  93      /**
  94       * Test getting filter SQL with parameters
  95       */
  96      public function test_get_sql_filter_parameters(): void {
  97          global $DB;
  98  
  99          $this->resetAfterTest();
 100  
 101          $category1 = $this->getDataGenerator()->create_category(['name' => 'One']);
 102          $category2 = $this->getDataGenerator()->create_category(['name' => 'Two', 'parent' => $category1->id]);
 103          $category3 = $this->getDataGenerator()->create_category(['name' => 'Three']);
 104  
 105          // Rather convoluted filter SQL, but enough to demonstrate usage of a parameter that gets used twice in the query.
 106          $paramzero = database::generate_param_name();
 107          $filter = new filter(
 108              category::class,
 109              'test',
 110              new lang_string('yes'),
 111              'testentity',
 112              "id + :{$paramzero}",
 113              [$paramzero => 0]
 114          );
 115  
 116          // When including sub-categories, the filter SQL is included twice (for the category itself, plus to find descendents).
 117          [$select, $params] = category::create($filter)->get_sql_filter([
 118              $filter->get_unique_identifier() . '_value' => $category1->id,
 119              $filter->get_unique_identifier() . '_subcategories' => true,
 120          ]);
 121  
 122          $categories = $DB->get_fieldset_select('course_categories', 'id', $select, $params);
 123          $this->assertEqualsCanonicalizing([$category1->id, $category2->id], $categories);
 124      }
 125  }