Differences Between: [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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 adding duplicate report condition 391 */ 392 public function test_add_report_condition_duplicate(): 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 // First one is fine. 401 report::add_report_condition($report->get('id'), 'user:email'); 402 403 $this->expectException(invalid_parameter_exception::class); 404 $this->expectExceptionMessage('Duplicate condition'); 405 report::add_report_condition($report->get('id'), 'user:email'); 406 } 407 408 /** 409 * Test deleting report condition 410 */ 411 public function test_delete_report_condition(): void { 412 $this->resetAfterTest(); 413 $this->setAdminUser(); 414 415 /** @var core_reportbuilder_generator $generator */ 416 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 417 $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]); 418 419 // Add two conditions. 420 $conditionfullname = $generator->create_condition([ 421 'reportid' => $report->get('id'), 422 'uniqueidentifier' => 'user:fullname', 423 ]); 424 $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']); 425 426 // Delete the first condition. 427 $result = report::delete_report_condition($report->get('id'), $conditionfullname->get('id')); 428 $this->assertTrue($result); 429 430 // Assert report conditions. 431 $conditions = filter::get_condition_records($report->get('id')); 432 $this->assertCount(1, $conditions); 433 434 $condition = reset($conditions); 435 $this->assertEquals('user:email', $condition->get('uniqueidentifier')); 436 $this->assertEquals(1, $condition->get('filterorder')); 437 } 438 439 /** 440 * Test deleting invalid report condition 441 */ 442 public function test_delete_report_condition_invalid(): void { 443 $this->resetAfterTest(); 444 $this->setAdminUser(); 445 446 /** @var core_reportbuilder_generator $generator */ 447 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 448 $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]); 449 450 $this->expectException(invalid_parameter_exception::class); 451 $this->expectExceptionMessage('Invalid condition'); 452 report::delete_report_condition($report->get('id'), 42); 453 } 454 455 /** 456 * Test re-ordering report condition 457 */ 458 public function test_reorder_report_condition(): void { 459 $this->resetAfterTest(); 460 $this->setAdminUser(); 461 462 /** @var core_reportbuilder_generator $generator */ 463 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 464 $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]); 465 466 // Add four conditions. 467 $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']); 468 $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']); 469 $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']); 470 $conditioncity = $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']); 471 472 // Move the city condition to second position. 473 $result = report::reorder_report_condition($report->get('id'), $conditioncity->get('id'), 2); 474 $this->assertTrue($result); 475 476 // Assert report conditions order. 477 $conditions = filter::get_condition_records($report->get('id'), 'filterorder'); 478 479 $conditionidentifiers = array_map(static function(filter $condition): string { 480 return $condition->get('uniqueidentifier'); 481 }, $conditions); 482 483 $this->assertEquals([ 484 'user:fullname', 485 'user:city', 486 'user:email', 487 'user:country', 488 ], $conditionidentifiers); 489 } 490 491 /** 492 * Test re-ordering invalid report condition 493 */ 494 public function test_reorder_report_condition_invalid(): void { 495 $this->resetAfterTest(); 496 $this->setAdminUser(); 497 498 /** @var core_reportbuilder_generator $generator */ 499 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 500 501 $report = $generator->create_report([ 502 'name' => 'My report', 503 'source' => users::class, 504 'default' => false, 505 ]); 506 507 $this->expectException(invalid_parameter_exception::class); 508 $this->expectExceptionMessage('Invalid condition'); 509 report::reorder_report_condition($report->get('id'), 42, 1); 510 } 511 512 /** 513 * Test adding report filter 514 */ 515 public function test_add_report_filter(): void { 516 $this->resetAfterTest(); 517 $this->setAdminUser(); 518 519 /** @var core_reportbuilder_generator $generator */ 520 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 521 $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]); 522 523 // Add first filter. 524 $filterfullname = report::add_report_filter($report->get('id'), 'user:fullname'); 525 $this->assertTrue(filter::record_exists_select('id = :id AND iscondition = 0', 526 ['id' => $filterfullname->get('id')])); 527 528 $this->assertEquals($report->get('id'), $filterfullname->get('reportid')); 529 $this->assertEquals('user:fullname', $filterfullname->get('uniqueidentifier')); 530 $this->assertEquals(1, $filterfullname->get('filterorder')); 531 532 // Add second filter. 533 $filteremail = report::add_report_filter($report->get('id'), 'user:email'); 534 $this->assertTrue(filter::record_exists_select('id = :id AND iscondition = 0', 535 ['id' => $filteremail->get('id')])); 536 537 $this->assertEquals($report->get('id'), $filteremail->get('reportid')); 538 $this->assertEquals('user:email', $filteremail->get('uniqueidentifier')); 539 $this->assertEquals(2, $filteremail->get('filterorder')); 540 } 541 542 /** 543 * Test adding invalid report filter 544 */ 545 public function test_add_report_filter_invalid(): void { 546 $this->resetAfterTest(); 547 $this->setAdminUser(); 548 549 /** @var core_reportbuilder_generator $generator */ 550 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 551 $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]); 552 553 $this->expectException(invalid_parameter_exception::class); 554 $this->expectExceptionMessage('Invalid filter'); 555 report::add_report_filter($report->get('id'), 'user:invalid'); 556 } 557 558 /** 559 * Test adding duplicate report filter 560 */ 561 public function test_add_report_filter_duplicate(): void { 562 $this->resetAfterTest(); 563 $this->setAdminUser(); 564 565 /** @var core_reportbuilder_generator $generator */ 566 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 567 $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]); 568 569 // First one is fine. 570 report::add_report_filter($report->get('id'), 'user:email'); 571 572 $this->expectException(invalid_parameter_exception::class); 573 $this->expectExceptionMessage('Duplicate filter'); 574 report::add_report_filter($report->get('id'), 'user:email'); 575 } 576 577 /** 578 * Test deleting report filter 579 */ 580 public function test_delete_report_filter(): void { 581 $this->resetAfterTest(); 582 $this->setAdminUser(); 583 584 /** @var core_reportbuilder_generator $generator */ 585 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 586 $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]); 587 588 // Add two filters. 589 $filterfullname = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']); 590 $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']); 591 592 // Delete the first filter. 593 $result = report::delete_report_filter($report->get('id'), $filterfullname->get('id')); 594 $this->assertTrue($result); 595 596 // Assert report filters. 597 $filters = filter::get_filter_records($report->get('id')); 598 $this->assertCount(1, $filters); 599 600 $filter = reset($filters); 601 $this->assertEquals('user:email', $filter->get('uniqueidentifier')); 602 $this->assertEquals(1, $filter->get('filterorder')); 603 } 604 605 /** 606 * Test deleting invalid report filter 607 */ 608 public function test_delete_report_filter_invalid(): void { 609 $this->resetAfterTest(); 610 $this->setAdminUser(); 611 612 /** @var core_reportbuilder_generator $generator */ 613 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 614 $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]); 615 616 $this->expectException(invalid_parameter_exception::class); 617 $this->expectExceptionMessage('Invalid filter'); 618 report::delete_report_filter($report->get('id'), 42); 619 } 620 621 /** 622 * Test re-ordering report filter 623 */ 624 public function test_reorder_report_filter(): void { 625 $this->resetAfterTest(); 626 $this->setAdminUser(); 627 628 /** @var core_reportbuilder_generator $generator */ 629 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 630 $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]); 631 632 // Add four filters. 633 $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']); 634 $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']); 635 $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']); 636 $filtercity = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']); 637 638 // Move the city filter to second position. 639 $result = report::reorder_report_filter($report->get('id'), $filtercity->get('id'), 2); 640 $this->assertTrue($result); 641 642 // Assert report filters order. 643 $filters = filter::get_filter_records($report->get('id'), 'filterorder'); 644 645 $filteridentifiers = array_map(static function(filter $filter): string { 646 return $filter->get('uniqueidentifier'); 647 }, $filters); 648 649 $this->assertEquals([ 650 'user:fullname', 651 'user:city', 652 'user:email', 653 'user:country', 654 ], $filteridentifiers); 655 } 656 657 /** 658 * Test re-ordering invalid report filter 659 */ 660 public function test_reorder_report_filter_invalid(): void { 661 $this->resetAfterTest(); 662 $this->setAdminUser(); 663 664 /** @var core_reportbuilder_generator $generator */ 665 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 666 667 $report = $generator->create_report([ 668 'name' => 'My report', 669 'source' => users::class, 670 'default' => false, 671 ]); 672 673 $this->expectException(invalid_parameter_exception::class); 674 $this->expectExceptionMessage('Invalid filter'); 675 report::reorder_report_filter($report->get('id'), 42, 1); 676 } 677 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body