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.
   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\reportbuilder\audience;
  20  
  21  use advanced_testcase;
  22  use context_system;
  23  use core_reportbuilder_generator;
  24  use core_user\reportbuilder\datasource\users;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Unit tests for system role report audience type
  30   *
  31   * @package     core_reportbuilder
  32   * @covers      \core_reportbuilder\reportbuilder\audience\systemrole
  33   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class systemrole_test extends advanced_testcase {
  37  
  38      /**
  39       * Test that this audience type description is generated correctly
  40       */
  41      public function test_get_description(): void {
  42          global $DB;
  43  
  44          $this->resetAfterTest();
  45  
  46          /** @var core_reportbuilder_generator $generator */
  47          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  48  
  49          self::setAdminUser();
  50  
  51          $role = $DB->get_record('role', ['shortname' => 'manager']);
  52          $rolename = role_get_name($role, null, ROLENAME_ALIAS);
  53  
  54          $report = $generator->create_report([
  55              'name' => 'My report',
  56              'source' => users::class,
  57              'default' => false,
  58          ]);
  59  
  60          $audience = systemrole::create($report->get('id'), ['roles' => [$role->id]]);
  61          $this->assertEquals($rolename, $audience->get_description());
  62      }
  63  
  64      /**
  65       * Test if user can add this audience type to the report
  66       */
  67      public function test_user_can_add(): void {
  68          $this->resetAfterTest();
  69  
  70          /** @var core_reportbuilder_generator $generator */
  71          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  72  
  73          self::setAdminUser();
  74          $roles = get_assignable_roles(context_system::instance(), ROLENAME_ALIAS);
  75  
  76          $report = $generator->create_report([
  77              'name' => 'My report',
  78              'source' => users::class,
  79              'default' => false,
  80          ]);
  81  
  82          $audience = systemrole::create($report->get('id'), ['roles' => array_keys($roles)]);
  83  
  84          // Admin user.
  85          self::setAdminUser();
  86          $this->assertTrue($audience->user_can_add());
  87  
  88          // Non-priveleged user.
  89          $user = self::getDataGenerator()->create_user();
  90          self::setUser($user);
  91          $this->assertFalse($audience->user_can_add());
  92      }
  93  
  94      /**
  95       * Test if user can edit this audience type
  96       */
  97      public function test_user_can_edit(): void {
  98          $this->resetAfterTest();
  99  
 100          /** @var core_reportbuilder_generator $generator */
 101          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 102  
 103          self::setAdminUser();
 104          $roles = get_assignable_roles(context_system::instance(), ROLENAME_ALIAS);
 105          $this->assertNotEmpty($roles);
 106  
 107          $report = $generator->create_report([
 108              'name' => 'My report',
 109              'source' => users::class,
 110              'default' => false,
 111          ]);
 112  
 113          $audience = systemrole::create($report->get('id'), ['roles' => array_keys($roles)]);
 114  
 115          // Admin user.
 116          self::setAdminUser();
 117          $this->assertTrue($audience->user_can_edit());
 118  
 119          // Non-priveleged user.
 120          $user = self::getDataGenerator()->create_user();
 121          self::setUser($user);
 122          $this->assertFalse($audience->user_can_edit());
 123      }
 124  
 125      /**
 126       * Test that sql generated is correct
 127       */
 128      public function test_get_sql(): void {
 129          global $DB;
 130          $this->resetAfterTest();
 131  
 132          /** @var core_reportbuilder_generator $generator */
 133          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 134  
 135          $report = $generator->create_report([
 136              'name' => 'My report',
 137              'source' => users::class,
 138              'default' => false,
 139          ]);
 140  
 141          $user1 = $this->getDataGenerator()->create_user();
 142          $user2 = $this->getDataGenerator()->create_user();
 143          $user3 = $this->getDataGenerator()->create_user();
 144  
 145          self::setAdminUser();
 146          $roles = get_assignable_roles(context_system::instance(), ROLENAME_ALIAS);
 147          $this->assertNotEmpty($roles);
 148  
 149          // Assign new role to user1 and user3.
 150          $roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
 151          role_assign($roleid, $user1->id, context_system::instance()->id);
 152          role_assign($roleid, $user3->id, context_system::instance()->id);
 153  
 154          $audience = systemrole::create($report->get('id'), ['roles' => [$roleid]]);
 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  }