See Release Notes
Long Term Support Release
Differences Between: [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]
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\aggregation; 20 21 use core_reportbuilder_testcase; 22 use core_reportbuilder_generator; 23 use core_reportbuilder\local\report\column; 24 use core_user\reportbuilder\datasource\users; 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php"); 30 31 /** 32 * Unit tests for group concatenation distinct aggregation 33 * 34 * @package core_reportbuilder 35 * @covers \core_reportbuilder\local\aggregation\base 36 * @covers \core_reportbuilder\local\aggregation\groupconcatdistinct 37 * @copyright 2021 Paul Holden <paulh@moodle.com> 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class groupconcatdistinct_test extends core_reportbuilder_testcase { 41 42 /** 43 * Test setup, we need to skip these tests on non-supported databases 44 */ 45 public function setUp(): void { 46 global $DB; 47 48 if (!groupconcatdistinct::compatible(column::TYPE_TEXT)) { 49 $this->markTestSkipped('Distinct group concatenation not supported in ' . $DB->get_dbfamily()); 50 } 51 } 52 53 /** 54 * Test aggregation when applied to column 55 */ 56 public function test_column_aggregation(): void { 57 $this->resetAfterTest(); 58 59 // Test subjects. 60 $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Banana']); 61 $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Apple']); 62 $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Banana']); 63 64 /** @var core_reportbuilder_generator $generator */ 65 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 66 $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]); 67 68 // First column, sorted. 69 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]); 70 71 // This is the column we'll aggregate. 72 $generator->create_column([ 73 'reportid' => $report->get('id'), 74 'uniqueidentifier' => 'user:lastname', 75 'aggregation' => groupconcatdistinct::get_class_name(), 76 ]); 77 78 // Assert lastname column was aggregated, and sorted predictably. 79 $content = $this->get_custom_report_content($report->get('id')); 80 $this->assertEquals([ 81 [ 82 'c0_firstname' => 'Admin', 83 'c1_lastname' => 'User', 84 ], 85 [ 86 'c0_firstname' => 'Bob', 87 'c1_lastname' => 'Apple, Banana', 88 ], 89 ], $content); 90 } 91 92 /** 93 * Test aggregation when applied to column with multiple fields 94 */ 95 public function test_column_aggregation_multiple_fields(): void { 96 $this->resetAfterTest(); 97 98 $user = $this->getDataGenerator()->create_user(['firstname' => 'Adam', 'lastname' => 'Apple']); 99 100 /** @var core_reportbuilder_generator $generator */ 101 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 102 $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]); 103 104 // This is the column we'll aggregate. 105 $generator->create_column([ 106 'reportid' => $report->get('id'), 107 'uniqueidentifier' => 'user:fullnamewithlink', 108 'aggregation' => groupconcatdistinct::get_class_name(), 109 ]); 110 111 $content = $this->get_custom_report_content($report->get('id')); 112 $this->assertCount(1, $content); 113 114 // Ensure users are sorted predictably (Adam -> Admin). 115 [$userone, $usertwo] = explode(', ', reset($content[0])); 116 $this->assertStringContainsString(fullname($user, true), $userone); 117 $this->assertStringContainsString(fullname(get_admin(), true), $usertwo); 118 } 119 120 /** 121 * Test aggregation when applied to column with callback 122 */ 123 public function test_column_aggregation_with_callback(): void { 124 $this->resetAfterTest(); 125 126 // Test subjects. 127 $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 1]); 128 $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 0]); 129 $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 1]); 130 131 /** @var core_reportbuilder_generator $generator */ 132 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 133 $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]); 134 135 // First column, sorted. 136 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]); 137 138 // This is the column we'll aggregate. 139 $generator->create_column([ 140 'reportid' => $report->get('id'), 141 'uniqueidentifier' => 'user:confirmed', 142 'aggregation' => groupconcatdistinct::get_class_name(), 143 ]); 144 145 // Assert confirmed column was aggregated, and sorted predictably with callback applied. 146 $content = $this->get_custom_report_content($report->get('id')); 147 $this->assertEquals([ 148 [ 149 'c0_firstname' => 'Admin', 150 'c1_confirmed' => 'Yes', 151 ], 152 [ 153 'c0_firstname' => 'Bob', 154 'c1_confirmed' => 'No, Yes', 155 ], 156 ], $content); 157 } 158 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body