Skip to content

Commit

Permalink
MDL-71559 core_user: Fields support for PROFILE_VISIBLE_TEACHERS
Browse files Browse the repository at this point in the history
  • Loading branch information
sammarshallou committed Jun 3, 2021
1 parent 30b8ad5 commit 007cde2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
4 changes: 4 additions & 0 deletions user/classes/fields.php
Expand Up @@ -380,6 +380,10 @@ public static function get_identity_fields(?\context $context, bool $allowcustom
case PROFILE_VISIBLE_PRIVATE:
$allowed = !$context || has_capability('moodle/user:viewalldetails', $context);
break;
case PROFILE_VISIBLE_TEACHERS:
// This is actually defined (in user/profile/lib.php) based on whether
// you have moodle/site:viewuseridentity in context. We already checked
// that, so treat it as visible (fall through).
case PROFILE_VISIBLE_ALL:
$allowed = true;
break;
Expand Down
45 changes: 29 additions & 16 deletions user/tests/fields_test.php
Expand Up @@ -23,7 +23,7 @@
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class fields_testcase extends \advanced_testcase {
class fields_test extends \advanced_testcase {

/**
* Tests getting the user picture fields.
Expand Down Expand Up @@ -51,18 +51,26 @@ public function test_get_name_fields() {
* Tests getting the identity fields.
*/
public function test_get_identity_fields() {
global $DB;
global $DB, $CFG;

$this->resetAfterTest();

// Create two custom profile fields, one of which is private.
require_once($CFG->dirroot . '/user/profile/lib.php');

// Create custom profile fields, one with each visibility option.
$generator = self::getDataGenerator();
$generator->create_custom_profile_field(['datatype' => 'text', 'shortname' => 'a', 'name' => 'A']);
$generator->create_custom_profile_field(['datatype' => 'text', 'shortname' => 'a', 'name' => 'A',
'visible' => PROFILE_VISIBLE_ALL]);
$generator->create_custom_profile_field(['datatype' => 'text', 'shortname' => 'b', 'name' => 'B',
'visible' => PROFILE_VISIBLE_PRIVATE]);

// Set the extra user fields to include email, department, and both custom profile fields.
set_config('showuseridentity', 'email,department,profile_field_a,profile_field_b');
$generator->create_custom_profile_field(['datatype' => 'text', 'shortname' => 'c', 'name' => 'C',
'visible' => PROFILE_VISIBLE_NONE]);
$generator->create_custom_profile_field(['datatype' => 'text', 'shortname' => 'd', 'name' => 'D',
'visible' => PROFILE_VISIBLE_TEACHERS]);

// Set the extra user fields to include email, department, and all custom profile fields.
set_config('showuseridentity', 'email,department,profile_field_a,profile_field_b,' .
'profile_field_c,profile_field_d');
set_config('hiddenuserfields', 'email');

// Create a test course and a student in the course.
Expand All @@ -74,15 +82,17 @@ public function test_get_identity_fields() {
$generator->enrol_user($user->id, $course->id, 'student');

// When no context is provided, it does no access checks and should return all specified.
$this->assertEquals(['email', 'department', 'profile_field_a', 'profile_field_b'],
$this->assertEquals(['email', 'department', 'profile_field_a', 'profile_field_b',
'profile_field_c', 'profile_field_d'],
fields::get_identity_fields(null));

// If you turn off custom profile fields, you don't get those.
$this->assertEquals(['email', 'department'], fields::get_identity_fields(null, false));

// Request in context as an administator.
$this->setAdminUser();
$this->assertEquals(['email', 'department', 'profile_field_a', 'profile_field_b'],
$this->assertEquals(['email', 'department', 'profile_field_a', 'profile_field_b',
'profile_field_c', 'profile_field_d'],
fields::get_identity_fields($coursecontext));
$this->assertEquals(['email', 'department'],
fields::get_identity_fields($coursecontext, false));
Expand All @@ -92,24 +102,26 @@ public function test_get_identity_fields() {
$this->setUser($user);
$this->assertEquals([], fields::get_identity_fields($coursecontext));

// Give the student the basic identity fields permission.
// Give the student the basic identity fields permission (also makes them count as 'teacher'
// for the teacher-restricted field).
$roleid = $DB->get_field('role', 'id', ['shortname' => 'student']);
role_change_permission($roleid, $coursecontext, 'moodle/site:viewuseridentity', CAP_ALLOW);
$this->assertEquals(['department', 'profile_field_a'],
$this->assertEquals(['department', 'profile_field_a', 'profile_field_d'],
fields::get_identity_fields($coursecontext));
$this->assertEquals(['department'],
fields::get_identity_fields($coursecontext, false));

// Give them permission to view hidden user fields.
role_change_permission($roleid, $coursecontext, 'moodle/course:viewhiddenuserfields', CAP_ALLOW);
$this->assertEquals(['email', 'department', 'profile_field_a'],
$this->assertEquals(['email', 'department', 'profile_field_a', 'profile_field_d'],
fields::get_identity_fields($coursecontext));
$this->assertEquals(['email', 'department'],
fields::get_identity_fields($coursecontext, false));

// Also give them permission to view all profile fields.
role_change_permission($roleid, $coursecontext, 'moodle/user:viewalldetails', CAP_ALLOW);
$this->assertEquals(['email', 'department', 'profile_field_a', 'profile_field_b'],
$this->assertEquals(['email', 'department', 'profile_field_a', 'profile_field_b',
'profile_field_c', 'profile_field_d'],
fields::get_identity_fields($coursecontext));
$this->assertEquals(['email', 'department'],
fields::get_identity_fields($coursecontext, false));
Expand All @@ -120,21 +132,22 @@ public function test_get_identity_fields() {

// Give them basic permission.
role_change_permission($roleid, $usercontext, 'moodle/site:viewuseridentity', CAP_ALLOW);
$this->assertEquals(['department', 'profile_field_a'],
$this->assertEquals(['department', 'profile_field_a', 'profile_field_d'],
fields::get_identity_fields($usercontext));
$this->assertEquals(['department'],
fields::get_identity_fields($usercontext, false));

// Give them the hidden user fields permission (it's a different one).
role_change_permission($roleid, $usercontext, 'moodle/user:viewhiddendetails', CAP_ALLOW);
$this->assertEquals(['email', 'department', 'profile_field_a'],
$this->assertEquals(['email', 'department', 'profile_field_a', 'profile_field_d'],
fields::get_identity_fields($usercontext));
$this->assertEquals(['email', 'department'],
fields::get_identity_fields($usercontext, false));

// Also give them permission to view all profile fields.
role_change_permission($roleid, $usercontext, 'moodle/user:viewalldetails', CAP_ALLOW);
$this->assertEquals(['email', 'department', 'profile_field_a', 'profile_field_b'],
$this->assertEquals(['email', 'department', 'profile_field_a', 'profile_field_b',
'profile_field_c', 'profile_field_d'],
fields::get_identity_fields($usercontext));
$this->assertEquals(['email', 'department'],
fields::get_identity_fields($usercontext, false));
Expand Down

0 comments on commit 007cde2

Please sign in to comment.