From 3bf770c8012d2d244a818a14c25428f2b6bbe4f4 Mon Sep 17 00:00:00 2001 From: Ruslan Kabalin Date: Mon, 29 Mar 2021 21:28:25 +0100 Subject: [PATCH] MDL-71051 core_user: fix issues and address review issues --- admin/settings/users.php | 6 +++--- badges/criteria/award_criteria_profile.php | 11 +++++++---- lib/tests/behat/showuseridentity.feature | 2 +- user/classes/form/profile_field_form.php | 8 ++++---- user/profile/definelib.php | 4 +--- .../social/tests/behat/social_profile_field.feature | 10 ++++++---- user/profile/lib.php | 2 +- 7 files changed, 23 insertions(+), 20 deletions(-) diff --git a/admin/settings/users.php b/admin/settings/users.php index 5a9b93c7b3f7e..aa602290f6e6f 100644 --- a/admin/settings/users.php +++ b/admin/settings/users.php @@ -231,9 +231,9 @@ function() { // Custom profile fields. $profilefields = profile_get_custom_fields(); - foreach ($profilefields as $key => $field) { - // Only reasonable-length fields can be used as identity fields. - if ($field->param2 > 255) { + foreach ($profilefields as $field) { + // Only reasonable-length text fields can be used as identity fields. + if ($field->param2 > 255 || $field->datatype != 'text') { continue; } $fields['profile_field_' . $field->shortname] = $field->name . ' *'; diff --git a/badges/criteria/award_criteria_profile.php b/badges/criteria/award_criteria_profile.php index 8cd323cd83c10..b014cf5a92b00 100644 --- a/badges/criteria/award_criteria_profile.php +++ b/badges/criteria/award_criteria_profile.php @@ -44,7 +44,7 @@ class award_criteria_profile extends award_criteria { * */ public function get_options(&$mform) { - global $CFG; + global $CFG, $DB; require_once($CFG->dirroot . '/user/profile/lib.php'); $none = true; @@ -93,13 +93,14 @@ public function get_options(&$mform) { foreach ($cfields as $field) { if (!isset($currentcat) || $currentcat != $field->categoryid) { $currentcat = $field->categoryid; - $mform->addElement('header', 'category_' . $currentcat, format_string($field->categoryname)); + $categoryname = $DB->get_field('user_info_category', 'name', ['id' => $field->categoryid]); + $mform->addElement('header', 'category_' . $currentcat, format_string($categoryname)); } $checked = false; if (in_array($field->id, $existing)) { $checked = true; } - $this->config_options($mform, array('id' => $field->fieldid, 'checked' => $checked, 'name' => $field->name, 'error' => false)); + $this->config_options($mform, array('id' => $field->id, 'checked' => $checked, 'name' => $field->name, 'error' => false)); $none = false; } } @@ -135,7 +136,9 @@ public function get_details($short = '') { foreach ($this->params as $p) { if (is_numeric($p['field'])) { $fields = profile_get_custom_fields(); - $str = $fields[$p['field']]->name ?? $p['field']; + // Get formatted field name if such field exists. + $str = isset($fields[$p['field']]->name) ? + format_string($fields[$p['field']]->name) : null; } else { $str = \core_user\fields::get_display_name($p['field']); } diff --git a/lib/tests/behat/showuseridentity.feature b/lib/tests/behat/showuseridentity.feature index a475ec67f19fd..53446924a3da8 100644 --- a/lib/tests/behat/showuseridentity.feature +++ b/lib/tests/behat/showuseridentity.feature @@ -22,7 +22,7 @@ Feature: Select user identity fields | user1 | C1 | manager | | user2 | C1 | manager | - Scenario: The admin settings screen should show text custom fields (and let you choose them) + Scenario: The admin settings screen should show text custom fields of certain length (and let you choose them) When I log in as "admin" And I navigate to "Users > Permissions > User policies" in site administration Then I should see "Speciality" in the "#admin-showuseridentity" "css_element" diff --git a/user/classes/form/profile_field_form.php b/user/classes/form/profile_field_form.php index ff2935c95d009..4dd6017bf5849 100644 --- a/user/classes/form/profile_field_form.php +++ b/user/classes/form/profile_field_form.php @@ -122,14 +122,14 @@ public function set_data_for_dynamic_submission(): void { $field = $this->get_field_record(); // Clean and prepare description for the editor. - $field->description = clean_text($field->description, $field->descriptionformat); - $field->description = array('text' => $field->description, 'format' => $field->descriptionformat, 'itemid' => 0); + $description = clean_text($field->description, $field->descriptionformat); + $field->description = ['text' => $description, 'format' => $field->descriptionformat, 'itemid' => 0]; // Convert the data format for. if (is_array($this->editors())) { foreach ($this->editors() as $editor) { if (isset($field->$editor)) { - $field->$editor = clean_text($field->$editor, $field->{$editor.'format'}); - $field->$editor = array('text' => $field->$editor, 'format' => $field->{$editor.'format'}, 'itemid' => 0); + $editordesc = clean_text($field->$editor, $field->{$editor.'format'}); + $field->$editor = ['text' => $editordesc, 'format' => $field->{$editor.'format'}, 'itemid' => 0]; } } } diff --git a/user/profile/definelib.php b/user/profile/definelib.php index f9c2e47d3661d..bf57b78b74352 100644 --- a/user/profile/definelib.php +++ b/user/profile/definelib.php @@ -488,10 +488,8 @@ function profile_list_categories() { * Create or update a profile category * * @param stdClass $data - * @throws coding_exception - * @throws dml_exception */ -function profile_save_category(stdClass $data) { +function profile_save_category(stdClass $data): void { global $DB; if (empty($data->id)) { diff --git a/user/profile/field/social/tests/behat/social_profile_field.feature b/user/profile/field/social/tests/behat/social_profile_field.feature index 8abf51d8cc568..572cf386566b6 100644 --- a/user/profile/field/social/tests/behat/social_profile_field.feature +++ b/user/profile/field/social/tests/behat/social_profile_field.feature @@ -8,14 +8,16 @@ Feature: Social profile fields can not have a duplicate shortname. Scenario: Verify you can edit social profile fields. Given I log in as "admin" When I navigate to "Users > Accounts > User profile fields" in site administration - And I set the field "datatype" to "Social" + And I click on "Create a new profile field" "link" + And I click on "Social" "link" And I set the following fields to these values: - | Short name | yahoo | | Networktype | Yahoo ID | + | Short name | yahoo | And I click on "Save changes" "button" - And I set the field "datatype" to "Social" + And I click on "Create a new profile field" "link" + And I click on "Social" "link" And I set the following fields to these values: - | Short name | yahoo | | Networktype | Yahoo ID | + | Short name | yahoo | And I click on "Save changes" "button" Then I should see "This short name is already in use" diff --git a/user/profile/lib.php b/user/profile/lib.php index 43dc668e2ab48..d4b59d44e8675 100644 --- a/user/profile/lib.php +++ b/user/profile/lib.php @@ -854,7 +854,7 @@ function profile_save_custom_fields($userid, $profilefields) { * current request for all fields so that it can be used quickly. * * @param string $shortname Shortname of custom profile field - * @return stdClass Object with properties id, shortname, name, visible, datatype, categoryid, etc + * @return stdClass|null Object with properties id, shortname, name, visible, datatype, categoryid, etc */ function profile_get_custom_field_data_by_shortname(string $shortname): ?stdClass { $cache = \cache::make_from_params(cache_store::MODE_REQUEST, 'core_profile', 'customfields',