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 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\columns\sort;
  20  
  21  use core_reportbuilder_generator;
  22  use external_api;
  23  use externallib_advanced_testcase;
  24  use core_reportbuilder\report_access_exception;
  25  use core_reportbuilder\local\models\column;
  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 toggling report column sorting
  35   *
  36   * @package     core_reportbuilder
  37   * @covers      \core_reportbuilder\external\columns\sort\toggle
  38   * @copyright   2021 Paul Holden <paulh@moodle.com>
  39   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class toggle_test extends externallib_advanced_testcase {
  42  
  43      /**
  44       * Text execute method
  45       */
  46      public function test_execute(): void {
  47          $this->resetAfterTest();
  48          $this->setAdminUser();
  49  
  50          /** @var core_reportbuilder_generator $generator */
  51          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  52          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
  53          $column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
  54  
  55          // Toggle sort descending.
  56          $result = toggle::execute($report->get('id'), $column->get('id'), true, SORT_DESC);
  57          $result = external_api::clean_returnvalue(toggle::execute_returns(), $result);
  58  
  59          $this->assertTrue($result['hassortablecolumns']);
  60          $this->assertCount(4, $result['sortablecolumns']);
  61          $columnid = $column->get('id');
  62          $sortablecolumn = array_filter($result['sortablecolumns'], function(array $column) use($columnid) {
  63              return $column['id'] == $columnid;
  64          });
  65          $sortablecolumn = reset($sortablecolumn);
  66          $this->assertEquals($columnid, $sortablecolumn['id']);
  67          $this->assertEquals('Email address', $sortablecolumn['title']);
  68          $this->assertEquals(SORT_DESC, $sortablecolumn['sortdirection']);
  69          $this->assertEquals(1, $sortablecolumn['sortenabled']);
  70          $this->assertEquals(4, $sortablecolumn['sortorder']);
  71          $this->assertEquals('t/downlong', $sortablecolumn['sorticon']['key']);
  72          $this->assertEquals('moodle', $sortablecolumn['sorticon']['component']);
  73          $str = get_string('columnsortdirectionasc', 'core_reportbuilder', 'Email address');
  74          $this->assertEquals($str, $sortablecolumn['sorticon']['title']);
  75  
  76          // Confirm column was updated.
  77          $columnupdated = new column($column->get('id'));
  78          $this->assertTrue($columnupdated->get('sortenabled'));
  79          $this->assertEquals(SORT_DESC, $columnupdated->get('sortdirection'));
  80      }
  81  
  82      /**
  83       * Test execute method for a user without permission to edit reports
  84       */
  85      public function test_execute_access_exception(): void {
  86          $this->resetAfterTest();
  87  
  88          /** @var core_reportbuilder_generator $generator */
  89          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  90          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
  91          $column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
  92  
  93          $user = $this->getDataGenerator()->create_user();
  94          $this->setUser($user);
  95  
  96          $this->expectException(report_access_exception::class);
  97          $this->expectExceptionMessage('You cannot edit this report');
  98          toggle::execute($report->get('id'), $column->get('id'), true, SORT_DESC);
  99      }
 100  }