From 3b2019a3e38f96499cdb6454d44fc469831c5f57 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Thu, 24 Mar 2022 23:10:25 +0000 Subject: [PATCH] MDL-29261 tool_uploaduser: warn user about invalid data values. --- admin/tool/uploaduser/classes/process.php | 8 +++++++- admin/tool/uploaduser/lang/en/deprecated.txt | 1 + .../uploaduser/lang/en/tool_uploaduser.php | 5 ++++- .../uploaduser/tests/upload_users_test.php | 20 +++++++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 admin/tool/uploaduser/lang/en/deprecated.txt diff --git a/admin/tool/uploaduser/classes/process.php b/admin/tool/uploaduser/classes/process.php index 8569812b878cb..40535fc0f3b4d 100644 --- a/admin/tool/uploaduser/classes/process.php +++ b/admin/tool/uploaduser/classes/process.php @@ -1371,8 +1371,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'); } } diff --git a/admin/tool/uploaduser/lang/en/deprecated.txt b/admin/tool/uploaduser/lang/en/deprecated.txt new file mode 100644 index 0000000000000..872f6cb9efe7a --- /dev/null +++ b/admin/tool/uploaduser/lang/en/deprecated.txt @@ -0,0 +1 @@ +invaliduserdata,tool_uploaduser diff --git a/admin/tool/uploaduser/lang/en/tool_uploaduser.php b/admin/tool/uploaduser/lang/en/tool_uploaduser.php index 7c480398eb1f8..ad74cd7388088 100644 --- a/admin/tool/uploaduser/lang/en/tool_uploaduser.php +++ b/admin/tool/uploaduser/lang/en/tool_uploaduser.php @@ -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'; @@ -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.'; diff --git a/admin/tool/uploaduser/tests/upload_users_test.php b/admin/tool/uploaduser/tests/upload_users_test.php index 49e204e716f37..559f19ca4c682 100644 --- a/admin/tool/uploaduser/tests/upload_users_test.php +++ b/admin/tool/uploaduser/tests/upload_users_test.php @@ -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 = <<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'] *