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 moodle_url;
  24  use core_reportbuilder\local\helpers\user_filter_manager;
  25  use core_reportbuilder\local\filters\text;
  26  use core_user\reportbuilder\datasource\users;
  27  
  28  /**
  29   * Unit tests for custom report exporter
  30   *
  31   * @package     core_reportbuilder
  32   * @covers      \core_reportbuilder\external\custom_report_exporter
  33   * @copyright   2022 Paul Holden <paulh@moodle.com>
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class custom_report_exporter_test extends advanced_testcase {
  37  
  38      /**
  39       * Test exported data structure when editing a report
  40       */
  41      public function test_export_editing(): void {
  42          global $PAGE;
  43  
  44          $this->resetAfterTest();
  45  
  46          /** @var core_reportbuilder_generator $generator */
  47          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  48          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
  49  
  50          $PAGE->set_url(new moodle_url('/'));
  51  
  52          $exporter = new custom_report_exporter($report, [], true);
  53          $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
  54  
  55          $this->assertNotEmpty($export->table);
  56          $this->assertEquals(0, $export->filtersapplied);
  57          $this->assertFalse($export->filterspresent);
  58          $this->assertEmpty($export->filtersform);
  59          $this->assertTrue($export->editmode);
  60  
  61          // The following are all generated by additional exporters.
  62          $this->assertNotEmpty($export->sidebarmenucards);
  63          $this->assertNotEmpty($export->conditions);
  64          $this->assertNotEmpty($export->filters);
  65          $this->assertNotEmpty($export->sorting);
  66          $this->assertNotEmpty($export->cardview);
  67      }
  68  
  69      /**
  70       * Test exported data structure when viewing a report
  71       */
  72      public function test_export_viewing(): void {
  73          global $PAGE;
  74  
  75          $this->resetAfterTest();
  76  
  77          /** @var core_reportbuilder_generator $generator */
  78          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  79          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
  80  
  81          $PAGE->set_url(new moodle_url('/'));
  82  
  83          $exporter = new custom_report_exporter($report, ['pagesize' => 10], false);
  84          $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
  85  
  86          $this->assertNotEmpty($export->table);
  87          $this->assertEquals(0, $export->filtersapplied);
  88          $this->assertFalse($export->filterspresent);
  89          $this->assertEmpty($export->filtersform);
  90          $this->assertFalse($export->editmode);
  91  
  92          // The following are all generated by additional exporters, and should not be present when not editing.
  93          $this->assertObjectNotHasAttribute('sidebarmenucards', $export);
  94          $this->assertObjectNotHasAttribute('conditions', $export);
  95          $this->assertObjectNotHasAttribute('filters', $export);
  96          $this->assertObjectNotHasAttribute('sorting', $export);
  97          $this->assertObjectNotHasAttribute('cardview', $export);
  98      }
  99  
 100      /**
 101       * Test exported data structure when filters are present
 102       */
 103      public function test_export_filters_present(): void {
 104          global $PAGE;
 105  
 106          $this->resetAfterTest();
 107  
 108          /** @var core_reportbuilder_generator $generator */
 109          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 110          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 111          $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 112  
 113          $PAGE->set_url(new moodle_url('/'));
 114  
 115          $exporter = new custom_report_exporter($report, ['pagesize' => 10], false);
 116          $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
 117  
 118          $this->assertTrue($export->filterspresent);
 119          $this->assertNotEmpty($export->filtersform);
 120          $this->assertEquals(0, $export->filtersapplied);
 121      }
 122  
 123      /**
 124       * Test exported data structure when filters are applied
 125       */
 126      public function test_export_filters_applied(): void {
 127          global $PAGE;
 128  
 129          $this->resetAfterTest();
 130  
 131          $user = $this->getDataGenerator()->create_user();
 132          $this->setUser($user);
 133  
 134          /** @var core_reportbuilder_generator $generator */
 135          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 136  
 137          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 138          $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 139  
 140          // Apply filter.
 141          user_filter_manager::set($report->get('id'), ['user:email_operator' => text::IS_NOT_EMPTY]);
 142  
 143          $PAGE->set_url(new moodle_url('/'));
 144  
 145          $exporter = new custom_report_exporter($report, ['pagesize' => 10], false);
 146          $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
 147  
 148          $this->assertTrue($export->filterspresent);
 149          $this->assertNotEmpty($export->filtersform);
 150          $this->assertEquals(1, $export->filtersapplied);
 151      }
 152  }