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_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  /**
  27   * Unit tests for all users report audience type
  28   *
  29   * @package     core_reportbuilder
  30   * @covers      \core_reportbuilder\reportbuilder\audience\allusers
  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 allusers_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          $audience = allusers::create($report->get('id'), []);
  52          $this->assertEquals(get_string('allsiteusers', 'core_reportbuilder'), $audience->get_description());
  53      }
  54  
  55      /**
  56       * Test if user can add this audience type to the report
  57       */
  58      public function test_user_can_add(): void {
  59          $this->resetAfterTest();
  60  
  61          /** @var core_reportbuilder_generator $generator */
  62          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  63  
  64          $report = $generator->create_report([
  65              'name' => 'My report',
  66              'source' => users::class,
  67              'default' => false,
  68          ]);
  69  
  70          $audience = allusers::create($report->get('id'), []);
  71  
  72          // Admin user.
  73          self::setAdminUser();
  74          $this->assertTrue($audience->user_can_add());
  75  
  76          // Non-priveleged user.
  77          $user = self::getDataGenerator()->create_user();
  78          self::setUser($user);
  79          $this->assertFalse($audience->user_can_add());
  80  
  81          // Grant priveleges to user.
  82          $roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
  83          assign_capability('moodle/user:viewalldetails', CAP_ALLOW, $roleid, context_system::instance()->id);
  84          role_assign($roleid, $user->id, context_system::instance()->id);
  85          $this->assertTrue($audience->user_can_add());
  86      }
  87  
  88      /**
  89       * Test if user can edit this audience type
  90       */
  91      public function test_user_can_edit(): void {
  92          $this->resetAfterTest();
  93  
  94          /** @var core_reportbuilder_generator $generator */
  95          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  96  
  97          $report = $generator->create_report([
  98              'name' => 'My report',
  99              'source' => users::class,
 100              'default' => false,
 101          ]);
 102  
 103          $audience = allusers::create($report->get('id'), []);
 104  
 105          // Admin user.
 106          self::setAdminUser();
 107          $this->assertTrue($audience->user_can_edit());
 108  
 109          // Non-priveleged user.
 110          $user = self::getDataGenerator()->create_user();
 111          self::setUser($user);
 112          $this->assertFalse($audience->user_can_edit());
 113  
 114          // Grant priveleges to user.
 115          $roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
 116          assign_capability('moodle/user:viewalldetails', CAP_ALLOW, $roleid, context_system::instance()->id);
 117          role_assign($roleid, $user->id, context_system::instance()->id);
 118          $this->assertTrue($audience->user_can_edit());
 119      }
 120  
 121      /**
 122       * Test that sql generated is correct
 123       */
 124      public function test_get_sql(): void {
 125          global $DB;
 126          $this->resetAfterTest();
 127  
 128          /** @var core_reportbuilder_generator $generator */
 129          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 130  
 131          $report = $generator->create_report([
 132              'name' => 'My report',
 133              'source' => users::class,
 134              'default' => false,
 135          ]);
 136  
 137          $user1 = $this->getDataGenerator()->create_user();
 138          $user2 = $this->getDataGenerator()->create_user();
 139          $user3 = $this->getDataGenerator()->create_user();
 140          $this->getDataGenerator()->create_user(['suspended' => 1]);
 141          $this->getDataGenerator()->create_user(['deleted' => 1]);
 142  
 143          $audience = allusers::create($report->get('id'), []);
 144  
 145          [$join, $where, $params] = $audience->get_sql('u');
 146          $query = 'SELECT u.* FROM {user} u ' . $join . ' WHERE ' . $where;
 147          $records = $DB->get_records_sql($query, $params);
 148  
 149          $this->assertEqualsCanonicalizing([get_admin()->id, $user1->id, $user2->id, $user3->id],
 150              array_column($records, 'id'));
 151      }
 152  }