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 - https://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 <https://www.gnu.org/licenses/>.
16
 
17
namespace tool_courserating\reportbuilder\datasource;
18
 
19
use core_reportbuilder\manager;
20
use core_reportbuilder\local\helpers\aggregation;
21
use core_reportbuilder\local\helpers\report;
22
use core_reportbuilder\table\custom_report_table_view;
23
use tool_courserating\api;
24
 
25
/**
26
 * Tests for reportbuilder datasource courseratings.
27
 *
28
 * TODO when the minimum supported version is Moodle 4.0 - change this class to extend
29
 * {@see core_reportbuilder_testcase} and remove the component check and the methods copied from that class
30
 *
31
 * @package     tool_courserating
32
 * @copyright   2022 Marina Glancy
33
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 *
35
 * @covers \tool_courserating\reportbuilder\datasource\courseratings
36
 */
37
final class courseratings_test extends \advanced_testcase {
38
 
39
    /**
40
     * setUp
41
     */
42
    protected function setUp(): void {
43
        $this->resetAfterTest();
44
        set_config(\tool_courserating\constants::SETTING_RATINGMODE,
45
            \tool_courserating\constants::RATEBY_ANYTIME, 'tool_courserating');
46
    }
47
 
48
    /**
49
     * Set up for test
50
     */
51
    protected function set_up_for_test() {
52
        $course = $this->getDataGenerator()->create_course();
53
        $this->getDataGenerator()->create_course();
54
        $user1 = $this->getDataGenerator()->create_user(['firstname' => 'User1']);
55
        $user2 = $this->getDataGenerator()->create_user(['firstname' => 'User2']);
56
        /** @var \tool_courserating_generator $generator */
57
        $generator = self::getDataGenerator()->get_plugin_generator('tool_courserating');
58
        $rating1 = $generator->create_rating($user1->id, $course->id, 3, 'hello <b>unclosed tag');
59
        usleep(1000); // Make sure timestamp is different on the ratings.
60
        $rating2 = $generator->create_rating($user2->id, $course->id, 2);
61
 
62
        $this->setUser($user2);
63
        api::flag_review($rating1->get('id'));
64
        $this->setAdminUser();
65
    }
66
 
67
    /**
68
     * Stress test datasource
69
     *
70
     * In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
71
     */
72
    public function test_stress_datasource(): void {
73
        if (!\core_component::get_component_directory('core_reportbuilder')) {
74
            // TODO remove when the minimum supported version is Moodle 4.0.
75
            $this->markTestSkipped('Report builder not found');
76
        }
77
        if (!PHPUNIT_LONGTEST) {
78
            $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
79
        }
80
 
81
        $this->set_up_for_test();
82
 
83
        $this->datasource_stress_test_columns(courseratings::class);
84
        $this->datasource_stress_test_columns_aggregation(courseratings::class);
85
        $this->datasource_stress_test_conditions(courseratings::class, 'course:coursefullnamewithlink');
86
    }
87
 
88
    /**
89
     * Test for report content
90
     *
91
     * @return void
92
     */
93
    public function test_content(): void {
94
        if (!\core_component::get_component_directory('core_reportbuilder')) {
95
            // TODO remove when the minimum supported version is Moodle 4.0.
96
            $this->markTestSkipped('Report builder not found');
97
        }
98
        $this->set_up_for_test();
99
 
100
        /** @var \core_reportbuilder_generator $generator */
101
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
102
        $report = $generator->create_report(['name' => 'CR', 'source' => courseratings::class]);
103
 
104
        // Add more columns.
105
        $c = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname']);
106
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'rating:rating']);
107
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'rating:review']);
108
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'rating:flags']);
109
        // Add flags column twice to check how different DB engines handle it.
110
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'rating:flags']);
111
        report::toggle_report_column_sorting($report->get('id'), $c->get('id'), true);
112
 
113
        // Analyse results.
114
        $content = $this->get_custom_report_content($report->get('id'));
115
        $this->assertCount(3, $content);
116
 
117
        $this->assertEquals(['2.5', '2.5', ''], array_column($content, 'c2_avgrating'));
118
        $this->assertEquals(['User1', 'User2', null], array_column($content, 'c4_firstname'));
119
        $this->assertEquals(['<div class="text_to_html">hello unclosed tag</div>', '', null],
120
            array_column($content, 'c6_review'));
121
        $this->assertEquals([1, 0, 0], array_column($content, 'c7_flags'));
122
        $this->assertEquals([1, 0, 0], array_column($content, 'c8_flags'));
123
    }
124
 
125
    /**
126
     * Retrieve content for given report as array of report data
127
     *
128
     * TODO remove when the minimum supported version is Moodle 4.0 and parent class is core_reportbuilder_testcase
129
     *
130
     * @param int $reportid
131
     * @param int $pagesize
132
     * @return array[]
133
     */
134
    protected function get_custom_report_content(int $reportid, int $pagesize = 30): array {
135
        $records = [];
136
 
137
        // Create table instance.
138
        $table = custom_report_table_view::create($reportid);
139
        $table->setup();
140
        $table->query_db($pagesize, false);
141
 
142
        // Extract raw data.
143
        foreach ($table->rawdata as $record) {
144
            $records[] = $table->format_row($record);
145
        }
146
 
147
        $table->close_recordset();
148
 
149
        return $records;
150
    }
151
 
152
    /**
153
     * Stress test a report source by iterating over all it's columns and asserting we can create a report for each
154
     *
155
     * TODO remove when the minimum supported version is Moodle 4.0 and parent class is core_reportbuilder_testcase
156
     *
157
     * @param string $source
158
     */
159
    protected function datasource_stress_test_columns(string $source): void {
160
 
161
        /** @var \core_reportbuilder_generator $generator */
162
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
163
 
164
        $report = $generator->create_report(['name' => 'Stress columns', 'source' => $source, 'default' => 0]);
165
        $instance = manager::get_report_from_persistent($report);
166
 
167
        // Iterate over each available column, ensure each works correctly independent of any others.
168
        $columnidentifiers = array_keys($instance->get_columns());
169
        foreach ($columnidentifiers as $columnidentifier) {
170
            $column = report::add_report_column($report->get('id'), $columnidentifier);
171
 
172
            // We are only asserting the report returns content without errors, not the content itself.
173
            try {
174
                $content = $this->get_custom_report_content($report->get('id'));
175
                $this->assertNotEmpty($content);
176
            } catch (\Throwable $exception) {
177
                $this->fail("Error for column '{$columnidentifier}': " . $exception->getMessage());
178
            }
179
 
180
            report::delete_report_column($report->get('id'), $column->get('id'));
181
        }
182
    }
183
 
184
    /**
185
     * Stress test a report source by iterating over all columns and asserting we can create a report while aggregating each
186
     *
187
     * TODO remove when the minimum supported version is Moodle 4.0 and parent class is core_reportbuilder_testcase
188
     *
189
     * @param string $source
190
     */
191
    protected function datasource_stress_test_columns_aggregation(string $source): void {
192
 
193
        /** @var \core_reportbuilder_generator $generator */
194
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
195
 
196
        $report = $generator->create_report(['name' => 'Stress aggregation', 'source' => $source, 'default' => 0]);
197
        $instance = manager::get_report_from_persistent($report);
198
 
199
        // Add every column.
200
        $columnidentifiers = array_keys($instance->get_columns());
201
        foreach ($columnidentifiers as $columnidentifier) {
202
            report::add_report_column($report->get('id'), $columnidentifier);
203
        }
204
 
205
        // Now iterate over each column, and apply all suitable aggregation types.
206
        foreach ($instance->get_active_columns() as $column) {
207
            $aggregations = aggregation::get_column_aggregations($column->get_type(), $column->get_disabled_aggregation());
208
            foreach (array_keys($aggregations) as $aggregation) {
209
                $column->get_persistent()->set('aggregation', $aggregation)->update();
210
 
211
                // We are only asserting the report returns content without errors, not the content itself.
212
                try {
213
                    $content = $this->get_custom_report_content($report->get('id'));
214
                    $this->assertNotEmpty($content);
215
                } catch (\Throwable $exception) {
216
                    $this->fail("Error for column '{$column->get_unique_identifier()}' with aggregation '{$aggregation}': " .
217
                        $exception->getMessage());
218
                }
219
            }
220
 
221
            // Reset the column aggregation.
222
            $column->get_persistent()->set('aggregation', null)->update();
223
        }
224
    }
225
 
226
    /**
227
     * Stress test a report source by iterating over all it's conditions and asserting we can create a report using each
228
     *
229
     * TODO remove when the minimum supported version is Moodle 4.0 and parent class is core_reportbuilder_testcase
230
     *
231
     * @param string $source
232
     * @param string $columnidentifier Should be a simple column, with as few fields and joins as possible, ideally selected
233
     *      from the base table itself
234
     */
235
    protected function datasource_stress_test_conditions(string $source, string $columnidentifier): void {
236
 
237
        /** @var \core_reportbuilder_generator $generator */
238
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
239
 
240
        $report = $generator->create_report(['name' => 'Stress conditions', 'source' => $source, 'default' => 0]);
241
        $instance = manager::get_report_from_persistent($report);
242
 
243
        // Add single column only (to ensure no conditions have reliance on any columns).
244
        report::add_report_column($report->get('id'), $columnidentifier);
245
 
246
        // Iterate over each available condition, ensure each works correctly independent of any others.
247
        $conditionidentifiers = array_keys($instance->get_conditions());
248
        foreach ($conditionidentifiers as $conditionidentifier) {
249
            $condition = report::add_report_condition($report->get('id'), $conditionidentifier);
250
            $conditioninstance = $instance->get_condition($condition->get('uniqueidentifier'));
251
 
252
            /** @var \core_reportbuilder\local\filters\base $conditionclass */
253
            $conditionclass = $conditioninstance->get_filter_class();
254
 
255
            // Set report condition values in order to activate it.
256
            $conditionvalues = $conditionclass::create($conditioninstance)->get_sample_values();
257
            if (empty($conditionvalues)) {
258
                debugging("Missing sample values from filter '{$conditionclass}'", DEBUG_DEVELOPER);
259
            }
260
            $instance->set_condition_values($conditionvalues);
261
 
262
            // We are only asserting the report returns content without errors, not the content itself.
263
            try {
264
                $content = $this->get_custom_report_content($report->get('id'));
265
                $this->assertIsArray($content);
266
            } catch (\Throwable $exception) {
267
                $this->fail("Error for condition '{$conditionidentifier}': " . $exception->getMessage());
268
            }
269
 
270
            report::delete_report_condition($report->get('id'), $condition->get('id'));
271
        }
272
    }
273
}