Skip to content

Commit

Permalink
Merge branch 'MDL-29261-403' of https://github.com/paulholden/moodle
Browse files Browse the repository at this point in the history
…into MOODLE_403_STABLE
  • Loading branch information
HuongNV13 committed Feb 1, 2024
2 parents fa924d4 + 97e996c commit 455a187
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion admin/tool/uploaduser/classes/process.php
Expand Up @@ -1384,8 +1384,14 @@ public function process_line(array $line) {
}
}
}

// 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');
}
}

Expand Down
1 change: 1 addition & 0 deletions admin/tool/uploaduser/lang/en/tool_uploaduser.php
Expand Up @@ -50,6 +50,7 @@
$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';
Expand Down
20 changes: 20 additions & 0 deletions admin/tool/uploaduser/tests/upload_users_test.php
Expand Up @@ -186,6 +186,26 @@ public function test_user_can_upload_with_course_enrolment_default_role(): void
$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']
*
Expand Down

0 comments on commit 455a187

Please sign in to comment.