diff --git a/admin/tool/uploaduser/classes/process.php b/admin/tool/uploaduser/classes/process.php index c3820a4fa12..f817797bb52 100644 --- a/admin/tool/uploaduser/classes/process.php +++ b/admin/tool/uploaduser/classes/process.php @@ -688,6 +688,17 @@ class process { break; case UU_USER_ADD_UPDATE: + if ($this->get_match_on_email()) { + if ($usersbyname = $DB->get_records('user', ['username' => $user->username])) { + foreach ($usersbyname as $userbyname) { + if (strtolower($userbyname->email) != strtolower($user->email)) { + $this->usersskipped++; + $this->upt->track('status', get_string('usernotaddedusernameexists', 'error'), 'warning'); + $skip = true; + } + } + } + } break; case UU_USER_UPDATE: diff --git a/admin/tool/uploaduser/tests/behat/upload_users.feature b/admin/tool/uploaduser/tests/behat/upload_users.feature index 18b00b8f6c8..11a38cdfc68 100644 --- a/admin/tool/uploaduser/tests/behat/upload_users.feature +++ b/admin/tool/uploaduser/tests/behat/upload_users.feature @@ -296,3 +296,25 @@ Feature: Upload users And I navigate to "Users > Accounts > Browse list of users" in site administration And I should see "Bilbo Baggins" And I should not see "Frodo Baggins" + + @javascript + Scenario: Create a new user when matching them on email where where the username already exists + Given the following "users" exist: + | username | firstname | lastname | email | + | bilbob | Samwise | Gamgee | samwise@example.com | + | frodob | Frodeo | Baspins | frodo@example.com | + And I log in as "admin" + And I navigate to "Users > Accounts > Upload users" in site administration + When I upload "lib/tests/fixtures/upload_users_email_matching.csv" file to "File" filemanager + And I press "Upload users" + Then I should see "Upload users preview" + And I set the following fields to these values: + | Upload type | Add new and update existing users | + | Existing user details | Override with file | + | Match on email address | Yes | + And I press "Upload users" + And I should see "User not added - username already exists under a different email" + And I press "Continue" + And I navigate to "Users > Accounts > Browse list of users" in site administration + And I should see "Samwise Gamgee" + And I should see "Frodo Baggins" diff --git a/lang/en/error.php b/lang/en/error.php index 135cf7d648c..f1df8d9da2b 100644 --- a/lang/en/error.php +++ b/lang/en/error.php @@ -614,6 +614,7 @@ $string['usermustbemnet'] = 'Users in the MNET access control list must be remot $string['usernamelowercase'] = 'The username must be in lower case'; $string['usernotaddederror'] = 'User not added - error'; $string['usernotaddedregistered'] = 'User not added - already registered'; +$string['usernotaddedusernameexists'] = 'User not added - username already exists under a different email'; $string['usernotavailable'] = 'The details of this user are not available to you'; $string['usernotdeletedadmin'] = 'User not deleted as administrator accounts cannot be deleted.'; $string['usernotdeleteddeleted'] = 'This user has already been deleted.';