Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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\manager;
23
use core_reportbuilder_generator;
24
use moodle_url;
25
use core_reportbuilder\local\helpers\user_filter_manager;
26
use core_reportbuilder\local\filters\text;
27
use core_user\reportbuilder\datasource\users;
28
 
29
/**
30
 * Unit tests for custom report exporter
31
 *
32
 * @package     core_reportbuilder
33
 * @covers      \core_reportbuilder\external\custom_report_exporter
34
 * @copyright   2022 Paul Holden <paulh@moodle.com>
35
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class custom_report_exporter_test extends advanced_testcase {
38
 
39
    /**
40
     * Test exported data structure when editing a report
41
     */
42
    public function test_export_editing(): void {
43
        global $PAGE;
44
 
45
        $this->resetAfterTest();
46
 
47
        /** @var core_reportbuilder_generator $generator */
48
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
49
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
50
        manager::get_report_from_persistent($report)->add_attributes(['data-foo' => 'bar', 'data-another' => '1']);
51
 
52
        $PAGE->set_url(new moodle_url('/'));
53
 
54
        $exporter = new custom_report_exporter($report, [], true);
55
        $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
56
 
57
        $this->assertNotEmpty($export->table);
58
        $this->assertEquals(0, $export->filtersapplied);
59
        $this->assertFalse($export->filterspresent);
60
        $this->assertEmpty($export->filtersform);
61
        $this->assertTrue($export->editmode);
62
        $this->assertEmpty($export->attributes);
63
 
64
        // The following are all generated by additional exporters.
65
        $this->assertNotEmpty($export->sidebarmenucards);
66
        $this->assertNotEmpty($export->conditions);
67
        $this->assertNotEmpty($export->filters);
68
        $this->assertNotEmpty($export->sorting);
69
        $this->assertNotEmpty($export->cardview);
70
    }
71
 
72
    /**
73
     * Test exported data structure when viewing a report
74
     */
75
    public function test_export_viewing(): void {
76
        global $PAGE;
77
 
78
        $this->resetAfterTest();
79
 
80
        /** @var core_reportbuilder_generator $generator */
81
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
82
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
83
        manager::get_report_from_persistent($report)->add_attributes(['data-foo' => 'bar', 'data-another' => '1']);
84
 
85
        $PAGE->set_url(new moodle_url('/'));
86
 
87
        $exporter = new custom_report_exporter($report, ['pagesize' => 10], false);
88
        $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
89
 
90
        $this->assertNotEmpty($export->table);
91
        $this->assertEquals(0, $export->filtersapplied);
92
        $this->assertFalse($export->filterspresent);
93
        $this->assertEmpty($export->filtersform);
94
        $this->assertFalse($export->editmode);
95
        $this->assertEquals([
96
            ['name' => 'data-foo', 'value' => 'bar'],
97
            ['name' => 'data-another', 'value' => '1']
98
        ], $export->attributes);
99
 
100
        // The following are all generated by additional exporters, and should not be present when not editing.
101
        $this->assertObjectNotHasProperty('sidebarmenucards', $export);
102
        $this->assertObjectNotHasProperty('conditions', $export);
103
        $this->assertObjectNotHasProperty('filters', $export);
104
        $this->assertObjectNotHasProperty('sorting', $export);
105
        $this->assertObjectNotHasProperty('cardview', $export);
106
    }
107
 
108
    /**
109
     * Test exported data structure when filters are present
110
     */
111
    public function test_export_filters_present(): void {
112
        global $PAGE;
113
 
114
        $this->resetAfterTest();
115
 
116
        /** @var core_reportbuilder_generator $generator */
117
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
118
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
119
        $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
120
 
121
        $PAGE->set_url(new moodle_url('/'));
122
 
123
        $exporter = new custom_report_exporter($report, ['pagesize' => 10], false);
124
        $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
125
 
126
        $this->assertTrue($export->filterspresent);
127
        $this->assertNotEmpty($export->filtersform);
128
        $this->assertEquals(0, $export->filtersapplied);
129
    }
130
 
131
    /**
132
     * Test exported data structure when filters are applied
133
     */
134
    public function test_export_filters_applied(): void {
135
        global $PAGE;
136
 
137
        $this->resetAfterTest();
138
 
139
        $user = $this->getDataGenerator()->create_user();
140
        $this->setUser($user);
141
 
142
        /** @var core_reportbuilder_generator $generator */
143
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
144
 
145
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
146
        $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
147
 
148
        // Apply filter.
149
        user_filter_manager::set($report->get('id'), ['user:email_operator' => text::IS_NOT_EMPTY]);
150
 
151
        $PAGE->set_url(new moodle_url('/'));
152
 
153
        $exporter = new custom_report_exporter($report, ['pagesize' => 10], false);
154
        $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
155
 
156
        $this->assertTrue($export->filterspresent);
157
        $this->assertNotEmpty($export->filtersform);
158
        $this->assertEquals(1, $export->filtersapplied);
159
    }
160
}