Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]
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 /** 18 * Tests for myprofilelib apis. 19 * 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. 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 global $CFG; 28 require_once($CFG->dirroot . '/lib/myprofilelib.php'); 29 30 /** 31 * Tests for myprofilelib apis. 32 * 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. 36 */ 37 class core_myprofilelib_testcase extends advanced_testcase { 38 39 /** 40 * @var stdClass The user. 41 */ 42 private $user; 43 44 /** 45 * @var stdClass The course. 46 */ 47 private $course; 48 49 /** 50 * @var \core_user\output\myprofile\tree The navigation tree. 51 */ 52 private $tree; 53 54 public function setUp(): void { 55 // Set the $PAGE->url value so core_myprofile_navigation() doesn't complain. 56 global $PAGE; 57 $PAGE->set_url('/test'); 58 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(); 64 } 65 66 /** 67 * Tests the core_myprofile_navigation() function as an admin viewing a user's course profile. 68 */ 69 public function test_core_myprofile_navigation_as_admin() { 70 $this->setAdminUser(); 71 $iscurrentuser = false; 72 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); 85 86 $nodes = $reflector->getProperty('nodes'); 87 $nodes->setAccessible(true); 88 $this->assertArrayHasKey('fullprofile', $nodes->getValue($this->tree)); 89 } 90 91 /** 92 * Tests the core_myprofile_navigation() function as a user without permission to view the full 93 * profile of another another user. 94 */ 95 public function test_core_myprofile_navigation_course_without_permission() { 96 $this->setUser($this->user2); 97 $iscurrentuser = false; 98 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)); 104 } 105 106 /** 107 * Tests the core_myprofile_navigation() function as the currently logged in user. 108 */ 109 public function test_core_myprofile_navigation_profile_link_as_current_user() { 110 $this->setUser($this->user); 111 $iscurrentuser = true; 112 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)); 118 } 119 120 /** 121 * Tests the core_myprofile_navigation() function as the admin viewing another user. 122 */ 123 public function test_core_myprofile_navigation_profile_link_as_admin() { 124 $this->setAdminUser(); 125 $iscurrentuser = false; 126 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)); 132 } 133 134 /** 135 * Tests the core_myprofile_navigation() function when viewing the preference page as an admin. 136 */ 137 public function test_core_myprofile_navigation_preference_as_admin() { 138 $this->setAdminUser(); 139 $iscurrentuser = false; 140 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)); 147 } 148 149 /** 150 * Tests the core_myprofile_navigation() function when viewing the preference 151 * page as another user without the ability to use the 'loginas' functionality. 152 */ 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; 157 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)); 163 } 164 165 /** 166 * Tests the core_myprofile_navigation() function as an admin viewing another user's contact details. 167 */ 168 public function test_core_myprofile_navigation_contact_fields_as_admin() { 169 global $CFG; 170 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; 178 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' 196 ); 197 foreach ($fields as $field => $value) { 198 $this->user->$field = $value; 199 } 200 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)); 208 } 209 foreach ($identityfields as $field) { 210 $this->assertArrayHasKey($field, $nodes->getValue($this->tree)); 211 } 212 } 213 214 /** 215 * Tests the core_myprofile_navigation() function as a user viewing another user's profile 216 * ensuring that the contact details are not shown. 217 */ 218 public function test_core_myprofile_navigation_contact_field_without_permission() { 219 global $CFG; 220 221 $iscurrentuser = false; 222 $hiddenfields = explode(',', $CFG->hiddenuserfields); 223 $identityfields = explode(',', $CFG->showuseridentity); 224 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)); 233 } 234 foreach ($identityfields as $field) { 235 $this->assertArrayNotHasKey($field, $nodes->getValue($this->tree)); 236 } 237 } 238 239 /** 240 * Tests the core_myprofile_navigation() function as an admin viewing another user's 241 * profile ensuring the login activity links are shown. 242 */ 243 public function test_core_myprofile_navigation_login_activity() { 244 // First access, last access, last ip. 245 $this->setAdminUser(); 246 $iscurrentuser = false; 247 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)); 255 } 256 257 /** 258 * Tests the core_myprofile_navigation() function as a user viewing another user's profile 259 * ensuring the login activity links are not shown. 260 */ 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; 266 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)); 274 } 275 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body