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