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 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\local\helpers;
  20  
  21  use advanced_testcase;
  22  use core_reportbuilder_generator;
  23  use invalid_parameter_exception;
  24  use core_reportbuilder\local\models\column;
  25  use core_reportbuilder\local\models\filter;
  26  use core_user\reportbuilder\datasource\users;
  27  
  28  /**
  29   * Unit tests for the report helper class
  30   *
  31   * @package     core_reportbuilder
  32   * @covers      \core_reportbuilder\local\helpers\report
  33   * @copyright   2021 Paul Holden <paulh@moodle.com>
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class report_test extends advanced_testcase {
  37  
  38      /**
  39       * Test deleting report
  40       */
  41      public function test_delete_report(): void {
  42          $this->resetAfterTest();
  43          $this->setAdminUser();
  44  
  45          /** @var core_reportbuilder_generator $generator */
  46          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  47  
  48          // Create Report1 and add some elements.
  49          $report1 = $generator->create_report(['name' => 'My report 1', 'source' => users::class, 'default' => false]);
  50          $column1 = $generator->create_column(['reportid' => $report1->get('id'), 'uniqueidentifier' => 'user:email']);
  51          $filter1 = $generator->create_filter(['reportid' => $report1->get('id'), 'uniqueidentifier' => 'user:email']);
  52          $condition1 = $generator->create_condition(['reportid' => $report1->get('id'), 'uniqueidentifier' => 'user:email']);
  53  
  54          // Create Report2 and add some elements.
  55          $report2 = $generator->create_report(['name' => 'My report 2', 'source' => users::class, 'default' => false]);
  56          $column2 = $generator->create_column(['reportid' => $report2->get('id'), 'uniqueidentifier' => 'user:email']);
  57          $filter2 = $generator->create_filter(['reportid' => $report2->get('id'), 'uniqueidentifier' => 'user:email']);
  58          $condition2 = $generator->create_condition(['reportid' => $report2->get('id'), 'uniqueidentifier' => 'user:email']);
  59  
  60          // Delete Report1.
  61          report::delete_report($report1->get('id'));
  62  
  63          // Make sure Report1, and all it's elements are deleted.
  64          $this->assertFalse($report1::record_exists($report1->get('id')));
  65          $this->assertFalse($column1::record_exists($column1->get('id')));
  66          $this->assertFalse($filter1::record_exists($filter1->get('id')));
  67          $this->assertFalse($condition1::record_exists($condition1->get('id')));
  68  
  69          // Make sure Report2, and all it's elements still exist.
  70          $this->assertTrue($report2::record_exists($report2->get('id')));
  71          $this->assertTrue($column2::record_exists($column2->get('id')));
  72          $this->assertTrue($filter2::record_exists($filter2->get('id')));
  73          $this->assertTrue($condition2::record_exists($condition2->get('id')));
  74      }
  75  
  76      /**
  77       * Testing adding report column
  78       */
  79      public function test_add_report_column(): void {
  80          $this->resetAfterTest();
  81          $this->setAdminUser();
  82  
  83          /** @var core_reportbuilder_generator $generator */
  84          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  85  
  86          $report = $generator->create_report([
  87              'name' => 'My report',
  88              'source' => users::class,
  89              'default' => false,
  90          ]);
  91  
  92          // Add first column.
  93          $columnfullname = report::add_report_column($report->get('id'), 'user:fullname');
  94          $this->assertTrue(column::record_exists($columnfullname->get('id')));
  95  
  96          $this->assertEquals($report->get('id'), $columnfullname->get('reportid'));
  97          $this->assertEquals('user:fullname', $columnfullname->get('uniqueidentifier'));
  98          $this->assertEquals(1, $columnfullname->get('columnorder'));
  99          $this->assertEquals(1, $columnfullname->get('sortorder'));
 100  
 101          // Add second column.
 102          $columnemail = report::add_report_column($report->get('id'), 'user:email');
 103          $this->assertTrue(column::record_exists($columnemail->get('id')));
 104  
 105          $this->assertEquals($report->get('id'), $columnemail->get('reportid'));
 106          $this->assertEquals('user:email', $columnemail->get('uniqueidentifier'));
 107          $this->assertEquals(2, $columnemail->get('columnorder'));
 108          $this->assertEquals(2, $columnemail->get('sortorder'));
 109      }
 110  
 111      /**
 112       * Test adding invalid report column
 113       */
 114      public function test_add_report_column_invalid(): void {
 115          $this->resetAfterTest();
 116          $this->setAdminUser();
 117  
 118          /** @var core_reportbuilder_generator $generator */
 119          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 120  
 121          $report = $generator->create_report([
 122              'name' => 'My report',
 123              'source' => users::class,
 124              'default' => false,
 125          ]);
 126  
 127          $this->expectException(invalid_parameter_exception::class);
 128          $this->expectExceptionMessage('Invalid column');
 129          report::add_report_column($report->get('id'), 'user:invalid');
 130      }
 131  
 132      /**
 133       * Testing deleting report column
 134       */
 135      public function test_delete_report_column(): void {
 136          $this->resetAfterTest();
 137          $this->setAdminUser();
 138  
 139          /** @var core_reportbuilder_generator $generator */
 140          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 141  
 142          $report = $generator->create_report([
 143              'name' => 'My report',
 144              'source' => users::class,
 145              'default' => false,
 146          ]);
 147  
 148          // Add two columns.
 149          $columnfullname = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
 150          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 151  
 152          // Delete the first column.
 153          $result = report::delete_report_column($report->get('id'), $columnfullname->get('id'));
 154          $this->assertTrue($result);
 155  
 156          // Assert report columns.
 157          $columns = column::get_records(['reportid' => $report->get('id')]);
 158          $this->assertCount(1, $columns);
 159  
 160          $column = reset($columns);
 161          $this->assertEquals('user:email', $column->get('uniqueidentifier'));
 162          $this->assertEquals(1, $column->get('columnorder'));
 163      }
 164  
 165      /**
 166       * Testing deleting invalid report column
 167       */
 168      public function test_delete_report_column_invalid(): void {
 169          $this->resetAfterTest();
 170          $this->setAdminUser();
 171  
 172          /** @var core_reportbuilder_generator $generator */
 173          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 174  
 175          $report = $generator->create_report([
 176              'name' => 'My report',
 177              'source' => users::class,
 178              'default' => false,
 179          ]);
 180  
 181          $this->expectException(invalid_parameter_exception::class);
 182          $this->expectExceptionMessage('Invalid column');
 183          report::delete_report_column($report->get('id'), 42);
 184      }
 185  
 186      /**
 187       * Testing re-ordering report column
 188       */
 189      public function test_reorder_report_column(): void {
 190          $this->resetAfterTest();
 191          $this->setAdminUser();
 192  
 193          /** @var core_reportbuilder_generator $generator */
 194          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 195  
 196          $report = $generator->create_report([
 197              'name' => 'My report',
 198              'source' => users::class,
 199              'default' => false,
 200          ]);
 201  
 202          // Add four columns.
 203          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
 204          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 205          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
 206          $columncity = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
 207  
 208          // Move the city column to second position.
 209          $result = report::reorder_report_column($report->get('id'), $columncity->get('id'), 2);
 210          $this->assertTrue($result);
 211  
 212          // Assert report columns order.
 213          $columns = column::get_records(['reportid' => $report->get('id')], 'columnorder');
 214  
 215          $columnidentifiers = array_map(static function(column $column): string {
 216              return $column->get('uniqueidentifier');
 217          }, $columns);
 218  
 219          $this->assertEquals([
 220              'user:fullname',
 221              'user:city',
 222              'user:email',
 223              'user:country',
 224          ], $columnidentifiers);
 225      }
 226  
 227      /**
 228       * Testing re-ordering invalid report column
 229       */
 230      public function test_reorder_report_column_invalid(): void {
 231          $this->resetAfterTest();
 232          $this->setAdminUser();
 233  
 234          /** @var core_reportbuilder_generator $generator */
 235          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 236  
 237          $report = $generator->create_report([
 238              'name' => 'My report',
 239              'source' => users::class,
 240              'default' => false,
 241          ]);
 242  
 243          $this->expectException(invalid_parameter_exception::class);
 244          $this->expectExceptionMessage('Invalid column');
 245          report::reorder_report_column($report->get('id'), 42, 1);
 246      }
 247  
 248      /**
 249       * Testing re-ordering report column sorting
 250       */
 251      public function test_reorder_report_column_sorting(): void {
 252          $this->resetAfterTest();
 253          $this->setAdminUser();
 254  
 255          /** @var core_reportbuilder_generator $generator */
 256          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 257          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 258  
 259          // Add four columns.
 260          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
 261          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 262          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
 263          $columncity = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
 264  
 265          // Move the city column to second position.
 266          $result = report::reorder_report_column_sorting($report->get('id'), $columncity->get('id'), 2);
 267          $this->assertTrue($result);
 268  
 269          // Assert report columns order.
 270          $columns = column::get_records(['reportid' => $report->get('id')], 'sortorder');
 271  
 272          $columnidentifiers = array_map(static function(column $column): string {
 273              return $column->get('uniqueidentifier');
 274          }, $columns);
 275  
 276          $this->assertEquals([
 277              'user:fullname',
 278              'user:city',
 279              'user:email',
 280              'user:country',
 281          ], $columnidentifiers);
 282      }
 283  
 284      /**
 285       * Testing re-ordering invalid report column sorting
 286       */
 287      public function test_reorder_report_column_sorting_invalid(): void {
 288          $this->resetAfterTest();
 289          $this->setAdminUser();
 290  
 291          /** @var core_reportbuilder_generator $generator */
 292          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 293  
 294          $report = $generator->create_report([
 295              'name' => 'My report',
 296              'source' => users::class,
 297              'default' => false,
 298          ]);
 299  
 300          $this->expectException(invalid_parameter_exception::class);
 301          $this->expectExceptionMessage('Invalid column');
 302          report::reorder_report_column_sorting($report->get('id'), 42, 1);
 303      }
 304  
 305      /**
 306       * Test toggling of report column sorting
 307       */
 308      public function test_toggle_report_column_sorting(): void {
 309          $this->resetAfterTest();
 310          $this->setAdminUser();
 311  
 312          /** @var core_reportbuilder_generator $generator */
 313          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 314          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
 315          $column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 316  
 317          // Toggle sort descending.
 318          $result = report::toggle_report_column_sorting($report->get('id'), $column->get('id'), true, SORT_DESC);
 319          $this->assertTrue($result);
 320  
 321          // Confirm column was updated.
 322          $columnupdated = new column($column->get('id'));
 323          $this->assertTrue($columnupdated->get('sortenabled'));
 324          $this->assertEquals(SORT_DESC, $columnupdated->get('sortdirection'));
 325      }
 326  
 327      /**
 328       * Test toggling of report column sorting with invalid column
 329       */
 330      public function test_toggle_report_column_sorting_invalid(): void {
 331          $this->resetAfterTest();
 332          $this->setAdminUser();
 333  
 334          /** @var core_reportbuilder_generator $generator */
 335          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 336          $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
 337  
 338          $this->expectException(invalid_parameter_exception::class);
 339          $this->expectExceptionMessage('Invalid column');
 340          report::toggle_report_column_sorting($report->get('id'), 42, false);
 341      }
 342  
 343      /**
 344       * Test adding report condition
 345       */
 346      public function test_add_report_condition(): void {
 347          $this->resetAfterTest();
 348          $this->setAdminUser();
 349  
 350          /** @var core_reportbuilder_generator $generator */
 351          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 352          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 353  
 354          // Add first condition.
 355          $conditionfullname = report::add_report_condition($report->get('id'), 'user:fullname');
 356          $this->assertTrue(filter::record_exists_select('id = :id AND iscondition = 1',
 357              ['id' => $conditionfullname->get('id')]));
 358  
 359          $this->assertEquals($report->get('id'), $conditionfullname->get('reportid'));
 360          $this->assertEquals('user:fullname', $conditionfullname->get('uniqueidentifier'));
 361          $this->assertEquals(1, $conditionfullname->get('filterorder'));
 362  
 363          // Add second condition.
 364          $conditionemail = report::add_report_condition($report->get('id'), 'user:email');
 365          $this->assertTrue(filter::record_exists_select('id = :id AND iscondition = 1',
 366              ['id' => $conditionemail->get('id')]));
 367  
 368          $this->assertEquals($report->get('id'), $conditionemail->get('reportid'));
 369          $this->assertEquals('user:email', $conditionemail->get('uniqueidentifier'));
 370          $this->assertEquals(2, $conditionemail->get('filterorder'));
 371      }
 372  
 373      /**
 374       * Test adding invalid report condition
 375       */
 376      public function test_add_report_condition_invalid(): void {
 377          $this->resetAfterTest();
 378          $this->setAdminUser();
 379  
 380          /** @var core_reportbuilder_generator $generator */
 381          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 382          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 383  
 384          $this->expectException(invalid_parameter_exception::class);
 385          $this->expectExceptionMessage('Invalid condition');
 386          report::add_report_condition($report->get('id'), 'user:invalid');
 387      }
 388  
 389      /**
 390       * Test deleting report condition
 391       */
 392      public function test_delete_report_condition(): void {
 393          $this->resetAfterTest();
 394          $this->setAdminUser();
 395  
 396          /** @var core_reportbuilder_generator $generator */
 397          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 398          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 399  
 400          // Add two conditions.
 401          $conditionfullname = $generator->create_condition([
 402              'reportid' => $report->get('id'),
 403              'uniqueidentifier' => 'user:fullname',
 404          ]);
 405          $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 406  
 407          // Delete the first condition.
 408          $result = report::delete_report_condition($report->get('id'), $conditionfullname->get('id'));
 409          $this->assertTrue($result);
 410  
 411          // Assert report conditions.
 412          $conditions = filter::get_condition_records($report->get('id'));
 413          $this->assertCount(1, $conditions);
 414  
 415          $condition = reset($conditions);
 416          $this->assertEquals('user:email', $condition->get('uniqueidentifier'));
 417          $this->assertEquals(1, $condition->get('filterorder'));
 418      }
 419  
 420      /**
 421       * Test deleting invalid report condition
 422       */
 423      public function test_delete_report_condition_invalid(): void {
 424          $this->resetAfterTest();
 425          $this->setAdminUser();
 426  
 427          /** @var core_reportbuilder_generator $generator */
 428          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 429          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 430  
 431          $this->expectException(invalid_parameter_exception::class);
 432          $this->expectExceptionMessage('Invalid condition');
 433          report::delete_report_condition($report->get('id'), 42);
 434      }
 435  
 436      /**
 437       * Test re-ordering report condition
 438       */
 439      public function test_reorder_report_condition(): void {
 440          $this->resetAfterTest();
 441          $this->setAdminUser();
 442  
 443          /** @var core_reportbuilder_generator $generator */
 444          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 445          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 446  
 447          // Add four conditions.
 448          $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
 449          $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 450          $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
 451          $conditioncity = $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
 452  
 453          // Move the city condition to second position.
 454          $result = report::reorder_report_condition($report->get('id'), $conditioncity->get('id'), 2);
 455          $this->assertTrue($result);
 456  
 457          // Assert report conditions order.
 458          $conditions = filter::get_condition_records($report->get('id'), 'filterorder');
 459  
 460          $conditionidentifiers = array_map(static function(filter $condition): string {
 461              return $condition->get('uniqueidentifier');
 462          }, $conditions);
 463  
 464          $this->assertEquals([
 465              'user:fullname',
 466              'user:city',
 467              'user:email',
 468              'user:country',
 469          ], $conditionidentifiers);
 470      }
 471  
 472      /**
 473       * Test re-ordering invalid report condition
 474       */
 475      public function test_reorder_report_condition_invalid(): void {
 476          $this->resetAfterTest();
 477          $this->setAdminUser();
 478  
 479          /** @var core_reportbuilder_generator $generator */
 480          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 481  
 482          $report = $generator->create_report([
 483              'name' => 'My report',
 484              'source' => users::class,
 485              'default' => false,
 486          ]);
 487  
 488          $this->expectException(invalid_parameter_exception::class);
 489          $this->expectExceptionMessage('Invalid condition');
 490          report::reorder_report_condition($report->get('id'), 42, 1);
 491      }
 492  
 493      /**
 494       * Test adding report filter
 495       */
 496      public function test_add_report_filter(): void {
 497          $this->resetAfterTest();
 498          $this->setAdminUser();
 499  
 500          /** @var core_reportbuilder_generator $generator */
 501          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 502          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 503  
 504          // Add first filter.
 505          $filterfullname = report::add_report_filter($report->get('id'), 'user:fullname');
 506          $this->assertTrue(filter::record_exists_select('id = :id AND iscondition = 0',
 507              ['id' => $filterfullname->get('id')]));
 508  
 509          $this->assertEquals($report->get('id'), $filterfullname->get('reportid'));
 510          $this->assertEquals('user:fullname', $filterfullname->get('uniqueidentifier'));
 511          $this->assertEquals(1, $filterfullname->get('filterorder'));
 512  
 513          // Add second filter.
 514          $filteremail = report::add_report_filter($report->get('id'), 'user:email');
 515          $this->assertTrue(filter::record_exists_select('id = :id AND iscondition = 0',
 516              ['id' => $filteremail->get('id')]));
 517  
 518          $this->assertEquals($report->get('id'), $filteremail->get('reportid'));
 519          $this->assertEquals('user:email', $filteremail->get('uniqueidentifier'));
 520          $this->assertEquals(2, $filteremail->get('filterorder'));
 521      }
 522  
 523      /**
 524       * Test adding invalid report filter
 525       */
 526      public function test_add_report_filter_invalid(): void {
 527          $this->resetAfterTest();
 528          $this->setAdminUser();
 529  
 530          /** @var core_reportbuilder_generator $generator */
 531          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 532          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 533  
 534          $this->expectException(invalid_parameter_exception::class);
 535          $this->expectExceptionMessage('Invalid filter');
 536          report::add_report_filter($report->get('id'), 'user:invalid');
 537      }
 538  
 539      /**
 540       * Test deleting report filter
 541       */
 542      public function test_delete_report_filter(): void {
 543          $this->resetAfterTest();
 544          $this->setAdminUser();
 545  
 546          /** @var core_reportbuilder_generator $generator */
 547          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 548          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 549  
 550          // Add two filters.
 551          $filterfullname = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
 552          $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 553  
 554          // Delete the first filter.
 555          $result = report::delete_report_filter($report->get('id'), $filterfullname->get('id'));
 556          $this->assertTrue($result);
 557  
 558          // Assert report filters.
 559          $filters = filter::get_filter_records($report->get('id'));
 560          $this->assertCount(1, $filters);
 561  
 562          $filter = reset($filters);
 563          $this->assertEquals('user:email', $filter->get('uniqueidentifier'));
 564          $this->assertEquals(1, $filter->get('filterorder'));
 565      }
 566  
 567      /**
 568       * Test deleting invalid report filter
 569       */
 570      public function test_delete_report_filter_invalid(): void {
 571          $this->resetAfterTest();
 572          $this->setAdminUser();
 573  
 574          /** @var core_reportbuilder_generator $generator */
 575          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 576          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 577  
 578          $this->expectException(invalid_parameter_exception::class);
 579          $this->expectExceptionMessage('Invalid filter');
 580          report::delete_report_filter($report->get('id'), 42);
 581      }
 582  
 583      /**
 584       * Test re-ordering report filter
 585       */
 586      public function test_reorder_report_filter(): void {
 587          $this->resetAfterTest();
 588          $this->setAdminUser();
 589  
 590          /** @var core_reportbuilder_generator $generator */
 591          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 592          $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 593  
 594          // Add four filters.
 595          $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
 596          $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 597          $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
 598          $filtercity = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
 599  
 600          // Move the city filter to second position.
 601          $result = report::reorder_report_filter($report->get('id'), $filtercity->get('id'), 2);
 602          $this->assertTrue($result);
 603  
 604          // Assert report filters order.
 605          $filters = filter::get_filter_records($report->get('id'), 'filterorder');
 606  
 607          $filteridentifiers = array_map(static function(filter $filter): string {
 608              return $filter->get('uniqueidentifier');
 609          }, $filters);
 610  
 611          $this->assertEquals([
 612              'user:fullname',
 613              'user:city',
 614              'user:email',
 615              'user:country',
 616          ], $filteridentifiers);
 617      }
 618  
 619      /**
 620       * Test re-ordering invalid report filter
 621       */
 622      public function test_reorder_report_filter_invalid(): void {
 623          $this->resetAfterTest();
 624          $this->setAdminUser();
 625  
 626          /** @var core_reportbuilder_generator $generator */
 627          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 628  
 629          $report = $generator->create_report([
 630              'name' => 'My report',
 631              'source' => users::class,
 632              'default' => false,
 633          ]);
 634  
 635          $this->expectException(invalid_parameter_exception::class);
 636          $this->expectExceptionMessage('Invalid filter');
 637          report::reorder_report_filter($report->get('id'), 42, 1);
 638      }
 639  }