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