Proyectos de Subversion Moodle

Rev

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