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\output;
  20  
  21  use advanced_testcase;
  22  use core_reportbuilder_generator;
  23  use core_reportbuilder\report_access_exception;
  24  use core_user\reportbuilder\datasource\users;
  25  
  26  /**
  27   * Unit tests for the schedule name editable class
  28   *
  29   * @package     core_reportbuilder
  30   * @covers      \core_reportbuilder\output\schedule_name_editable
  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 schedule_name_editable_test extends advanced_testcase {
  35  
  36      /**
  37       * Test update method
  38       */
  39      public function test_update(): void {
  40          global $PAGE;
  41  
  42          $this->resetAfterTest();
  43          $this->setAdminUser();
  44  
  45          /** @var core_reportbuilder_generator $generator */
  46          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  47  
  48          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
  49          $schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
  50  
  51          $editable = schedule_name_editable::update($schedule->get('id'), 'New name');
  52          $result = $editable->export_for_template($PAGE->get_renderer('core'));
  53          $this->assertEquals('New name', $result['value']);
  54  
  55          // Reload persistent, assert update.
  56          $this->assertEquals('New name', $schedule->read()->get('name'));
  57      }
  58  
  59      /**
  60       * Test update method for a user without permission to edit reports
  61       */
  62      public function test_update_access_exception(): void {
  63          $this->resetAfterTest();
  64  
  65          /** @var core_reportbuilder_generator $generator */
  66          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  67  
  68          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
  69          $schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
  70  
  71          $user = $this->getDataGenerator()->create_user();
  72          $this->setUser($user);
  73  
  74          $this->expectException(report_access_exception::class);
  75          $this->expectExceptionMessage('You cannot edit this report');
  76          schedule_name_editable::update($schedule->get('id'), 'New name');
  77      }
  78  
  79      /**
  80       * Test update method via component callback
  81       *
  82       * @covers ::core_reportbuilder_inplace_editable
  83       */
  84      public function test_update_callback(): void {
  85          $this->resetAfterTest();
  86          $this->setAdminUser();
  87  
  88          /** @var core_reportbuilder_generator $generator */
  89          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  90  
  91          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
  92          $schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
  93  
  94          $params = ['schedulename', $schedule->get('id'), 'New name'];
  95          $editable = component_callback('core_reportbuilder', 'inplace_editable', $params);
  96          $this->assertInstanceOf(schedule_name_editable::class, $editable);
  97  
  98          // Reload persistent, assert update.
  99          $this->assertEquals('New name', $schedule->read()->get('name'));
 100      }
 101  }