| 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\local\helpers;
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | use advanced_testcase;
 | 
        
           | 1441 | ariadna | 22 | use core\context\system;
 | 
        
           | 1 | efrain | 23 | use core_reportbuilder_generator;
 | 
        
           | 1441 | ariadna | 24 | use core_reportbuilder\{datasource, system_report_factory};
 | 
        
           |  |  | 25 | use core_reportbuilder\local\models\{audience, column, filter, schedule};
 | 
        
           |  |  | 26 | use core_reportbuilder\local\systemreports\report_access_list;
 | 
        
           | 1 | efrain | 27 | use core_tag_tag;
 | 
        
           |  |  | 28 | use core_user\reportbuilder\datasource\users;
 | 
        
           | 1441 | ariadna | 29 | use invalid_parameter_exception;
 | 
        
           | 1 | efrain | 30 |   | 
        
           |  |  | 31 | /**
 | 
        
           |  |  | 32 |  * Unit tests for the report helper class
 | 
        
           |  |  | 33 |  *
 | 
        
           |  |  | 34 |  * @package     core_reportbuilder
 | 
        
           |  |  | 35 |  * @covers      \core_reportbuilder\local\helpers\report
 | 
        
           |  |  | 36 |  * @copyright   2021 Paul Holden <paulh@moodle.com>
 | 
        
           |  |  | 37 |  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 38 |  */
 | 
        
           | 1441 | ariadna | 39 | final class report_test extends advanced_testcase {
 | 
        
           | 1 | efrain | 40 |   | 
        
           |  |  | 41 |     /**
 | 
        
           |  |  | 42 |      * Test creation report
 | 
        
           |  |  | 43 |      */
 | 
        
           |  |  | 44 |     public function test_create_report(): void {
 | 
        
           |  |  | 45 |         $this->resetAfterTest();
 | 
        
           |  |  | 46 |         $this->setAdminUser();
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 |         $report = report::create_report((object) [
 | 
        
           |  |  | 49 |             'name' => 'My report with tags',
 | 
        
           |  |  | 50 |             'source' => users::class,
 | 
        
           |  |  | 51 |             'tags' => ['cat', 'dog'],
 | 
        
           |  |  | 52 |         ]);
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |         $this->assertEquals('My report with tags', $report->get('name'));
 | 
        
           |  |  | 55 |         $this->assertEquals(datasource::TYPE_CUSTOM_REPORT, $report->get('type'));
 | 
        
           | 1441 | ariadna | 56 |         $this->assertEqualsCanonicalizing(
 | 
        
           |  |  | 57 |             ['cat', 'dog'],
 | 
        
           |  |  | 58 |             array_values(core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $report->get('id'))),
 | 
        
           |  |  | 59 |         );
 | 
        
           | 1 | efrain | 60 |   | 
        
           |  |  | 61 |         $report = report::create_report((object) [
 | 
        
           |  |  | 62 |             'name' => 'My report without tags',
 | 
        
           |  |  | 63 |             'source' => users::class,
 | 
        
           |  |  | 64 |         ]);
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 |         $this->assertEquals('My report without tags', $report->get('name'));
 | 
        
           |  |  | 67 |         $this->assertEquals(datasource::TYPE_CUSTOM_REPORT, $report->get('type'));
 | 
        
           |  |  | 68 |         $this->assertEmpty(core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report',
 | 
        
           |  |  | 69 |             $report->get('id')));
 | 
        
           |  |  | 70 |     }
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 |     /**
 | 
        
           |  |  | 73 |      * Test updating report
 | 
        
           |  |  | 74 |      */
 | 
        
           |  |  | 75 |     public function test_update_report(): void {
 | 
        
           |  |  | 76 |         $this->resetAfterTest();
 | 
        
           |  |  | 77 |         $this->setAdminUser();
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 80 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 81 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'uniquerows' => 0]);
 | 
        
           |  |  | 82 |   | 
        
           |  |  | 83 |         $reportupdated = report::update_report((object) [
 | 
        
           |  |  | 84 |             'id' => $report->get('id'),
 | 
        
           |  |  | 85 |             'name' => 'My renamed report without add tags',
 | 
        
           |  |  | 86 |             'uniquerows' => 1,
 | 
        
           |  |  | 87 |         ]);
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 |         $this->assertEquals('My renamed report without add tags', $reportupdated->get('name'));
 | 
        
           |  |  | 90 |         $this->assertTrue($reportupdated->get('uniquerows'));
 | 
        
           |  |  | 91 |         $this->assertEmpty(core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report',
 | 
        
           |  |  | 92 |             $reportupdated->get('id')));
 | 
        
           |  |  | 93 |   | 
        
           |  |  | 94 |         $reportupdated = report::update_report((object) [
 | 
        
           |  |  | 95 |             'id' => $report->get('id'),
 | 
        
           |  |  | 96 |             'name' => 'My renamed report adding tags',
 | 
        
           |  |  | 97 |             'uniquerows' => 1,
 | 
        
           |  |  | 98 |             'tags' => ['cat', 'dog'],
 | 
        
           |  |  | 99 |         ]);
 | 
        
           |  |  | 100 |   | 
        
           |  |  | 101 |         $this->assertEquals('My renamed report adding tags', $reportupdated->get('name'));
 | 
        
           |  |  | 102 |         $this->assertTrue($reportupdated->get('uniquerows'));
 | 
        
           | 1441 | ariadna | 103 |         $this->assertEqualsCanonicalizing(
 | 
        
           |  |  | 104 |             ['cat', 'dog'],
 | 
        
           |  |  | 105 |             array_values(core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $reportupdated->get('id'))),
 | 
        
           |  |  | 106 |         );
 | 
        
           | 1 | efrain | 107 |     }
 | 
        
           |  |  | 108 |   | 
        
           |  |  | 109 |     /**
 | 
        
           | 1441 | ariadna | 110 |      * Test duplicate report
 | 
        
           |  |  | 111 |      */
 | 
        
           |  |  | 112 |     public function test_duplicate_report(): void {
 | 
        
           |  |  | 113 |         $this->resetAfterTest();
 | 
        
           |  |  | 114 |         $this->setAdminUser();
 | 
        
           |  |  | 115 |   | 
        
           |  |  | 116 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 117 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 |         // Create custom report containing single column, condition and filter..
 | 
        
           |  |  | 120 |         $report = $generator->create_report(['name' => 'Report 1', 'source' => users::class, 'default' => 0]);
 | 
        
           |  |  | 121 |         $column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
 | 
        
           |  |  | 122 |         $condition = $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname']);
 | 
        
           |  |  | 123 |         $filter = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 | 
        
           |  |  | 124 |   | 
        
           |  |  | 125 |         // Add an audience and schedule.
 | 
        
           |  |  | 126 |         $audience = $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => []]);
 | 
        
           |  |  | 127 |         $schedule = $generator->create_schedule([
 | 
        
           |  |  | 128 |             'reportid' => $report->get('id'),
 | 
        
           |  |  | 129 |             'name' => 'My schedule',
 | 
        
           |  |  | 130 |             'audiences' => json_encode([
 | 
        
           |  |  | 131 |                 $audience->get_persistent()->get('id'),
 | 
        
           |  |  | 132 |             ]),
 | 
        
           |  |  | 133 |         ]);
 | 
        
           |  |  | 134 |   | 
        
           |  |  | 135 |         // Duplicate report with audiences and schedules.
 | 
        
           |  |  | 136 |         $newreport = report::duplicate_report($report, 'Report 1 copy', true, true);
 | 
        
           |  |  | 137 |         $this->assertNotEquals($report->get('id'), $newreport->get('id'));
 | 
        
           |  |  | 138 |         $this->assertEquals('Report 1 copy', $newreport->get('name'));
 | 
        
           |  |  | 139 |         $this->assertEquals(users::class, $newreport->get('source'));
 | 
        
           |  |  | 140 |   | 
        
           |  |  | 141 |         // Assert new report columns.
 | 
        
           |  |  | 142 |         $newcolumns = column::get_records(['reportid' => $newreport->get('id')]);
 | 
        
           |  |  | 143 |         $this->assertCount(1, $newcolumns);
 | 
        
           |  |  | 144 |         [$newcolumn] = $newcolumns;
 | 
        
           |  |  | 145 |         $this->assertNotEquals($column->get('id'), $newcolumn->get('id'));
 | 
        
           |  |  | 146 |         $this->assertEquals('user:lastname', $newcolumn->get('uniqueidentifier'));
 | 
        
           |  |  | 147 |   | 
        
           |  |  | 148 |         // Assert new report conditions.
 | 
        
           |  |  | 149 |         $newconditions = filter::get_condition_records($newreport->get('id'));
 | 
        
           |  |  | 150 |         $this->assertCount(1, $newconditions);
 | 
        
           |  |  | 151 |         [$newcondition] = $newconditions;
 | 
        
           |  |  | 152 |         $this->assertNotEquals($condition->get('id'), $newcondition->get('id'));
 | 
        
           |  |  | 153 |         $this->assertEquals('user:firstname', $newcondition->get('uniqueidentifier'));
 | 
        
           |  |  | 154 |   | 
        
           |  |  | 155 |         // Assert new report filters.
 | 
        
           |  |  | 156 |         $newfilters = filter::get_filter_records($newreport->get('id'));
 | 
        
           |  |  | 157 |         $this->assertCount(1, $newfilters);
 | 
        
           |  |  | 158 |         [$newfilter] = $newfilters;
 | 
        
           |  |  | 159 |         $this->assertNotEquals($filter->get('id'), $newfilter->get('id'));
 | 
        
           |  |  | 160 |         $this->assertEquals('user:email', $newfilter->get('uniqueidentifier'));
 | 
        
           |  |  | 161 |   | 
        
           |  |  | 162 |         // Assert new report audiences.
 | 
        
           |  |  | 163 |         $newaudiences = audience::get_records(['reportid' => $newreport->get('id')]);
 | 
        
           |  |  | 164 |         $this->assertCount(1, $newaudiences);
 | 
        
           |  |  | 165 |         [$newaudience] = $newaudiences;
 | 
        
           |  |  | 166 |         $this->assertNotEquals($audience->get_persistent()->get('id'), $newaudience->get('id'));
 | 
        
           |  |  | 167 |   | 
        
           |  |  | 168 |          // Assert new report schedules.
 | 
        
           |  |  | 169 |         $newschedules = schedule::get_records(['reportid' => $newreport->get('id')]);
 | 
        
           |  |  | 170 |         $this->assertCount(1, $newschedules);
 | 
        
           |  |  | 171 |         [$newschedule] = $newschedules;
 | 
        
           |  |  | 172 |         $this->assertNotEquals($schedule->get('id'), $newschedule->get('id'));
 | 
        
           |  |  | 173 |         $this->assertEquals([
 | 
        
           |  |  | 174 |             $newaudience->get('id'),
 | 
        
           |  |  | 175 |         ], (array) json_decode($newschedule->get('audiences')));
 | 
        
           |  |  | 176 |   | 
        
           |  |  | 177 |         // Duplicate report without schedules.
 | 
        
           |  |  | 178 |         $newreporttwo = report::duplicate_report($report, 'Report 1 copy #2', true, false);
 | 
        
           |  |  | 179 |         $this->assertEquals(1, column::count_records(['reportid' => $newreporttwo->get('id')]));
 | 
        
           |  |  | 180 |         $this->assertEquals(1, filter::count_records(['reportid' => $newreporttwo->get('id'), 'iscondition' => 1]));
 | 
        
           |  |  | 181 |         $this->assertEquals(1, filter::count_records(['reportid' => $newreporttwo->get('id'), 'iscondition' => 0]));
 | 
        
           |  |  | 182 |         $this->assertEquals(1, audience::count_records(['reportid' => $newreporttwo->get('id')]));
 | 
        
           |  |  | 183 |         $this->assertEquals(0, schedule::count_records(['reportid' => $newreporttwo->get('id')]));
 | 
        
           |  |  | 184 |   | 
        
           |  |  | 185 |         // Duplicate report without audiences or schedules.
 | 
        
           |  |  | 186 |         $newreportthree = report::duplicate_report($report, 'Report 1 copy #3', false, false);
 | 
        
           |  |  | 187 |         $this->assertEquals(1, column::count_records(['reportid' => $newreportthree->get('id')]));
 | 
        
           |  |  | 188 |         $this->assertEquals(1, filter::count_records(['reportid' => $newreportthree->get('id'), 'iscondition' => 1]));
 | 
        
           |  |  | 189 |         $this->assertEquals(1, filter::count_records(['reportid' => $newreportthree->get('id'), 'iscondition' => 0]));
 | 
        
           |  |  | 190 |         $this->assertEquals(0, audience::count_records(['reportid' => $newreportthree->get('id')]));
 | 
        
           |  |  | 191 |         $this->assertEquals(0, schedule::count_records(['reportid' => $newreportthree->get('id')]));
 | 
        
           |  |  | 192 |     }
 | 
        
           |  |  | 193 |   | 
        
           |  |  | 194 |     /**
 | 
        
           | 1 | efrain | 195 |      * Test deleting report
 | 
        
           |  |  | 196 |      */
 | 
        
           |  |  | 197 |     public function test_delete_report(): void {
 | 
        
           |  |  | 198 |         $this->resetAfterTest();
 | 
        
           |  |  | 199 |         $this->setAdminUser();
 | 
        
           |  |  | 200 |   | 
        
           |  |  | 201 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 202 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 203 |   | 
        
           |  |  | 204 |         // Create Report1 and add some elements.
 | 
        
           |  |  | 205 |         $report1 = $generator->create_report(['name' => 'My report 1', 'source' => users::class, 'default' => false,
 | 
        
           |  |  | 206 |             'tags' => ['cat', 'dog']]);
 | 
        
           |  |  | 207 |         $column1 = $generator->create_column(['reportid' => $report1->get('id'), 'uniqueidentifier' => 'user:email']);
 | 
        
           |  |  | 208 |         $filter1 = $generator->create_filter(['reportid' => $report1->get('id'), 'uniqueidentifier' => 'user:email']);
 | 
        
           |  |  | 209 |         $condition1 = $generator->create_condition(['reportid' => $report1->get('id'), 'uniqueidentifier' => 'user:email']);
 | 
        
           |  |  | 210 |   | 
        
           |  |  | 211 |         // Create Report2 and add some elements.
 | 
        
           |  |  | 212 |         $report2 = $generator->create_report(['name' => 'My report 2', 'source' => users::class, 'default' => false]);
 | 
        
           |  |  | 213 |         $column2 = $generator->create_column(['reportid' => $report2->get('id'), 'uniqueidentifier' => 'user:email']);
 | 
        
           |  |  | 214 |         $filter2 = $generator->create_filter(['reportid' => $report2->get('id'), 'uniqueidentifier' => 'user:email']);
 | 
        
           |  |  | 215 |         $condition2 = $generator->create_condition(['reportid' => $report2->get('id'), 'uniqueidentifier' => 'user:email']);
 | 
        
           |  |  | 216 |   | 
        
           |  |  | 217 |         // Delete Report1.
 | 
        
           |  |  | 218 |         $result = report::delete_report($report1->get('id'));
 | 
        
           |  |  | 219 |         $this->assertTrue($result);
 | 
        
           |  |  | 220 |   | 
        
           |  |  | 221 |         // Make sure Report1, and all it's elements are deleted.
 | 
        
           |  |  | 222 |         $this->assertFalse($report1::record_exists($report1->get('id')));
 | 
        
           |  |  | 223 |         $this->assertFalse($column1::record_exists($column1->get('id')));
 | 
        
           |  |  | 224 |         $this->assertFalse($filter1::record_exists($filter1->get('id')));
 | 
        
           |  |  | 225 |         $this->assertFalse($condition1::record_exists($condition1->get('id')));
 | 
        
           |  |  | 226 |         $this->assertEmpty(core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $report1->get('id')));
 | 
        
           |  |  | 227 |   | 
        
           |  |  | 228 |         // Make sure Report2, and all it's elements still exist.
 | 
        
           |  |  | 229 |         $this->assertTrue($report2::record_exists($report2->get('id')));
 | 
        
           |  |  | 230 |         $this->assertTrue($column2::record_exists($column2->get('id')));
 | 
        
           |  |  | 231 |         $this->assertTrue($filter2::record_exists($filter2->get('id')));
 | 
        
           |  |  | 232 |         $this->assertTrue($condition2::record_exists($condition2->get('id')));
 | 
        
           |  |  | 233 |     }
 | 
        
           |  |  | 234 |   | 
        
           |  |  | 235 |     /**
 | 
        
           |  |  | 236 |      * Testing adding report column
 | 
        
           |  |  | 237 |      */
 | 
        
           |  |  | 238 |     public function test_add_report_column(): void {
 | 
        
           |  |  | 239 |         $this->resetAfterTest();
 | 
        
           |  |  | 240 |         $this->setAdminUser();
 | 
        
           |  |  | 241 |   | 
        
           |  |  | 242 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 243 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 244 |   | 
        
           |  |  | 245 |         $report = $generator->create_report([
 | 
        
           |  |  | 246 |             'name' => 'My report',
 | 
        
           |  |  | 247 |             'source' => users::class,
 | 
        
           |  |  | 248 |             'default' => false,
 | 
        
           |  |  | 249 |         ]);
 | 
        
           |  |  | 250 |   | 
        
           |  |  | 251 |         // Add first column.
 | 
        
           |  |  | 252 |         $columnfullname = report::add_report_column($report->get('id'), 'user:fullname');
 | 
        
           |  |  | 253 |         $this->assertTrue(column::record_exists($columnfullname->get('id')));
 | 
        
           |  |  | 254 |   | 
        
           |  |  | 255 |         $this->assertEquals($report->get('id'), $columnfullname->get('reportid'));
 | 
        
           |  |  | 256 |         $this->assertEquals('user:fullname', $columnfullname->get('uniqueidentifier'));
 | 
        
           |  |  | 257 |         $this->assertEquals(1, $columnfullname->get('columnorder'));
 | 
        
           |  |  | 258 |         $this->assertEquals(1, $columnfullname->get('sortorder'));
 | 
        
           |  |  | 259 |   | 
        
           |  |  | 260 |         // Add second column.
 | 
        
           |  |  | 261 |         $columnemail = report::add_report_column($report->get('id'), 'user:email');
 | 
        
           |  |  | 262 |         $this->assertTrue(column::record_exists($columnemail->get('id')));
 | 
        
           |  |  | 263 |   | 
        
           |  |  | 264 |         $this->assertEquals($report->get('id'), $columnemail->get('reportid'));
 | 
        
           |  |  | 265 |         $this->assertEquals('user:email', $columnemail->get('uniqueidentifier'));
 | 
        
           |  |  | 266 |         $this->assertEquals(2, $columnemail->get('columnorder'));
 | 
        
           |  |  | 267 |         $this->assertEquals(2, $columnemail->get('sortorder'));
 | 
        
           |  |  | 268 |     }
 | 
        
           |  |  | 269 |   | 
        
           |  |  | 270 |     /**
 | 
        
           |  |  | 271 |      * Test adding invalid report column
 | 
        
           |  |  | 272 |      */
 | 
        
           |  |  | 273 |     public function test_add_report_column_invalid(): void {
 | 
        
           |  |  | 274 |         $this->resetAfterTest();
 | 
        
           |  |  | 275 |         $this->setAdminUser();
 | 
        
           |  |  | 276 |   | 
        
           |  |  | 277 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 278 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 279 |   | 
        
           |  |  | 280 |         $report = $generator->create_report([
 | 
        
           |  |  | 281 |             'name' => 'My report',
 | 
        
           |  |  | 282 |             'source' => users::class,
 | 
        
           |  |  | 283 |             'default' => false,
 | 
        
           |  |  | 284 |         ]);
 | 
        
           |  |  | 285 |   | 
        
           |  |  | 286 |         $this->expectException(invalid_parameter_exception::class);
 | 
        
           |  |  | 287 |         $this->expectExceptionMessage('Invalid column');
 | 
        
           |  |  | 288 |         report::add_report_column($report->get('id'), 'user:invalid');
 | 
        
           |  |  | 289 |     }
 | 
        
           |  |  | 290 |   | 
        
           |  |  | 291 |     /**
 | 
        
           |  |  | 292 |      * Testing deleting report column
 | 
        
           |  |  | 293 |      */
 | 
        
           |  |  | 294 |     public function test_delete_report_column(): void {
 | 
        
           |  |  | 295 |         $this->resetAfterTest();
 | 
        
           |  |  | 296 |         $this->setAdminUser();
 | 
        
           |  |  | 297 |   | 
        
           |  |  | 298 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 299 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 300 |   | 
        
           |  |  | 301 |         $report = $generator->create_report([
 | 
        
           |  |  | 302 |             'name' => 'My report',
 | 
        
           |  |  | 303 |             'source' => users::class,
 | 
        
           |  |  | 304 |             'default' => false,
 | 
        
           |  |  | 305 |         ]);
 | 
        
           |  |  | 306 |   | 
        
           |  |  | 307 |         // Add two columns.
 | 
        
           |  |  | 308 |         $columnfullname = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
 | 
        
           |  |  | 309 |         $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 | 
        
           |  |  | 310 |   | 
        
           |  |  | 311 |         // Delete the first column.
 | 
        
           |  |  | 312 |         $result = report::delete_report_column($report->get('id'), $columnfullname->get('id'));
 | 
        
           |  |  | 313 |         $this->assertTrue($result);
 | 
        
           |  |  | 314 |   | 
        
           |  |  | 315 |         // Assert report columns.
 | 
        
           |  |  | 316 |         $columns = column::get_records(['reportid' => $report->get('id')]);
 | 
        
           |  |  | 317 |         $this->assertCount(1, $columns);
 | 
        
           |  |  | 318 |   | 
        
           |  |  | 319 |         $column = reset($columns);
 | 
        
           |  |  | 320 |         $this->assertEquals('user:email', $column->get('uniqueidentifier'));
 | 
        
           |  |  | 321 |         $this->assertEquals(1, $column->get('columnorder'));
 | 
        
           |  |  | 322 |     }
 | 
        
           |  |  | 323 |   | 
        
           |  |  | 324 |     /**
 | 
        
           |  |  | 325 |      * Testing deleting invalid report column
 | 
        
           |  |  | 326 |      */
 | 
        
           |  |  | 327 |     public function test_delete_report_column_invalid(): void {
 | 
        
           |  |  | 328 |         $this->resetAfterTest();
 | 
        
           |  |  | 329 |         $this->setAdminUser();
 | 
        
           |  |  | 330 |   | 
        
           |  |  | 331 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 332 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 333 |   | 
        
           |  |  | 334 |         $report = $generator->create_report([
 | 
        
           |  |  | 335 |             'name' => 'My report',
 | 
        
           |  |  | 336 |             'source' => users::class,
 | 
        
           |  |  | 337 |             'default' => false,
 | 
        
           |  |  | 338 |         ]);
 | 
        
           |  |  | 339 |   | 
        
           |  |  | 340 |         $this->expectException(invalid_parameter_exception::class);
 | 
        
           |  |  | 341 |         $this->expectExceptionMessage('Invalid column');
 | 
        
           |  |  | 342 |         report::delete_report_column($report->get('id'), 42);
 | 
        
           |  |  | 343 |     }
 | 
        
           |  |  | 344 |   | 
        
           |  |  | 345 |     /**
 | 
        
           |  |  | 346 |      * Testing re-ordering report column
 | 
        
           |  |  | 347 |      */
 | 
        
           |  |  | 348 |     public function test_reorder_report_column(): void {
 | 
        
           |  |  | 349 |         $this->resetAfterTest();
 | 
        
           |  |  | 350 |         $this->setAdminUser();
 | 
        
           |  |  | 351 |   | 
        
           |  |  | 352 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 353 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 354 |   | 
        
           |  |  | 355 |         $report = $generator->create_report([
 | 
        
           |  |  | 356 |             'name' => 'My report',
 | 
        
           |  |  | 357 |             'source' => users::class,
 | 
        
           |  |  | 358 |             'default' => false,
 | 
        
           |  |  | 359 |         ]);
 | 
        
           |  |  | 360 |   | 
        
           |  |  | 361 |         // Add four columns.
 | 
        
           |  |  | 362 |         $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
 | 
        
           |  |  | 363 |         $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 | 
        
           |  |  | 364 |         $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
 | 
        
           |  |  | 365 |         $columncity = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
 | 
        
           |  |  | 366 |   | 
        
           |  |  | 367 |         // Move the city column to second position.
 | 
        
           |  |  | 368 |         $result = report::reorder_report_column($report->get('id'), $columncity->get('id'), 2);
 | 
        
           |  |  | 369 |         $this->assertTrue($result);
 | 
        
           |  |  | 370 |   | 
        
           |  |  | 371 |         // Assert report columns order.
 | 
        
           |  |  | 372 |         $columns = column::get_records(['reportid' => $report->get('id')], 'columnorder');
 | 
        
           |  |  | 373 |   | 
        
           |  |  | 374 |         $columnidentifiers = array_map(static function(column $column): string {
 | 
        
           |  |  | 375 |             return $column->get('uniqueidentifier');
 | 
        
           |  |  | 376 |         }, $columns);
 | 
        
           |  |  | 377 |   | 
        
           |  |  | 378 |         $this->assertEquals([
 | 
        
           |  |  | 379 |             'user:fullname',
 | 
        
           |  |  | 380 |             'user:city',
 | 
        
           |  |  | 381 |             'user:email',
 | 
        
           |  |  | 382 |             'user:country',
 | 
        
           |  |  | 383 |         ], $columnidentifiers);
 | 
        
           |  |  | 384 |     }
 | 
        
           |  |  | 385 |   | 
        
           |  |  | 386 |     /**
 | 
        
           |  |  | 387 |      * Testing re-ordering invalid report column
 | 
        
           |  |  | 388 |      */
 | 
        
           |  |  | 389 |     public function test_reorder_report_column_invalid(): void {
 | 
        
           |  |  | 390 |         $this->resetAfterTest();
 | 
        
           |  |  | 391 |         $this->setAdminUser();
 | 
        
           |  |  | 392 |   | 
        
           |  |  | 393 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 394 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 395 |   | 
        
           |  |  | 396 |         $report = $generator->create_report([
 | 
        
           |  |  | 397 |             'name' => 'My report',
 | 
        
           |  |  | 398 |             'source' => users::class,
 | 
        
           |  |  | 399 |             'default' => false,
 | 
        
           |  |  | 400 |         ]);
 | 
        
           |  |  | 401 |   | 
        
           |  |  | 402 |         $this->expectException(invalid_parameter_exception::class);
 | 
        
           |  |  | 403 |         $this->expectExceptionMessage('Invalid column');
 | 
        
           |  |  | 404 |         report::reorder_report_column($report->get('id'), 42, 1);
 | 
        
           |  |  | 405 |     }
 | 
        
           |  |  | 406 |   | 
        
           |  |  | 407 |     /**
 | 
        
           |  |  | 408 |      * Testing re-ordering report column sorting
 | 
        
           |  |  | 409 |      */
 | 
        
           |  |  | 410 |     public function test_reorder_report_column_sorting(): void {
 | 
        
           |  |  | 411 |         $this->resetAfterTest();
 | 
        
           |  |  | 412 |         $this->setAdminUser();
 | 
        
           |  |  | 413 |   | 
        
           |  |  | 414 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 415 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 416 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 | 
        
           |  |  | 417 |   | 
        
           |  |  | 418 |         // Add four columns.
 | 
        
           |  |  | 419 |         $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
 | 
        
           |  |  | 420 |         $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 | 
        
           |  |  | 421 |         $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
 | 
        
           |  |  | 422 |         $columncity = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
 | 
        
           |  |  | 423 |   | 
        
           |  |  | 424 |         // Move the city column to second position.
 | 
        
           |  |  | 425 |         $result = report::reorder_report_column_sorting($report->get('id'), $columncity->get('id'), 2);
 | 
        
           |  |  | 426 |         $this->assertTrue($result);
 | 
        
           |  |  | 427 |   | 
        
           |  |  | 428 |         // Assert report columns order.
 | 
        
           |  |  | 429 |         $columns = column::get_records(['reportid' => $report->get('id')], 'sortorder');
 | 
        
           |  |  | 430 |   | 
        
           |  |  | 431 |         $columnidentifiers = array_map(static function(column $column): string {
 | 
        
           |  |  | 432 |             return $column->get('uniqueidentifier');
 | 
        
           |  |  | 433 |         }, $columns);
 | 
        
           |  |  | 434 |   | 
        
           |  |  | 435 |         $this->assertEquals([
 | 
        
           |  |  | 436 |             'user:fullname',
 | 
        
           |  |  | 437 |             'user:city',
 | 
        
           |  |  | 438 |             'user:email',
 | 
        
           |  |  | 439 |             'user:country',
 | 
        
           |  |  | 440 |         ], $columnidentifiers);
 | 
        
           |  |  | 441 |     }
 | 
        
           |  |  | 442 |   | 
        
           |  |  | 443 |     /**
 | 
        
           |  |  | 444 |      * Testing re-ordering invalid report column sorting
 | 
        
           |  |  | 445 |      */
 | 
        
           |  |  | 446 |     public function test_reorder_report_column_sorting_invalid(): void {
 | 
        
           |  |  | 447 |         $this->resetAfterTest();
 | 
        
           |  |  | 448 |         $this->setAdminUser();
 | 
        
           |  |  | 449 |   | 
        
           |  |  | 450 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 451 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 452 |   | 
        
           |  |  | 453 |         $report = $generator->create_report([
 | 
        
           |  |  | 454 |             'name' => 'My report',
 | 
        
           |  |  | 455 |             'source' => users::class,
 | 
        
           |  |  | 456 |             'default' => false,
 | 
        
           |  |  | 457 |         ]);
 | 
        
           |  |  | 458 |   | 
        
           |  |  | 459 |         $this->expectException(invalid_parameter_exception::class);
 | 
        
           |  |  | 460 |         $this->expectExceptionMessage('Invalid column');
 | 
        
           |  |  | 461 |         report::reorder_report_column_sorting($report->get('id'), 42, 1);
 | 
        
           |  |  | 462 |     }
 | 
        
           |  |  | 463 |   | 
        
           |  |  | 464 |     /**
 | 
        
           |  |  | 465 |      * Test toggling of report column sorting
 | 
        
           |  |  | 466 |      */
 | 
        
           |  |  | 467 |     public function test_toggle_report_column_sorting(): void {
 | 
        
           |  |  | 468 |         $this->resetAfterTest();
 | 
        
           |  |  | 469 |         $this->setAdminUser();
 | 
        
           |  |  | 470 |   | 
        
           |  |  | 471 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 472 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 473 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
 | 
        
           |  |  | 474 |         $column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 | 
        
           |  |  | 475 |   | 
        
           |  |  | 476 |         // Toggle sort descending.
 | 
        
           |  |  | 477 |         $result = report::toggle_report_column_sorting($report->get('id'), $column->get('id'), true, SORT_DESC);
 | 
        
           |  |  | 478 |         $this->assertTrue($result);
 | 
        
           |  |  | 479 |   | 
        
           |  |  | 480 |         // Confirm column was updated.
 | 
        
           |  |  | 481 |         $columnupdated = new column($column->get('id'));
 | 
        
           |  |  | 482 |         $this->assertTrue($columnupdated->get('sortenabled'));
 | 
        
           |  |  | 483 |         $this->assertEquals(SORT_DESC, $columnupdated->get('sortdirection'));
 | 
        
           |  |  | 484 |     }
 | 
        
           |  |  | 485 |   | 
        
           |  |  | 486 |     /**
 | 
        
           |  |  | 487 |      * Test toggling of report column sorting with invalid column
 | 
        
           |  |  | 488 |      */
 | 
        
           |  |  | 489 |     public function test_toggle_report_column_sorting_invalid(): void {
 | 
        
           |  |  | 490 |         $this->resetAfterTest();
 | 
        
           |  |  | 491 |         $this->setAdminUser();
 | 
        
           |  |  | 492 |   | 
        
           |  |  | 493 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 494 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 495 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
 | 
        
           |  |  | 496 |   | 
        
           |  |  | 497 |         $this->expectException(invalid_parameter_exception::class);
 | 
        
           |  |  | 498 |         $this->expectExceptionMessage('Invalid column');
 | 
        
           |  |  | 499 |         report::toggle_report_column_sorting($report->get('id'), 42, false);
 | 
        
           |  |  | 500 |     }
 | 
        
           |  |  | 501 |   | 
        
           |  |  | 502 |     /**
 | 
        
           |  |  | 503 |      * Test adding report condition
 | 
        
           |  |  | 504 |      */
 | 
        
           |  |  | 505 |     public function test_add_report_condition(): void {
 | 
        
           |  |  | 506 |         $this->resetAfterTest();
 | 
        
           |  |  | 507 |         $this->setAdminUser();
 | 
        
           |  |  | 508 |   | 
        
           |  |  | 509 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 510 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 511 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 | 
        
           |  |  | 512 |   | 
        
           |  |  | 513 |         // Add first condition.
 | 
        
           |  |  | 514 |         $conditionfullname = report::add_report_condition($report->get('id'), 'user:fullname');
 | 
        
           |  |  | 515 |         $this->assertTrue(filter::record_exists_select('id = :id AND iscondition = 1',
 | 
        
           |  |  | 516 |             ['id' => $conditionfullname->get('id')]));
 | 
        
           |  |  | 517 |   | 
        
           |  |  | 518 |         $this->assertEquals($report->get('id'), $conditionfullname->get('reportid'));
 | 
        
           |  |  | 519 |         $this->assertEquals('user:fullname', $conditionfullname->get('uniqueidentifier'));
 | 
        
           |  |  | 520 |         $this->assertEquals(1, $conditionfullname->get('filterorder'));
 | 
        
           |  |  | 521 |   | 
        
           |  |  | 522 |         // Add second condition.
 | 
        
           |  |  | 523 |         $conditionemail = report::add_report_condition($report->get('id'), 'user:email');
 | 
        
           |  |  | 524 |         $this->assertTrue(filter::record_exists_select('id = :id AND iscondition = 1',
 | 
        
           |  |  | 525 |             ['id' => $conditionemail->get('id')]));
 | 
        
           |  |  | 526 |   | 
        
           |  |  | 527 |         $this->assertEquals($report->get('id'), $conditionemail->get('reportid'));
 | 
        
           |  |  | 528 |         $this->assertEquals('user:email', $conditionemail->get('uniqueidentifier'));
 | 
        
           |  |  | 529 |         $this->assertEquals(2, $conditionemail->get('filterorder'));
 | 
        
           |  |  | 530 |     }
 | 
        
           |  |  | 531 |   | 
        
           |  |  | 532 |     /**
 | 
        
           |  |  | 533 |      * Test adding invalid report condition
 | 
        
           |  |  | 534 |      */
 | 
        
           |  |  | 535 |     public function test_add_report_condition_invalid(): void {
 | 
        
           |  |  | 536 |         $this->resetAfterTest();
 | 
        
           |  |  | 537 |         $this->setAdminUser();
 | 
        
           |  |  | 538 |   | 
        
           |  |  | 539 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 540 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 541 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 | 
        
           |  |  | 542 |   | 
        
           |  |  | 543 |         $this->expectException(invalid_parameter_exception::class);
 | 
        
           |  |  | 544 |         $this->expectExceptionMessage('Invalid condition');
 | 
        
           |  |  | 545 |         report::add_report_condition($report->get('id'), 'user:invalid');
 | 
        
           |  |  | 546 |     }
 | 
        
           |  |  | 547 |   | 
        
           |  |  | 548 |     /**
 | 
        
           |  |  | 549 |      * Test adding duplicate report condition
 | 
        
           |  |  | 550 |      */
 | 
        
           |  |  | 551 |     public function test_add_report_condition_duplicate(): void {
 | 
        
           |  |  | 552 |         $this->resetAfterTest();
 | 
        
           |  |  | 553 |         $this->setAdminUser();
 | 
        
           |  |  | 554 |   | 
        
           |  |  | 555 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 556 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 557 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 | 
        
           |  |  | 558 |   | 
        
           |  |  | 559 |         // First one is fine.
 | 
        
           |  |  | 560 |         report::add_report_condition($report->get('id'), 'user:email');
 | 
        
           |  |  | 561 |   | 
        
           |  |  | 562 |         $this->expectException(invalid_parameter_exception::class);
 | 
        
           |  |  | 563 |         $this->expectExceptionMessage('Duplicate condition');
 | 
        
           |  |  | 564 |         report::add_report_condition($report->get('id'), 'user:email');
 | 
        
           |  |  | 565 |     }
 | 
        
           |  |  | 566 |   | 
        
           |  |  | 567 |     /**
 | 
        
           |  |  | 568 |      * Test deleting report condition
 | 
        
           |  |  | 569 |      */
 | 
        
           |  |  | 570 |     public function test_delete_report_condition(): void {
 | 
        
           |  |  | 571 |         $this->resetAfterTest();
 | 
        
           |  |  | 572 |         $this->setAdminUser();
 | 
        
           |  |  | 573 |   | 
        
           |  |  | 574 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 575 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 576 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 | 
        
           |  |  | 577 |   | 
        
           |  |  | 578 |         // Add two conditions.
 | 
        
           |  |  | 579 |         $conditionfullname = $generator->create_condition([
 | 
        
           |  |  | 580 |             'reportid' => $report->get('id'),
 | 
        
           |  |  | 581 |             'uniqueidentifier' => 'user:fullname',
 | 
        
           |  |  | 582 |         ]);
 | 
        
           |  |  | 583 |         $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 | 
        
           |  |  | 584 |   | 
        
           |  |  | 585 |         // Delete the first condition.
 | 
        
           |  |  | 586 |         $result = report::delete_report_condition($report->get('id'), $conditionfullname->get('id'));
 | 
        
           |  |  | 587 |         $this->assertTrue($result);
 | 
        
           |  |  | 588 |   | 
        
           |  |  | 589 |         // Assert report conditions.
 | 
        
           |  |  | 590 |         $conditions = filter::get_condition_records($report->get('id'));
 | 
        
           |  |  | 591 |         $this->assertCount(1, $conditions);
 | 
        
           |  |  | 592 |   | 
        
           |  |  | 593 |         $condition = reset($conditions);
 | 
        
           |  |  | 594 |         $this->assertEquals('user:email', $condition->get('uniqueidentifier'));
 | 
        
           |  |  | 595 |         $this->assertEquals(1, $condition->get('filterorder'));
 | 
        
           |  |  | 596 |     }
 | 
        
           |  |  | 597 |   | 
        
           |  |  | 598 |     /**
 | 
        
           |  |  | 599 |      * Test deleting invalid report condition
 | 
        
           |  |  | 600 |      */
 | 
        
           |  |  | 601 |     public function test_delete_report_condition_invalid(): void {
 | 
        
           |  |  | 602 |         $this->resetAfterTest();
 | 
        
           |  |  | 603 |         $this->setAdminUser();
 | 
        
           |  |  | 604 |   | 
        
           |  |  | 605 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 606 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 607 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 | 
        
           |  |  | 608 |   | 
        
           |  |  | 609 |         $this->expectException(invalid_parameter_exception::class);
 | 
        
           |  |  | 610 |         $this->expectExceptionMessage('Invalid condition');
 | 
        
           |  |  | 611 |         report::delete_report_condition($report->get('id'), 42);
 | 
        
           |  |  | 612 |     }
 | 
        
           |  |  | 613 |   | 
        
           |  |  | 614 |     /**
 | 
        
           |  |  | 615 |      * Test re-ordering report condition
 | 
        
           |  |  | 616 |      */
 | 
        
           |  |  | 617 |     public function test_reorder_report_condition(): void {
 | 
        
           |  |  | 618 |         $this->resetAfterTest();
 | 
        
           |  |  | 619 |         $this->setAdminUser();
 | 
        
           |  |  | 620 |   | 
        
           |  |  | 621 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 622 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 623 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 | 
        
           |  |  | 624 |   | 
        
           |  |  | 625 |         // Add four conditions.
 | 
        
           |  |  | 626 |         $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
 | 
        
           |  |  | 627 |         $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 | 
        
           |  |  | 628 |         $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
 | 
        
           |  |  | 629 |         $conditioncity = $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
 | 
        
           |  |  | 630 |   | 
        
           |  |  | 631 |         // Move the city condition to second position.
 | 
        
           |  |  | 632 |         $result = report::reorder_report_condition($report->get('id'), $conditioncity->get('id'), 2);
 | 
        
           |  |  | 633 |         $this->assertTrue($result);
 | 
        
           |  |  | 634 |   | 
        
           |  |  | 635 |         // Assert report conditions order.
 | 
        
           |  |  | 636 |         $conditions = filter::get_condition_records($report->get('id'), 'filterorder');
 | 
        
           |  |  | 637 |   | 
        
           |  |  | 638 |         $conditionidentifiers = array_map(static function(filter $condition): string {
 | 
        
           |  |  | 639 |             return $condition->get('uniqueidentifier');
 | 
        
           |  |  | 640 |         }, $conditions);
 | 
        
           |  |  | 641 |   | 
        
           |  |  | 642 |         $this->assertEquals([
 | 
        
           |  |  | 643 |             'user:fullname',
 | 
        
           |  |  | 644 |             'user:city',
 | 
        
           |  |  | 645 |             'user:email',
 | 
        
           |  |  | 646 |             'user:country',
 | 
        
           |  |  | 647 |         ], $conditionidentifiers);
 | 
        
           |  |  | 648 |     }
 | 
        
           |  |  | 649 |   | 
        
           |  |  | 650 |     /**
 | 
        
           |  |  | 651 |      * Test re-ordering invalid report condition
 | 
        
           |  |  | 652 |      */
 | 
        
           |  |  | 653 |     public function test_reorder_report_condition_invalid(): void {
 | 
        
           |  |  | 654 |         $this->resetAfterTest();
 | 
        
           |  |  | 655 |         $this->setAdminUser();
 | 
        
           |  |  | 656 |   | 
        
           |  |  | 657 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 658 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 659 |   | 
        
           |  |  | 660 |         $report = $generator->create_report([
 | 
        
           |  |  | 661 |             'name' => 'My report',
 | 
        
           |  |  | 662 |             'source' => users::class,
 | 
        
           |  |  | 663 |             'default' => false,
 | 
        
           |  |  | 664 |         ]);
 | 
        
           |  |  | 665 |   | 
        
           |  |  | 666 |         $this->expectException(invalid_parameter_exception::class);
 | 
        
           |  |  | 667 |         $this->expectExceptionMessage('Invalid condition');
 | 
        
           |  |  | 668 |         report::reorder_report_condition($report->get('id'), 42, 1);
 | 
        
           |  |  | 669 |     }
 | 
        
           |  |  | 670 |   | 
        
           |  |  | 671 |     /**
 | 
        
           |  |  | 672 |      * Test adding report filter
 | 
        
           |  |  | 673 |      */
 | 
        
           |  |  | 674 |     public function test_add_report_filter(): void {
 | 
        
           |  |  | 675 |         $this->resetAfterTest();
 | 
        
           |  |  | 676 |         $this->setAdminUser();
 | 
        
           |  |  | 677 |   | 
        
           |  |  | 678 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 679 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 680 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 | 
        
           |  |  | 681 |   | 
        
           |  |  | 682 |         // Add first filter.
 | 
        
           |  |  | 683 |         $filterfullname = report::add_report_filter($report->get('id'), 'user:fullname');
 | 
        
           |  |  | 684 |         $this->assertTrue(filter::record_exists_select('id = :id AND iscondition = 0',
 | 
        
           |  |  | 685 |             ['id' => $filterfullname->get('id')]));
 | 
        
           |  |  | 686 |   | 
        
           |  |  | 687 |         $this->assertEquals($report->get('id'), $filterfullname->get('reportid'));
 | 
        
           |  |  | 688 |         $this->assertEquals('user:fullname', $filterfullname->get('uniqueidentifier'));
 | 
        
           |  |  | 689 |         $this->assertEquals(1, $filterfullname->get('filterorder'));
 | 
        
           |  |  | 690 |   | 
        
           |  |  | 691 |         // Add second filter.
 | 
        
           |  |  | 692 |         $filteremail = report::add_report_filter($report->get('id'), 'user:email');
 | 
        
           |  |  | 693 |         $this->assertTrue(filter::record_exists_select('id = :id AND iscondition = 0',
 | 
        
           |  |  | 694 |             ['id' => $filteremail->get('id')]));
 | 
        
           |  |  | 695 |   | 
        
           |  |  | 696 |         $this->assertEquals($report->get('id'), $filteremail->get('reportid'));
 | 
        
           |  |  | 697 |         $this->assertEquals('user:email', $filteremail->get('uniqueidentifier'));
 | 
        
           |  |  | 698 |         $this->assertEquals(2, $filteremail->get('filterorder'));
 | 
        
           |  |  | 699 |     }
 | 
        
           |  |  | 700 |   | 
        
           |  |  | 701 |     /**
 | 
        
           |  |  | 702 |      * Test adding invalid report filter
 | 
        
           |  |  | 703 |      */
 | 
        
           |  |  | 704 |     public function test_add_report_filter_invalid(): void {
 | 
        
           |  |  | 705 |         $this->resetAfterTest();
 | 
        
           |  |  | 706 |         $this->setAdminUser();
 | 
        
           |  |  | 707 |   | 
        
           |  |  | 708 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 709 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 710 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 | 
        
           |  |  | 711 |   | 
        
           |  |  | 712 |         $this->expectException(invalid_parameter_exception::class);
 | 
        
           |  |  | 713 |         $this->expectExceptionMessage('Invalid filter');
 | 
        
           |  |  | 714 |         report::add_report_filter($report->get('id'), 'user:invalid');
 | 
        
           |  |  | 715 |     }
 | 
        
           |  |  | 716 |   | 
        
           |  |  | 717 |     /**
 | 
        
           |  |  | 718 |      * Test adding duplicate report filter
 | 
        
           |  |  | 719 |      */
 | 
        
           |  |  | 720 |     public function test_add_report_filter_duplicate(): void {
 | 
        
           |  |  | 721 |         $this->resetAfterTest();
 | 
        
           |  |  | 722 |         $this->setAdminUser();
 | 
        
           |  |  | 723 |   | 
        
           |  |  | 724 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 725 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 726 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 | 
        
           |  |  | 727 |   | 
        
           |  |  | 728 |         // First one is fine.
 | 
        
           |  |  | 729 |         report::add_report_filter($report->get('id'), 'user:email');
 | 
        
           |  |  | 730 |   | 
        
           |  |  | 731 |         $this->expectException(invalid_parameter_exception::class);
 | 
        
           |  |  | 732 |         $this->expectExceptionMessage('Duplicate filter');
 | 
        
           |  |  | 733 |         report::add_report_filter($report->get('id'), 'user:email');
 | 
        
           |  |  | 734 |     }
 | 
        
           |  |  | 735 |   | 
        
           |  |  | 736 |     /**
 | 
        
           |  |  | 737 |      * Test deleting report filter
 | 
        
           |  |  | 738 |      */
 | 
        
           |  |  | 739 |     public function test_delete_report_filter(): void {
 | 
        
           |  |  | 740 |         $this->resetAfterTest();
 | 
        
           |  |  | 741 |         $this->setAdminUser();
 | 
        
           |  |  | 742 |   | 
        
           |  |  | 743 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 744 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 745 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 | 
        
           |  |  | 746 |   | 
        
           |  |  | 747 |         // Add two filters.
 | 
        
           |  |  | 748 |         $filterfullname = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
 | 
        
           |  |  | 749 |         $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 | 
        
           |  |  | 750 |   | 
        
           |  |  | 751 |         // Delete the first filter.
 | 
        
           |  |  | 752 |         $result = report::delete_report_filter($report->get('id'), $filterfullname->get('id'));
 | 
        
           |  |  | 753 |         $this->assertTrue($result);
 | 
        
           |  |  | 754 |   | 
        
           |  |  | 755 |         // Assert report filters.
 | 
        
           |  |  | 756 |         $filters = filter::get_filter_records($report->get('id'));
 | 
        
           |  |  | 757 |         $this->assertCount(1, $filters);
 | 
        
           |  |  | 758 |   | 
        
           |  |  | 759 |         $filter = reset($filters);
 | 
        
           |  |  | 760 |         $this->assertEquals('user:email', $filter->get('uniqueidentifier'));
 | 
        
           |  |  | 761 |         $this->assertEquals(1, $filter->get('filterorder'));
 | 
        
           |  |  | 762 |     }
 | 
        
           |  |  | 763 |   | 
        
           |  |  | 764 |     /**
 | 
        
           |  |  | 765 |      * Test deleting invalid report filter
 | 
        
           |  |  | 766 |      */
 | 
        
           |  |  | 767 |     public function test_delete_report_filter_invalid(): void {
 | 
        
           |  |  | 768 |         $this->resetAfterTest();
 | 
        
           |  |  | 769 |         $this->setAdminUser();
 | 
        
           |  |  | 770 |   | 
        
           |  |  | 771 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 772 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 773 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 | 
        
           |  |  | 774 |   | 
        
           |  |  | 775 |         $this->expectException(invalid_parameter_exception::class);
 | 
        
           |  |  | 776 |         $this->expectExceptionMessage('Invalid filter');
 | 
        
           |  |  | 777 |         report::delete_report_filter($report->get('id'), 42);
 | 
        
           |  |  | 778 |     }
 | 
        
           |  |  | 779 |   | 
        
           |  |  | 780 |     /**
 | 
        
           |  |  | 781 |      * Test re-ordering report filter
 | 
        
           |  |  | 782 |      */
 | 
        
           |  |  | 783 |     public function test_reorder_report_filter(): void {
 | 
        
           |  |  | 784 |         $this->resetAfterTest();
 | 
        
           |  |  | 785 |         $this->setAdminUser();
 | 
        
           |  |  | 786 |   | 
        
           |  |  | 787 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 788 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 789 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
 | 
        
           |  |  | 790 |   | 
        
           |  |  | 791 |         // Add four filters.
 | 
        
           |  |  | 792 |         $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
 | 
        
           |  |  | 793 |         $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
 | 
        
           |  |  | 794 |         $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
 | 
        
           |  |  | 795 |         $filtercity = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
 | 
        
           |  |  | 796 |   | 
        
           |  |  | 797 |         // Move the city filter to second position.
 | 
        
           |  |  | 798 |         $result = report::reorder_report_filter($report->get('id'), $filtercity->get('id'), 2);
 | 
        
           |  |  | 799 |         $this->assertTrue($result);
 | 
        
           |  |  | 800 |   | 
        
           |  |  | 801 |         // Assert report filters order.
 | 
        
           |  |  | 802 |         $filters = filter::get_filter_records($report->get('id'), 'filterorder');
 | 
        
           |  |  | 803 |   | 
        
           |  |  | 804 |         $filteridentifiers = array_map(static function(filter $filter): string {
 | 
        
           |  |  | 805 |             return $filter->get('uniqueidentifier');
 | 
        
           |  |  | 806 |         }, $filters);
 | 
        
           |  |  | 807 |   | 
        
           |  |  | 808 |         $this->assertEquals([
 | 
        
           |  |  | 809 |             'user:fullname',
 | 
        
           |  |  | 810 |             'user:city',
 | 
        
           |  |  | 811 |             'user:email',
 | 
        
           |  |  | 812 |             'user:country',
 | 
        
           |  |  | 813 |         ], $filteridentifiers);
 | 
        
           |  |  | 814 |     }
 | 
        
           |  |  | 815 |   | 
        
           |  |  | 816 |     /**
 | 
        
           |  |  | 817 |      * Test re-ordering invalid report filter
 | 
        
           |  |  | 818 |      */
 | 
        
           |  |  | 819 |     public function test_reorder_report_filter_invalid(): void {
 | 
        
           |  |  | 820 |         $this->resetAfterTest();
 | 
        
           |  |  | 821 |         $this->setAdminUser();
 | 
        
           |  |  | 822 |   | 
        
           |  |  | 823 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 824 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 825 |   | 
        
           |  |  | 826 |         $report = $generator->create_report([
 | 
        
           |  |  | 827 |             'name' => 'My report',
 | 
        
           |  |  | 828 |             'source' => users::class,
 | 
        
           |  |  | 829 |             'default' => false,
 | 
        
           |  |  | 830 |         ]);
 | 
        
           |  |  | 831 |   | 
        
           |  |  | 832 |         $this->expectException(invalid_parameter_exception::class);
 | 
        
           |  |  | 833 |         $this->expectExceptionMessage('Invalid filter');
 | 
        
           |  |  | 834 |         report::reorder_report_filter($report->get('id'), 42, 1);
 | 
        
           |  |  | 835 |     }
 | 
        
           | 1441 | ariadna | 836 |   | 
        
           |  |  | 837 |     /**
 | 
        
           |  |  | 838 |      * Test getting row count for a custom report
 | 
        
           |  |  | 839 |      */
 | 
        
           |  |  | 840 |     public function test_get_report_row_count_custom_report(): void {
 | 
        
           |  |  | 841 |         $this->resetAfterTest();
 | 
        
           |  |  | 842 |   | 
        
           |  |  | 843 |         $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 844 |   | 
        
           |  |  | 845 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 846 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 847 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
 | 
        
           |  |  | 848 |   | 
        
           |  |  | 849 |         // There are two users, the admin plus the user we just created.
 | 
        
           |  |  | 850 |         $this->assertEquals(2, report::get_report_row_count($report->get('id')));
 | 
        
           |  |  | 851 |     }
 | 
        
           |  |  | 852 |   | 
        
           |  |  | 853 |     /**
 | 
        
           |  |  | 854 |      * Test getting row count for a system report
 | 
        
           |  |  | 855 |      */
 | 
        
           |  |  | 856 |     public function test_get_report_row_count_system_report(): void {
 | 
        
           |  |  | 857 |         $this->resetAfterTest();
 | 
        
           |  |  | 858 |         $this->setAdminUser();
 | 
        
           |  |  | 859 |   | 
        
           |  |  | 860 |         $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 861 |   | 
        
           |  |  | 862 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 863 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 864 |   | 
        
           |  |  | 865 |         $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
 | 
        
           |  |  | 866 |         $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => []]);
 | 
        
           |  |  | 867 |   | 
        
           |  |  | 868 |         $reportaccesslist = system_report_factory::create(
 | 
        
           |  |  | 869 |             report_access_list::class,
 | 
        
           |  |  | 870 |             system::instance(),
 | 
        
           |  |  | 871 |             parameters: ['id' => $report->get('id')],
 | 
        
           |  |  | 872 |         )->get_report_persistent();
 | 
        
           |  |  | 873 |   | 
        
           |  |  | 874 |         // There are two users, the admin plus the user we just created.
 | 
        
           |  |  | 875 |         $this->assertEquals(
 | 
        
           |  |  | 876 |             2,
 | 
        
           |  |  | 877 |             report::get_report_row_count($reportaccesslist->get('id'), ['id' => $report->get('id')],
 | 
        
           |  |  | 878 |         ));
 | 
        
           |  |  | 879 |     }
 | 
        
           | 1 | efrain | 880 | }
 |