Skip to content

A free open source IT asset/license management system

Importer tests + Fixes (#3733)

Daniel Meltzerf432f98e1234
diff --git a/app/Console/Commands/ObjectImportCommand.php b/app/Console/Commands/ObjectImportCommand.php
index 00c08b471a..07dfd5aeaf 100644
--- a/app/Console/Commands/ObjectImportCommand.php
+++ b/app/Console/Commands/ObjectImportCommand.php
@@ -72,7 +72,6 @@ class ObjectImportCommand extends Command
$classString = "App\\Importer\\{$class}Importer";
$importer = new $classString($filename);
$importer->setCallbacks([$this, 'log'], [$this, 'progress'], [$this, 'errorCallback'])
- ->setTestRun($this->option('testrun'))
->setUserId($this->option('user_id'))
->setUpdating($this->option('update'))
->setUsernameFormat($this->option('username_format'));
@@ -173,7 +172,6 @@ class ObjectImportCommand extends Command
return array(
array('email_format', null, InputOption::VALUE_REQUIRED, 'The format of the email addresses that should be generated. Options are firstname.lastname, firstname, filastname', null),
array('username_format', null, InputOption::VALUE_REQUIRED, 'The format of the username that should be generated. Options are firstname.lastname, firstname, filastname, email', null),
- array('testrun', null, InputOption::VALUE_NONE, 'If set, will parse and output data without adding to database', null),
array('logfile', null, InputOption::VALUE_REQUIRED, 'The path to log output to. storage/logs/importer.log by default', storage_path('logs/importer.log') ),
array('item-type', null, InputOption::VALUE_REQUIRED, 'Item Type To import. Valid Options are Asset, Consumable, Accessory, License, or User', 'Asset'),
array('web-importer', null, InputOption::VALUE_NONE, 'Internal: packages output for use with the web importer'),
diff --git a/app/Importer/AccessoryImporter.php b/app/Importer/AccessoryImporter.php
index 51281fcbaf..30b7be5e22 100644
--- a/app/Importer/AccessoryImporter.php
+++ b/app/Importer/AccessoryImporter.php
@@ -35,20 +35,12 @@ class AccessoryImporter extends ItemImporter
$this->log('Updating Accessory');
$accessory->update($this->sanitizeItemForUpdating($accessory));
- if (!$this->testRun) {
- $accessory->save();
- }
+ $accessory->save();
return;
}
$this->log("No Matching Accessory, Creating a new one");
$accessory = new Accessory();
$accessory->fill($this->sanitizeItemForStoring($accessory));
-
- if ($this->testRun) {
- $this->log('TEST RUN - Accessory ' . $this->item["name"] . ' not created');
- return;
- }
-
if ($accessory->save()) {
$accessory->logCreate('Imported using CSV Importer');
$this->log('Accessory ' . $this->item["name"] . ' was created');
diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.php
index cb19316881..33719cbe7d 100644
--- a/app/Importer/AssetImporter.php
+++ b/app/Importer/AssetImporter.php
@@ -61,7 +61,7 @@ class AssetImporter extends ItemImporter
}
$this->item['image'] = $this->findCsvMatch($row, "image");
- $this->item['warranty_months'] = intval($this->findCsvMatch($row, "warranty months"));
+ $this->item['warranty_months'] = intval($this->findCsvMatch($row, "warranty"));
$this->item['model_id'] = $this->createOrFetchAssetModel($row);
if (!$this->item['status_id'] && !$editingAsset) {
$this->log("No status field found, defaulting to first status.");
@@ -85,13 +85,11 @@ class AssetImporter extends ItemImporter
$asset->{$custom_field} = $val;
}
}
- if (!$this->testRun) {
- if ($asset->save()) {
- $asset->logCreate('Imported using csv importer');
- $this->log('Asset ' . $this->item["name"] . ' with serial number ' . $this->item['serial'] . ' was created');
- return;
- }
- $this->logError($asset, 'Asset "' . $this->item['name'].'"');
+ if ($asset->save()) {
+ $asset->logCreate('Imported using csv importer');
+ $this->log('Asset ' . $this->item["name"] . ' with serial number ' . $this->item['serial'] . ' was created');
+ return;
}
+ $this->logError($asset, 'Asset "' . $this->item['name'].'"');
}
}
diff --git a/app/Importer/ComponentImporter.php b/app/Importer/ComponentImporter.php
index 1c2f53a290..c7dd9d2a02 100644
--- a/app/Importer/ComponentImporter.php
+++ b/app/Importer/ComponentImporter.php
@@ -41,20 +41,13 @@ class ComponentImporter extends ItemImporter
$this->log("Updating Component");
$component = $this->components[$componentId];
$component->update($this->sanitizeItemFor($component));
- if (!$this->testRun) {
- $component->save();
- }
+ $component->save();
return;
}
$this->log("No matching component, creating one");
$component = new Component;
$component->fill($$this->sanitizeItemForStoring($component));
- if ($this->testRun) {
- $this->log('TEST RUN - Component ' . $this->item["name"] . ' not created');
- return;
- }
-
if ($component->save()) {
$component->logCreate('Imported using CSV Importer');
$this->log("Component " . $this->item["name"] . ' was created');
diff --git a/app/Importer/ConsumableImporter.php b/app/Importer/ConsumableImporter.php
index 880c44afd6..c54b935539 100644
--- a/app/Importer/ConsumableImporter.php
+++ b/app/Importer/ConsumableImporter.php
@@ -34,19 +34,12 @@ class ConsumableImporter extends ItemImporter
}
$this->log('Updating Consumable');
$consumable->update($this->sanitizeItemForUpdating($consumable));
- if (!$this->testRun) {
- $consumable->save();
- }
+ $consumable->save();
return;
}
$this->log("No matching consumable, creating one");
$consumable = new Consumable();
$consumable->fill($this->sanitizeItemForStoring($consumable));
-
- if ($this->testRun) {
- $this->log('TEST RUN - Consumable ' . $this->item['name'] . ' not created');
- return;
- }
if ($consumable->save()) {
$consumable->logCreate('Imported using CSV Importer');
$this->log("Consumable " . $this->item["name"] . ' was created');
diff --git a/app/Importer/Importer.php b/app/Importer/Importer.php
index 8202ce0dc3..51e3fe2a31 100644
--- a/app/Importer/Importer.php
+++ b/app/Importer/Importer.php
@@ -1,7 +1,6 @@
<?php
namespace App\Importer;
-
use App\Models\CustomField;
use App\Models\Setting;
use App\Models\User;
@@ -13,16 +12,7 @@ use League\Csv\Reader;
abstract class Importer
{
- /**
- * @var string
- */
- private $filename;
- private $csv;
- /**
- * Should we persist to database?
- * @var bool
- */
- protected $testRun;
+ protected $csv;
/**
* Id of User performing import
* @var
@@ -33,6 +23,42 @@ abstract class Importer
* @var bool
*/
protected $updating;
+ /**
+ * Default Map of item fields->csv names
+ * @var array
+ */
+ private $defaultFieldMap = [
+ 'asset_tag' => 'asset tag',
+ 'category' => 'category',
+ 'company' => 'company',
+ 'item_name' => 'item name',
+ 'image' => 'image',
+ 'expiration_date' => 'expiration date',
+ 'location' => 'location',
+ 'notes' => 'notes',
+ 'license_email' => 'licensed to email',
+ 'license_name' => "licensed to name",
+ 'maintained' => 'maintained',
+ 'manufacturer' => 'manufacturer',
+ 'asset_model' => "model name",
+ 'model_number' => 'model number',
+ 'order_number' => 'order number',
+ 'purchase_cost' => 'purchase cost',
+ 'purchase_date' => 'purchase date',
+ 'purchase_order' => 'purchase order',
+ 'qty' => 'quantity',
+ 'reassignable' => 'reassignable',
+ 'requestable' => 'requestable',
+ 'seats' => 'seats',
+ 'serial_number' => 'serial number',
+ 'status' => 'status',
+ 'supplier' => 'supplier',
+ 'termination_date' => 'termination date',
+ 'warranty_months' => 'warranty',
+ 'name' => 'name',
+ 'email' => 'email',
+ 'username' => 'username'
+ ];
/**
* Map of item fields->csv names
* @var array
@@ -58,13 +84,18 @@ abstract class Importer
/**
* ObjectImporter constructor.
- * @param string $filename
+ * @param string $file
*/
- public function __construct($filename)
+ public function __construct($file)
{
-
- $this->filename = $filename;
- $this->csv = Reader::createFromPath($filename);
+ $this->fieldMap = $this->defaultFieldMap;
+ // By default the importer passes a url to the file.
+ // However, for testing we also support passing a string directly
+ if (is_file($file)) {
+ $this->csv = Reader::createFromPath($file);
+ } else {
+ $this->csv = Reader::createFromString($file);
+ }
$this->csv->setNewLine('\r\n');
if (! ini_get("auto_detect_line_endings")) {
ini_set("auto_detect_line_endings", '1');
@@ -76,14 +107,17 @@ abstract class Importer
public function import()
{
- $results = $this->csv->fetchAssoc();
+ $headerRow = $this->csv->fetchOne();
+ $results = $this->normalizeInputArray($this->csv->fetchAssoc());
$this->customFields = CustomField::All(['name']);
DB::transaction(function () use (&$results) {
Model::unguard();
$resultsCount = sizeof($results);
foreach ($results as $row) {
$this->handle($row);
- call_user_func($this->progressCallback, $resultsCount);
+ if ($this->progressCallback) {
+ call_user_func($this->progressCallback, $resultsCount);
+ }
$this->log('------------- Action Summary ----------------');
}
@@ -104,11 +138,13 @@ abstract class Importer
*/
public function findCsvMatch(array $array, $key, $default = '')
{
+
$val = $default;
-// dd($array);
- if($customKey = $this->lookupCustomKey($key)) {
+
+ if ($customKey = $this->lookupCustomKey($key)) {
$key = $customKey;
}
+
$this->log("Custom Key: ${key}");
if (array_key_exists($key, $array)) {
$val = e(Encoding::toUTF8(trim($array[ $key ])));
@@ -128,12 +164,26 @@ abstract class Importer
public function lookupCustomKey($key)
{
// dd($this->fieldMap);
+
if (array_key_exists($key, $this->fieldMap)) {
- // $this->log("Found a match in our custom map: {$key} is " . $this->fieldMap[$key]);
- return $key = $this->fieldMap[$key];
+ $this->log("Found a match in our custom map: {$key} is " . $this->fieldMap[$key]);
+ return $this->fieldMap[$key];
}
return null;
}
+
+ /**
+ * @param $results
+ * @return array
+ */
+ public function normalizeInputArray($results)
+ {
+ $newArray = [];
+ foreach ($results as $index => $arrayToNormalize) {
+ $newArray[$index] = array_change_key_case($arrayToNormalize);
+ }
+ return $newArray;
+ }
/**
* Figure out the fieldname of the custom field
*
@@ -150,12 +200,16 @@ abstract class Importer
protected function log($string)
{
- call_user_func($this->logCallback, $string);
+ if ($this->logCallback) {
+ call_user_func($this->logCallback, $string);
+ }
}
protected function logError($item, $field)
{
- call_user_func($this->errorCallback, $item, $field, $item->getErrors());
+ if ($this->errorCallback) {
+ call_user_func($this->errorCallback, $item, $field, $item->getErrors());
+ }
}
/**
@@ -183,6 +237,7 @@ abstract class Importer
$user_username = '';
// No name was given
} elseif (empty($user_name)) {
+
$this->log('No user data provided - skipping user creation, just adding asset');
//$user_username = '';
} else {
@@ -205,12 +260,10 @@ abstract class Importer
}
}
}
-
$user = new User;
- if ($this->testRun) {
- return $user;
- }
+
if (!empty($user_username)) {
+
if ($user = User::MatchEmailOrUsername($user_username, $user_email)
->whereNotNull('username')->first()) {
$this->log('User '.$user_username.' already exists');
@@ -233,36 +286,6 @@ abstract class Importer
return $user;
}
-
-
- /**
- * Sets the value of filename.
- *
- * @param string $filename the filename
- *
- * @return self
- */
- public function setFilename($filename)
- {
- $this->filename = $filename;
-
- return $this;
- }
-
- /**
- * Sets the Should we persist to database?.
- *
- * @param bool $testRun the test run
- *
- * @return self
- */
- public function setTestRun($testRun)
- {
- $this->testRun = $testRun;
-
- return $this;
- }
-
/**
* Sets the Id of User performing import.
*
@@ -301,9 +324,10 @@ abstract class Importer
public function setFieldMappings($fields)
{
// Some initial sanitization.
+ $fields = array_map('strtolower', $fields);
+ $this->fieldMap = array_merge($this->defaultFieldMap, $fields);
- $this->fieldMap = $fields;
- $this->log($this->fieldMap);
+ // $this->log($this->fieldMap);
return $this;
}
diff --git a/app/Importer/ItemImporter.php b/app/Importer/ItemImporter.php
index c964567a0b..76f1629b40 100644
--- a/app/Importer/ItemImporter.php
+++ b/app/Importer/ItemImporter.php
@@ -50,21 +50,19 @@ class ItemImporter extends Importer
if ($this->shouldUpdateField($item_supplier)) {
$this->item['supplier_id'] = $this->createOrFetchSupplier($item_supplier);
}
-
$this->item["name"] = $this->findCsvMatch($row, "item_name");
$this->item["notes"] = $this->findCsvMatch($row, "notes");
$this->item["order_number"] = $this->findCsvMatch($row, "order_number");
$this->item["purchase_cost"] = $this->findCsvMatch($row, "purchase_cost");
$this->item["purchase_date"] = null;
- if ($this->findCsvMatch($row, "purchase date")!='') {
- $this->item["purchase_date"] = date("Y-m-d 00:00:01", strtotime($this->findCsvMatch($row, "purchase date")));
+ if ($this->findCsvMatch($row, "purchase_date")!='') {
+ $this->item["purchase_date"] = date("Y-m-d 00:00:01", strtotime($this->findCsvMatch($row, "purchase_date")));
}
-
$this->item["qty"] = $this->findCsvMatch($row, "quantity");
$this->item["requestable"] = $this->findCsvMatch($row, "requestable");
$this->item["user_id"] = $this->user_id;
- $this->item['serial'] = $this->findCsvMatch($row, "serial number");
+ $this->item['serial'] = $this->findCsvMatch($row, "serial_number");
// NO need to call this method if we're running the user import.
// TODO: Merge these methods.
if(get_class($this) !== UserImporter::class) {
@@ -72,7 +70,6 @@ class ItemImporter extends Importer
$this->item['assigned_to'] = $this->item['user']->id;
}
}
-
}
/**
@@ -145,6 +142,7 @@ class ItemImporter extends Importer
*/
public function createOrFetchAssetModel(array $row)
{
+
$asset_model_name = $this->findCsvMatch($row, "asset_model");
$asset_modelNumber = $this->findCsvMatch($row, "model_number");
// TODO: At the moment, this means we can't update the model number if the model name stays the same.
@@ -169,9 +167,7 @@ class ItemImporter extends Importer
$item['name'] = $asset_model_name;
$item['model_number'] = $asset_modelNumber;
$asset_model->update($item);
- if (!$this->testRun) {
- $asset_model->save();
- }
+ $asset_model->save();
$this->log("Asset Model Updated");
return $asset_model->id;
}
@@ -184,10 +180,6 @@ class ItemImporter extends Importer
$asset_model->fill($item);
$item = null;
- if ($this->testRun) {
- $this->log('TEST RUN - asset_model ' . $asset_model->name . ' not created');
- return $asset_model->id;
- }
if ($asset_model->save()) {
$this->log('Asset Model ' . $asset_model_name . ' with model number ' . $asset_modelNumber . ' was created');
@@ -227,9 +219,6 @@ class ItemImporter extends Importer
$category->category_type = $item_type;
$category->user_id = $this->user_id;
- if ($this->testRun) {
- return $category->id;
- }
if ($category->save()) {
$this->log('Category ' . $asset_category . ' was created');
return $category->id;
@@ -257,10 +246,6 @@ class ItemImporter extends Importer
$company = new Company();
$company->name = $asset_company_name;
- if ($this->testRun) {
-
- return $company->id;
- }
if ($company->save()) {
$this->log('Company ' . $asset_company_name . ' was created');
return $company->id;
@@ -279,6 +264,7 @@ class ItemImporter extends Importer
*/
public function createOrFetchStatusLabel($asset_statuslabel_name)
{
+
if (empty($asset_statuslabel_name)) {
return null;
}
@@ -296,10 +282,6 @@ class ItemImporter extends Importer
$status->pending = 0;
$status->archived = 0;
- if ($this->testRun) {
- return $status->id;
- }
-
if ($status->save()) {
$this->log('Status ' . $asset_statuslabel_name . ' was created');
return $status->id;
@@ -336,9 +318,6 @@ class ItemImporter extends Importer
$manufacturer->name = $item_manufacturer;
$manufacturer->user_id = $this->user_id;
- if ($this->testRun) {
- return $manufacturer->id;
- }
if ($manufacturer->save()) {
$this->log('Manufacturer ' . $manufacturer->name . ' was created');
return $manufacturer->id;
@@ -376,9 +355,6 @@ class ItemImporter extends Importer
$location->country = '';
$location->user_id = $this->user_id;
- if ($this->testRun) {
- return $location->id;
- }
if ($location->save()) {
$this->log('Location ' . $asset_location . ' was created');
return $location->id;
@@ -412,9 +388,6 @@ class ItemImporter extends Importer
$supplier->name = $item_supplier;
$supplier->user_id = $this->user_id;
- if ($this->testRun) {
- return $supplier->id;
- }
if ($supplier->save()) {
$this->log('Supplier ' . $item_supplier . ' was created');
return $supplier->id;
diff --git a/app/Importer/LicenseImporter.php b/app/Importer/LicenseImporter.php
index e2a9f01701..207e346a52 100644
--- a/app/Importer/LicenseImporter.php
+++ b/app/Importer/LicenseImporter.php
@@ -10,18 +10,15 @@ use App\Models\Manufacturer;
class LicenseImporter extends ItemImporter
{
- protected $licenses;
public function __construct($filename)
{
parent::__construct($filename);
- $this->licenses = License::all();
}
protected function handle($row)
{
// ItemImporter handles the general fetching.
parent::handle($row);
-
$this->createLicenseIfNotExists($row);
}
@@ -37,7 +34,6 @@ class LicenseImporter extends ItemImporter
{
$editingLicense = false;
$license = License::where('name', $this->item['name'])->first();
-
if ($license) {
if (!$this->updating) {
$this->log('A matching License ' . $this->item['name'] . ' already exists');
@@ -48,12 +44,12 @@ class LicenseImporter extends ItemImporter
$editingLicense = true;
} else {
$this->log("No Matching License, Creating a new one");
+ $license = new License;
}
- $license = new License;
$asset_tag = $this->item['asset_tag'] = $this->findCsvMatch($row, 'asset_tag'); // used for checkout out to an asset.
$this->item['expiration_date'] = $this->findCsvMatch($row, 'expiration_date');
- $this->item['license_email'] = $this->findCsvMatch($row, "licensed_to_email");
- $this->item['license_name'] = $this->findCsvMatch($row, "licensed_to_name");
+ $this->item['license_email'] = $this->findCsvMatch($row, "license_email");
+ $this->item['license_name'] = $this->findCsvMatch($row, "license_name");
$this->item['maintained'] = $this->findCsvMatch($row, 'maintained');
$this->item['purchase_order'] = $this->findCsvMatch($row, 'purchase_order');
$this->item['reassignable'] = $this->findCsvMatch($row, 'reassignable');
@@ -65,31 +61,28 @@ class LicenseImporter extends ItemImporter
} else {
$license->fill($this->sanitizeItemForStoring($license));
}
+ if ($license->save()) {
+ $license->logCreate('Imported using csv importer');
+ $this->log('License ' . $this->item["name"] . ' with serial number ' . $this->item['serial'] . ' was created');
- if (!$this->testRun) {
- if ($license->save()) {
- $license->logCreate('Imported using csv importer');
- $this->log('License ' . $this->item["name"] . ' with serial number ' . $this->item['serial'] . ' was created');
-
- // Lets try to checkout seats if the fields exist and we have seats.
- if ($license->seats > 0) {
- $user = $this->item['user'];
- $asset = Asset::where('asset_tag', $asset_tag)->first();
- $targetLicense = $license->licenseSeats()->first();
- if ($user) {
- $targetLicense->assigned_to = $user->id;
- if ($asset) {
- $targetLicense->asset_id = $asset->id;
- }
- $targetLicense->save();
- } elseif ($asset) {
+ // Lets try to checkout seats if the fields exist and we have seats.
+ if ($license->seats > 0) {
+ $user = $this->item['user'];
+ $asset = Asset::where('asset_tag', $asset_tag)->first();
+ $targetLicense = $license->licenseSeats()->first();
+ if ($user) {
+ $targetLicense->assigned_to = $user->id;
+ if ($asset) {
$targetLicense->asset_id = $asset->id;
- $targetLicense->save();
}
+ $targetLicense->save();
+ } elseif ($asset) {
+ $targetLicense->asset_id = $asset->id;
+ $targetLicense->save();
}
- return;
}
- $this->logError($license, 'License "' . $this->item['name'].'"');
+ return;
}
+ $this->logError($license, 'License "' . $this->item['name'].'"');
}
}
diff --git a/app/Importer/UserImporter.php b/app/Importer/UserImporter.php
index 383088305d..1b0d086f62 100644
--- a/app/Importer/UserImporter.php
+++ b/app/Importer/UserImporter.php
@@ -47,19 +47,13 @@ class UserImporter extends ItemImporter
$this->log('Updating User');
// $user = $this->users[$userId];
$user->update($this->sanitizeItemForUpdating($user));
- if (!$this->testRun) {
- $user->save();
- }
+ $user->save();
return;
}
$this->log("No matching user, creating one");
$user = new User();
$user->fill($this->sanitizeItemForStoring($user));
- if ($this->testRun) {
- $this->log('TEST RUN - User ' . $this->item['name'] . ' not created');
- return;
- }
if ($user->save()) {
// $user->logCreate('Imported using CSV Importer');
$this->log("User " . $this->item["name"] . ' was created');
diff --git a/app/Importer/import_mappings.md b/app/Importer/import_mappings.md
index ad356ea615..f2fff2c2f4 100644
--- a/app/Importer/import_mappings.md
+++ b/app/Importer/import_mappings.md
@@ -1,19 +1,19 @@
| CSV | Item | Applicable Types |
|---------------------|------------------|-------------------------------------------|
| asset tag | asset_tag | Asset |
-| category | category_id | All |
-| company | company_id | All |
-| item name | name | All |
+| category | category | All |
+| company | company | All |
+| item name | item_name | All |
| image | image | asset |
| expiration date | expiration_date | License |
-| location | location_id | All |
+| location | location | All |
| notes | notes | All |
| licensed to email | license_email | License |
| licensed to name | license_name | License |
| maintained | maintained | License |
-| manufacturer | manufacturer_id | All |
-| model name | model_id | Asset |
-| model number | model_id | Asset |
+| manufacturer | manufacturer | All |
+| model name | asset_model | Asset |
+| model number | model_number | Asset |
| order number | order_number | All ? |
| purchase cost | purchase_cost | All ? |
| purchase date | purchase_date | All ? |
@@ -23,8 +23,8 @@
| requestable | requestable | Asset, Accessory? |
| seats | seats | License |
| serial number | serial | asset, license |
-| status | status_id | asset ? All |
-| supplier | supplier_id | Asset ? All |
+| status | status | asset ? All |
+| supplier | supplier | Asset ? All |
| termination date | termination_date | License |
| warranty months | warranty_months | asset |
| User Related Fields | assigned_to | Asset |
diff --git a/app/Models/Accessory.php b/app/Models/Accessory.php
index 1dbe768657..96589e76f4 100755
--- a/app/Models/Accessory.php
+++ b/app/Models/Accessory.php
@@ -17,8 +17,11 @@ class Accessory extends SnipeModel
use Loggable, Presentable;
use SoftDeletes;
- protected $dates = ['deleted_at'];
+ protected $dates = ['deleted_at', 'purchase_date'];
protected $table = 'accessories';
+ protected $casts = [
+ 'requestable' => 'boolean'
+ ];
/**
* Accessory validation rules
@@ -28,7 +31,7 @@ class Accessory extends SnipeModel
'qty' => 'required|integer|min:1',
'category_id' => 'required|integer|exists:categories,id',
'company_id' => 'integer|nullable',
- 'min_amt' => 'integer|min:0',
+ 'min_amt' => 'integer|min:0|nullable',
'purchase_cost' => 'numeric|nullable',
);
@@ -61,6 +64,15 @@ class Accessory extends SnipeModel
'requestable'
];
+ public function setRequestableAttribute($value)
+ {
+ if ($value == '') {
+ $value = null;
+ }
+ $this->attributes['requestable'] = filter_var($value, FILTER_VALIDATE_BOOLEAN);
+ return;
+ }
+
public function company()
{
return $this->belongsTo('\App\Models\Company', 'company_id');
diff --git a/app/Models/Consumable.php b/app/Models/Consumable.php
index d374731182..84d67c4c66 100644
--- a/app/Models/Consumable.php
+++ b/app/Models/Consumable.php
@@ -14,6 +14,9 @@ class Consumable extends SnipeModel
protected $dates = ['deleted_at'];
protected $table = 'consumables';
+ protected $casts = [
+ 'requestable' => 'boolean'
+ ];
/**
@@ -56,6 +59,14 @@ class Consumable extends SnipeModel
'requestable'
];
+ public function setRequestableAttribute($value)
+ {
+ if ($value == '') {
+ $value = null;
+ }
+ $this->attributes['requestable'] = filter_var($value, FILTER_VALIDATE_BOOLEAN);
+ return;
+ }
public function admin()
{
diff --git a/app/Models/User.php b/app/Models/User.php
index cb242b37c1..45c87d8ec1 100755
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -317,7 +317,7 @@ class User extends SnipeModel implements AuthenticatableContract, CanResetPasswo
// There is a last name given
} else {
- $last_name = str_replace($first_name, '', $users_name);
+ $last_name = str_replace($first_name . ' ', '', $users_name);
if ($format=='filastname') {
$email_last_name.=str_replace(' ', '', $last_name);
diff --git a/composer.json b/composer.json
index f3781f5b1c..2f98b961d1 100644
--- a/composer.json
+++ b/composer.json
@@ -47,7 +47,8 @@
},
"autoload-dev": {
"classmap": [
- "tests/TestCase.php"
+ "tests/TestCase.php",
+ "tests/unit/BaseTest.php"
]
},
"scripts": {
diff --git a/config/app.php b/config/app.php
index ef207e62c3..f210cd44f9 100755
--- a/config/app.php
+++ b/config/app.php
@@ -78,7 +78,7 @@ return [
|
*/
- 'locale' => env('APP_LOCALE', 'en'),
+ 'locale' => env('APP_LOCALE', 'en_US.UTF-8'),
/*
|--------------------------------------------------------------------------
diff --git a/tests/unit/AccessoryTest.php b/tests/unit/AccessoryTest.php
index ad9f68a332..6548ddf427 100644
--- a/tests/unit/AccessoryTest.php
+++ b/tests/unit/AccessoryTest.php
@@ -6,18 +6,12 @@ use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
-class AccessoryTest extends \Codeception\TestCase\Test
+class AccessoryTest extends BaseTest
{
/**
* @var \UnitTester
*/
protected $tester;
- use DatabaseTransactions;
-
- protected function _before()
- {
- Artisan::call('migrate');
- }
public function testAccessoryAdd()
{
diff --git a/tests/unit/AssetMaintenanceTest.php b/tests/unit/AssetMaintenanceTest.php
index be2d5e1282..1f9e5b4ef1 100644
--- a/tests/unit/AssetMaintenanceTest.php
+++ b/tests/unit/AssetMaintenanceTest.php
@@ -3,7 +3,7 @@
use App\Models\AssetMaintenance;
-class AssetMaintenanceTest extends \Codeception\Test\Unit
+class AssetMaintenanceTest extends BaseTest
{
/**
* @var \UnitTester
diff --git a/tests/unit/AssetModelTest.php b/tests/unit/AssetModelTest.php
index 825017923a..281ea95f32 100644
--- a/tests/unit/AssetModelTest.php
+++ b/tests/unit/AssetModelTest.php
@@ -6,18 +6,13 @@ use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Support\Facades\Hash;
-class AssetModelTest extends \Codeception\TestCase\Test
+class AssetModelTest extends BaseTest
{
/**
* @var \UnitTester
*/
protected $tester;
- protected function _before()
- {
- Artisan::call('migrate');
- }
-
public function testAssetModelAdd()
{
$assetmodel = factory(AssetModel::class)->make();
diff --git a/tests/unit/AssetTest.php b/tests/unit/AssetTest.php
index 293cda32f8..8996b7993d 100644
--- a/tests/unit/AssetTest.php
+++ b/tests/unit/AssetTest.php
@@ -6,18 +6,12 @@ use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Support\Facades\Hash;
-class AssetTest extends \Codeception\TestCase\Test
+class AssetTest extends BaseTest
{
/**
* @var \UnitTester
*/
protected $tester;
- use DatabaseTransactions;
-
- protected function _before()
- {
- Artisan::call('migrate');
- }
public function testAssetAdd()
{
@@ -207,8 +201,6 @@ class AssetTest extends \Codeception\TestCase\Test
public function testAnAssetCanBeCheckedOut()
{
// This tests Asset::checkOut(), Asset::assignedTo(), Asset::assignedAssets(), Asset::assetLoc(), Asset::assignedType(), defaultLoc()
- // Need to mock settings here to avoid issues with checkout notifications.
- factory(App\Models\Setting::class)->create();
$asset = factory(Asset::class)->create();
$adminUser = factory(App\Models\User::class)->states('superuser')->create();
Auth::login($adminUser);
diff --git a/tests/unit/BaseTest.php b/tests/unit/BaseTest.php
new file mode 100644
index 0000000000..c4d09324af
--- /dev/null
+++ b/tests/unit/BaseTest.php
@@ -0,0 +1,14 @@
+<?php
+use Illuminate\Foundation\Testing\DatabaseMigrations;
+use Illuminate\Foundation\Testing\DatabaseTransactions;
+use Illuminate\Foundation\Testing\WithoutMiddleware;
+
+class BaseTest extends \Codeception\TestCase\Test
+{
+ use DatabaseTransactions;
+ protected function _before()
+ {
+ Artisan::call('migrate');
+ factory(App\Models\Setting::class)->create();
+ }
+}
diff --git a/tests/unit/CategoryTest.php b/tests/unit/CategoryTest.php
index e42ecc140b..c882b23eaf 100644
--- a/tests/unit/CategoryTest.php
+++ b/tests/unit/CategoryTest.php
@@ -5,18 +5,12 @@ use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
-class CategoryTest extends \Codeception\TestCase\Test
+class CategoryTest extends BaseTest
{
/**
* @var \UnitTester
*/
protected $tester;
- use DatabaseMigrations;
-
- protected function _before()
- {
- Artisan::call('migrate');
- }
public function testAssetCategoryAdd()
{
diff --git a/tests/unit/CompanyTest.php b/tests/unit/CompanyTest.php
index 8127cc10b5..5808289f19 100644
--- a/tests/unit/CompanyTest.php
+++ b/tests/unit/CompanyTest.php
@@ -5,23 +5,14 @@ use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
-class CompanyTest extends \Codeception\TestCase\Test
+class CompanyTest extends BaseTest
{
/**
* @var \UnitTester
*/
protected $tester;
- private $company;
- use DatabaseMigrations;
- protected function _before()
- {
- Artisan::call('migrate');
-
- $this->company = factory(Company::class)->create();
- }
-
- public function testAssetAdd()
+ public function testCompanyAdd()
{
$company = factory(Company::class)->make();
$values = [
@@ -49,43 +40,43 @@ class CompanyTest extends \Codeception\TestCase\Test
public function testACompanyCanHaveUsers()
{
- $this->company = factory(Company::class)->create();
- factory(App\Models\User::class, 1)->create(['company_id'=>$this->company->id]);
- $this->assertCount(1, $this->company->users);
+ $company = factory(Company::class)->create();
+ factory(App\Models\User::class, 1)->create(['company_id'=>$company->id]);
+ $this->assertCount(1, $company->users);
}
public function testACompanyCanHaveAssets()
{
- $this->company = factory(Company::class)->create();
- factory(App\Models\Asset::class, 1)->create(['company_id'=>$this->company->id]);
- $this->assertCount(1, $this->company->assets);
+ $company = factory(Company::class)->create();
+ factory(App\Models\Asset::class, 1)->create(['company_id'=>$company->id]);
+ $this->assertCount(1, $company->assets);
}
public function testACompanyCanHaveLicenses()
{
- $this->company = factory(Company::class)->create();
- factory(App\Models\License::class, 1)->create(['company_id'=>$this->company->id]);
- $this->assertCount(1, $this->company->licenses);
+ $company = factory(Company::class)->create();
+ factory(App\Models\License::class, 1)->create(['company_id'=>$company->id]);
+ $this->assertCount(1, $company->licenses);
}
public function testACompanyCanHaveAccessories()
{
- $this->company = factory(Company::class)->create();
- factory(App\Models\Accessory::class, 1)->create(['company_id'=>$this->company->id]);
- $this->assertCount(1, $this->company->accessories);
+ $company = factory(Company::class)->create();
+ factory(App\Models\Accessory::class, 1)->create(['company_id'=>$company->id]);
+ $this->assertCount(1, $company->accessories);
}
public function testACompanyCanHaveConsumables()
{
- $this->company = factory(Company::class)->create();
- factory(App\Models\Consumable::class, 1)->create(['company_id'=>$this->company->id]);
- $this->assertCount(1, $this->company->consumables);
+ $company = factory(Company::class)->create();
+ factory(App\Models\Consumable::class, 1)->create(['company_id'=>$company->id]);
+ $this->assertCount(1, $company->consumables);
}
public function testACompanyCanHaveComponents()
{
- $this->company = factory(Company::class)->create();
- factory(App\Models\Component::class, 1)->create(['company_id'=>$this->company->id]);
- $this->assertCount(1, $this->company->components);
+ $company = factory(Company::class)->create();
+ factory(App\Models\Component::class, 1)->create(['company_id'=>$company->id]);
+ $this->assertCount(1, $company->components);
}
}
diff --git a/tests/unit/ConsumableTest.php b/tests/unit/ConsumableTest.php
index f3dbf63a24..45abaece7e 100644
--- a/tests/unit/ConsumableTest.php
+++ b/tests/unit/ConsumableTest.php
@@ -5,18 +5,12 @@ use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
-class ConsumableTest extends \Codeception\TestCase\Test
+class ConsumableTest extends BaseTest
{
/**
* @var \UnitTester
*/
protected $tester;
- use DatabaseMigrations;
-
- protected function _before()
- {
- Artisan::call('migrate');
- }
public function testConsumableAdd()
{
diff --git a/tests/unit/CustomFieldTest.php b/tests/unit/CustomFieldTest.php
index 07c9800e7d..4626784c2f 100644
--- a/tests/unit/CustomFieldTest.php
+++ b/tests/unit/CustomFieldTest.php
@@ -9,15 +9,9 @@ use Illuminate\Foundation\Testing\DatabaseTransactions;
* Test strings for db column names gathered from
* http://www.omniglot.com/language/phrases/hovercraft.htm
*/
-class CustomFieldTest extends \Codeception\TestCase\Test
+class CustomFieldTest extends BaseTest
{
protected $tester;
- use DatabaseMigrations;
-
- protected function _before()
- {
- Artisan::call('migrate');
- }
public function testConstructor()
{
diff --git a/tests/unit/DepartmentTest.php b/tests/unit/DepartmentTest.php
index 8aa97d807f..c3e2603e11 100644
--- a/tests/unit/DepartmentTest.php
+++ b/tests/unit/DepartmentTest.php
@@ -6,18 +6,12 @@ use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Support\Facades\Hash;
-class DepartmentTest extends \Codeception\TestCase\Test
+class DepartmentTest extends BaseTest
{
/**
* @var \UnitTester
*/
protected $tester;
- use DatabaseTransactions;
-
- protected function _before()
- {
- Artisan::call('migrate');
- }
public function testDepartmentAdd()
{
diff --git a/tests/unit/DepreciationTest.php b/tests/unit/DepreciationTest.php
index 2e61f721ca..0f80e8637d 100644
--- a/tests/unit/DepreciationTest.php
+++ b/tests/unit/DepreciationTest.php
@@ -4,18 +4,12 @@ use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
-class DepreciationTest extends \Codeception\TestCase\Test
+class DepreciationTest extends BaseTest
{
/**
* @var \UnitTester
*/
protected $tester;
- use DatabaseMigrations;
-
- protected function _before()
- {
- Artisan::call('migrate');
- }
public function testDepreciationAdd()
{
diff --git a/tests/unit/ImporterTest.php b/tests/unit/ImporterTest.php
new file mode 100644
index 0000000000..a90963211d
--- /dev/null
+++ b/tests/unit/ImporterTest.php
@@ -0,0 +1,554 @@
+<?php
+use App\Importer\AccessoryImporter;
+use App\Importer\AssetImporter;
+use App\Importer\LicenseImporter;
+use App\Importer\ConsumableImporter;
+use App\Models\Accessory;
+use App\Models\AssetModel;
+use App\Models\Category;
+use Illuminate\Foundation\Testing\DatabaseMigrations;
+use Illuminate\Foundation\Testing\DatabaseTransactions;
+use Illuminate\Foundation\Testing\WithoutMiddleware;
+use Illuminate\Support\Facades\Hash;
+
+class ImporterTest extends BaseTest
+{
+ /**
+ * @var \UnitTester
+ */
+ protected $tester;
+
+ public function testDefaultImportAsset()
+ {
+ $csv = <<<'EOT'
+Name,Email,Username,item Name,Category,Model name,Manufacturer,Model Number,Serial number,Asset Tag,Location,Notes,Purchase Date,Purchase Cost,Company,Status,Warranty,Supplier
+Bonnie Nelson,[email protected],bnelson0,eget nunc donec quis,quam,massa id,Linkbridge,6377018600094472,27aa8378-b0f4-4289-84a4-405da95c6147,970882174-8,Daping,"Curabitur in libero ut massa volutpat convallis. Morbi odio odio, elementum eu, interdum eu, tincidunt in, leo. Maecenas pulvinar lobortis est.",2016-04-05,133289.59,Alpha,Undeployable,14,Blogspan
+EOT;
+ $this->import(new AssetImporter($csv));
+ // Did we create a user?
+
+ $this->tester->seeRecord('users', [
+ 'first_name' => 'Bonnie',
+ 'last_name' => 'Nelson',
+ 'email' => '[email protected]',
+ ]);
+ $this->tester->seeRecord('categories', [
+ 'name' => 'quam'
+ ]);
+
+ $this->tester->seeRecord('models', [
+ 'name' => 'massa id',
+ 'model_number' => 6377018600094472
+ ]);
+
+ $this->tester->seeRecord('manufacturers', [
+ 'name' => 'Linkbridge'
+ ]);
+
+ $this->tester->seeRecord('locations', [
+ 'name' => 'Daping'
+ ]);
+
+ $this->tester->seeRecord('companies', [
+ 'name' => 'Alpha'
+ ]);
+
+ $this->tester->seeRecord('status_labels', [
+ 'name' => 'Undeployable'
+ ]);
+
+ $this->tester->seeRecord('suppliers', [
+ 'name' => 'Blogspan'
+ ]);
+ $this->tester->seeRecord('assets', [
+ 'name' => 'eget nunc donec quis',
+ 'serial' => '27aa8378-b0f4-4289-84a4-405da95c6147',
+ 'asset_tag' => '970882174-8',
+ 'notes' => "Curabitur in libero ut massa volutpat convallis. Morbi odio odio, elementum eu, interdum eu, tincidunt in, leo. Maecenas pulvinar lobortis est.",
+ 'purchase_date' => '2016-04-05 00:00:01',
+ 'purchase_cost' => 133289.59,
+ 'warranty_months' => 14
+ ]);
+ }
+
+ public function testUpdateAsset()
+ {
+ $csv = <<<'EOT'
+Name,Email,Username,item Name,Category,Model name,Manufacturer,Model Number,Serial number,Asset Tag,Location,Notes,Purchase Date,Purchase Cost,Company,Status,Warranty,Supplier
+Bonnie Nelson,[email protected],bnelson0,eget nunc donec quis,quam,massa id,Linkbridge,6377018600094472,27aa8378-b0f4-4289-84a4-405da95c6147,970882174-8,Daping,"Curabitur in libero ut massa volutpat convallis. Morbi odio odio, elementum eu, interdum eu, tincidunt in, leo. Maecenas pulvinar lobortis est.",2016-04-05,133289.59,Alpha,Undeployable,14,Blogspan
+EOT;
+ $this->import(new AssetImporter($csv));
+ $updatedCSV = <<<'EOT'
+item Name,Category,Model name,Manufacturer,Model Number,Serial number,Asset Tag,Location,Notes,Purchase Date,Purchase Cost,Company,Status,Warranty,Supplier
+A new name,some other category,Another Model,Linkbridge 32,356,67433477,970882174-8,New Location,I have no notes,2018-04-05,25.59,Another Company,Ready To Go,18,Not Creative
+EOT;
+ $importer = new AssetImporter($updatedCSV);
+ $importer->setUserId(1)
+ ->setUpdating(true)
+ ->setUsernameFormat('firstname.lastname')
+ ->import();
+
+ $this->tester->seeRecord('categories', [
+ 'name' => 'some other category'
+ ]);
+
+ $this->tester->seeRecord('models', [
+ 'name' => 'Another Model',
+ 'model_number' => 356
+ ]);
+
+ $this->tester->seeRecord('manufacturers', [
+ 'name' => 'Linkbridge 32'
+ ]);
+
+ $this->tester->seeRecord('locations', [
+ 'name' => 'New Location'
+ ]);
+
+ $this->tester->seeRecord('companies', [
+ 'name' => 'Another Company'
+ ]);
+
+ $this->tester->seeRecord('status_labels', [
+ 'name' => 'Ready To Go'
+ ]);
+
+ $this->tester->seeRecord('suppliers', [
+ 'name' => 'Not Creative'
+ ]);
+
+ $this->tester->seeRecord('assets', [
+ 'name' => 'A new name',
+ 'serial' => '67433477',
+ 'asset_tag' => '970882174-8',
+ 'notes' => "I have no notes",
+ 'purchase_date' => '2018-04-05 00:00:01',
+ 'purchase_cost' => 25.59,
+ 'warranty_months' => 18
+ ]);
+ }
+
+ public function testCustomMappingImport()
+ {
+ $csv = <<<'EOT'
+Name,Email,Username,object name,Cat,Model name,Manufacturer,Model Number,Serial number,Asset,Loc,Some Notes,Purchase Date,Purchase Cost,comp,Status,Warranty,Supplier
+Bonnie Nelson,[email protected],bnelson0,eget nunc donec quis,quam,massa id,Linkbridge,6377018600094472,27aa8378-b0f4-4289-84a4-405da95c6147,970882174-8,Daping,"Curabitur in libero ut massa volutpat convallis. Morbi odio odio, elementum eu, interdum eu, tincidunt in, leo. Maecenas pulvinar lobortis est.",2016-04-05,133289.59,Alpha,Undeployable,14,Blogspan
+EOT;
+
+ $customFieldMap = [
+ 'asset_tag' => 'Asset',
+ 'category' => 'Cat',
+ 'company' => 'comp',
+ 'item_name' => 'object name',
+ 'expiration_date' => 'expiration date',
+ 'location' => 'loc',
+ 'notes' => 'Some Notes',
+ 'asset_model' => "model name",
+ ];
+
+ $this->import(new AssetImporter($csv), $customFieldMap);
+ // Did we create a user?
+
+ $this->tester->seeRecord('users', [
+ 'first_name' => 'Bonnie',
+ 'last_name' => 'Nelson',
+ 'email' => '[email protected]',
+ ]);
+
+ $this->tester->seeRecord('categories', [
+ 'name' => 'quam'
+ ]);
+
+ $this->tester->seeRecord('models', [
+ 'name' => 'massa id',
+ 'model_number' => 6377018600094472
+ ]);
+
+ $this->tester->seeRecord('manufacturers', [
+ 'name' => 'Linkbridge'
+ ]);
+
+ $this->tester->seeRecord('locations', [
+ 'name' => 'Daping'
+ ]);
+
+ $this->tester->seeRecord('companies', [
+ 'name' => 'Alpha'
+ ]);
+
+ $this->tester->seeRecord('status_labels', [
+ 'name' => 'Undeployable'
+ ]);
+
+ $this->tester->seeRecord('suppliers', [
+ 'name' => 'Blogspan'
+ ]);
+ $this->tester->seeRecord('assets', [
+ 'name' => 'eget nunc donec quis',
+ 'serial' => '27aa8378-b0f4-4289-84a4-405da95c6147',
+ 'asset_tag' => '970882174-8',
+ 'notes' => "Curabitur in libero ut massa volutpat convallis. Morbi odio odio, elementum eu, interdum eu, tincidunt in, leo. Maecenas pulvinar lobortis est.",
+ 'purchase_date' => '2016-04-05 00:00:01',
+ 'purchase_cost' => 133289.59,
+ 'warranty_months' => 14
+ ]);
+ }
+
+ public function testDefaultAccessoryImport()
+ {
+ $csv = <<<'EOT'
+Item Name,Purchase Date,Purchase Cost,Location,Company,Order Number,Category,Requestable,Quantity
+Walter Carter,09/01/2006,,metus. Vivamus,Macromedia,J935H60W,Customers,False,278
+EOT;
+ $this->import(new AccessoryImporter($csv));
+ $this->tester->seeRecord('accessories', [
+ 'name' => 'Walter Carter',
+ 'purchase_date' => '2006-09-01 00:00:01',
+ 'order_number' => 'J935H60W',
+ 'requestable' => 0,
+ 'qty' => 278
+ ]);
+
+ $this->tester->seeRecord('locations', [
+ 'name' => 'metus. Vivamus'
+ ]);
+
+ $this->tester->seeRecord('companies', [
+ 'name' => 'Macromedia'
+ ]);
+
+ $this->tester->seeRecord('categories', [
+ 'name' => 'Customers'
+ ]);
+
+ }
+
+ public function testDefaultAccessoryUpdate()
+ {
+ $csv = <<<'EOT'
+Item Name,Purchase Date,Purchase Cost,Location,Company,Order Number,Category,Requestable,Quantity
+Walter Carter,09/01/2006,,metus. Vivamus,Macromedia,J935H60W,Customers,False,278
+EOT;
+ $this->import(new AccessoryImporter($csv));
+ $this->tester->seeNumRecords(1, 'accessories');
+
+
+ $updatedCSV = <<<'EOT'
+Item Name,Purchase Date,Purchase Cost,Location,Company,Order Number,Category,Requestable,Quantity
+Walter Carter,09/01/2015,350,metus. Vivamus,Macromedia,35GGH,Customers,True,12
+EOT;
+ $importer = new AccessoryImporter($updatedCSV);
+ $importer->setUserId(1)
+ ->setUpdating(true)
+ ->import();
+ // At this point we should still only have one record.
+ $this->tester->seeNumRecords(1, 'accessories');
+ // But instead these.
+ $this->tester->seeRecord('accessories', [
+ 'name' => 'Walter Carter',
+ 'purchase_date' => '2015-09-01 00:00:01',
+ 'order_number' => '35GGH',
+ 'requestable' => 1,
+ 'qty' => 12
+ ]);
+ }
+
+ public function testCustomAccessoryImport()
+ {
+ $csv = <<<'EOT'
+Name,Pur Date,Cost,Loc,Comp,Order Num,Cat,Request,Quan
+Walter Carter,09/01/2006,,metus. Vivamus,Macromedia,J935H60W,Customers,False,278
+EOT;
+
+ $customFieldMap = [
+ 'category' => 'Cat',
+ 'company' => 'Comp',
+ 'item_name' => 'Name',
+ 'location' => 'Loc',
+ 'purchase_date' => 'Pur Date',
+ 'purchase_cost' => "Cost",
+ 'order_number' => 'Order Num',
+ 'requestable' => 'Request',
+ 'quantity' => 'Quan'
+ ];
+ $this->import(new AccessoryImporter($csv), $customFieldMap);
+ // dd($this->tester->grabRecord('accessories'));
+ $this->tester->seeRecord('accessories', [
+ 'name' => 'Walter Carter',
+ 'purchase_date' => '2006-09-01 00:00:01',
+ 'order_number' => 'J935H60W',
+ 'requestable' => 0,
+ 'qty' => 278
+ ]);
+
+ $this->tester->seeRecord('locations', [
+ 'name' => 'metus. Vivamus'
+ ]);
+
+ $this->tester->seeRecord('companies', [
+ 'name' => 'Macromedia'
+ ]);
+
+ $this->tester->seeRecord('categories', [
+ 'name' => 'Customers'
+ ]);
+
+ }
+
+ public function testDefaultConsumableImport()
+ {
+ $csv = <<<'EOT'
+Item Name,Purchase Date,Purchase Cost,Location,Company,Order Number,Category,Requestable,Quantity
+eget,01/03/2011,$85.91,mauris blandit mattis.,Lycos,T295T06V,Triamterene/Hydrochlorothiazide,No,322
+EOT;
+ $this->import(new ConsumableImporter($csv));
+ // dd($this->tester->grabRecord('consumables'));
+ $this->tester->seeRecord('consumables', [
+ 'name' => 'eget',
+ 'purchase_date' => '2011-01-03 00:00:01',
+ 'purchase_cost' => 85.91,
+ 'order_number' => 'T295T06V',
+ 'requestable' => 0,
+ 'qty' => 322
+ ]);
+
+ $this->tester->seeRecord('locations', [
+ 'name' => 'mauris blandit mattis.'
+ ]);
+
+ $this->tester->seeRecord('companies', [
+ 'name' => 'Lycos'
+ ]);
+
+ $this->tester->seeRecord('categories', [
+ 'name' => 'Triamterene/Hydrochlorothiazide'
+ ]);
+
+ }
+
+ public function testDefaultConsumableUpdate()
+ {
+ $csv = <<<'EOT'
+Item Name,Purchase Date,Purchase Cost,Location,Company,Order Number,Category,Requestable,Quantity
+eget,01/03/2011,85.91,mauris blandit mattis.,Lycos,T295T06V,Triamterene/Hydrochlorothiazide,No,322
+EOT;
+ $this->import(new ConsumableImporter($csv));
+ $this->tester->seeNumRecords(1, 'consumables');
+
+
+ $updatedCSV = <<<'EOT'
+Item Name,Purchase Date,Purchase Cost,Location,Company,Order Number,Category,Requestable,Quantity
+eget,12/05/2016,35.45,mauris blandit mattis.,Lycos,3666FF,New Cat,Yes,15
+EOT;
+ $importer = new ConsumableImporter($updatedCSV);
+ $importer->setUserId(1)
+ ->setUpdating(true)
+ ->import();
+ // At this point we should still only have one record.
+ $this->tester->seeNumRecords(1, 'consumables');
+ // But instead these.
+ $this->tester->seeRecord('consumables', [
+ 'name' => 'eget',
+ 'purchase_date' => '2016-12-05 00:00:01',
+ 'purchase_cost' => 35.45,
+ 'order_number' => '3666FF',
+ 'requestable' => 1,
+ 'qty' => 15
+ ]);
+ }
+
+ public function testCustomConsumableImport()
+ {
+ $csv = <<<'EOT'
+Name,pur Date,Pur Cost,Loc,Comp,Order Num,Kat,Request,Quan
+eget,01/03/2011,85.91,mauris blandit mattis.,Lycos,T295T06V,Triamterene/Hydrochlorothiazide,No,322
+EOT;
+
+ $customFieldMap = [
+ 'category' => 'Kat',
+ 'company' => 'Comp',
+ 'item_name' => 'Name',
+ 'location' => 'Loc',
+ 'purchase_date' => 'Pur date',
+ 'purchase_cost' => "Pur Cost",
+ 'order_number' => 'Order Num',
+ 'requestable' => 'Request',
+ 'quantity' => 'Quan'
+ ];
+ $this->import(new ConsumableImporter($csv), $customFieldMap);
+ $this->tester->seeRecord('consumables', [
+ 'name' => 'eget',
+ 'purchase_date' => '2011-01-03 00:00:01',
+ 'purchase_cost' => 85.91,
+ 'order_number' => 'T295T06V',
+ 'requestable' => 0,
+ 'qty' => 322
+ ]);
+
+ $this->tester->seeRecord('locations', [
+ 'name' => 'mauris blandit mattis.'
+ ]);
+
+ $this->tester->seeRecord('companies', [
+ 'name' => 'Lycos'
+ ]);
+
+ $this->tester->seeRecord('categories', [
+ 'name' => 'Triamterene/Hydrochlorothiazide'
+ ]);
+
+ }
+
+ public function testDefaultLicenseImport()
+ {
+ $csv = <<<'EOT'
+Name,Email,Username,Item name,serial number,manufacturer,purchase date,purchase cost,purchase order,order number,Licensed To Name,Licensed to Email,expiration date,maintained,reassignable,seats,company,supplier,notes
+Helen Anderson,[email protected],cspencer0,Argentum Malachite Athletes Foot Relief,1aa5b0eb-79c5-40b2-8943-5472a6893c3c,"Beer, Leannon and Lubowitz",07/13/2012,$79.66,53008,386436062-5,Cynthia Spencer,[email protected],01/27/2016,false,no,80,"Haag, Schmidt and Farrell","Hegmann, Mohr and Cremin",Sed ante. Vivamus tortor. Duis mattis egestas metus.
+EOT;
+ $this->import(new LicenseImporter($csv));
+ // dd($this->tester->grabRecord('licenses'));
+
+ $this->tester->seeRecord('licenses', [
+ 'name' => 'Argentum Malachite Athletes Foot Relief',
+ 'purchase_date' => '2012-07-13 00:00:01',
+ 'seats' => 80,
+ 'license_email' => '[email protected]',
+ 'order_number' => '386436062-5',
+ 'license_name' => 'Cynthia Spencer',
+ 'expiration_date' => '2016-01-27',
+ 'maintained' => 0,
+ 'notes' => 'Sed ante. Vivamus tortor. Duis mattis egestas metus.',
+ 'purchase_cost' => 79.66,
+ 'purchase_order' => "53008",
+ 'reassignable' => 0,
+ 'serial' => '1aa5b0eb-79c5-40b2-8943-5472a6893c3c',
+ ]);
+
+ $this->tester->seeRecord('manufacturers', [
+ 'name' => 'Beer, Leannon and Lubowitz'
+ ]);
+
+ $this->tester->seeRecord('suppliers', [
+ 'name' => "Hegmann, Mohr and Cremin",
+ ]);
+
+ $this->tester->seeRecord('companies', [
+ 'name' => 'Haag, Schmidt and Farrell'
+ ]);
+
+ $this->tester->seeNumRecords(80, 'license_seats');
+
+ }
+
+ public function testDefaultLicenseUpdate()
+ {
+ $csv = <<<'EOT'
+Name,Email,Username,Item name,serial number,manufacturer,purchase date,purchase cost,purchase order,order number,Licensed To Name,Licensed to Email,expiration date,maintained,reassignable,seats,company,supplier,notes
+Helen Anderson,[email protected],cspencer0,Argentum Malachite Athletes Foot Relief,1aa5b0eb-79c5-40b2-8943-5472a6893c3c,"Beer, Leannon and Lubowitz",07/13/2012,$79.66,53008,386436062-5,Cynthia Spencer,[email protected],01/27/2016,false,no,80,"Haag, Schmidt and Farrell","Hegmann, Mohr and Cremin",Sed ante. Vivamus tortor. Duis mattis egestas metus.
+EOT;
+ $this->import(new LicenseImporter($csv));
+ $this->tester->seeNumRecords(1, 'licenses');
+
+
+ $updatedCSV = <<<'EOT'
+Item name,serial number,manufacturer,purchase date,purchase cost,purchase order,order number,Licensed To Name,Licensed to Email,expiration date,maintained,reassignable,seats,company,supplier,notes
+Argentum Malachite Athletes Foot Relief,7435753-467734,"Beer, Leannon and Lubowitz",05/15/2019,$1865.34,63 ar,18334,A Legend,[email protected],04/27/2016,yes,true,64,"Haag, Schmidt and Farrell","Hegmann, Mohr and Cremin",Sed ante. Vivamus tortor. Duis mattis egestas metus.
+EOT;
+ $importer = new LicenseImporter($updatedCSV);
+ $importer->setUserId(1)
+ ->setUpdating(true)
+ ->import();
+ // At this point we should still only have one record.
+ $this->tester->seeNumRecords(1, 'licenses');
+ // But instead these.
+ // dd($this->tester->grabRecord('licenses'));
+ $this->tester->seeRecord('licenses', [
+ 'name' => 'Argentum Malachite Athletes Foot Relief',
+ 'purchase_date' => '2019-05-15 00:00:01',
+ 'seats' => 64,
+ 'license_email' => '[email protected]',
+ 'order_number' => '18334',
+ 'license_name' => 'A Legend',
+ 'expiration_date' => '2016-04-27',
+ 'maintained' => 1,
+ 'notes' => 'Sed ante. Vivamus tortor. Duis mattis egestas metus.',
+ 'purchase_cost' => 1865.34,
+ 'purchase_order' => "63 ar",
+ 'reassignable' => 1,
+ 'serial' => '7435753-467734',
+ ]);
+ // License seats are soft deleted
+ $this->tester->seeNumRecords(64, 'license_seats', ['deleted_at' => null]);
+ }
+
+ public function testCustomLicenseImport()
+ {
+ $csv = <<<'EOT'
+Name,Email,Username,Object name,serial num,manuf,pur date,pur cost,purc order,order num,Licensed To,Licensed Email,expire date,maint,reass,seat,comp,supplier,note
+Helen Anderson,[email protected],cspencer0,Argentum Malachite Athletes Foot Relief,1aa5b0eb-79c5-40b2-8943-5472a6893c3c,"Beer, Leannon and Lubowitz",07/13/2012,$79.66,53008,386436062-5,Cynthia Spencer,[email protected],01/27/2016,false,no,80,"Haag, Schmidt and Farrell","Hegmann, Mohr and Cremin",Sed ante. Vivamus tortor. Duis mattis egestas metus.
+EOT;
+
+ $customFieldMap = [
+ 'company' => 'Comp',
+ 'expiration_date' => 'expire date',
+ 'item_name' => 'Object Name',
+ 'license_email' => 'licensed email',
+ 'license_name' => 'licensed to',
+ 'maintained' => 'maint',
+ 'manufacturer' => 'manuf',
+ 'notes' => 'note',
+ 'order_number' => 'Order Num',
+ 'purchase_cost' => "Pur Cost",
+ 'purchase_date' => 'Pur date',
+ 'purchase_order' => "Purc Order",
+ 'quantity' => 'Quan',
+ 'reassignable' => 'reass',
+ 'requestable' => 'Request',
+ 'seats' => 'seat',
+ 'serial_number' => 'serial num',
+ ];
+ $this->import(new LicenseImporter($csv), $customFieldMap);
+ // dd($this->tester->grabRecord('licenses'));
+ $this->tester->seeRecord('licenses', [
+ 'name' => 'Argentum Malachite Athletes Foot Relief',
+ 'purchase_date' => '2012-07-13 00:00:01',
+ 'seats' => 80,
+ 'license_email' => '[email protected]',
+ 'order_number' => '386436062-5',
+ 'license_name' => 'Cynthia Spencer',
+ 'expiration_date' => '2016-01-27',
+ 'maintained' => 0,
+ 'notes' => 'Sed ante. Vivamus tortor. Duis mattis egestas metus.',
+ 'purchase_cost' => 79.66,
+ 'purchase_order' => "53008",
+ 'reassignable' => 0,
+ 'serial' => '1aa5b0eb-79c5-40b2-8943-5472a6893c3c',
+ ]);
+
+ $this->tester->seeRecord('manufacturers', [
+ 'name' => 'Beer, Leannon and Lubowitz'
+ ]);
+
+ $this->tester->seeRecord('suppliers', [
+ 'name' => "Hegmann, Mohr and Cremin",
+ ]);
+
+ $this->tester->seeRecord('companies', [
+ 'name' => 'Haag, Schmidt and Farrell'
+ ]);
+
+ $this->tester->seeNumRecords(80, 'license_seats');
+
+ }
+
+ private function import($importer, $mappings = null)
+ {
+ if ($mappings) {
+ $importer->setFieldMappings($mappings);
+ }
+ $importer->setUserId(1)
+ ->setUpdating(false)
+ ->setUsernameFormat('firstname.lastname')
+ ->import();
+ }
+}
diff --git a/tests/unit/LocationTest.php b/tests/unit/LocationTest.php
index 57efe8937f..8380c4b0f6 100644
--- a/tests/unit/LocationTest.php
+++ b/tests/unit/LocationTest.php
@@ -5,18 +5,12 @@ use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
-class LocationTest extends \Codeception\TestCase\Test
+class LocationTest extends BaseTest
{
/**
* @var \UnitTester
*/
protected $tester;
- use DatabaseMigrations;
-
- protected function _before()
- {
- Artisan::call('migrate');
- }
public function testAssetAdd()
{
diff --git a/tests/unit/ManufacturerTest.php b/tests/unit/ManufacturerTest.php
index 96d443d93d..53b09dea7d 100644
--- a/tests/unit/ManufacturerTest.php
+++ b/tests/unit/ManufacturerTest.php
@@ -5,18 +5,12 @@ use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
-class ManufacturerTest extends \Codeception\TestCase\Test
+class ManufacturerTest extends BaseTest
{
/**
* @var \UnitTester
*/
protected $tester;
- use DatabaseMigrations;
-
- protected function _before()
- {
- Artisan::call('migrate');
- }
public function testManufacturerAdd()
{
diff --git a/tests/unit/PermissionsTest.php b/tests/unit/PermissionsTest.php
index 09e9e0af56..ea648293bc 100644
--- a/tests/unit/PermissionsTest.php
+++ b/tests/unit/PermissionsTest.php
@@ -10,12 +10,12 @@ use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
-class PermissionsTest extends \Codeception\TestCase\Test
+class PermissionsTest extends BaseTest
{
public function _before()
{
- Artisan::call('migrate');
+ parent::_before();
$this->noHardware = [
'assets.view' => false,
'assets.create' => false,
diff --git a/tests/unit/SnipeModelTest.php b/tests/unit/SnipeModelTest.php
index 452afa49c1..c7771cb9b5 100644
--- a/tests/unit/SnipeModelTest.php
+++ b/tests/unit/SnipeModelTest.php
@@ -3,7 +3,7 @@
use App\Models\SnipeModel;
-class SnipeModelTest extends \Codeception\TestCase\Test
+class SnipeModelTest extends BaseTest
{
/**
* @var \UnitTester
@@ -14,11 +14,6 @@ class SnipeModelTest extends \Codeception\TestCase\Test
/**
* @test
*/
-
- protected function _before()
- {
- Artisan::call('migrate');
- }
public function it_sets_purchase_dates_appropriately()
{
$c = new SnipeModel;
diff --git a/tests/unit/StatuslabelTest.php b/tests/unit/StatuslabelTest.php
index 5354d81f18..93ecee1ce6 100644
--- a/tests/unit/StatuslabelTest.php
+++ b/tests/unit/StatuslabelTest.php
@@ -5,18 +5,12 @@ use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
-class StatuslabelTest extends \Codeception\TestCase\Test
+class StatuslabelTest extends BaseTest
{
/**
* @var \UnitTester
*/
protected $tester;
- use DatabaseMigrations;
-
- protected function _before()
- {
- Artisan::call('migrate');
- }
public function testRTDStatuslabelAdd()
{
diff --git a/tests/unit/SupplierTest.php b/tests/unit/SupplierTest.php
index f14f61363a..c5b415d2fb 100644
--- a/tests/unit/SupplierTest.php
+++ b/tests/unit/SupplierTest.php
@@ -5,18 +5,12 @@ use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
-class SupplierTest extends \Codeception\TestCase\Test
+class SupplierTest extends BaseTest
{
/**
* @var \UnitTester
*/
protected $tester;
- use DatabaseMigrations;
-
- protected function _before()
- {
- Artisan::call('migrate');
- }
public function testSupplierAdd()
{
diff --git a/tests/unit/UserTest.php b/tests/unit/UserTest.php
index 45ae8ac10d..dd515d0cef 100644
--- a/tests/unit/UserTest.php
+++ b/tests/unit/UserTest.php
@@ -5,18 +5,12 @@ use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
-class UserTest extends \Codeception\TestCase\Test
+class UserTest extends BaseTest
{
/**
* @var \UnitTester
*/
protected $tester;
- use DatabaseMigrations;
-
- protected function _before()
- {
- Artisan::call('migrate');
- }
public function testUserAdd()
{