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.
   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\local\models;
  20  
  21  use advanced_testcase;
  22  use core_reportbuilder\event\report_created;
  23  use core_reportbuilder\event\report_deleted;
  24  use core_reportbuilder\event\report_updated;
  25  use core_reportbuilder_generator;
  26  use core_user\reportbuilder\datasource\users;
  27  
  28  /**
  29   * Unit tests for the report model
  30   *
  31   * @package     core_reportbuilder
  32   * @covers      \core_reportbuilder\local\models\report
  33   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class report_test extends advanced_testcase {
  37  
  38      /**
  39       * Tests for report_created event
  40       *
  41       * @return report
  42       *
  43       * @covers \core_reportbuilder\event\report_created
  44       */
  45      public function test_report_created_event(): report {
  46          $this->resetAfterTest();
  47          $this->setAdminUser();
  48  
  49          /** @var core_reportbuilder_generator $generator */
  50          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  51  
  52          // Catch the events.
  53          $sink = $this->redirectEvents();
  54          $report = $generator->create_report([
  55              'name' => 'My report',
  56              'source' => users::class,
  57              'default' => false,
  58          ]);
  59          $events = $sink->get_events();
  60          $sink->close();
  61  
  62          // Validate the event.
  63          $this->assertCount(1, $events);
  64  
  65          $event = reset($events);
  66          $this->assertInstanceOf(report_created::class, $event);
  67          $this->assertEquals(report::TABLE, $event->objecttable);
  68          $this->assertEquals($report->get('id'), $event->objectid);
  69          $this->assertEquals($report->get('name'), $event->other['name']);
  70          $this->assertEquals($report->get('source'), $event->other['source']);
  71          $this->assertEquals($report->get_context()->id, $event->contextid);
  72  
  73          return $report;
  74      }
  75  
  76      /**
  77       * Tests for report_updated event
  78       *
  79       * @param report $report
  80       * @return report
  81       *
  82       * @depends test_report_created_event
  83       * @covers \core_reportbuilder\event\report_updated
  84       */
  85      public function test_report_updated_event(report $report): report {
  86          global $DB;
  87  
  88          $this->resetAfterTest();
  89          $this->setAdminUser();
  90  
  91          // Re-create the persistent.
  92          $report = new report($DB->insert_record(report::TABLE, $report->to_record()));
  93  
  94          // Catch the events.
  95          $sink = $this->redirectEvents();
  96          $report->set('name', 'New report name')->update();
  97          $events = $sink->get_events();
  98          $sink->close();
  99  
 100          // Validate the event.
 101          $this->assertCount(1, $events);
 102  
 103          $event = reset($events);
 104          $this->assertInstanceOf(report_updated::class, $event);
 105          $this->assertEquals(report::TABLE, $event->objecttable);
 106          $this->assertEquals($report->get('id'), $event->objectid);
 107          $this->assertEquals('New report name', $event->other['name']);
 108          $this->assertEquals($report->get('source'), $event->other['source']);
 109          $this->assertEquals($report->get_context()->id, $event->contextid);
 110  
 111          return $report;
 112      }
 113  
 114      /**
 115       * Tests for report_deleted event
 116       *
 117       * @param report $report
 118       *
 119       * @depends test_report_updated_event
 120       * @covers \core_reportbuilder\event\report_deleted
 121       */
 122      public function test_report_deleted_event(report $report): void {
 123          global $DB;
 124  
 125          $this->resetAfterTest();
 126          $this->setAdminUser();
 127  
 128          // Re-create the persistent (remembering report ID which is removed from persistent upon deletion).
 129          $reportid = $DB->insert_record(report::TABLE, $report->to_record());
 130          $report = new report($reportid);
 131  
 132          // Catch the events.
 133          $sink = $this->redirectEvents();
 134          $report->delete();
 135          $events = $sink->get_events();
 136          $sink->close();
 137  
 138          // Validate the event.
 139          $this->assertCount(1, $events);
 140  
 141          $event = reset($events);
 142          $this->assertInstanceOf(report_deleted::class, $event);
 143          $this->assertEquals(report::TABLE, $event->objecttable);
 144          $this->assertEquals($reportid, $event->objectid);
 145          $this->assertEquals($report->get('name'), $event->other['name']);
 146          $this->assertEquals($report->get('source'), $event->other['source']);
 147          $this->assertEquals($report->get_context()->id, $event->contextid);
 148      }
 149  }