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 400 and 401] [Versions 401 and 402] [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(3, $export->availableconditions);
  58          [$conditionscategory, $conditionscourse, $conditionstag] = $export->availableconditions;
  59  
  60          // Course category conditions, assert structure of first item.
  61          $this->assertEquals('Course category', $conditionscategory['optiongroup']['text']);
  62          $this->assertGreaterThanOrEqual(1, $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, $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, $conditionstag['optiongroup']['values']);
  82          $this->assertEquals([
  83              'value' => 'tag:name',
  84              'visiblename' => 'Tag name',
  85          ], $conditionstag['optiongroup']['values'][0]);
  86  
  87          // The active conditions are contained inside form HTML, just assert there's something present.
  88          $this->assertTrue($export->hasactiveconditions);
  89          $this->assertNotEmpty($export->activeconditionsform);
  90          $this->assertNotEmpty($export->helpicon);
  91      }
  92  
  93      /**
  94       * Test exported data structure for report with no conditions
  95       */
  96      public function test_export_no_conditions(): void {
  97          global $PAGE;
  98  
  99          $this->resetAfterTest();
 100  
 101          /** @var core_reportbuilder_generator $generator */
 102          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 103          $report = $generator->create_report(['name' => 'My report', 'source' => courses::class, 'default' => false]);
 104  
 105          $reportinstance = manager::get_report_from_persistent($report);
 106  
 107          $exporter = new custom_report_conditions_exporter(null, ['report' => $reportinstance]);
 108          $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
 109  
 110          $this->assertFalse($export->hasactiveconditions);
 111          $this->assertEmpty($export->activeconditionsform);
 112      }
 113  }