Merge branch 'MDL-70125_39-3' of git://github.com/mdjnelson/moodle into MOODLE_39_STABLE
[moodle.git] / lib / tests / myprofilelib_test.php
blob6cb5dc91be39acd81d834375dce9c6c4bef0218d
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 * Tests for myprofilelib apis.
20 * @package core
21 * @copyright 2015 onwards Ankit agarwal <ankit.agrr@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/lib/myprofilelib.php');
30 /**
31 * Tests for myprofilelib apis.
33 * @package core
34 * @copyright 2015 onwards Ankit agarwal <ankit.agrr@gmail.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
37 class core_myprofilelib_testcase extends advanced_testcase {
39 /**
40 * @var stdClass The user.
42 private $user;
44 /**
45 * @var stdClass The course.
47 private $course;
49 /**
50 * @var \core_user\output\myprofile\tree The navigation tree.
52 private $tree;
54 public function setUp() {
55 // Set the $PAGE->url value so core_myprofile_navigation() doesn't complain.
56 global $PAGE;
57 $PAGE->set_url('/test');
59 $this->user = $this->getDataGenerator()->create_user();
60 $this->user2 = $this->getDataGenerator()->create_user();
61 $this->course = $this->getDataGenerator()->create_course();
62 $this->tree = new \core_user\output\myprofile\tree();
63 $this->resetAfterTest();
66 /**
67 * Tests the core_myprofile_navigation() function as an admin viewing a user's course profile.
69 public function test_core_myprofile_navigation_as_admin() {
70 $this->setAdminUser();
71 $iscurrentuser = false;
73 // Test tree as admin user.
74 core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
75 $reflector = new ReflectionObject($this->tree);
76 $categories = $reflector->getProperty('categories');
77 $categories->setAccessible(true);
78 $cats = $categories->getValue($this->tree);
79 $this->assertArrayHasKey('contact', $cats);
80 $this->assertArrayHasKey('coursedetails', $cats);
81 $this->assertArrayHasKey('miscellaneous', $cats);
82 $this->assertArrayHasKey('reports', $cats);
83 $this->assertArrayHasKey('administration', $cats);
84 $this->assertArrayHasKey('loginactivity', $cats);
86 $nodes = $reflector->getProperty('nodes');
87 $nodes->setAccessible(true);
88 $this->assertArrayHasKey('fullprofile', $nodes->getValue($this->tree));
91 /**
92 * Tests the core_myprofile_navigation() function as a user without permission to view the full
93 * profile of another another user.
95 public function test_core_myprofile_navigation_course_without_permission() {
96 $this->setUser($this->user2);
97 $iscurrentuser = false;
99 core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
100 $reflector = new ReflectionObject($this->tree);
101 $nodes = $reflector->getProperty('nodes');
102 $nodes->setAccessible(true);
103 $this->assertArrayNotHasKey('fullprofile', $nodes->getValue($this->tree));
107 * Tests the core_myprofile_navigation() function as the currently logged in user.
109 public function test_core_myprofile_navigation_profile_link_as_current_user() {
110 $this->setUser($this->user);
111 $iscurrentuser = true;
113 core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
114 $reflector = new ReflectionObject($this->tree);
115 $nodes = $reflector->getProperty('nodes');
116 $nodes->setAccessible(true);
117 $this->assertArrayHasKey('editprofile', $nodes->getValue($this->tree));
121 * Tests the core_myprofile_navigation() function as the admin viewing another user.
123 public function test_core_myprofile_navigation_profile_link_as_admin() {
124 $this->setAdminUser();
125 $iscurrentuser = false;
127 core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
128 $reflector = new ReflectionObject($this->tree);
129 $nodes = $reflector->getProperty('nodes');
130 $nodes->setAccessible(true);
131 $this->assertArrayHasKey('editprofile', $nodes->getValue($this->tree));
135 * Tests the core_myprofile_navigation() function when viewing the preference page as an admin.
137 public function test_core_myprofile_navigation_preference_as_admin() {
138 $this->setAdminUser();
139 $iscurrentuser = false;
141 core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
142 $reflector = new ReflectionObject($this->tree);
143 $nodes = $reflector->getProperty('nodes');
144 $nodes->setAccessible(true);
145 $this->assertArrayHasKey('preferences', $nodes->getValue($this->tree));
146 $this->assertArrayHasKey('loginas', $nodes->getValue($this->tree));
150 * Tests the core_myprofile_navigation() function when viewing the preference
151 * page as another user without the ability to use the 'loginas' functionality.
153 public function test_core_myprofile_navigation_preference_without_permission() {
154 // Login as link for a user who doesn't have the capability to login as.
155 $this->setUser($this->user2);
156 $iscurrentuser = false;
158 core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
159 $reflector = new ReflectionObject($this->tree);
160 $nodes = $reflector->getProperty('nodes');
161 $nodes->setAccessible(true);
162 $this->assertArrayNotHasKey('loginas', $nodes->getValue($this->tree));
166 * Tests the core_myprofile_navigation() function as an admin viewing another user's contact details.
168 public function test_core_myprofile_navigation_contact_fields_as_admin() {
169 global $CFG;
171 // User contact fields.
172 set_config("hiddenuserfields", "country,city,webpage,icqnumber,skypeid,yahooid,aimid,msnid");
173 set_config("showuseridentity", "email,address,phone1,phone2,institution,department,idnumber");
174 $hiddenfields = explode(',', $CFG->hiddenuserfields);
175 $identityfields = explode(',', $CFG->showuseridentity);
176 $this->setAdminUser();
177 $iscurrentuser = false;
179 // Make sure fields are not empty.
180 $fields = array(
181 'country' => 'AU',
182 'city' => 'Silent hill',
183 'url' => 'Ghosts',
184 'icq' => 'Wth is ICQ?',
185 'skype' => 'derp',
186 'yahoo' => 'are you living in the 90\'s?',
187 'aim' => 'are you for real?',
188 'msn' => '...',
189 'email' => 'Rulelikeaboss@example.com',
190 'address' => 'Didn\'t I mention silent hill already ?',
191 'phone1' => '123',
192 'phone2' => '234',
193 'institution' => 'strange land',
194 'department' => 'video game/movie',
195 'idnumber' => 'SLHL'
197 foreach ($fields as $field => $value) {
198 $this->user->$field = $value;
201 // User with proper permissions.
202 core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null);
203 $reflector = new ReflectionObject($this->tree);
204 $nodes = $reflector->getProperty('nodes');
205 $nodes->setAccessible(true);
206 foreach ($hiddenfields as $field) {
207 $this->assertArrayHasKey($field, $nodes->getValue($this->tree));
209 foreach ($identityfields as $field) {
210 $this->assertArrayHasKey($field, $nodes->getValue($this->tree));
215 * Tests the core_myprofile_navigation() function as a user viewing another user's profile
216 * ensuring that the contact details are not shown.
218 public function test_core_myprofile_navigation_contact_field_without_permission() {
219 global $CFG;
221 $iscurrentuser = false;
222 $hiddenfields = explode(',', $CFG->hiddenuserfields);
223 $identityfields = explode(',', $CFG->showuseridentity);
225 // User without permission.
226 $this->setUser($this->user2);
227 core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null);
228 $reflector = new ReflectionObject($this->tree);
229 $nodes = $reflector->getProperty('nodes');
230 $nodes->setAccessible(true);
231 foreach ($hiddenfields as $field) {
232 $this->assertArrayNotHasKey($field, $nodes->getValue($this->tree));
234 foreach ($identityfields as $field) {
235 $this->assertArrayNotHasKey($field, $nodes->getValue($this->tree));
240 * Tests the core_myprofile_navigation() function as an admin viewing another user's
241 * profile ensuring the login activity links are shown.
243 public function test_core_myprofile_navigation_login_activity() {
244 // First access, last access, last ip.
245 $this->setAdminUser();
246 $iscurrentuser = false;
248 core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null);
249 $reflector = new ReflectionObject($this->tree);
250 $nodes = $reflector->getProperty('nodes');
251 $nodes->setAccessible(true);
252 $this->assertArrayHasKey('firstaccess', $nodes->getValue($this->tree));
253 $this->assertArrayHasKey('lastaccess', $nodes->getValue($this->tree));
254 $this->assertArrayHasKey('lastip', $nodes->getValue($this->tree));
258 * Tests the core_myprofile_navigation() function as a user viewing another user's profile
259 * ensuring the login activity links are not shown.
261 public function test_core_myprofile_navigationn_login_activity_without_permission() {
262 // User without permission.
263 set_config("hiddenuserfields", "firstaccess,lastaccess,lastip");
264 $this->setUser($this->user2);
265 $iscurrentuser = false;
267 core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null);
268 $reflector = new ReflectionObject($this->tree);
269 $nodes = $reflector->getProperty('nodes');
270 $nodes->setAccessible(true);
271 $this->assertArrayNotHasKey('firstaccess', $nodes->getValue($this->tree));
272 $this->assertArrayNotHasKey('lastaccess', $nodes->getValue($this->tree));
273 $this->assertArrayNotHasKey('lastip', $nodes->getValue($this->tree));