Skip to content

Commit

Permalink
Merge branch 'MDL-77723' of https://github.com/paulholden/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyatregubov committed May 30, 2023
2 parents 3dfd0b7 + 2881840 commit 88f0bd7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions user/classes/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,13 @@ public static function get_identity_fields(?\context $context, bool $allowcustom
$allowed = false;
if ($allowcustom) {
require_once($CFG->dirroot . '/user/profile/lib.php');

// Ensure the field exists (it may have been deleted since user identity was configured).
$field = profile_get_custom_field_data_by_shortname($matches[1], false);
$fieldinstance = profile_get_user_field($field->datatype, $field->id, 0, $field);
$allowed = $fieldinstance->is_visible($context);
if ($field !== null) {
$fieldinstance = profile_get_user_field($field->datatype, $field->id, 0, $field);
$allowed = $fieldinstance->is_visible($context);
}
}
if (!$allowed) {
unset($extra[$key]);
Expand Down
22 changes: 22 additions & 0 deletions user/tests/fields_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @package core
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \core_user\fields
*/
class fields_test extends \advanced_testcase {

Expand Down Expand Up @@ -153,6 +154,27 @@ public function test_get_identity_fields() {
fields::get_identity_fields($usercontext, false));
}

/**
* Test getting identity fields, when one of them refers to a non-existing custom profile field
*/
public function test_get_identity_fields_invalid(): void {
$this->resetAfterTest();

$this->getDataGenerator()->create_custom_profile_field([
'datatype' => 'text',
'shortname' => 'real',
'name' => 'I\'m real',
]);

// The "fake" profile field does not exist.
set_config('showuseridentity', 'email,profile_field_real,profile_field_fake');

$this->assertEquals([
'email',
'profile_field_real',
], fields::get_identity_fields(null));
}

/**
* Tests the get_required_fields function.
*
Expand Down

0 comments on commit 88f0bd7

Please sign in to comment.