Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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_user\reportbuilder\datasource\users;
  25  
  26  /**
  27   * Unit tests for custom report data exporter
  28   *
  29   * @package     core_reportbuilder
  30   * @covers      \core_reportbuilder\external\custom_report_data_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_data_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          $this->getDataGenerator()->create_user(['firstname' => 'Zoe', 'lastname' => 'Zebra', 'email' => 'u1@example.com']);
  45          $this->getDataGenerator()->create_user(['firstname' => 'Charlie', 'lastname' => 'Carrot', 'email' => 'u2@example.com']);
  46  
  47          /** @var core_reportbuilder_generator $generator */
  48          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  49  
  50          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
  51          $generator->create_column([
  52              'reportid' => $report->get('id'),
  53              'uniqueidentifier' => 'user:fullname',
  54              'heading' => 'Lovely user',
  55              'sortenabled' => 1,
  56          ]);
  57          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
  58  
  59          $reportinstance = manager::get_report_from_persistent($report);
  60  
  61          $exporter = new custom_report_data_exporter(null, ['report' => $reportinstance, 'page' => 0, 'perpage' => 2]);
  62          $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
  63  
  64          // There are three users (admin plus the two previouly created), but we're paging the first two only.
  65          $this->assertEquals(['Lovely user', 'Email address'], $export->headers);
  66          $this->assertEquals([
  67              [
  68                  'columns' => ['Admin User', 'admin@example.com'],
  69              ],
  70              [
  71                  'columns' => ['Charlie Carrot', 'u2@example.com'],
  72              ],
  73          ], $export->rows);
  74          $this->assertEquals(3, $export->totalrowcount);
  75      }
  76  }