Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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\external\systemreports;
  20  
  21  use core\context\system;
  22  use core_external\external_api;
  23  use externallib_advanced_testcase;
  24  use core_reportbuilder\local\systemreports\reports_list;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
  30  
  31  /**
  32   * Unit tests of external class for validating access to a system report
  33   *
  34   * @package     core_reportbuilder
  35   * @covers      \core_reportbuilder\external\systemreports\can_view
  36   * @copyright   2023 Paul Holden <paulh@moodle.com>
  37   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class can_view_test extends externallib_advanced_testcase {
  40  
  41      /**
  42       * Text execute method
  43       */
  44      public function test_execute(): void {
  45          $this->resetAfterTest();
  46          $this->setAdminUser();
  47  
  48          $result = can_view::execute(reports_list::class, ['contextid' => system::instance()->id], '', '', 0, []);
  49          $result = external_api::clean_returnvalue(can_view::execute_returns(), $result);
  50  
  51          $this->assertTrue($result);
  52      }
  53  
  54      /**
  55       * Test execute method for a user without permission to view report
  56       */
  57      public function test_execute_access_none(): void {
  58          global $DB;
  59  
  60          $this->resetAfterTest();
  61  
  62          $user = $this->getDataGenerator()->create_user();
  63          $this->setUser($user);
  64  
  65          $userrole = $DB->get_field('role', 'id', ['shortname' => 'user']);
  66          unassign_capability('moodle/reportbuilder:view', $userrole, system::instance());
  67  
  68          $result = can_view::execute(reports_list::class, ['contextid' => system::instance()->id], '', '', 0, []);
  69          $result = external_api::clean_returnvalue(can_view::execute_returns(), $result);
  70  
  71          $this->assertFalse($result);
  72      }
  73  }