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 402 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_cohort\reportbuilder\datasource;
  20  
  21  use core_reportbuilder_generator;
  22  use core_reportbuilder_testcase;
  23  use core_reportbuilder\manager;
  24  use core_reportbuilder\local\filters\user;
  25  use core_user;
  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 cohorts datasource
  34   *
  35   * @package     core_cohort
  36   * @covers      \core_cohort\reportbuilder\datasource\cohorts
  37   * @copyright   2021 Paul Holden <paulh@moodle.com>
  38   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class cohorts_test extends core_reportbuilder_testcase {
  41  
  42      /**
  43       * Test cohorts datasource
  44       */
  45      public function test_cohorts_datasource(): void {
  46          $this->resetAfterTest();
  47  
  48          // Test subject.
  49          $cohort = $this->getDataGenerator()->create_cohort([
  50              'name' => 'Legends',
  51              'idnumber' => 'C101',
  52              'description' => 'Cohort for the legends',
  53          ]);
  54  
  55          $user = $this->getDataGenerator()->create_user(['firstname' => 'Lionel', 'lastname' => 'Richards']);
  56          cohort_add_member($cohort->id, $user->id);
  57  
  58          /** @var core_reportbuilder_generator $generator */
  59          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  60          $report = $generator->create_report(['name' => 'Cohorts', 'source' => cohorts::class]);
  61  
  62          // Add user fullname column to the report.
  63          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
  64  
  65          $content = $this->get_custom_report_content($report->get('id'));
  66          $this->assertCount(1, $content);
  67  
  68          $contentrow = array_values(reset($content));
  69          $this->assertEquals([
  70              'Legends', // Name.
  71              'System', // Context.
  72              'C101', // ID number.
  73              '<div class="text_to_html">Cohort for the legends</div>', // Description.
  74              'Lionel Richards', // User.
  75          ], $contentrow);
  76      }
  77  
  78      /**
  79       * Data provider for {@see test_cohorts_datasource_user_select}
  80       *
  81       * @return array[]
  82       */
  83      public function cohorts_datasource_user_select_provider(): array {
  84          return [
  85              ['user01', 'Cohort01'],
  86              ['user02', 'Cohort02'],
  87          ];
  88      }
  89  
  90      /**
  91       * Test cohorts datasource, while adding the user select condition
  92       *
  93       * @param string $username
  94       * @param string $expectedcohort
  95       *
  96       * @dataProvider cohorts_datasource_user_select_provider
  97       */
  98      public function test_cohorts_datasource_user_select(string $username, string $expectedcohort): void {
  99          $this->resetAfterTest();
 100  
 101          // First cohort/user member.
 102          $cohort01 = $this->getDataGenerator()->create_cohort(['name' => 'Cohort01']);
 103          $user01 = $this->getDataGenerator()->create_user(['username' => 'user01']);
 104          cohort_add_member($cohort01->id, $user01->id);
 105  
 106          // Second cohort/user member.
 107          $cohort02 = $this->getDataGenerator()->create_cohort(['name' => 'Cohort02']);
 108          $user02 = $this->getDataGenerator()->create_user(['username' => 'user02']);
 109          cohort_add_member($cohort02->id, $user02->id);
 110  
 111          /** @var core_reportbuilder_generator $generator */
 112          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 113          $report = $generator->create_report(['name' => 'User cohorts', 'source' => cohorts::class, 'default' => 0]);
 114  
 115          // Add cohort name and user fullname columns.
 116          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:name']);
 117          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']);
 118  
 119          // Add condition to limit report data to current user.
 120          $condition = $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:userselect']);
 121          manager::get_report_from_persistent($report)->set_condition_values([
 122              $condition->get('uniqueidentifier') . '_operator' => user::USER_CURRENT,
 123          ]);
 124  
 125          // Switch user, request report.
 126          $currentuser = core_user::get_user_by_username($username);
 127          $this->setUser($currentuser);
 128  
 129          $content = $this->get_custom_report_content($report->get('id'));
 130          $this->assertCount(1, $content);
 131  
 132          $contentrow = array_values(reset($content));
 133          $this->assertEquals([$expectedcohort, $username], $contentrow);
 134      }
 135  
 136      /**
 137       * Stress test datasource
 138       *
 139       * In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
 140       */
 141      public function test_stress_datasource(): void {
 142          if (!PHPUNIT_LONGTEST) {
 143              $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
 144          }
 145  
 146          $this->resetAfterTest();
 147  
 148          $cohort = $this->getDataGenerator()->create_cohort();
 149          $user = $this->getDataGenerator()->create_user();
 150          cohort_add_member($cohort->id, $user->id);
 151  
 152          $this->datasource_stress_test_columns(cohorts::class);
 153          $this->datasource_stress_test_columns_aggregation(cohorts::class);
 154          $this->datasource_stress_test_conditions(cohorts::class, 'cohort:name');
 155      }
 156  }