From 92671823d8633d563019939d191a6c816bf17321 Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Wed, 18 Jul 2018 22:15:07 -0400 Subject: [PATCH] Userimport fixes/improvements (#5884) * Send notification when user is created. * Flesh out default user mappings * Add user importing test. --- app/Importer/Importer.php | 7 ++++++- app/Importer/UserImporter.php | 14 +++++++++++++- tests/unit/ImporterTest.php | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/app/Importer/Importer.php b/app/Importer/Importer.php index 7ed405b17b..c660038af0 100644 --- a/app/Importer/Importer.php +++ b/app/Importer/Importer.php @@ -60,7 +60,12 @@ abstract class Importer 'warranty_months' => 'warranty', 'full_name' => 'full name', 'email' => 'email', - 'username' => 'username' + 'username' => 'username', + 'jobtitle' => 'job title', + 'employee_num' => 'employee number', + 'phone_number' => 'phone number', + 'first_name' => 'first name', + 'last_name' => 'last name', ]; /** * Map of item fields->csv names diff --git a/app/Importer/UserImporter.php b/app/Importer/UserImporter.php index 258f339060..0e3ff01b83 100644 --- a/app/Importer/UserImporter.php +++ b/app/Importer/UserImporter.php @@ -4,6 +4,7 @@ namespace App\Importer; use App\Helpers\Helper; use App\Models\User; +use App\Notifications\WelcomeNotification; class UserImporter extends ItemImporter { @@ -26,6 +27,7 @@ class UserImporter extends ItemImporter * * @author Daniel Melzter * @since 4.0 + * @param array $row */ public function createUserIfNotExists(array $row) { @@ -55,14 +57,24 @@ class UserImporter extends ItemImporter $this->log("No matching user, creating one"); $user = new User(); $user->fill($this->sanitizeItemForStoring($user)); - if ($user->save()) { // $user->logCreate('Imported using CSV Importer'); $this->log("User " . $this->item["name"] . ' was created'); + if($user->email) { + $data = [ + 'email' => $user->email, + 'username' => $user->username, + 'first_name' => $user->first_name, + 'last_name' => $user->last_name, + 'password' => $this->tempPassword, + ]; + $user->notify(new WelcomeNotification($data)); + } $user = null; $this->item = null; return; } + $this->logError($user, 'User'); return; } diff --git a/tests/unit/ImporterTest.php b/tests/unit/ImporterTest.php index ae80239d0b..37bb137b79 100644 --- a/tests/unit/ImporterTest.php +++ b/tests/unit/ImporterTest.php @@ -3,6 +3,7 @@ use App\Importer\AccessoryImporter; use App\Importer\AssetImporter; use App\Importer\ConsumableImporter; use App\Importer\LicenseImporter; +use App\Importer\UserImporter; use App\Models\Accessory; use App\Models\Asset; use App\Models\AssetModel; @@ -14,6 +15,7 @@ use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\Notification; class ImporterTest extends BaseTest { @@ -681,6 +683,36 @@ EOT; $this->tester->seeNumRecords(80, 'license_seats'); } + + public function testDefaultUserImport() + { + Notification::fake(); + $this->signIn(); + $csv = <<<'EOT' +First Name,Last Name,email,Username,Location,Phone Number,Job Title,Employee Number,Company +Blanche,O'Collopy,bocollopy0@livejournal.com,bocollopy0,Hinapalanan,63-(199)661-2186,Clinical Specialist,7080919053,Morar-Ward +Jessie,Primo,,jprimo1,Korenovsk,7-(885)578-0266,Paralegal,6284292031,Jast-Stiedemann + +EOT; + $this->import(new UserImporter($csv)); + + $this->tester->seeRecord('users', [ + 'first_name' => 'Blanche', + 'last_name' => "O'Collopy", + 'email' => 'bocollopy0@livejournal.com', + 'username' => 'bocollopy0', + 'phone' => '63-(199)661-2186', + 'jobtitle' => 'Clinical Specialist', + 'employee_num' => '7080919053' + ]); + + $this->tester->seeRecord('companies', [ + 'name' => 'Morar-Ward' + ]); + + Notification::assertSentTo(User::find(2), \App\Notifications\WelcomeNotification::class); + Notification::assertNotSentTo(User::find(3), \App\Notifications\WelcomeNotification::class); + } private function import($importer, $mappings = null) { if ($mappings) {