Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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          // The root of the available conditions property should contain two entities.
  55          $this->assertTrue($export->hasavailableconditions);
  56          $this->assertCount(2, $export->availableconditions);
  57  
  58          [$conditionscategory, $conditionscourse] = $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' => 'Category name',
  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          $this->assertTrue($export->hasactiveconditions);
  80          $this->assertNotEmpty($export->activeconditionsform);
  81          $this->assertNotEmpty($export->helpicon);
  82      }
  83  
  84      /**
  85       * Test exported data structure for report with no conditions
  86       */
  87      public function test_export_no_conditions(): void {
  88          global $PAGE;
  89  
  90          $this->resetAfterTest();
  91  
  92          /** @var core_reportbuilder_generator $generator */
  93          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  94          $report = $generator->create_report(['name' => 'My report', 'source' => courses::class, 'default' => false]);
  95  
  96          $reportinstance = manager::get_report_from_persistent($report);
  97  
  98          $exporter = new custom_report_conditions_exporter(null, ['report' => $reportinstance]);
  99          $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
 100  
 101          $this->assertFalse($export->hasactiveconditions);
 102          $this->assertEmpty($export->activeconditionsform);
 103      }
 104  }