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; 20 21 use advanced_testcase; 22 use core_reportbuilder_generator; 23 use core_reportbuilder\manager; 24 use core_reportbuilder\local\helpers\report; 25 use core_course\reportbuilder\datasource\courses; 26 27 /** 28 * Unit tests for custom report filters exporter 29 * 30 * @package core_reportbuilder 31 * @covers \core_reportbuilder\external\custom_report_filters_exporter 32 * @copyright 2022 Paul Holden <paulh@moodle.com> 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 class custom_report_filters_exporter_test extends advanced_testcase { 36 37 /** 38 * Test exported data structure 39 */ 40 public function test_export(): void { 41 global $PAGE; 42 43 $this->resetAfterTest(); 44 45 /** @var core_reportbuilder_generator $generator */ 46 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 47 $report = $generator->create_report(['name' => 'My report', 'source' => courses::class, 'default' => false]); 48 49 // Add a couple of filters. 50 $filtercategoryname = $generator->create_filter([ 51 'reportid' => $report->get('id'), 52 'uniqueidentifier' => 'course_category:name', 53 ]); 54 $filtercourseidnumber = $generator->create_filter([ 55 'reportid' => $report->get('id'), 56 'uniqueidentifier' => 'course:idnumber', 57 ]); 58 59 // Move course ID number filter to first place. 60 report::reorder_report_filter($report->get('id'), $filtercourseidnumber->get('id'), 1); 61 62 $reportinstance = manager::get_report_from_persistent($report); 63 64 $exporter = new custom_report_filters_exporter(null, ['report' => $reportinstance]); 65 $export = $exporter->export($PAGE->get_renderer('core_reportbuilder')); 66 67 // The root of the available filters property should contain two entities. 68 $this->assertTrue($export->hasavailablefilters); 69 $this->assertCount(2, $export->availablefilters); 70 71 [$filterscategory, $filterscourse] = $export->availablefilters; 72 73 // Course category filters, assert structure of first item. 74 $this->assertEquals('Course category', $filterscategory['optiongroup']['text']); 75 $this->assertGreaterThanOrEqual(1, $filterscategory['optiongroup']['values']); 76 $this->assertEquals([ 77 'value' => 'course_category:idnumber', 78 'visiblename' => 'Category ID number', 79 ], $filterscategory['optiongroup']['values'][0]); 80 81 // Course filters, assert structure of first item. 82 $this->assertEquals('Course', $filterscourse['optiongroup']['text']); 83 $this->assertGreaterThanOrEqual(1, $filterscourse['optiongroup']['values']); 84 $this->assertEquals([ 85 'value' => 'course:fullname', 86 'visiblename' => 'Course full name', 87 ], $filterscourse['optiongroup']['values'][0]); 88 89 // Make sure the active filters we added, aren't present in available filters. 90 $filterscourseavailable = array_column($filterscourse['optiongroup']['values'], 'value'); 91 $this->assertNotContains('course_category:name', $filterscourseavailable); 92 $this->assertNotContains('course:idnumber', $filterscourseavailable); 93 94 $this->assertTrue($export->hasactivefilters); 95 $this->assertCount(2, $export->activefilters); 96 [$activefiltercourseidnumber, $activefiltercategoryname] = $export->activefilters; 97 98 // Course ID number filter. 99 $this->assertEquals($filtercourseidnumber->get('id'), $activefiltercourseidnumber['id']); 100 $this->assertEquals('Course', $activefiltercourseidnumber['entityname']); 101 $this->assertEquals('Course ID number', $activefiltercourseidnumber['heading']); 102 $this->assertEquals(1, $activefiltercourseidnumber['sortorder']); 103 104 // Course category filter. 105 $this->assertEquals($filtercategoryname->get('id'), $activefiltercategoryname['id']); 106 $this->assertEquals('Course category', $activefiltercategoryname['entityname']); 107 $this->assertEquals('Category name', $activefiltercategoryname['heading']); 108 $this->assertEquals(2, $activefiltercategoryname['sortorder']); 109 110 $this->assertNotEmpty($export->helpicon); 111 } 112 113 /** 114 * Test exported data structure for report with no filters 115 */ 116 public function test_export_no_filters(): void { 117 global $PAGE; 118 119 $this->resetAfterTest(); 120 121 /** @var core_reportbuilder_generator $generator */ 122 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 123 $report = $generator->create_report(['name' => 'My report', 'source' => courses::class, 'default' => false]); 124 125 $reportinstance = manager::get_report_from_persistent($report); 126 127 $exporter = new custom_report_filters_exporter(null, ['report' => $reportinstance]); 128 $export = $exporter->export($PAGE->get_renderer('core_reportbuilder')); 129 130 $this->assertFalse($export->hasactivefilters); 131 $this->assertEmpty($export->activefilters); 132 } 133 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body