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_reportbuilder\local\helpers\report;
  25  use core_user\reportbuilder\datasource\users;
  26  
  27  /**
  28   * Unit tests for custom report column sorting exporter
  29   *
  30   * @package     core_reportbuilder
  31   * @covers      \core_reportbuilder\external\custom_report_columns_sorting_exporter
  32   * @copyright   2022 Paul Holden <paulh@moodle.com>
  33   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class custom_report_columns_sorting_exporter_test extends advanced_testcase {
  36  
  37      /**
  38       * Test exported data structure
  39       */
  40      public function test_export(): void {
  41          global $PAGE;
  42  
  43          $this->resetAfterTest();
  44  
  45          /** @var core_reportbuilder_generator $generator */
  46          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  47          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
  48  
  49          // Add a couple of columns.
  50          $columnfullname = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
  51          $columnemail = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
  52  
  53          // Enable sorting on the email column, move it to first place.
  54          report::toggle_report_column_sorting($report->get('id'), $columnemail->get('id'), true, SORT_DESC);
  55          report::reorder_report_column_sorting($report->get('id'), $columnemail->get('id'), 1);
  56  
  57          $reportinstance = manager::get_report_from_persistent($report);
  58  
  59          $exporter = new custom_report_columns_sorting_exporter(null, ['report' => $reportinstance]);
  60          $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
  61  
  62          $this->assertTrue($export->hassortablecolumns);
  63          $this->assertCount(2, $export->sortablecolumns);
  64          [$sortcolumnemail, $sortcolumnfullname] = $export->sortablecolumns;
  65  
  66          // Email column.
  67          $this->assertEquals($columnemail->get('id'), $sortcolumnemail['id']);
  68          $this->assertEquals('Email address', $sortcolumnemail['heading']);
  69          $this->assertEquals(1, $sortcolumnemail['sortenabled']);
  70          $this->assertEquals(1, $sortcolumnemail['sortorder']);
  71          $this->assertEquals(SORT_DESC, $sortcolumnemail['sortdirection']);
  72          $this->assertEquals('Disable initial sorting for column Email address', $sortcolumnemail['sortenabledtitle']);
  73          $this->assertEquals('Sort column \'Email address\' ascending', $sortcolumnemail['sorticon']['title']);
  74  
  75          // Fullname column.
  76          $this->assertEquals($columnfullname->get('id'), $sortcolumnfullname['id']);
  77          $this->assertEquals('Full name', $sortcolumnfullname['heading']);
  78          $this->assertEquals(0, $sortcolumnfullname['sortenabled']);
  79          $this->assertEquals(2, $sortcolumnfullname['sortorder']);
  80          $this->assertEquals(SORT_ASC, $sortcolumnfullname['sortdirection']);
  81          $this->assertEquals('Enable initial sorting for column Full name', $sortcolumnfullname['sortenabledtitle']);
  82          $this->assertEquals('Sort column \'Full name\' descending', $sortcolumnfullname['sorticon']['title']);
  83  
  84          $this->assertNotEmpty($export->helpicon);
  85      }
  86  
  87      /**
  88       * Test exported data structure for report with columns, but they aren't sortable
  89       */
  90      public function test_export_no_sortable_columns(): void {
  91          global $PAGE;
  92  
  93          $this->resetAfterTest();
  94  
  95          /** @var core_reportbuilder_generator $generator */
  96          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  97          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
  98          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:picture']);
  99  
 100          $reportinstance = manager::get_report_from_persistent($report);
 101  
 102          $exporter = new custom_report_columns_sorting_exporter(null, ['report' => $reportinstance]);
 103          $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
 104  
 105          $this->assertFalse($export->hassortablecolumns);
 106          $this->assertEmpty($export->sortablecolumns);
 107      }
 108  
 109      /**
 110       * Test exported data structure for report with no columns
 111       */
 112      public function test_export_no_columns(): void {
 113          global $PAGE;
 114  
 115          $this->resetAfterTest();
 116  
 117          /** @var core_reportbuilder_generator $generator */
 118          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 119          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 120  
 121          $reportinstance = manager::get_report_from_persistent($report);
 122  
 123          $exporter = new custom_report_columns_sorting_exporter(null, ['report' => $reportinstance]);
 124          $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
 125  
 126          $this->assertFalse($export->hassortablecolumns);
 127          $this->assertEmpty($export->sortablecolumns);
 128      }
 129  }