2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Unit tests for user/profile/lib.php.
21 * @copyright 2014 The Open University
22 * @licensehttp://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
30 * Unit tests for user/profile/lib.php.
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
{
38 * Tests profile_get_custom_fields function and checks it is consistent
39 * with profile_user_record.
41 public function test_get_custom_fields() {
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 = $DB->insert_record('user_info_field', array(
50 'shortname' => 'frogdesc', 'name' => 'Description of frog', 'categoryid' => 1,
51 'datatype' => 'textarea'));
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 // Add another custom field, this time of normal text type.
66 $id2 = $DB->insert_record('user_info_field', array(
67 'shortname' => 'frogname', 'name' => 'Name of frog', 'categoryid' => 1,
68 'datatype' => 'text'));
70 // Check both are returned using normal option.
71 $result = profile_get_custom_fields();
72 $this->assertArrayHasKey($id2, $result);
73 $this->assertEquals('frogname', $result[$id2]->shortname
);
75 // And check that only the one is returned the other way.
76 $this->assertArrayHasKey($id2, profile_get_custom_fields(true));
78 // Check profile_user_record returns same field.
79 $this->assertObjectHasAttribute('frogname', profile_user_record($user->id
));
83 * Make sure that all profile fields can be initialised without arguments.
85 public function test_default_constructor() {
87 require_once($CFG->dirroot
. '/user/profile/definelib.php');
88 $datatypes = profile_list_datatypes();
89 foreach ($datatypes as $datatype => $datatypename) {
90 require_once($CFG->dirroot
. '/user/profile/field/' .
91 $datatype . '/field.class.php');
92 $newfield = 'profile_field_' . $datatype;
93 $formfield = new $newfield();
94 $this->assertNotNull($formfield);
99 * Test profile_view function
101 public function test_profile_view() {
104 $this->resetAfterTest();
106 // Course without sections.
107 $course = $this->getDataGenerator()->create_course();
108 $context = context_course
::instance($course->id
);
109 $user = $this->getDataGenerator()->create_user();
110 $usercontext = context_user
::instance($user->id
);
112 $this->setUser($user);
114 // Redirect events to the sink, so we can recover them later.
115 $sink = $this->redirectEvents();
117 profile_view($user, $context, $course);
118 $events = $sink->get_events();
119 $event = reset($events);
121 // Check the event details are correct.
122 $this->assertInstanceOf('\core\event\user_profile_viewed', $event);
123 $this->assertEquals($context, $event->get_context());
124 $this->assertEquals($user->id
, $event->relateduserid
);
125 $this->assertEquals($course->id
, $event->other
['courseid']);
126 $this->assertEquals($course->shortname
, $event->other
['courseshortname']);
127 $this->assertEquals($course->fullname
, $event->other
['coursefullname']);
129 profile_view($user, $usercontext);
130 $events = $sink->get_events();
131 $event = array_pop($events);
134 $this->assertInstanceOf('\core\event\user_profile_viewed', $event);
135 $this->assertEquals($usercontext, $event->get_context());
136 $this->assertEquals($user->id
, $event->relateduserid
);