Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 400 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_reportbuilder\local\entities;
  20  
  21  use advanced_testcase;
  22  
  23  /**
  24   * Unit tests for user entity
  25   *
  26   * @package     core_reportbuilder
  27   * @covers      \core_reportbuilder\local\entities\user
  28   * @copyright   2021 Paul Holden <paulh@moodle.com>
  29   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class user_test extends advanced_testcase {
  32  
  33      /**
  34       * Test getting user identity column
  35       */
  36      public function test_get_identity_column(): void {
  37          $this->resetAfterTest();
  38  
  39          $this->getDataGenerator()->create_custom_profile_field(['datatype' => 'text', 'name' => 'Hi', 'shortname' => 'hi']);
  40  
  41          $user = new user();
  42          $user->initialise();
  43  
  44          $columnusername = $user->get_identity_column('username');
  45          $this->assertEquals('user:username', $columnusername->get_unique_identifier());
  46  
  47          $columnprofilefield = $user->get_identity_column('profile_field_hi');
  48          $this->assertEquals('user:profilefield_hi', $columnprofilefield->get_unique_identifier());
  49      }
  50  
  51      /**
  52       * Test getting user identity filter
  53       */
  54      public function test_get_identity_filter(): void {
  55          $this->resetAfterTest();
  56  
  57          $this->getDataGenerator()->create_custom_profile_field(['datatype' => 'text', 'name' => 'Hi', 'shortname' => 'hi']);
  58  
  59          $user = new user();
  60          $user->initialise();
  61  
  62          $filterusername = $user->get_identity_filter('username');
  63          $this->assertEquals('user:username', $filterusername->get_unique_identifier());
  64  
  65          $filterprofilefield = $user->get_identity_filter('profile_field_hi');
  66          $this->assertEquals('user:profilefield_hi', $filterprofilefield->get_unique_identifier());
  67      }
  68  
  69      /**
  70       * Data provider for {@see test_get_name_fields_select}
  71       *
  72       * @return array
  73       */
  74      public function get_name_fields_select_provider(): array {
  75          return [
  76              ['firstname lastname', ['firstname', 'lastname']],
  77              ['firstname middlename lastname', ['firstname', 'middlename', 'lastname']],
  78              ['alternatename lastname firstname', ['alternatename', 'lastname', 'firstname']],
  79          ];
  80      }
  81  
  82      /**
  83       * Tests the helper method for selecting all of a users' name fields
  84       *
  85       * @param string $fullnamedisplay
  86       * @param string[] $expecteduserfields
  87       *
  88       * @dataProvider get_name_fields_select_provider
  89       */
  90      public function test_get_name_fields_select(string $fullnamedisplay, array $expecteduserfields): void {
  91          global $DB;
  92  
  93          $this->resetAfterTest(true);
  94  
  95          set_config('alternativefullnameformat', $fullnamedisplay);
  96  
  97          $fields = user::get_name_fields_select('u');
  98          $user = $DB->get_record_sql("SELECT {$fields} FROM {user} u WHERE username = :username", ['username' => 'admin']);
  99  
 100          // Ensure we received back all name fields.
 101          $this->assertEquals($expecteduserfields, array_keys((array) $user));
 102      }
 103  }