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.
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

declare(strict_types=1);

namespace core_reportbuilder\local\entities;

use advanced_testcase;

/**
 * Unit tests for user entity
 *
 * @package     core_reportbuilder
 * @covers      \core_reportbuilder\local\entities\user
 * @copyright   2021 Paul Holden <paulh@moodle.com>
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class user_test extends advanced_testcase {

    /**
> * Test getting user identity column * Data provider for {@see test_get_name_fields_select} > */ * > public function test_get_identity_column(): void { * @return array > $this->resetAfterTest(); */ > public function get_name_fields_select_provider(): array { > $this->getDataGenerator()->create_custom_profile_field(['datatype' => 'text', 'name' => 'Hi', 'shortname' => 'hi']); return [ > ['firstname lastname', ['firstname', 'lastname']], > $user = new user(); ['firstname middlename lastname', ['firstname', 'middlename', 'lastname']], > $user->initialise(); ['alternatename lastname firstname', ['alternatename', 'lastname', 'firstname']], > ]; > $columnusername = $user->get_identity_column('username'); } > $this->assertEquals('user:username', $columnusername->get_unique_identifier()); > /** > $columnprofilefield = $user->get_identity_column('profile_field_hi'); * Tests the helper method for selecting all of a users' name fields > $this->assertEquals('user:profilefield_hi', $columnprofilefield->get_unique_identifier()); * > } * @param string $fullnamedisplay > * @param string[] $expecteduserfields > /** * > * Test getting user identity filter * @dataProvider get_name_fields_select_provider > */ */ > public function test_get_identity_filter(): void { public function test_get_name_fields_select(string $fullnamedisplay, array $expecteduserfields): void { > $this->resetAfterTest(); global $DB; > > $this->getDataGenerator()->create_custom_profile_field(['datatype' => 'text', 'name' => 'Hi', 'shortname' => 'hi']); $this->resetAfterTest(true); > > $user = new user(); set_config('alternativefullnameformat', $fullnamedisplay); > $user->initialise(); > $fields = user::get_name_fields_select('u'); > $filterusername = $user->get_identity_filter('username'); $user = $DB->get_record_sql("SELECT {$fields} FROM {user} u WHERE username = :username", ['username' => 'admin']); > $this->assertEquals('user:username', $filterusername->get_unique_identifier()); > // Ensure we received back all name fields. > $filterprofilefield = $user->get_identity_filter('profile_field_hi'); $this->assertEquals($expecteduserfields, array_keys((array) $user)); > $this->assertEquals('user:profilefield_hi', $filterprofilefield->get_unique_identifier()); } > } } > > /**