2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 declare(strict_types
=1);
19 namespace core_reportbuilder\external
;
21 use advanced_testcase
;
22 use core_reportbuilder_generator
;
24 use core_reportbuilder\local\helpers\user_filter_manager
;
25 use core_reportbuilder\local\filters\text
;
26 use core_user\reportbuilder\datasource\users
;
29 * Unit tests for custom report exporter
31 * @package core_reportbuilder
32 * @covers \core_reportbuilder\external\custom_report_exporter
33 * @copyright 2022 Paul Holden <paulh@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class custom_report_exporter_test
extends advanced_testcase
{
39 * Test exported data structure when editing a report
41 public function test_export_editing(): void
{
44 $this->resetAfterTest();
46 /** @var core_reportbuilder_generator $generator */
47 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
48 $report = $generator->create_report(['name' => 'My report', 'source' => users
::class, 'default' => false]);
50 $PAGE->set_url(new moodle_url('/'));
52 $exporter = new custom_report_exporter($report, [], true);
53 $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
55 $this->assertNotEmpty($export->table
);
56 $this->assertEquals(0, $export->filtersapplied
);
57 $this->assertFalse($export->filterspresent
);
58 $this->assertEmpty($export->filtersform
);
59 $this->assertTrue($export->editmode
);
61 // The following are all generated by additional exporters.
62 $this->assertNotEmpty($export->sidebarmenucards
);
63 $this->assertNotEmpty($export->conditions
);
64 $this->assertNotEmpty($export->filters
);
65 $this->assertNotEmpty($export->sorting
);
66 $this->assertNotEmpty($export->cardview
);
70 * Test exported data structure when viewing a report
72 public function test_export_viewing(): void
{
75 $this->resetAfterTest();
77 /** @var core_reportbuilder_generator $generator */
78 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
79 $report = $generator->create_report(['name' => 'My report', 'source' => users
::class, 'default' => false]);
81 $PAGE->set_url(new moodle_url('/'));
83 $exporter = new custom_report_exporter($report, ['pagesize' => 10], false);
84 $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
86 $this->assertNotEmpty($export->table
);
87 $this->assertEquals(0, $export->filtersapplied
);
88 $this->assertFalse($export->filterspresent
);
89 $this->assertEmpty($export->filtersform
);
90 $this->assertFalse($export->editmode
);
92 // The following are all generated by additional exporters, and should not be present when not editing.
93 $this->assertObjectNotHasAttribute('sidebarmenucards', $export);
94 $this->assertObjectNotHasAttribute('conditions', $export);
95 $this->assertObjectNotHasAttribute('filters', $export);
96 $this->assertObjectNotHasAttribute('sorting', $export);
97 $this->assertObjectNotHasAttribute('cardview', $export);
101 * Test exported data structure when filters are present
103 public function test_export_filters_present(): void
{
106 $this->resetAfterTest();
108 /** @var core_reportbuilder_generator $generator */
109 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
110 $report = $generator->create_report(['name' => 'My report', 'source' => users
::class, 'default' => false]);
111 $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
113 $PAGE->set_url(new moodle_url('/'));
115 $exporter = new custom_report_exporter($report, ['pagesize' => 10], false);
116 $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
118 $this->assertTrue($export->filterspresent
);
119 $this->assertNotEmpty($export->filtersform
);
120 $this->assertEquals(0, $export->filtersapplied
);
124 * Test exported data structure when filters are applied
126 public function test_export_filters_applied(): void
{
129 $this->resetAfterTest();
131 $user = $this->getDataGenerator()->create_user();
132 $this->setUser($user);
134 /** @var core_reportbuilder_generator $generator */
135 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
137 $report = $generator->create_report(['name' => 'My report', 'source' => users
::class, 'default' => false]);
138 $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
141 user_filter_manager
::set($report->get('id'), ['user:email_operator' => text
::IS_NOT_EMPTY
]);
143 $PAGE->set_url(new moodle_url('/'));
145 $exporter = new custom_report_exporter($report, ['pagesize' => 10], false);
146 $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
148 $this->assertTrue($export->filterspresent
);
149 $this->assertNotEmpty($export->filtersform
);
150 $this->assertEquals(1, $export->filtersapplied
);