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\audience_created;
  24  use core_reportbuilder\event\audience_deleted;
  25  use core_reportbuilder\event\audience_updated;
  26  use core_reportbuilder_generator;
  27  use core_user\reportbuilder\datasource\users;
  28  
  29  /**
  30   * Unit tests for the audience model
  31   *
  32   * @package     core_reportbuilder
  33   * @covers      \core_reportbuilder\local\models\audience
  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 audience_test extends advanced_testcase {
  38  
  39      /**
  40       * Tests for audience_created event
  41       *
  42       * @return persistent[]
  43       *
  44       * @covers \core_reportbuilder\event\audience_created
  45       */
  46      public function test_audience_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          $audience = $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => []])
  62              ->get_persistent();
  63          $events = $sink->get_events();
  64          $sink->close();
  65  
  66          // Validate the event.
  67          $this->assertCount(1, $events);
  68  
  69          $event = reset($events);
  70          $this->assertInstanceOf(audience_created::class, $event);
  71          $this->assertEquals(audience::TABLE, $event->objecttable);
  72          $this->assertEquals($audience->get('id'), $event->objectid);
  73          $this->assertEquals($report->get('id'), $event->other['reportid']);
  74          $this->assertEquals($report->get_context()->id, $event->contextid);
  75  
  76          return [$report, $audience];
  77      }
  78  
  79      /**
  80       * Tests for audience_updated event
  81       *
  82       * @param persistent[] $persistents
  83       * @return persistent[]
  84       *
  85       * @depends test_audience_created_event
  86       * @covers \core_reportbuilder\event\audience_updated
  87       */
  88      public function test_audience_updated_event(array $persistents): array {
  89          global $DB;
  90  
  91          $this->resetAfterTest();
  92          $this->setAdminUser();
  93  
  94          // Re-create the persistents.
  95          [$report, $audience] = $persistents;
  96          $report = new report($DB->insert_record(report::TABLE, $report->to_record()));
  97          $audience = new audience($DB->insert_record(audience::TABLE, $audience->to_record()));
  98  
  99          // Catch the events.
 100          $sink = $this->redirectEvents();
 101          $audience->set('heading', 'Hello')->update();
 102          $events = $sink->get_events();
 103          $sink->close();
 104  
 105          // Validate the event.
 106          $this->assertCount(1, $events);
 107  
 108          $event = reset($events);
 109          $this->assertInstanceOf(audience_updated::class, $event);
 110          $this->assertEquals(audience::TABLE, $event->objecttable);
 111          $this->assertEquals($audience->get('id'), $event->objectid);
 112          $this->assertEquals($report->get('id'), $event->other['reportid']);
 113          $this->assertEquals($report->get_context()->id, $event->contextid);
 114  
 115          return [$report, $audience];
 116      }
 117  
 118      /**
 119       * Tests for audience_deleted event
 120       *
 121       * @param persistent[] $persistents
 122       *
 123       * @depends test_audience_updated_event
 124       * @covers \core_reportbuilder\event\audience_deleted
 125       */
 126      public function test_audience_deleted_event(array $persistents): void {
 127          global $DB;
 128  
 129          $this->resetAfterTest();
 130          $this->setAdminUser();
 131  
 132          // Re-create the persistents (remembering audience ID which is removed from persistent upon deletion).
 133          [$report, $audience] = $persistents;
 134          $report = new report($DB->insert_record(report::TABLE, $report->to_record()));
 135  
 136          $audienceid = $DB->insert_record(audience::TABLE, $audience->to_record());
 137          $audience = new audience($audienceid);
 138  
 139          // Catch the events.
 140          $sink = $this->redirectEvents();
 141          $audience->delete();
 142          $events = $sink->get_events();
 143          $sink->close();
 144  
 145          // Validate the event.
 146          $this->assertCount(1, $events);
 147  
 148          $event = reset($events);
 149          $this->assertInstanceOf(audience_deleted::class, $event);
 150          $this->assertEquals(audience::TABLE, $event->objecttable);
 151          $this->assertEquals($audienceid, $event->objectid);
 152          $this->assertEquals($report->get('id'), $event->other['reportid']);
 153          $this->assertEquals($report->get_context()->id, $event->contextid);
 154      }
 155  }