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]

   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\filters;
  20  
  21  use advanced_testcase;
  22  use lang_string;
  23  use core_reportbuilder\local\report\filter;
  24  
  25  /**
  26   * Unit tests for user report filter
  27   *
  28   * @package     core_reportbuilder
  29   * @covers      \core_reportbuilder\local\filters\base
  30   * @covers      \core_reportbuilder\local\filters\user
  31   * @copyright   2021 Paul Holden <paulh@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class user_test extends advanced_testcase {
  35  
  36      /**
  37       * Data provider for {@see test_get_sql_filter}
  38       *
  39       * @return array
  40       */
  41      public function get_sql_filter_simple(): array {
  42          return [
  43              [user::USER_ANY, ['admin', 'guest', 'user01', 'user02']],
  44              [user::USER_CURRENT, ['user01']],
  45          ];
  46      }
  47  
  48      /**
  49       * Test getting filter SQL
  50       *
  51       * @param int $operator
  52       * @param string[] $expectedusernames
  53       *
  54       * @dataProvider get_sql_filter_simple
  55       */
  56      public function test_get_sql_filter(int $operator, array $expectedusernames): void {
  57          global $DB;
  58  
  59          $this->resetAfterTest();
  60  
  61          $user01 = $this->getDataGenerator()->create_user(['username' => 'user01']);
  62          $user02 = $this->getDataGenerator()->create_user(['username' => 'user02']);
  63  
  64          $this->setUser($user01);
  65  
  66          $filter = new filter(
  67              user::class,
  68              'test',
  69              new lang_string('yes'),
  70              'testentity',
  71              'id'
  72          );
  73  
  74          // Create instance of our filter, passing given operator.
  75          [$select, $params] = user::create($filter)->get_sql_filter([
  76              $filter->get_unique_identifier() . '_operator' => $operator,
  77          ]);
  78  
  79          $usernames = $DB->get_fieldset_select('user', 'username', $select, $params);
  80          $this->assertEqualsCanonicalizing($expectedusernames, $usernames);
  81      }
  82  
  83      /**
  84       * Test getting filter SQL using specific user selection operator/value
  85       */
  86      public function test_get_sql_filter_select_user(): void {
  87          global $DB;
  88  
  89          $this->resetAfterTest();
  90  
  91          $user01 = $this->getDataGenerator()->create_user(['username' => 'user01']);
  92          $user02 = $this->getDataGenerator()->create_user(['username' => 'user02']);
  93  
  94          $filter = new filter(
  95              user::class,
  96              'test',
  97              new lang_string('yes'),
  98              'testentity',
  99              'id'
 100          );
 101  
 102          // Create instance of our filter, passing given operator/value matching second user.
 103          [$select, $params] = user::create($filter)->get_sql_filter([
 104              $filter->get_unique_identifier() . '_operator' => user::USER_SELECT,
 105              $filter->get_unique_identifier() . '_value' => [$user02->id],
 106          ]);
 107  
 108          $usernames = $DB->get_fieldset_select('user', 'username', $select, $params);
 109          $this->assertEquals([$user02->username], $usernames);
 110      }
 111  }