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.

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

   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\reports;
  20  
  21  use context_system;
  22  use core_reportbuilder_generator;
  23  use external_api;
  24  use externallib_advanced_testcase;
  25  use core_reportbuilder\report_access_exception;
  26  use core_user\reportbuilder\datasource\users;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  global $CFG;
  31  require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
  32  
  33  /**
  34   * Unit tests of external class for getting reports
  35   *
  36   * @package     core_reportbuilder
  37   * @covers      \core_reportbuilder\external\reports\get
  38   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  39   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class get_test extends externallib_advanced_testcase {
  42  
  43      /**
  44       * Text execute method for edit mode
  45       */
  46      public function test_execute_editmode(): void {
  47          $this->resetAfterTest();
  48          $this->setAdminUser();
  49  
  50          /** @var core_reportbuilder_generator $generator */
  51          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  52  
  53          $report = $generator->create_report([
  54              'name' => 'My report',
  55              'source' => users::class,
  56              'default' => false,
  57          ]);
  58  
  59          // Add two filters.
  60          $filterfullname = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
  61          $filteremail = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
  62  
  63          $result = get::execute($report->get('id'), true);
  64          $result = external_api::clean_returnvalue(get::execute_returns(), $result);
  65  
  66          $this->assertEquals($result['id'], $report->get('id'));
  67          $this->assertEquals($result['name'], 'My report');
  68          $this->assertEquals($result['source'], users::class);
  69          $this->assertNotEmpty($result['table']);
  70          $this->assertNotEmpty($result['javascript']);
  71          $this->assertFalse($result['filterspresent']);
  72          $this->assertEmpty($result['filtersform']);
  73          $this->assertTrue($result['editmode']);
  74  
  75          // Confirm editor-specific data is returned.
  76          $this->assertNotEmpty($result['sidebarmenucards']);
  77          $this->assertNotEmpty($result['conditions']);
  78          $this->assertNotEmpty($result['filters']);
  79          $this->assertTrue($result['filters']['hasavailablefilters']);
  80          $this->assertNotEmpty($result['filters']['availablefilters']);
  81          $this->assertTrue($result['filters']['hasactivefilters']);
  82          $this->assertEquals($filterfullname->get('id'), $result['filters']['activefilters'][0]['id']);
  83          $this->assertEquals($filteremail->get('id'), $result['filters']['activefilters'][1]['id']);
  84          $this->assertNotEmpty($result['sorting']);
  85          $this->assertNotEmpty($result['cardview']);
  86      }
  87  
  88      /**
  89       * Text execute method for preview mode
  90       */
  91      public function test_execute_previewmode(): void {
  92          $this->resetAfterTest();
  93          $this->setAdminUser();
  94  
  95          /** @var core_reportbuilder_generator $generator */
  96          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  97  
  98          $report = $generator->create_report([
  99              'name' => 'My report',
 100              'source' => users::class,
 101              'default' => false,
 102          ]);
 103  
 104          // Add two filters.
 105          $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
 106          $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 107  
 108          $result = get::execute($report->get('id'), false);
 109          $result = external_api::clean_returnvalue(get::execute_returns(), $result);
 110  
 111          $this->assertEquals($result['id'], $report->get('id'));
 112          $this->assertEquals($result['name'], 'My report');
 113          $this->assertEquals($result['source'], users::class);
 114          $this->assertNotEmpty($result['table']);
 115          $this->assertNotEmpty($result['javascript']);
 116          $this->assertTrue($result['filterspresent']);
 117          $this->assertNotEmpty($result['filtersform']);
 118          $this->assertFalse($result['editmode']);
 119  
 120          // Confirm editor-specific data is not returned.
 121          $this->assertArrayNotHasKey('sidebarmenucards', $result);
 122          $this->assertArrayNotHasKey('conditions', $result);
 123          $this->assertArrayNotHasKey('filters', $result);
 124          $this->assertArrayNotHasKey('sorting', $result);
 125          $this->assertArrayNotHasKey('cardview', $result);
 126      }
 127  
 128      /**
 129       * Test execute method for a user without permission to edit reports
 130       */
 131      public function test_execute_access_exception(): void {
 132          $this->resetAfterTest();
 133  
 134          /** @var core_reportbuilder_generator $generator */
 135          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 136          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
 137  
 138          $user = $this->getDataGenerator()->create_user();
 139          $this->setUser($user);
 140  
 141          $this->expectException(report_access_exception::class);
 142          $this->expectExceptionMessage('You cannot edit this report');
 143          get::execute($report->get('id'), true);
 144      }
 145  
 146      /**
 147       * Test execute method for a user without permission to view reports
 148       */
 149      public function test_execute_view_access_exception(): void {
 150          $this->resetAfterTest();
 151  
 152          /** @var core_reportbuilder_generator $generator */
 153          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 154          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
 155  
 156          $user = $this->getDataGenerator()->create_user();
 157          $contextid = context_system::instance()->id;
 158          $roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
 159          assign_capability('moodle/reportbuilder:view', CAP_PROHIBIT, $roleid, $contextid);
 160          role_assign($roleid, $user->id, $contextid);
 161  
 162          $this->setUser($user);
 163  
 164          $this->expectException(report_access_exception::class);
 165          $this->expectExceptionMessage('You cannot view this report');
 166          get::execute($report->get('id'), false);
 167      }
 168  }