Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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