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_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  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
  31  
  32  /**
  33   * Unit tests for sum aggregation
  34   *
  35   * @package     core_reportbuilder
  36   * @covers      \core_reportbuilder\local\aggregation\base
  37   * @covers      \core_reportbuilder\local\aggregation\sum
  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 sum_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', 'suspended' => 1]);
  51          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]);
  52  
  53          /** @var core_reportbuilder_generator $generator */
  54          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  55          $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
  56  
  57          // First column, sorted.
  58          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
  59  
  60          // This is the column we'll aggregate.
  61          $generator->create_column([
  62              'reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => sum::get_class_name()
  63          ]);
  64  
  65          $content = $this->get_custom_report_content($report->get('id'));
  66          $this->assertEquals([
  67              [
  68                  'c0_firstname' => 'Admin',
  69                  'c1_suspended' => 0,
  70              ],
  71              [
  72                  'c0_firstname' => 'Bob',
  73                  'c1_suspended' => 2,
  74              ],
  75          ], $content);
  76      }
  77  
  78      /**
  79       * Test aggregation when applied to column with callback
  80       */
  81      public function test_column_aggregation_with_callback(): void {
  82          $this->resetAfterTest();
  83  
  84          // Test subjects.
  85          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]);
  86          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]);
  87  
  88          /** @var core_reportbuilder_generator $generator */
  89          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  90          $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
  91  
  92          // First column, sorted.
  93          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
  94  
  95          // This is the column we'll aggregate.
  96          $generator->create_column(
  97              ['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => sum::get_class_name()]
  98          );
  99  
 100          // Set callback to format the column (hack column definition to ensure callbacks are executed).
 101          $instance = manager::get_report_from_persistent($report);
 102          $instance->get_column('user:suspended')
 103              ->set_type(column::TYPE_INTEGER)
 104              ->set_callback(static function(int $value): string {
 105                  return "{$value} suspended";
 106              });
 107  
 108          $content = $this->get_custom_report_content($report->get('id'));
 109          $this->assertEquals([
 110              [
 111                  'c0_firstname' => 'Admin',
 112                  'c1_suspended' => '0 suspended',
 113              ],
 114              [
 115                  'c0_firstname' => 'Bob',
 116                  'c1_suspended' => '2 suspended',
 117              ],
 118          ], $content);
 119      }
 120  }