This commit is contained in:
Huong Nguyen 2024-02-01 10:45:11 +07:00
commit 5f78501071
4 changed files with 32 additions and 2 deletions

View File

@ -1384,8 +1384,14 @@ class process {
}
}
}
// Warn user about invalid data values.
if (($invalid = \core_user::validate($user)) !== true) {
$this->upt->track('status', get_string('invaliduserdata', 'tool_uploaduser', s($user->username)), 'warning');
$listseparator = get_string('listsep', 'langconfig') . ' ';
$this->upt->track('status', get_string('invaliduserdatavalues', 'tool_uploaduser', [
'username' => s($user->username),
'values' => implode($listseparator, array_keys($invalid)),
]), 'warning');
}
}

View File

@ -0,0 +1 @@
invaliduserdata,tool_uploaduser

View File

@ -49,7 +49,7 @@ $string['examplecsv_help'] = 'To use the example text file, download it then ope
The example text file may also be used for testing, as you are able to preview user data and can choose to cancel the action before user accounts are created.';
$string['infoprefix'] = 'Info:';
$string['invalidupdatetype'] = 'This option cannot be selected with the chosen upload type.';
$string['invaliduserdata'] = 'Invalid data detected for user {$a} and it has been automatically cleaned.';
$string['invaliduserdatavalues'] = 'Invalid data detected for user \'{$a->username}\' ({$a->values}), which has been automatically cleaned.';
$string['invalidtheme'] = 'Theme "{$a}" is not installed and will be ignored.';
$string['linex'] = 'Line {$a}';
$string['matchemail'] = 'Match on email address';
@ -120,3 +120,6 @@ $string['uuupdatetype'] = 'Existing user details';
$string['uuusernametemplate'] = 'Username template';
$string['privacy:metadata'] = 'The User upload plugin does not store any personal data.';
$string['warningprefix'] = 'Warning:';
// Deprecated since Moodle 4.4.
$string['invaliduserdata'] = 'Invalid data detected for user {$a} and it has been automatically cleaned.';

View File

@ -186,6 +186,26 @@ EOF;
$this->assertEquals('student1', reset($usersasdefaultrole)->username);
}
/**
* Test that invalid data contained in uploaded CSV triggers appropriate warnings
*/
public function test_user_upload_user_validate(): void {
$this->resetAfterTest();
$this->setAdminUser();
$csv = <<<EOF
username,firstname,lastname,email,country
student1,Student,One,s1@example.com,Wales
EOF;
$output = $this->process_csv_upload($csv, ['--uutype=' . UU_USER_ADDNEW]);
// We should get the debugging from the user class itself, as well as warning in the output regarding the same.
$this->assertDebuggingCalled('The property \'country\' has invalid data and has been cleaned.');
$this->assertStringContainsString('Invalid data detected for user \'student1\' (country), ' .
'which has been automatically cleaned.', $output);
}
/**
* Generate cli_helper and mock $_SERVER['argv']
*