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 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\filters;
  20  
  21  use core_reportbuilder\manager;
  22  use core_reportbuilder_generator;
  23  use external_api;
  24  use externallib_advanced_testcase;
  25  use core_reportbuilder\report_access_exception;
  26  use core_reportbuilder\local\models\filter;
  27  use core_user\reportbuilder\datasource\users;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  global $CFG;
  32  require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
  33  
  34  /**
  35   * Unit tests of external class for adding report filters
  36   *
  37   * @package     core_reportbuilder
  38   * @covers      \core_reportbuilder\external\filters\add
  39   * @copyright   2021 Paul Holden <paulh@moodle.com>
  40   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class add_test extends externallib_advanced_testcase {
  43  
  44      /**
  45       * Text execute method
  46       */
  47      public function test_execute(): void {
  48          $this->resetAfterTest();
  49          $this->setAdminUser();
  50  
  51          /** @var core_reportbuilder_generator $generator */
  52          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  53  
  54          $report = $generator->create_report([
  55              'name' => 'My report',
  56              'source' => users::class,
  57              'default' => false,
  58          ]);
  59  
  60          // Add filter.
  61          $result = add::execute($report->get('id'), 'user:fullname');
  62          $result = external_api::clean_returnvalue(add::execute_returns(), $result);
  63          $this->assertTrue($result['hasavailablefilters']);
  64          $this->assertEquals('User', $result['availablefilters'][0]['optiongroup']['text']);
  65          $this->assertNotEmpty($result['availablefilters'][0]['optiongroup']['values']);
  66          $this->assertTrue($result['hasactivefilters']);
  67          $this->assertCount(1, $result['activefilters']);
  68          $this->assertEquals('Full name', $result['activefilters'][0]['heading']);
  69  
  70          // Assert report filters.
  71          $filters = filter::get_filter_records($report->get('id'));
  72          $this->assertCount(1, $filters);
  73          $this->assertEquals('user:fullname', reset($filters)->get('uniqueidentifier'));
  74      }
  75  
  76      /**
  77       * Test execute method for a user without permission to edit reports
  78       */
  79      public function test_execute_access_exception(): void {
  80          $this->resetAfterTest();
  81  
  82          /** @var core_reportbuilder_generator $generator */
  83          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  84          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
  85  
  86          $user = $this->getDataGenerator()->create_user();
  87          $this->setUser($user);
  88  
  89          $this->expectException(report_access_exception::class);
  90          $this->expectExceptionMessage('You cannot edit this report');
  91          add::execute($report->get('id'), 'user:fullname');
  92      }
  93  }