Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 400 and 402] [Versions 401 and 402]

   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 sum aggregation
  35   *
  36   * @package     core_reportbuilder
  37   * @covers      \core_reportbuilder\local\aggregation\base
  38   * @covers      \core_reportbuilder\local\aggregation\sum
  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 sum_test extends core_reportbuilder_testcase {
  43  
  44      /**
  45       * Test aggregation when applied to column
  46       */
  47      public function test_column_aggregation(): void {
  48          $this->resetAfterTest();
  49  
  50          // Test subjects.
  51          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]);
  52          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]);
  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'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => sum::get_class_name()
  64          ]);
  65  
  66          $content = $this->get_custom_report_content($report->get('id'));
  67          $this->assertEquals([
  68              [
  69                  'c0_firstname' => 'Admin',
  70                  'c1_suspended' => 0,
  71              ],
  72              [
  73                  'c0_firstname' => 'Bob',
  74                  'c1_suspended' => 2,
  75              ],
  76          ], $content);
  77      }
  78  
  79      /**
  80       * Test aggregation when applied to column with callback
  81       */
  82      public function test_column_aggregation_with_callback(): void {
  83          $this->resetAfterTest();
  84  
  85          // Test subjects.
  86          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]);
  87          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]);
  88  
  89          /** @var core_reportbuilder_generator $generator */
  90          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  91          $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
  92  
  93          // First column, sorted.
  94          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
  95  
  96          // This is the column we'll aggregate.
  97          $generator->create_column(
  98              ['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => sum::get_class_name()]
  99          );
 100  
 101          // Set callback to format the column (hack column definition to ensure callbacks are executed).
 102          $instance = manager::get_report_from_persistent($report);
 103          $instance->get_column('user:suspended')
 104              ->set_type(column::TYPE_INTEGER)
 105              ->set_callback(static function(int $value, stdClass $row, $arguments, ?string $aggregation): string {
 106                  // Simple callback to return the given value, and append aggregation type.
 107                  return "{$value} ({$aggregation})";
 108              });
 109  
 110          $content = $this->get_custom_report_content($report->get('id'));
 111          $this->assertEquals([
 112              [
 113                  'c0_firstname' => 'Admin',
 114                  'c1_suspended' => '0 (sum)',
 115              ],
 116              [
 117                  'c0_firstname' => 'Bob',
 118                  'c1_suspended' => '2 (sum)',
 119              ],
 120          ], $content);
 121      }
 122  }