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.

Differences Between: [Versions 400 and 402] [Versions 401 and 402]

   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\external\columns\sort;
  20  
  21  use core_reportbuilder_generator;
  22  use core_external\external_api;
  23  use externallib_advanced_testcase;
  24  use core_reportbuilder\report_access_exception;
  25  use core_user\reportbuilder\datasource\users;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
  31  
  32  /**
  33   * Unit tests of external class for retrieving report column sorting
  34   *
  35   * @package     core_reportbuilder
  36   * @covers      \core_reportbuilder\external\columns\sort\get
  37   * @copyright   2021 Paul Holden <paulh@moodle.com>
  38   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class get_test extends externallib_advanced_testcase {
  41  
  42      /**
  43       * Text execute method
  44       */
  45      public function test_execute(): void {
  46          $this->resetAfterTest();
  47          $this->setAdminUser();
  48  
  49          /** @var core_reportbuilder_generator $generator */
  50          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  51          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
  52  
  53          // Add some columns (note 'user:picture' is not a sortable column).
  54          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
  55          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
  56          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:picture']);
  57  
  58          $result = get::execute($report->get('id'));
  59          $result = external_api::clean_returnvalue(get::execute_returns(), $result);
  60  
  61          $this->assertTrue($result['hassortablecolumns']);
  62  
  63          $sortablecolumntitles = array_column($result['sortablecolumns'], 'title');
  64          $this->assertEquals([
  65              'Full name',
  66              'Email address',
  67          ], $sortablecolumntitles);
  68      }
  69  
  70      /**
  71       * Test execute method for a user without permission to edit reports
  72       */
  73      public function test_execute_access_exception(): void {
  74          $this->resetAfterTest();
  75  
  76          /** @var core_reportbuilder_generator $generator */
  77          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  78          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
  79  
  80          $user = $this->getDataGenerator()->create_user();
  81          $this->setUser($user);
  82  
  83          $this->expectException(report_access_exception::class);
  84          $this->expectExceptionMessage('You cannot edit this report');
  85          get::execute($report->get('id'));
  86      }
  87  }