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.

Differences Between: [Versions 400 and 403] [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\external;
  20  
  21  use advanced_testcase;
  22  use core_reportbuilder_generator;
  23  use core_reportbuilder\manager;
  24  use core_reportbuilder\local\helpers\report;
  25  use core_course\reportbuilder\datasource\courses;
  26  
  27  /**
  28   * Unit tests for custom report filters exporter
  29   *
  30   * @package     core_reportbuilder
  31   * @covers      \core_reportbuilder\external\custom_report_filters_exporter
  32   * @copyright   2022 Paul Holden <paulh@moodle.com>
  33   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class custom_report_filters_exporter_test extends advanced_testcase {
  36  
  37      /**
  38       * Test exported data structure
  39       */
  40      public function test_export(): void {
  41          global $PAGE;
  42  
  43          $this->resetAfterTest();
  44  
  45          /** @var core_reportbuilder_generator $generator */
  46          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  47          $report = $generator->create_report(['name' => 'My report', 'source' => courses::class, 'default' => false]);
  48  
  49          // Add a couple of filters.
  50          $filtercategoryname = $generator->create_filter([
  51              'reportid' => $report->get('id'),
  52              'uniqueidentifier' => 'course_category:name',
  53          ]);
  54          $filtercourseidnumber = $generator->create_filter([
  55              'reportid' => $report->get('id'),
  56              'uniqueidentifier' => 'course:idnumber',
  57          ]);
  58  
  59          // Move course ID number filter to first place.
  60          report::reorder_report_filter($report->get('id'), $filtercourseidnumber->get('id'), 1);
  61  
  62          $reportinstance = manager::get_report_from_persistent($report);
  63  
  64          $exporter = new custom_report_filters_exporter(null, ['report' => $reportinstance]);
  65          $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
  66  
  67          $this->assertTrue($export->hasavailablefilters);
  68  
  69          // The root of the available filters property should contain each entity.
  70          $this->assertCount(4, $export->availablefilters);
  71          [$filterscategory, $filterscourse, $filterstag, $filtersfile] = $export->availablefilters;
  72  
  73          // Course category filters, assert structure of first item.
  74          $this->assertEquals('Course category', $filterscategory['optiongroup']['text']);
  75          $this->assertGreaterThanOrEqual(1, count($filterscategory['optiongroup']['values']));
  76          $this->assertEquals([
  77              'value' => 'course_category:text',
  78              'visiblename' => 'Category name',
  79          ], $filterscategory['optiongroup']['values'][0]);
  80  
  81          // Course filters, assert structure of first item.
  82          $this->assertEquals('Course', $filterscourse['optiongroup']['text']);
  83          $this->assertGreaterThanOrEqual(1, count($filterscourse['optiongroup']['values']));
  84          $this->assertEquals([
  85              'value' => 'course:fullname',
  86              'visiblename' => 'Course full name',
  87          ], $filterscourse['optiongroup']['values'][0]);
  88  
  89          // Make sure the active filters we added, aren't present in available filters.
  90          $filterscourseavailable = array_column($filterscourse['optiongroup']['values'], 'value');
  91          $this->assertNotContains('course_category:name', $filterscourseavailable);
  92          $this->assertNotContains('course:idnumber', $filterscourseavailable);
  93  
  94          // Tag filters, assert structure of first item.
  95          $this->assertEquals('Tag', $filterstag['optiongroup']['text']);
  96          $this->assertGreaterThanOrEqual(1, count($filterstag['optiongroup']['values']));
  97          $this->assertEquals([
  98              'value' => 'tag:name',
  99              'visiblename' => 'Tag name',
 100          ], $filterstag['optiongroup']['values'][0]);
 101  
 102          // File filters, assert structure of first item.
 103          $this->assertEquals('Course image', $filtersfile['optiongroup']['text']);
 104          $this->assertGreaterThanOrEqual(1, count($filtersfile['optiongroup']['values']));
 105          $this->assertEquals([
 106              'value' => 'file:name',
 107              'visiblename' => 'Filename',
 108          ], $filtersfile['optiongroup']['values'][0]);
 109  
 110          $this->assertTrue($export->hasactivefilters);
 111          $this->assertCount(2, $export->activefilters);
 112          [$activefiltercourseidnumber, $activefiltercategoryname] = $export->activefilters;
 113  
 114          // Course ID number filter.
 115          $this->assertEquals($filtercourseidnumber->get('id'), $activefiltercourseidnumber['id']);
 116          $this->assertEquals('Course', $activefiltercourseidnumber['entityname']);
 117          $this->assertEquals('Course ID number', $activefiltercourseidnumber['heading']);
 118          $this->assertEquals(1, $activefiltercourseidnumber['sortorder']);
 119  
 120          // Course category filter.
 121          $this->assertEquals($filtercategoryname->get('id'), $activefiltercategoryname['id']);
 122          $this->assertEquals('Course category', $activefiltercategoryname['entityname']);
 123          $this->assertEquals('Select category', $activefiltercategoryname['heading']);
 124          $this->assertEquals(2, $activefiltercategoryname['sortorder']);
 125  
 126          $this->assertNotEmpty($export->helpicon);
 127      }
 128  
 129      /**
 130       * Test exported data structure for report with no filters
 131       */
 132      public function test_export_no_filters(): void {
 133          global $PAGE;
 134  
 135          $this->resetAfterTest();
 136  
 137          /** @var core_reportbuilder_generator $generator */
 138          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 139          $report = $generator->create_report(['name' => 'My report', 'source' => courses::class, 'default' => false]);
 140  
 141          $reportinstance = manager::get_report_from_persistent($report);
 142  
 143          $exporter = new custom_report_filters_exporter(null, ['report' => $reportinstance]);
 144          $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
 145  
 146          $this->assertFalse($export->hasactivefilters);
 147          $this->assertEmpty($export->activefilters);
 148      }
 149  }