Skip to content

Commit

Permalink
Merge branch 'MDL-29261' of https://github.com/paulholden/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
HuongNV13 committed Feb 1, 2024
2 parents c35f04c + 3b2019a commit 5f78501
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
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/deprecated.txt
@@ -0,0 +1 @@
invaliduserdata,tool_uploaduser
5 changes: 4 additions & 1 deletion admin/tool/uploaduser/lang/en/tool_uploaduser.php
Expand Up @@ -49,7 +49,7 @@
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';
Expand Down Expand Up @@ -120,3 +120,6 @@
$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.';
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 5f78501

Please sign in to comment.