1 |
efrain |
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 |
use core\context\system;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Unit tests for user entity
|
|
|
26 |
*
|
|
|
27 |
* @package core_reportbuilder
|
|
|
28 |
* @covers \core_reportbuilder\local\entities\user
|
|
|
29 |
* @copyright 2021 Paul Holden <paulh@moodle.com>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
class user_test extends advanced_testcase {
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Test getting user identity column
|
|
|
36 |
*/
|
|
|
37 |
public function test_get_identity_column(): void {
|
|
|
38 |
$this->resetAfterTest();
|
|
|
39 |
|
|
|
40 |
$this->getDataGenerator()->create_custom_profile_field(['datatype' => 'text', 'name' => 'Hi', 'shortname' => 'hi']);
|
|
|
41 |
|
|
|
42 |
$user = new user();
|
|
|
43 |
$user->initialise();
|
|
|
44 |
|
|
|
45 |
$columnusername = $user->get_identity_column('username');
|
|
|
46 |
$this->assertEquals('user:username', $columnusername->get_unique_identifier());
|
|
|
47 |
|
|
|
48 |
$columnprofilefield = $user->get_identity_column('profile_field_hi');
|
|
|
49 |
$this->assertEquals('user:profilefield_hi', $columnprofilefield->get_unique_identifier());
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Test getting all user identity columns
|
|
|
54 |
*/
|
|
|
55 |
public function test_get_identity_columns(): void {
|
|
|
56 |
$this->resetAfterTest();
|
|
|
57 |
$this->setAdminUser();
|
|
|
58 |
|
|
|
59 |
$this->getDataGenerator()->create_custom_profile_field(['datatype' => 'text', 'name' => 'Hi', 'shortname' => 'hi']);
|
|
|
60 |
set_config('showuseridentity', 'username,profilefield_hi');
|
|
|
61 |
$context = system::instance();
|
|
|
62 |
|
|
|
63 |
$user = new user();
|
|
|
64 |
$user->initialise();
|
|
|
65 |
|
|
|
66 |
// All columns.
|
|
|
67 |
$this->assertEqualsCanonicalizing([
|
|
|
68 |
'user:username',
|
|
|
69 |
'user:profilefield_hi',
|
|
|
70 |
], array_map(
|
|
|
71 |
fn($column) => $column->get_unique_identifier(),
|
|
|
72 |
$user->get_identity_columns($context),
|
|
|
73 |
));
|
|
|
74 |
|
|
|
75 |
// Exclude username.
|
|
|
76 |
$columns = $user->get_identity_columns($context, ['username']);
|
|
|
77 |
$this->assertCount(1, $columns);
|
|
|
78 |
$this->assertEquals('user:profilefield_hi', reset($columns)->get_unique_identifier());
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Test getting user identity filter
|
|
|
83 |
*/
|
|
|
84 |
public function test_get_identity_filter(): void {
|
|
|
85 |
$this->resetAfterTest();
|
|
|
86 |
|
|
|
87 |
$this->getDataGenerator()->create_custom_profile_field(['datatype' => 'text', 'name' => 'Hi', 'shortname' => 'hi']);
|
|
|
88 |
|
|
|
89 |
$user = new user();
|
|
|
90 |
$user->initialise();
|
|
|
91 |
|
|
|
92 |
$filterusername = $user->get_identity_filter('username');
|
|
|
93 |
$this->assertEquals('user:username', $filterusername->get_unique_identifier());
|
|
|
94 |
|
|
|
95 |
$filterprofilefield = $user->get_identity_filter('profile_field_hi');
|
|
|
96 |
$this->assertEquals('user:profilefield_hi', $filterprofilefield->get_unique_identifier());
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Test getting all user identity filters
|
|
|
101 |
*/
|
|
|
102 |
public function test_get_identity_filters(): void {
|
|
|
103 |
$this->resetAfterTest();
|
|
|
104 |
$this->setAdminUser();
|
|
|
105 |
|
|
|
106 |
$this->getDataGenerator()->create_custom_profile_field(['datatype' => 'text', 'name' => 'Hi', 'shortname' => 'hi']);
|
|
|
107 |
set_config('showuseridentity', 'username,profilefield_hi');
|
|
|
108 |
$context = system::instance();
|
|
|
109 |
|
|
|
110 |
$user = new user();
|
|
|
111 |
$user->initialise();
|
|
|
112 |
|
|
|
113 |
// All filters.
|
|
|
114 |
$this->assertEqualsCanonicalizing([
|
|
|
115 |
'user:username',
|
|
|
116 |
'user:profilefield_hi',
|
|
|
117 |
], array_map(
|
|
|
118 |
fn($filter) => $filter->get_unique_identifier(),
|
|
|
119 |
$user->get_identity_columns($context),
|
|
|
120 |
));
|
|
|
121 |
|
|
|
122 |
// Exclude username.
|
|
|
123 |
$filters = $user->get_identity_filters($context, ['username']);
|
|
|
124 |
$this->assertCount(1, $filters);
|
|
|
125 |
$this->assertEquals('user:profilefield_hi', reset($filters)->get_unique_identifier());
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Data provider for {@see test_get_name_fields_select}
|
|
|
130 |
*
|
|
|
131 |
* @return array
|
|
|
132 |
*/
|
|
|
133 |
public function get_name_fields_select_provider(): array {
|
|
|
134 |
return [
|
|
|
135 |
['firstname', ['firstname']],
|
|
|
136 |
['firstname lastname', ['firstname', 'lastname']],
|
|
|
137 |
['firstname middlename lastname', ['firstname', 'middlename', 'lastname']],
|
|
|
138 |
['alternatename lastname firstname', ['alternatename', 'lastname', 'firstname']],
|
|
|
139 |
];
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Tests the helper method for selecting all of a users' name fields
|
|
|
144 |
*
|
|
|
145 |
* @param string $fullnamedisplay
|
|
|
146 |
* @param string[] $expecteduserfields
|
|
|
147 |
*
|
|
|
148 |
* @dataProvider get_name_fields_select_provider
|
|
|
149 |
*/
|
|
|
150 |
public function test_get_name_fields_select(string $fullnamedisplay, array $expecteduserfields): void {
|
|
|
151 |
global $DB;
|
|
|
152 |
|
|
|
153 |
$this->resetAfterTest(true);
|
|
|
154 |
|
|
|
155 |
set_config('alternativefullnameformat', $fullnamedisplay);
|
|
|
156 |
|
|
|
157 |
// As a user without permission to view all fields we always get the standard ones.
|
|
|
158 |
$fields = user::get_name_fields_select('u');
|
|
|
159 |
$user = $DB->get_record_sql("SELECT {$fields} FROM {user} u WHERE username = :username", ['username' => 'admin']);
|
|
|
160 |
$this->assertEquals(['firstname', 'lastname'], array_keys((array) $user));
|
|
|
161 |
|
|
|
162 |
// As the admin we get all name fields from alternativefullnameformat.
|
|
|
163 |
$this->setAdminUser();
|
|
|
164 |
$fields = user::get_name_fields_select('u');
|
|
|
165 |
$user = $DB->get_record_sql("SELECT {$fields} FROM {user} u WHERE username = :username", ['username' => 'admin']);
|
|
|
166 |
$this->assertEquals($expecteduserfields, array_keys((array) $user));
|
|
|
167 |
}
|
|
|
168 |
}
|