Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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\tests;
20
 
21
use advanced_testcase;
22
use core_reportbuilder_generator;
23
use core_reportbuilder\manager;
24
use core_reportbuilder\local\helpers\{aggregation, report, user_filter_manager};
25
use core_reportbuilder\table\custom_report_table_view;
26
use Throwable;
27
 
28
/**
29
 * Helper base class for reportbuilder unit tests
30
 *
31
 * @package     core_reportbuilder
32
 * @copyright   2021 Paul Holden <paulh@moodle.com>
33
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
abstract class core_reportbuilder_testcase extends advanced_testcase {
36
 
37
    /**
38
     * Retrieve content for given report as array of report data
39
     *
40
     * @param int $reportid
41
     * @param int $pagesize
42
     * @param array $filtervalues
43
     * @return array[]
44
     */
45
    protected function get_custom_report_content(int $reportid, int $pagesize = 30, array $filtervalues = []): array {
46
        $records = [];
47
 
48
        // Apply filter values.
49
        user_filter_manager::set($reportid, $filtervalues);
50
 
51
        // Create table instance.
52
        $table = custom_report_table_view::create($reportid);
53
        $table->setup();
54
        $table->query_db($pagesize, false);
55
 
56
        // Extract raw data.
57
        foreach ($table->rawdata as $record) {
58
            $records[] = $table->format_row($record);
59
        }
60
 
61
        $table->close_recordset();
62
 
63
        return $records;
64
    }
65
 
66
    /**
67
     * Stress test a report source by iterating over all it's columns, enabling sorting where possible and asserting we can
68
     * create a report for each
69
     *
70
     * @param string $source
71
     */
72
    protected function datasource_stress_test_columns(string $source): void {
73
 
74
        /** @var core_reportbuilder_generator $generator */
75
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
76
 
77
        $report = $generator->create_report(['name' => 'Stress columns', 'source' => $source, 'default' => 0]);
78
        $instance = manager::get_report_from_persistent($report);
79
 
80
        // Iterate over each available column, ensure each works correctly independent of any others.
81
        foreach ($instance->get_columns() as $columnidentifier => $columninstance) {
82
            $column = report::add_report_column($report->get('id'), $columnidentifier);
83
 
84
            // Enable sorting of the column where possible.
85
            if ($columninstance->get_is_sortable()) {
86
                report::toggle_report_column_sorting($report->get('id'), $column->get('id'), true, SORT_DESC);
87
            }
88
 
89
            // We are only asserting the report returns content without errors, not the content itself.
90
            try {
91
                $content = $this->get_custom_report_content($report->get('id'));
92
                $this->assertNotEmpty($content);
93
 
94
                // Ensure appropriate debugging was triggered for deprecated column.
95
                if ($columninstance->get_is_deprecated()) {
96
                    $this->assertDebuggingCalled(null, DEBUG_DEVELOPER);
97
                }
98
            } catch (Throwable $exception) {
99
                $this->fail("Error for column '{$columnidentifier}': " . $exception->getMessage());
100
            }
101
 
102
            report::delete_report_column($report->get('id'), $column->get('id'));
103
        }
104
    }
105
 
106
    /**
107
     * Stress test a report source by iterating over all columns and asserting we can create a report while aggregating each
108
     *
109
     * @param string $source
110
     */
111
    protected function datasource_stress_test_columns_aggregation(string $source): void {
112
 
113
        /** @var core_reportbuilder_generator $generator */
114
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
115
 
116
        $report = $generator->create_report(['name' => 'Stress aggregation', 'source' => $source, 'default' => 0]);
117
        $instance = manager::get_report_from_persistent($report);
118
 
119
        // Add every column.
120
        $columndeprecatedcount = 0;
121
        foreach ($instance->get_columns() as $columnidentifier => $column) {
122
            $columndeprecatedcount += (int) $column->get_is_deprecated();
123
            report::add_report_column($report->get('id'), $columnidentifier);
124
        }
125
 
126
        // Now iterate over each column, and apply all suitable aggregation types.
127
        $columns = $instance->get_active_columns();
128
        $this->assertDebuggingCalledCount($columndeprecatedcount, null,
129
            array_fill(0, $columndeprecatedcount, DEBUG_DEVELOPER));
130
        foreach ($columns as $column) {
131
            $aggregations = aggregation::get_column_aggregations($column->get_type(), $column->get_disabled_aggregation());
132
            foreach (array_keys($aggregations) as $aggregation) {
133
                $column->get_persistent()->set('aggregation', $aggregation)->update();
134
 
135
                // We are only asserting the report returns content without errors, not the content itself.
136
                try {
137
                    $content = $this->get_custom_report_content($report->get('id'));
138
                    $this->assertNotEmpty($content);
139
 
140
                    // Ensure appropriate debugging was triggered for deprecated columns.
141
                    $this->assertDebuggingCalledCount($columndeprecatedcount, null,
142
                        array_fill(0, $columndeprecatedcount, DEBUG_DEVELOPER));
143
                } catch (Throwable $exception) {
144
                    $this->fail("Error for column '{$column->get_unique_identifier()}' with aggregation '{$aggregation}': " .
145
                        $exception->getMessage());
146
                }
147
            }
148
 
149
            // Reset the column aggregation.
150
            $column->get_persistent()->set('aggregation', null)->update();
151
        }
152
    }
153
 
154
    /**
155
     * Stress test a report source by iterating over all it's conditions and asserting we can create a report using each
156
     *
157
     * @param string $source
158
     * @param string $columnidentifier Should be a simple column, with as few fields and joins as possible, ideally selected
159
     *      from the base table itself
160
     */
161
    protected function datasource_stress_test_conditions(string $source, string $columnidentifier): void {
162
 
163
        /** @var core_reportbuilder_generator $generator */
164
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
165
 
166
        $report = $generator->create_report(['name' => 'Stress conditions', 'source' => $source, 'default' => 0]);
167
        $instance = manager::get_report_from_persistent($report);
168
 
169
        // Add single column only (to ensure no conditions have reliance on any columns).
170
        report::add_report_column($report->get('id'), $columnidentifier);
171
 
172
        // Iterate over each available condition, ensure each works correctly independent of any others.
173
        $conditionidentifiers = array_keys($instance->get_conditions());
174
        foreach ($conditionidentifiers as $conditionidentifier) {
175
            $condition = report::add_report_condition($report->get('id'), $conditionidentifier);
176
            $conditioninstance = $instance->get_condition($condition->get('uniqueidentifier'));
177
 
178
            /** @var \core_reportbuilder\local\filters\base $conditionclass */
179
            $conditionclass = $conditioninstance->get_filter_class();
180
 
181
            // Set report condition values in order to activate it.
182
            $conditionvalues = $conditionclass::create($conditioninstance)->get_sample_values();
183
            if (empty($conditionvalues)) {
184
                debugging("Missing sample values from filter '{$conditionclass}'", DEBUG_DEVELOPER);
185
            }
186
            $instance->set_condition_values($conditionvalues);
187
 
188
            // We are only asserting the report returns content without errors, not the content itself.
189
            try {
190
                $content = $this->get_custom_report_content($report->get('id'));
191
                $this->assertIsArray($content);
192
 
193
                // Ensure appropriate debugging was triggered for deprecated condition.
194
                if ($conditioninstance->get_is_deprecated()) {
195
                    $this->assertDebuggingCalled(null, DEBUG_DEVELOPER);
196
                }
197
            } catch (Throwable $exception) {
198
                $this->fail("Error for condition '{$conditionidentifier}': " . $exception->getMessage());
199
            }
200
 
201
            report::delete_report_condition($report->get('id'), $condition->get('id'));
202
        }
203
    }
204
}