Userimport fixes/improvements (#5884)

* Send notification when user is created.
* Flesh out default user mappings
* Add user importing test.
This commit is contained in:
Daniel Meltzer 2018-07-18 22:15:07 -04:00 committed by snipe
parent 19396b2107
commit 92671823d8
3 changed files with 51 additions and 2 deletions

View file

@ -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

View file

@ -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;
}

View file

@ -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) {