Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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_badges_generator;
  22  use core_badges\reportbuilder\datasource\badges;
  23  use core_reportbuilder_testcase;
  24  use core_reportbuilder_generator;
  25  use core_user\reportbuilder\datasource\users;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
  31  
  32  /**
  33   * Unit tests for group concatenation aggregation
  34   *
  35   * @package     core_reportbuilder
  36   * @covers      \core_reportbuilder\local\aggregation\base
  37   * @covers      \core_reportbuilder\local\aggregation\groupconcat
  38   * @copyright   2021 Paul Holden <paulh@moodle.com>
  39   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class groupconcat_test extends core_reportbuilder_testcase {
  42  
  43      /**
  44       * Test aggregation when applied to column
  45       */
  46      public function test_column_aggregation(): void {
  47          $this->resetAfterTest();
  48  
  49          // Test subjects.
  50          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Banana']);
  51          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Apple']);
  52          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Banana']);
  53  
  54          /** @var core_reportbuilder_generator $generator */
  55          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  56          $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
  57  
  58          // First column, sorted.
  59          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
  60  
  61          // This is the column we'll aggregate.
  62          $generator->create_column([
  63              'reportid' => $report->get('id'),
  64              'uniqueidentifier' => 'user:lastname',
  65              'aggregation' => groupconcat::get_class_name(),
  66          ]);
  67  
  68          // Assert lastname column was aggregated, and sorted predictably.
  69          $content = $this->get_custom_report_content($report->get('id'));
  70          $this->assertEquals([
  71              [
  72                  'c0_firstname' => 'Admin',
  73                  'c1_lastname' => 'User',
  74              ],
  75              [
  76                  'c0_firstname' => 'Bob',
  77                  'c1_lastname' => 'Apple, Banana, Banana',
  78              ],
  79          ], $content);
  80      }
  81  
  82      /**
  83       * Test aggregation when applied to column with multiple fields
  84       */
  85      public function test_column_aggregation_multiple_fields(): void {
  86          $this->resetAfterTest();
  87  
  88          $user = $this->getDataGenerator()->create_user(['firstname' => 'Adam', 'lastname' => 'Apple']);
  89  
  90          /** @var core_reportbuilder_generator $generator */
  91          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  92          $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
  93  
  94          // This is the column we'll aggregate.
  95          $generator->create_column([
  96              'reportid' => $report->get('id'),
  97              'uniqueidentifier' => 'user:fullnamewithlink',
  98              'aggregation' => groupconcat::get_class_name(),
  99          ]);
 100  
 101          $content = $this->get_custom_report_content($report->get('id'));
 102          $this->assertCount(1, $content);
 103  
 104          // Ensure users are sorted predictably (Adam -> Admin).
 105          [$userone, $usertwo] = explode(', ', reset($content[0]));
 106          $this->assertStringContainsString(fullname($user, true), $userone);
 107          $this->assertStringContainsString(fullname(get_admin(), true), $usertwo);
 108      }
 109  
 110      /**
 111       * Test aggregation when applied to column with callback
 112       */
 113      public function test_column_aggregation_with_callback(): void {
 114          $this->resetAfterTest();
 115  
 116          // Test subjects.
 117          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 1]);
 118          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 0]);
 119          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 1]);
 120  
 121          /** @var core_reportbuilder_generator $generator */
 122          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 123          $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
 124  
 125          // First column, sorted.
 126          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
 127  
 128          // This is the column we'll aggregate.
 129          $generator->create_column([
 130              'reportid' => $report->get('id'),
 131              'uniqueidentifier' => 'user:confirmed',
 132              'aggregation' => groupconcat::get_class_name(),
 133          ]);
 134  
 135          // Assert confirmed column was aggregated, and sorted predictably with callback applied.
 136          $content = $this->get_custom_report_content($report->get('id'));
 137          $this->assertEquals([
 138              [
 139                  'c0_firstname' => 'Admin',
 140                  'c1_confirmed' => 'Yes',
 141              ],
 142              [
 143                  'c0_firstname' => 'Bob',
 144                  'c1_confirmed' => 'No, Yes, Yes',
 145              ],
 146          ], $content);
 147      }
 148  
 149      /**
 150       * Test aggregation when applied to column with callback that expects/handles null values
 151       */
 152      public function test_datasource_aggregate_column_callback_with_null(): void {
 153          $this->resetAfterTest();
 154          $this->setAdminUser();
 155  
 156          $userone = $this->getDataGenerator()->create_user(['description' => 'First user']);
 157          $usertwo = $this->getDataGenerator()->create_user(['description' => 'Second user']);
 158  
 159          /** @var core_badges_generator $generator */
 160          $generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
 161  
 162          // Create course badge, issue to both users.
 163          $badgeone = $generator->create_badge(['name' => 'First badge']);
 164          $badgeone->issue($userone->id, true);
 165          $badgeone->issue($usertwo->id, true);
 166  
 167          // Create second badge, without issuing to anyone.
 168          $badgetwo = $generator->create_badge(['name' => 'Second badge']);
 169  
 170          /** @var core_reportbuilder_generator $generator */
 171          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 172          $report = $generator->create_report(['name' => 'Badges', 'source' => badges::class, 'default' => 0]);
 173  
 174          // First column, sorted.
 175          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:name', 'sortenabled' => 1]);
 176  
 177          // This is the column we'll aggregate.
 178          $generator->create_column([
 179              'reportid' => $report->get('id'),
 180              'uniqueidentifier' => 'user:description',
 181              'aggregation' => groupconcat::get_class_name(),
 182          ]);
 183  
 184          // Assert description column was aggregated, with callbacks accounting for null values.
 185          $content = $this->get_custom_report_content($report->get('id'));
 186          $this->assertEquals([
 187              [
 188                  'c0_name' => $badgeone->name,
 189                  'c1_description' => "{$userone->description}, {$usertwo->description}",
 190              ],
 191              [
 192                  'c0_name' => $badgetwo->name,
 193                  'c1_description' => '',
 194              ],
 195          ], $content);
 196      }
 197  }