Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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;
  20  
  21  use advanced_testcase;
  22  use core_reportbuilder_generator;
  23  use core_reportbuilder\local\models\{audience, column, filter, report, schedule};
  24  use core_user\reportbuilder\datasource\users;
  25  
  26  /**
  27   * Unit tests for the test data generator
  28   *
  29   * Note that assertions of created data content is performed in other testcases of the relevant classes, in the majority of cases
  30   * here we just want to assert that the thing we created actually exists
  31   *
  32   * @package     core_reportbuilder
  33   * @covers      \core_reportbuilder_generator
  34   * @copyright   2022 Paul Holden <paulh@moodle.com>
  35   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class generator_test extends advanced_testcase {
  38  
  39      /**
  40       * Test creating a report
  41       */
  42      public function test_create_report(): void {
  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]);
  48  
  49          $this->assertTrue(report::record_exists($report->get('id')));
  50      }
  51  
  52      /**
  53       * Test creating a column
  54       */
  55      public function test_create_column(): void {
  56          $this->resetAfterTest();
  57  
  58          /** @var core_reportbuilder_generator $generator */
  59          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  60  
  61          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
  62          $column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
  63  
  64          $this->assertTrue(column::record_exists($column->get('id')));
  65      }
  66  
  67      /**
  68       * Test creating a column, specifying additional properties
  69       */
  70      public function test_create_column_additional_properties(): void {
  71          $this->resetAfterTest();
  72  
  73          /** @var core_reportbuilder_generator $generator */
  74          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  75  
  76          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => 0]);
  77          $column = $generator->create_column([
  78              'reportid' => $report->get('id'),
  79              'uniqueidentifier' => 'user:lastname',
  80              'heading' => 'My pants',
  81              'sortenabled' => 1,
  82          ]);
  83  
  84          $this->assertEquals('My pants', $column->get('heading'));
  85          $this->assertTrue($column->get('sortenabled'));
  86      }
  87  
  88      /**
  89       * Test creating a filter
  90       */
  91      public function test_create_filter(): void {
  92          $this->resetAfterTest();
  93  
  94          /** @var core_reportbuilder_generator $generator */
  95          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  96  
  97          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
  98          $filter = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
  99  
 100          $this->assertTrue(filter::record_exists($filter->get('id')));
 101      }
 102  
 103      /**
 104       * Test creating a filter, specifying additional properties
 105       */
 106      public function test_create_filter_additional_properties(): void {
 107          $this->resetAfterTest();
 108  
 109          /** @var core_reportbuilder_generator $generator */
 110          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 111  
 112          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => 0]);
 113          $filter = $generator->create_filter([
 114              'reportid' => $report->get('id'),
 115              'uniqueidentifier' => 'user:lastname',
 116              'heading' => 'My pants',
 117          ]);
 118  
 119          $this->assertEquals('My pants', $filter->get('heading'));
 120      }
 121  
 122      /**
 123       * Test creating a condition
 124       */
 125      public function test_create_condition(): void {
 126          $this->resetAfterTest();
 127  
 128          /** @var core_reportbuilder_generator $generator */
 129          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 130  
 131          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
 132          $condition = $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
 133  
 134          $this->assertTrue(filter::record_exists($condition->get('id')));
 135      }
 136  
 137      /**
 138       * Test creating a condition, specifying additional properties
 139       */
 140      public function test_create_condition_additional_properties(): void {
 141          $this->resetAfterTest();
 142  
 143          /** @var core_reportbuilder_generator $generator */
 144          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 145  
 146          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => 0]);
 147          $condition = $generator->create_condition([
 148              'reportid' => $report->get('id'),
 149              'uniqueidentifier' => 'user:lastname',
 150              'heading' => 'My pants',
 151          ]);
 152  
 153          $this->assertEquals('My pants', $condition->get('heading'));
 154      }
 155  
 156      /**
 157       * Test creating an audience
 158       */
 159      public function test_create_audience(): void {
 160          $this->resetAfterTest();
 161  
 162          /** @var core_reportbuilder_generator $generator */
 163          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 164  
 165          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
 166          $audience = $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => []]);
 167  
 168          $this->assertTrue(audience::record_exists($audience->get_persistent()->get('id')));
 169      }
 170  
 171      /**
 172       * Test creating a schedule
 173       */
 174      public function test_create_schedule(): void {
 175          $this->resetAfterTest();
 176  
 177          /** @var core_reportbuilder_generator $generator */
 178          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 179  
 180          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
 181          $schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
 182  
 183          $this->assertTrue(schedule::record_exists($schedule->get('id')));
 184      }
 185  }