Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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->assertEquals(1, $result['editmode']);
  74          $this->assertTrue($result['filters']['hasavailablefilters']);
  75          $this->assertNotEmpty($result['filters']['availablefilters']);
  76          $this->assertTrue($result['filters']['hasactivefilters']);
  77          $this->assertEquals($filterfullname->get('id'), $result['filters']['activefilters'][0]['id']);
  78          $this->assertEquals($filteremail->get('id'), $result['filters']['activefilters'][1]['id']);
  79      }
  80  
  81      /**
  82       * Text execute method for preview mode
  83       */
  84      public function test_execute_previewmode(): void {
  85          $this->resetAfterTest();
  86          $this->setAdminUser();
  87  
  88          /** @var core_reportbuilder_generator $generator */
  89          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  90  
  91          $report = $generator->create_report([
  92              'name' => 'My report',
  93              'source' => users::class,
  94              'default' => false,
  95          ]);
  96  
  97          // Add two filters.
  98          $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
  99          $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 100  
 101          $result = get::execute($report->get('id'), false);
 102          $result = external_api::clean_returnvalue(get::execute_returns(), $result);
 103  
 104          $this->assertEquals($result['id'], $report->get('id'));
 105          $this->assertEquals($result['name'], 'My report');
 106          $this->assertEquals($result['source'], users::class);
 107          $this->assertNotEmpty($result['table']);
 108          $this->assertNotEmpty($result['javascript']);
 109          $this->assertTrue($result['filterspresent']);
 110          $this->assertNotEmpty($result['filtersform']);
 111          $this->assertEquals(0, $result['editmode']);
 112          $this->assertEmpty($result['filters']);
 113          $this->assertEmpty($result['conditions']);
 114          $this->assertEmpty($result['sorting']);
 115          $this->assertEmpty($result['cardview']);
 116      }
 117  
 118      /**
 119       * Test execute method for a user without permission to edit reports
 120       */
 121      public function test_execute_access_exception(): void {
 122          $this->resetAfterTest();
 123  
 124          /** @var core_reportbuilder_generator $generator */
 125          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 126          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
 127  
 128          $user = $this->getDataGenerator()->create_user();
 129          $this->setUser($user);
 130  
 131          $this->expectException(report_access_exception::class);
 132          $this->expectExceptionMessage('You cannot edit this report');
 133          get::execute($report->get('id'), true);
 134      }
 135  
 136      /**
 137       * Test execute method for a user without permission to view reports
 138       */
 139      public function test_execute_view_access_exception(): void {
 140          $this->resetAfterTest();
 141  
 142          /** @var core_reportbuilder_generator $generator */
 143          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 144          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
 145  
 146          $user = $this->getDataGenerator()->create_user();
 147          $contextid = context_system::instance()->id;
 148          $roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
 149          assign_capability('moodle/reportbuilder:view', CAP_PROHIBIT, $roleid, $contextid);
 150          role_assign($roleid, $user->id, $contextid);
 151  
 152          $this->setUser($user);
 153  
 154          $this->expectException(report_access_exception::class);
 155          $this->expectExceptionMessage('You cannot view this report');
 156          get::execute($report->get('id'), false);
 157      }
 158  }