MDL-71051 core_user: move edit profile category form to classes
[moodle.git] / user / tests / profilelib_test.php
blob77da39879f1e59a6350ad8197878199a7c9531f3
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.
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/>.
17 /**
18 * Unit tests for user/profile/lib.php.
20 * @package core_user
21 * @copyright 2014 The Open University
22 * @licensehttp://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
29 /**
30 * Unit tests for user/profile/lib.php.
32 * @package core_user
33 * @copyright 2014 The Open University
34 * @licensehttp://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class core_user_profilelib_testcase extends advanced_testcase {
37 /**
38 * Tests profile_get_custom_fields function and checks it is consistent
39 * with profile_user_record.
41 public function test_get_custom_fields() {
42 global $CFG;
43 require_once($CFG->dirroot . '/user/profile/lib.php');
45 $this->resetAfterTest();
46 $user = $this->getDataGenerator()->create_user();
48 // Add a custom field of textarea type.
49 $id1 = $this->getDataGenerator()->create_custom_profile_field([
50 'shortname' => 'frogdesc', 'name' => 'Description of frog',
51 'datatype' => 'textarea'])->id;
53 // Check the field is returned.
54 $result = profile_get_custom_fields();
55 $this->assertArrayHasKey($id1, $result);
56 $this->assertEquals('frogdesc', $result[$id1]->shortname);
58 // Textarea types are not included in user data though, so if we
59 // use the 'only in user data' parameter, there is still nothing.
60 $this->assertArrayNotHasKey($id1, profile_get_custom_fields(true));
62 // Check that profile_user_record returns same (no) fields.
63 $this->assertObjectNotHasAttribute('frogdesc', profile_user_record($user->id));
65 // Check that profile_user_record returns all the fields when requested.
66 $this->assertObjectHasAttribute('frogdesc', profile_user_record($user->id, false));
68 // Add another custom field, this time of normal text type.
69 $id2 = $this->getDataGenerator()->create_custom_profile_field(array(
70 'shortname' => 'frogname', 'name' => 'Name of frog',
71 'datatype' => 'text'))->id;
73 // Check both are returned using normal option.
74 $result = profile_get_custom_fields();
75 $this->assertArrayHasKey($id2, $result);
76 $this->assertEquals('frogname', $result[$id2]->shortname);
78 // And check that only the one is returned the other way.
79 $this->assertArrayHasKey($id2, profile_get_custom_fields(true));
81 // Check profile_user_record returns same field.
82 $this->assertObjectHasAttribute('frogname', profile_user_record($user->id));
84 // Check that profile_user_record returns all the fields when requested.
85 $this->assertObjectHasAttribute('frogname', profile_user_record($user->id, false));
88 /**
89 * Make sure that all profile fields can be initialised without arguments.
91 public function test_default_constructor() {
92 global $DB, $CFG;
93 require_once($CFG->dirroot . '/user/profile/definelib.php');
94 $datatypes = profile_list_datatypes();
95 foreach ($datatypes as $datatype => $datatypename) {
96 require_once($CFG->dirroot . '/user/profile/field/' .
97 $datatype . '/field.class.php');
98 $newfield = 'profile_field_' . $datatype;
99 $formfield = new $newfield();
100 $this->assertNotNull($formfield);
105 * Test profile_view function
107 public function test_profile_view() {
108 global $USER;
110 $this->resetAfterTest();
112 // Course without sections.
113 $course = $this->getDataGenerator()->create_course();
114 $context = context_course::instance($course->id);
115 $user = $this->getDataGenerator()->create_user();
116 $usercontext = context_user::instance($user->id);
118 $this->setUser($user);
120 // Redirect events to the sink, so we can recover them later.
121 $sink = $this->redirectEvents();
123 profile_view($user, $context, $course);
124 $events = $sink->get_events();
125 $event = reset($events);
127 // Check the event details are correct.
128 $this->assertInstanceOf('\core\event\user_profile_viewed', $event);
129 $this->assertEquals($context, $event->get_context());
130 $this->assertEquals($user->id, $event->relateduserid);
131 $this->assertEquals($course->id, $event->other['courseid']);
132 $this->assertEquals($course->shortname, $event->other['courseshortname']);
133 $this->assertEquals($course->fullname, $event->other['coursefullname']);
135 profile_view($user, $usercontext);
136 $events = $sink->get_events();
137 $event = array_pop($events);
138 $sink->close();
140 $this->assertInstanceOf('\core\event\user_profile_viewed', $event);
141 $this->assertEquals($usercontext, $event->get_context());
142 $this->assertEquals($user->id, $event->relateduserid);
147 * Test that {@link user_not_fully_set_up()} takes required custom fields into account.
149 public function test_profile_has_required_custom_fields_set() {
150 global $CFG;
151 require_once($CFG->dirroot.'/mnet/lib.php');
153 $this->resetAfterTest();
155 // Add a required, visible, unlocked custom field.
156 $this->getDataGenerator()->create_custom_profile_field(['shortname' => 'house', 'name' => 'House', 'required' => 1,
157 'visible' => 1, 'locked' => 0, 'datatype' => 'text']);
159 // Add an optional, visible, unlocked custom field.
160 $this->getDataGenerator()->create_custom_profile_field(['shortname' => 'pet', 'name' => 'Pet', 'required' => 0,
161 'visible' => 1, 'locked' => 0, 'datatype' => 'text']);
163 // Add required but invisible custom field.
164 $this->getDataGenerator()->create_custom_profile_field(['shortname' => 'secretid', 'name' => 'Secret ID',
165 'required' => 1, 'visible' => 0, 'locked' => 0, 'datatype' => 'text']);
167 // Add required but locked custom field.
168 $this->getDataGenerator()->create_custom_profile_field(['shortname' => 'muggleborn', 'name' => 'Muggle-born',
169 'required' => 1, 'visible' => 1, 'locked' => 1, 'datatype' => 'checkbox']);
171 // Create some student accounts.
172 $hermione = $this->getDataGenerator()->create_user();
173 $harry = $this->getDataGenerator()->create_user();
174 $ron = $this->getDataGenerator()->create_user();
175 $draco = $this->getDataGenerator()->create_user();
177 // Hermione has all available custom fields filled (of course she has).
178 profile_save_data((object)['id' => $hermione->id, 'profile_field_house' => 'Gryffindor']);
179 profile_save_data((object)['id' => $hermione->id, 'profile_field_pet' => 'Crookshanks']);
181 // Harry has only the optional field filled.
182 profile_save_data((object)['id' => $harry->id, 'profile_field_pet' => 'Hedwig']);
184 // Draco has only the required field filled.
185 profile_save_data((object)['id' => $draco->id, 'profile_field_house' => 'Slytherin']);
187 // Only students with required fields filled should be considered as fully set up in the default (strict) mode.
188 $this->assertFalse(user_not_fully_set_up($hermione));
189 $this->assertFalse(user_not_fully_set_up($draco));
190 $this->assertTrue(user_not_fully_set_up($harry));
191 $this->assertTrue(user_not_fully_set_up($ron));
193 // In the lax mode, students do not need to have required fields filled.
194 $this->assertFalse(user_not_fully_set_up($hermione, false));
195 $this->assertFalse(user_not_fully_set_up($draco, false));
196 $this->assertFalse(user_not_fully_set_up($harry, false));
197 $this->assertFalse(user_not_fully_set_up($ron, false));
199 // Lack of required core field is seen as a problem in either mode.
200 unset($hermione->email);
201 $this->assertTrue(user_not_fully_set_up($hermione, true));
202 $this->assertTrue(user_not_fully_set_up($hermione, false));
204 // When confirming remote MNet users, we do not have custom fields available.
205 $roamingharry = mnet_strip_user($harry, ['firstname', 'lastname', 'email']);
206 $roaminghermione = mnet_strip_user($hermione, ['firstname', 'lastname', 'email']);
208 $this->assertTrue(user_not_fully_set_up($roamingharry, true));
209 $this->assertFalse(user_not_fully_set_up($roamingharry, false));
210 $this->assertTrue(user_not_fully_set_up($roaminghermione, true));
211 $this->assertTrue(user_not_fully_set_up($roaminghermione, false));
215 * Test that user generator sets the custom profile fields
217 public function test_profile_fields_in_generator() {
218 global $CFG;
219 require_once($CFG->dirroot.'/mnet/lib.php');
221 $this->resetAfterTest();
223 // Add a required, visible, unlocked custom field.
224 $this->getDataGenerator()->create_custom_profile_field(['shortname' => 'house', 'name' => 'House', 'required' => 1,
225 'visible' => 1, 'locked' => 0, 'datatype' => 'text', 'defaultdata' => null]);
227 // Create some student accounts.
228 $hermione = $this->getDataGenerator()->create_user(['profile_field_house' => 'Gryffindor']);
229 $harry = $this->getDataGenerator()->create_user();
231 // Only students with required fields filled should be considered as fully set up.
232 $this->assertFalse(user_not_fully_set_up($hermione));
233 $this->assertTrue(user_not_fully_set_up($harry));
235 // Test that the profile fields were actually set.
236 $profilefields1 = profile_user_record($hermione->id);
237 $this->assertEquals('Gryffindor', $profilefields1->house);
239 $profilefields2 = profile_user_record($harry->id);
240 $this->assertObjectHasAttribute('house', $profilefields2);
241 $this->assertNull($profilefields2->house);
245 * Tests the profile_get_custom_field_data_by_shortname function when working normally.
247 public function test_profile_get_custom_field_data_by_shortname_normal() {
248 global $DB, $CFG;
249 require_once($CFG->dirroot . '/user/profile/lib.php');
251 $this->resetAfterTest();
253 // Create 3 profile fields.
254 $generator = $this->getDataGenerator();
255 $field1 = $generator->create_custom_profile_field(['datatype' => 'text',
256 'shortname' => 'speciality', 'name' => 'Speciality',
257 'visible' => PROFILE_VISIBLE_ALL]);
258 $field2 = $generator->create_custom_profile_field(['datatype' => 'menu',
259 'shortname' => 'veggie', 'name' => 'Vegetarian',
260 'visible' => PROFILE_VISIBLE_PRIVATE]);
262 // Get the first field data and check it is correct.
263 $data = profile_get_custom_field_data_by_shortname('speciality');
264 $this->assertEquals('Speciality', $data->name);
265 $this->assertEquals(PROFILE_VISIBLE_ALL, $data->visible);
266 $this->assertEquals($field1->id, $data->id);
268 // Get the second field data, checking there is no database query this time.
269 $before = $DB->perf_get_queries();
270 $data = profile_get_custom_field_data_by_shortname('veggie');
271 $this->assertEquals($before, $DB->perf_get_queries());
272 $this->assertEquals('Vegetarian', $data->name);
273 $this->assertEquals(PROFILE_VISIBLE_PRIVATE, $data->visible);
274 $this->assertEquals($field2->id, $data->id);
278 * Tests the profile_get_custom_field_data_by_shortname function with a field that doesn't exist.
280 public function test_profile_get_custom_field_data_by_shortname_missing() {
281 global $CFG;
282 require_once($CFG->dirroot . '/user/profile/lib.php');
284 $this->assertNull(profile_get_custom_field_data_by_shortname('speciality'));