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 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 column cards exporter
  28   *
  29   * @package     core_reportbuilder
  30   * @covers      \core_reportbuilder\external\custom_report_column_cards_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_column_cards_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]);
  47  
  48          $reportinstance = manager::get_report_from_persistent($report);
  49  
  50          $exporter = new custom_report_column_cards_exporter(null, ['report' => $reportinstance]);
  51          $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
  52  
  53          // The root of the menu cards property should contain each entity.
  54          $this->assertCount(3, $export->menucards);
  55          [$menucardcategory, $menucardcourse, $menucardtag] = $export->menucards;
  56  
  57          // Course category entity menu card.
  58          $this->assertEquals('Course category', $menucardcategory['name']);
  59          $this->assertEquals('course_category', $menucardcategory['key']);
  60          $this->assertNotEmpty($menucardcategory['items']);
  61  
  62          // Test the structure of the first menu card item.
  63          $menucarditem = reset($menucardcategory['items']);
  64          $this->assertEquals([
  65              'name' => 'Category name',
  66              'identifier' => 'course_category:name',
  67              'title' => 'Add column \'Category name\'',
  68              'action' => 'report-add-column',
  69          ], $menucarditem);
  70  
  71          // Course entity menu card.
  72          $this->assertEquals('Course', $menucardcourse['name']);
  73          $this->assertEquals('course', $menucardcourse['key']);
  74          $this->assertNotEmpty($menucardcourse['items']);
  75  
  76          // Test the structure of the first menu card item.
  77          $menucarditem = reset($menucardcourse['items']);
  78          $this->assertEquals([
  79              'name' => 'Course full name with link',
  80              'identifier' => 'course:coursefullnamewithlink',
  81              'title' => 'Add column \'Course full name with link\'',
  82              'action' => 'report-add-column',
  83          ], $menucarditem);
  84  
  85          // Tag entity menu card.
  86          $this->assertEquals('Tag', $menucardtag['name']);
  87          $this->assertEquals('tag', $menucardtag['key']);
  88          $this->assertNotEmpty($menucardtag['items']);
  89  
  90          // Test the structure of the first menu card item.
  91          $menucarditem = reset($menucardtag['items']);
  92          $this->assertEquals([
  93              'name' => 'Tag name',
  94              'identifier' => 'tag:name',
  95              'title' => 'Add column \'Tag name\'',
  96              'action' => 'report-add-column',
  97          ], $menucarditem);
  98      }
  99  }