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 avg aggregation
  32   *
  33   * @package     core_reportbuilder
  34   * @covers      \core_reportbuilder\local\aggregation\avg
  35   * @copyright   2022 Paul Holden <paulh@moodle.com>
  36   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class avg_test extends core_reportbuilder_testcase {
  39  
  40      /**
  41       * Test aggregation when applied to column
  42       */
  43      public function test_column_aggregation(): void {
  44          $this->resetAfterTest();
  45  
  46          // Test subjects.
  47          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]);
  48          $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 0]);
  49  
  50          /** @var core_reportbuilder_generator $generator */
  51          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  52          $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
  53  
  54          // First column, sorted.
  55          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
  56  
  57          // This is the column we'll aggregate.
  58          $generator->create_column(
  59              ['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => avg::get_class_name()]
  60          );
  61  
  62          $content = $this->get_custom_report_content($report->get('id'));
  63          $this->assertEquals([
  64              [
  65                  'c0_firstname' => 'Admin',
  66                  'c1_suspended' => '0.0',
  67              ],
  68              [
  69                  'c0_firstname' => 'Bob',
  70                  'c1_suspended' => '0.5',
  71              ],
  72          ], $content);
  73      }
  74  }