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_course\reportbuilder\datasource\courses;
  25  
  26  /**
  27   * Unit tests for custom report conditions exporter
  28   *
  29   * @package     core_reportbuilder
  30   * @covers      \core_reportbuilder\external\custom_report_conditions_exporter
  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 custom_report_conditions_exporter_test extends advanced_testcase {
  35  
  36      /**
  37       * Test exported data structure
  38       */
  39      public function test_export(): void {
  40          global $PAGE;
  41  
  42          $this->resetAfterTest();
  43  
  44          /** @var core_reportbuilder_generator $generator */
  45          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  46          $report = $generator->create_report(['name' => 'My report', 'source' => courses::class, 'default' => false]);
  47          $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:shortname']);
  48  
  49          $reportinstance = manager::get_report_from_persistent($report);
  50  
  51          $exporter = new custom_report_conditions_exporter(null, ['report' => $reportinstance]);
  52          $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
  53  
  54          $this->assertTrue($export->hasavailableconditions);
  55  
  56          // The root of the available conditions property should contain each entity.
  57          $this->assertCount(4, $export->availableconditions);
  58          [$conditionscategory, $conditionscourse, $conditionstag, $conditionsfile] = $export->availableconditions;
  59  
  60          // Course category conditions, assert structure of first item.
  61          $this->assertEquals('Course category', $conditionscategory['optiongroup']['text']);
  62          $this->assertGreaterThanOrEqual(1, count($conditionscategory['optiongroup']['values']));
  63          $this->assertEquals([
  64              'value' => 'course_category:name',
  65              'visiblename' => 'Select category',
  66          ], $conditionscategory['optiongroup']['values'][0]);
  67  
  68          // Course conditions, assert structure of first item.
  69          $this->assertEquals('Course', $conditionscourse['optiongroup']['text']);
  70          $this->assertGreaterThanOrEqual(1, count($conditionscourse['optiongroup']['values']));
  71          $this->assertEquals([
  72              'value' => 'course:fullname',
  73              'visiblename' => 'Course full name',
  74          ], $conditionscourse['optiongroup']['values'][0]);
  75  
  76          // Make sure the active condition we added, isn't present in available conditions.
  77          $this->assertNotContains('course:shortname', array_column($conditionscourse['optiongroup']['values'], 'value'));
  78  
  79          // Tag conditions, assert structure of first item.
  80          $this->assertEquals('Tag', $conditionstag['optiongroup']['text']);
  81          $this->assertGreaterThanOrEqual(1, count($conditionstag['optiongroup']['values']));
  82          $this->assertEquals([
  83              'value' => 'tag:name',
  84              'visiblename' => 'Tag name',
  85          ], $conditionstag['optiongroup']['values'][0]);
  86  
  87          // File conditions, assert structure of first item.
  88          $this->assertEquals('Course image', $conditionsfile['optiongroup']['text']);
  89          $this->assertGreaterThanOrEqual(1, count($conditionsfile['optiongroup']['values']));
  90          $this->assertEquals([
  91              'value' => 'file:name',
  92              'visiblename' => 'Filename',
  93          ], $conditionsfile['optiongroup']['values'][0]);
  94  
  95          // The active conditions are contained inside form HTML, just assert there's something present.
  96          $this->assertTrue($export->hasactiveconditions);
  97          $this->assertNotEmpty($export->activeconditionsform);
  98          $this->assertNotEmpty($export->helpicon);
  99      }
 100  
 101      /**
 102       * Test exported data structure for report with no conditions
 103       */
 104      public function test_export_no_conditions(): void {
 105          global $PAGE;
 106  
 107          $this->resetAfterTest();
 108  
 109          /** @var core_reportbuilder_generator $generator */
 110          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 111          $report = $generator->create_report(['name' => 'My report', 'source' => courses::class, 'default' => false]);
 112  
 113          $reportinstance = manager::get_report_from_persistent($report);
 114  
 115          $exporter = new custom_report_conditions_exporter(null, ['report' => $reportinstance]);
 116          $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
 117  
 118          $this->assertFalse($export->hasactiveconditions);
 119          $this->assertEmpty($export->activeconditionsform);
 120      }
 121  }