Importer tests + Fixes (#3733)

* Fix Bug in User::generateFormattedNameFromFullName

In a name "John Doe", this method would split it into "John" and " Doe",
Leaving a space in the last name when importing to the database.  Strip
this space.

* Cleanup/fix some item mapping.

Also make some changes to the importer schema to allow for unit testing.
Generate a default item mapping, and then merge that with any custom
mappings.

* Beginning work on importer unit tests.

* Strip out testrun branches from importer.  It added a lot of complexity and was not terribly useful with web importer as it stood, might reconsider down the road however.

* Normalize the mapped keys when using custom field mappings.

* Add test for custom asset import mapping.

* Make all unit tests inherit from a new custom base.

This baseclass currently calls Artisan::migrate() and seeds a Settings
instance.  This fixes unit tests after the autoincrement bits.

* Store requestable as a boolean.  Fixes some import oddities

* Work on tests for accessory importer.

* Test for custom mapping of accessory import, also adjust the internal field for purchase date.

* Update default locale fallback for currency detection

* Fix Reassignable in consumable as well.

* More importer tests and fixes.
This commit is contained in:
Daniel Meltzer 2017-07-11 22:37:02 -05:00 committed by snipe
parent 9bcfe0748b
commit f432f98e12
34 changed files with 763 additions and 306 deletions

View file

@ -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'),

View file

@ -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');

View file

@ -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'].'"');
}
}

View file

@ -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');

View file

@ -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');

View file

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

View file

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

View file

@ -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'].'"');
}
}

View file

@ -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');

View file

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

View file

@ -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');

View file

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

View file

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

View file

@ -47,7 +47,8 @@
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
"tests/TestCase.php",
"tests/unit/BaseTest.php"
]
},
"scripts": {

View file

@ -78,7 +78,7 @@ return [
|
*/
'locale' => env('APP_LOCALE', 'en'),
'locale' => env('APP_LOCALE', 'en_US.UTF-8'),
/*
|--------------------------------------------------------------------------

View file

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

View file

@ -3,7 +3,7 @@
use App\Models\AssetMaintenance;
class AssetMaintenanceTest extends \Codeception\Test\Unit
class AssetMaintenanceTest extends BaseTest
{
/**
* @var \UnitTester

View file

@ -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();

View file

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

14
tests/unit/BaseTest.php Normal file
View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

554
tests/unit/ImporterTest.php Normal file
View file

@ -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,bnelson0@cdbaby.com,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' => 'bnelson0@cdbaby.com',
]);
$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,bnelson0@cdbaby.com,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,bnelson0@cdbaby.com,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' => 'bnelson0@cdbaby.com',
]);
$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,cspencer0@privacy.gov.au,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,cspencer0@gov.uk,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' => 'cspencer0@gov.uk',
'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,cspencer0@privacy.gov.au,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,cspencer0@gov.uk,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,Legendary@gov.uk,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' => 'Legendary@gov.uk',
'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,cspencer0@privacy.gov.au,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,cspencer0@gov.uk,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' => 'cspencer0@gov.uk',
'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();
}
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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