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.
   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\audience;
  20  
  21  use advanced_testcase;
  22  use context;
  23  use core_reportbuilder_generator;
  24  use core_user\reportbuilder\datasource\users;
  25  
  26  /**
  27   * Unit tests for cohort member report audience type
  28   *
  29   * @package     core_reportbuilder
  30   * @covers      \core_cohort\reportbuilder\audience\cohortmember
  31   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class cohortmember_test extends advanced_testcase {
  35  
  36      /**
  37       * Test that this audience type description is generated correctly
  38       */
  39      public function test_get_description(): void {
  40          $this->resetAfterTest();
  41  
  42          /** @var core_reportbuilder_generator $generator */
  43          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  44  
  45          $report = $generator->create_report([
  46              'name' => 'My report',
  47              'source' => users::class,
  48              'default' => false,
  49          ]);
  50  
  51          $cohort = self::getDataGenerator()->create_cohort();
  52          $audience = cohortmember::create($report->get('id'), ['cohorts' => [$cohort->id]]);
  53          $this->assertEquals($cohort->name, $audience->get_description());
  54      }
  55  
  56      /**
  57       * Test if user can add this audience type to the report
  58       */
  59      public function test_user_can_add(): void {
  60          $this->resetAfterTest();
  61  
  62          /** @var core_reportbuilder_generator $generator */
  63          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  64  
  65          $report = $generator->create_report([
  66              'name' => 'My report',
  67              'source' => users::class,
  68              'default' => false,
  69          ]);
  70  
  71          // Admin user.
  72          self::setAdminUser();
  73  
  74          $cohort = self::getDataGenerator()->create_cohort();
  75          $context = context::instance_by_id($cohort->contextid);
  76          $audience = cohortmember::create($report->get('id'), ['cohorts' => [$cohort->id]]);
  77  
  78          $this->assertTrue($audience->user_can_add());
  79  
  80          // Non-priveleged user.
  81          $user = self::getDataGenerator()->create_user();
  82          self::setUser($user);
  83          $this->assertFalse($audience->user_can_add());
  84  
  85          // Grant priveleges to user (moodle/cohort:view).
  86          $roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
  87          assign_capability('moodle/cohort:view', CAP_ALLOW, $roleid, $context->id);
  88          role_assign($roleid, $user->id, $context->id);
  89          $this->assertTrue($audience->user_can_add());
  90      }
  91  
  92      /**
  93       * Test if user can edit this audience type
  94       */
  95      public function test_user_can_edit(): void {
  96          $this->resetAfterTest();
  97  
  98          /** @var core_reportbuilder_generator $generator */
  99          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 100  
 101          $report = $generator->create_report([
 102              'name' => 'My report',
 103              'source' => users::class,
 104              'default' => false,
 105          ]);
 106  
 107          $cohort = self::getDataGenerator()->create_cohort();
 108          $context = context::instance_by_id($cohort->contextid);
 109          $audience = cohortmember::create($report->get('id'), ['cohorts' => [$cohort->id]]);
 110  
 111          // Admin user.
 112          self::setAdminUser();
 113          $this->assertTrue($audience->user_can_edit());
 114  
 115          // Non-priveleged user.
 116          $user = self::getDataGenerator()->create_user();
 117          self::setUser($user);
 118          $this->assertFalse($audience->user_can_edit());
 119  
 120          // Grant priveleges to user (moodle/cohort:view).
 121          $roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
 122          assign_capability('moodle/cohort:view', CAP_ALLOW, $roleid, $context->id);
 123          role_assign($roleid, $user->id, $context->id);
 124          $this->assertTrue($audience->user_can_edit());
 125      }
 126  
 127      /**
 128       * Test that sql generated is correct
 129       */
 130      public function test_get_sql(): void {
 131          global $DB;
 132          $this->resetAfterTest();
 133  
 134          /** @var core_reportbuilder_generator $generator */
 135          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 136  
 137          $report = $generator->create_report([
 138              'name' => 'My report',
 139              'source' => users::class,
 140              'default' => false,
 141          ]);
 142  
 143          $cohort = self::getDataGenerator()->create_cohort();
 144  
 145          $user1 = $this->getDataGenerator()->create_user();
 146          $user2 = $this->getDataGenerator()->create_user();
 147          $user3 = $this->getDataGenerator()->create_user();
 148  
 149          // Add user1 into cohort.
 150          cohort_add_member($cohort->id, $user1->id);
 151          // Add user3 into cohort.
 152          cohort_add_member($cohort->id, $user3->id);
 153  
 154          $audience = cohortmember::create($report->get('id'), ['cohorts' => [$cohort->id]]);
 155  
 156          [$join, $where, $params] = $audience->get_sql('u');
 157          $query = 'SELECT u.* FROM {user} u ' . $join . ' WHERE ' . $where;
 158          $records = $DB->get_records_sql($query, $params);
 159  
 160          $this->assertEqualsCanonicalizing([$user1->id, $user3->id], array_column($records, 'id'));
 161      }
 162  
 163      /**
 164       * Test if this audience type is available to use
 165       */
 166      public function test_is_available(): void {
 167          $this->resetAfterTest();
 168  
 169          /** @var core_reportbuilder_generator $generator */
 170          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 171  
 172          self::setAdminUser();
 173  
 174          // Check with no cohorts available in the system.
 175          $report = $generator->create_report([
 176              'name' => 'My report',
 177              'source' => users::class,
 178              'default' => false,
 179          ]);
 180          $audience = cohortmember::create($report->get('id'), ['cohorts' => []]);
 181          $this->assertFalse($audience->is_available());
 182  
 183          // Check with cohorts available in the system.
 184          self::getDataGenerator()->create_cohort();
 185          $report = $generator->create_report([
 186              'name' => 'My report',
 187              'source' => users::class,
 188              'default' => false,
 189          ]);
 190          $audience2 = cohortmember::create($report->get('id'), ['cohorts' => []]);
 191          $this->assertTrue($audience2->is_available());
 192      }
 193  }