Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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_user\reportbuilder\datasource\users;
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
  29  
  30  /**
  31   * Unit tests for group concatenation distinct aggregation
  32   *
  33   * @package     core_reportbuilder
  34   * @covers      \core_reportbuilder\local\aggregation\base
  35   * @covers      \core_reportbuilder\local\aggregation\groupconcatdistinct
  36   * @copyright   2021 Paul Holden <paulh@moodle.com>
  37   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class groupconcatdistinct_test extends core_reportbuilder_testcase {
  40  
  41      /**
  42       * Test setup, we need to skip these tests on non-supported databases
  43       */
  44      public function setUp(): void {
  45          global $DB;
  46  
  47          $dbfamily = $DB->get_dbfamily();
  48          if (!in_array($dbfamily, ['mysql', 'postgres'])) {
  49              $this->markTestSkipped("Distinct group concatenation not supported in {$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 callback
  94       */
  95      public function test_column_aggregation_with_callback(): void {
  96          $this->resetAfterTest();
  97  
  98          // Test subjects.
  99          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 1]);
 100          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 0]);
 101          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 1]);
 102  
 103          /** @var core_reportbuilder_generator $generator */
 104          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 105          $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
 106  
 107          // First column, sorted.
 108          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
 109  
 110          // This is the column we'll aggregate.
 111          $generator->create_column([
 112              'reportid' => $report->get('id'),
 113              'uniqueidentifier' => 'user:confirmed',
 114              'aggregation' => groupconcatdistinct::get_class_name(),
 115          ]);
 116  
 117          // Assert confirmed column was aggregated, and sorted predictably with callback applied.
 118          $content = $this->get_custom_report_content($report->get('id'));
 119          $this->assertEquals([
 120              [
 121                  'c0_firstname' => 'Admin',
 122                  'c1_confirmed' => 'Yes',
 123              ],
 124              [
 125                  'c0_firstname' => 'Bob',
 126                  'c1_confirmed' => 'No, Yes',
 127              ],
 128          ], $content);
 129      }
 130  }