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\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 report name editable class
  28   *
  29   * @package     core_reportbuilder
  30   * @covers      \core_reportbuilder\output\report_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 report_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          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
  48  
  49          $editable = report_name_editable::update($report->get('id'), 'New name');
  50          $result = $editable->export_for_template($PAGE->get_renderer('core'));
  51          $this->assertEquals('New name', $result['value']);
  52  
  53          // Reload persistent, assert update.
  54          $this->assertEquals('New name', $report->read()->get('name'));
  55      }
  56  
  57      /**
  58       * Test update method for a user without permission to edit reports
  59       */
  60      public function test_update_access_exception(): void {
  61          $this->resetAfterTest();
  62  
  63          /** @var core_reportbuilder_generator $generator */
  64          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  65          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
  66  
  67          $user = $this->getDataGenerator()->create_user();
  68          $this->setUser($user);
  69  
  70          $this->expectException(report_access_exception::class);
  71          $this->expectExceptionMessage('You cannot edit this report');
  72          report_name_editable::update($report->get('id'), 'New name');
  73      }
  74  
  75      /**
  76       * Test update method via component callback
  77       *
  78       * @covers ::core_reportbuilder_inplace_editable
  79       */
  80      public function test_update_callback(): void {
  81          $this->resetAfterTest();
  82          $this->setAdminUser();
  83  
  84          /** @var core_reportbuilder_generator $generator */
  85          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  86          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
  87  
  88          $params = ['reportname', $report->get('id'), 'New name'];
  89          $editable = component_callback('core_reportbuilder', 'inplace_editable', $params);
  90          $this->assertInstanceOf(report_name_editable::class, $editable);
  91  
  92          // Reload persistent, assert update.
  93          $this->assertEquals('New name', $report->read()->get('name'));
  94      }
  95  }