MDL-72634 user: populate default city/country during user creation.

This commit is contained in:
Paul Holden 2022-01-31 19:42:36 +00:00
parent 34ce1463cc
commit b6d8939959
3 changed files with 37 additions and 4 deletions

View File

@ -3902,10 +3902,6 @@ function create_user_record($username, $password, $auth = 'manual') {
}
}
if (!isset($newuser->city)) {
$newuser->city = '';
}
$newuser->auth = $auth;
$newuser->username = $username;

View File

@ -94,6 +94,13 @@ function user_create_user($user, $updatepassword = true, $triggerevent = true) {
if (!isset($user->lang)) {
$user->lang = core_user::get_property_default('lang');
}
if (!isset($user->city)) {
$user->city = core_user::get_property_default('city');
}
if (!isset($user->country)) {
// The default value of $CFG->country is 0, but that isn't a valid property for the user field, so switch to ''.
$user->country = core_user::get_property_default('country') ?: '';
}
$user->timecreated = time();
$user->timemodified = $user->timecreated;

View File

@ -300,6 +300,36 @@ class core_userliblib_testcase extends advanced_testcase {
$this->assertDebuggingNotCalled();
}
/**
* Test that creating users populates default values
*
* @covers ::user_create_user
*/
public function test_user_create_user_default_values(): void {
global $CFG;
$this->resetAfterTest();
// Update default values for city/country (both initially empty).
set_config('defaultcity', 'Nadi');
set_config('country', 'FJ');
$userid = user_create_user((object) [
'username' => 'newuser',
], false, false);
$user = core_user::get_user($userid);
$this->assertEquals($CFG->calendartype, $user->calendartype);
$this->assertEquals($CFG->defaultpreference_maildisplay, $user->maildisplay);
$this->assertEquals($CFG->defaultpreference_mailformat, $user->mailformat);
$this->assertEquals($CFG->defaultpreference_maildigest, $user->maildigest);
$this->assertEquals($CFG->defaultpreference_autosubscribe, $user->autosubscribe);
$this->assertEquals($CFG->defaultpreference_trackforums, $user->trackforums);
$this->assertEquals($CFG->lang, $user->lang);
$this->assertEquals($CFG->defaultcity, $user->city);
$this->assertEquals($CFG->country, $user->country);
}
/**
* Test that {@link user_create_user()} throws exception when invalid username is provided.
*