Merge pull request #15616 from marcusmoore/test/importer-test-updates

Improve importer tests
This commit is contained in:
snipe 2024-10-07 22:29:35 +01:00 committed by GitHub
commit 56e7ea6677
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 568 additions and 579 deletions

View file

@ -2,10 +2,13 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Import extends Model
{
use HasFactory;
protected $casts = [
'header_row' => 'array',
'first_row' => 'array',

View file

@ -41,7 +41,7 @@ class ImportFactory extends Factory
$fileBuilder = Importing\AccessoriesImportFileBuilder::new();
$attributes['name'] = "{$attributes['name']} Accessories";
$attributes['import_type'] = 'Accessories';
$attributes['import_type'] = 'accessory';
$attributes['header_row'] = $fileBuilder->toCsv()[0];
$attributes['first_row'] = $fileBuilder->firstRow();
@ -76,7 +76,7 @@ class ImportFactory extends Factory
public function component()
{
return $this->state(function (array $attributes) {
$fileBuilder = Importing\ComponentsImportFileBuilder::times();
$fileBuilder = Importing\ComponentsImportFileBuilder::new();
$attributes['name'] = "{$attributes['name']} Components";
$attributes['import_type'] = 'component';

View file

@ -0,0 +1,16 @@
<?php
namespace Tests\Feature\Importing\Api;
use App\Models\User;
class GeneralImportTest extends ImportDataTestCase
{
public function testRequiresExistingImport()
{
$this->actingAsForApi(User::factory()->canImport()->create());
$this->importFileResponse(['import' => 9999, 'import-type' => 'accessory'])
->assertStatusMessageIs('import-errors');
}
}

View file

@ -4,21 +4,20 @@ namespace Tests\Feature\Importing\Api;
use App\Models\Accessory;
use App\Models\Actionlog;
use App\Models\Company;
use Database\Factories\AccessoryFactory;
use Database\Factories\CompanyFactory;
use App\Models\Import;
use App\Models\User;
use Illuminate\Support\Str;
use Database\Factories\UserFactory;
use Database\Factories\ImportFactory;
use PHPUnit\Framework\Attributes\Test;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Arr;
use Illuminate\Testing\TestResponse;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\Support\Importing\AccessoriesImportFileBuilder as ImportFileBuilder;
use Tests\Support\Importing\CleansUpImportFiles;
class ImportAccessoriesTest extends ImportDataTestCase
class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissionsRequirement
{
use CleansUpImportFiles;
use WithFaker;
protected function importFileResponse(array $parameters = []): TestResponse
@ -31,14 +30,9 @@ class ImportAccessoriesTest extends ImportDataTestCase
}
#[Test]
#[DataProvider('permissionsTestData')]
public function onlyUserWithPermissionCanImportAccessories(array|string $permissions): void
public function testRequiresPermission()
{
$permissions = collect((array) $permissions)
->map(fn (string $permission) => [$permission => '1'])
->toJson();
$this->actingAsForApi(UserFactory::new()->create(['permissions' => $permissions]));
$this->actingAsForApi(User::factory()->create());
$this->importFileResponse(['import' => 44])->assertForbidden();
}
@ -46,9 +40,9 @@ class ImportAccessoriesTest extends ImportDataTestCase
#[Test]
public function userWithImportAccessoryPermissionCanImportAccessories(): void
{
$this->actingAsForApi(UserFactory::new()->canImport()->create());
$this->actingAsForApi(User::factory()->canImport()->create());
$import = ImportFactory::new()->accessory()->create();
$import = Import::factory()->accessory()->create();
$this->importFileResponse(['import' => $import->id])->assertOk();
}
@ -58,9 +52,9 @@ class ImportAccessoriesTest extends ImportDataTestCase
{
$importFileBuilder = ImportFileBuilder::new();
$row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])
->assertOk()
->assertExactJson([
@ -81,23 +75,23 @@ class ImportAccessoriesTest extends ImportDataTestCase
->where('item_id', $newAccessory->id)
->sole();
$this->assertEquals($activityLog->action_type, 'create');
$this->assertEquals($activityLog->action_source, 'importer');
$this->assertEquals($activityLog->company_id, $newAccessory->company->id);
$this->assertEquals('create', $activityLog->action_type);
$this->assertEquals('importer', $activityLog->action_source);
$this->assertEquals($newAccessory->company->id, $activityLog->company_id);
$this->assertEquals($newAccessory->name, $row['itemName']);
$this->assertEquals($newAccessory->qty, $row['quantity']);
$this->assertEquals($newAccessory->purchase_date->toDateString(), $row['purchaseDate']);
$this->assertEquals($newAccessory->purchase_cost, $row['purchaseCost']);
$this->assertEquals($newAccessory->order_number, $row['orderNumber']);
$this->assertEquals($newAccessory->notes, $row['notes']);
$this->assertEquals($newAccessory->category->name, $row['category']);
$this->assertEquals($newAccessory->category->category_type, 'accessory');
$this->assertEquals($newAccessory->manufacturer->name, $row['manufacturerName']);
$this->assertEquals($newAccessory->supplier->name, $row['supplierName']);
$this->assertEquals($newAccessory->location->name, $row['location']);
$this->assertEquals($newAccessory->company->name, $row['companyName']);
$this->assertEquals($newAccessory->model_number, $row['modelNumber']);
$this->assertEquals($row['itemName'], $newAccessory->name);
$this->assertEquals($row['quantity'], $newAccessory->qty);
$this->assertEquals($row['purchaseDate'], $newAccessory->purchase_date->toDateString());
$this->assertEquals($row['purchaseCost'], $newAccessory->purchase_cost);
$this->assertEquals($row['orderNumber'], $newAccessory->order_number);
$this->assertEquals($row['notes'], $newAccessory->notes);
$this->assertEquals($row['category'], $newAccessory->category->name);
$this->assertEquals('accessory', $newAccessory->category->category_type);
$this->assertEquals($row['manufacturerName'], $newAccessory->manufacturer->name);
$this->assertEquals($row['supplierName'], $newAccessory->supplier->name);
$this->assertEquals($row['location'], $newAccessory->location->name);
$this->assertEquals($row['companyName'], $newAccessory->company->name);
$this->assertEquals($row['modelNumber'], $newAccessory->model_number);
$this->assertFalse($newAccessory->requestable);
$this->assertNull($newAccessory->min_amt);
$this->assertNull($newAccessory->user_id);
@ -111,9 +105,9 @@ class ImportAccessoriesTest extends ImportDataTestCase
$importFileBuilder = new ImportFileBuilder([$row]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->importFileResponse(['import' => $import->id])->assertOk();
}
@ -122,25 +116,25 @@ class ImportAccessoriesTest extends ImportDataTestCase
public function willFormatDate(): void
{
$importFileBuilder = ImportFileBuilder::new(['purchaseDate' => '2022/10/10']);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$accessory = Accessory::query()
->where('name', $importFileBuilder->firstRow()['itemName'])
->sole(['purchase_date']);
$this->assertEquals($accessory->purchase_date->toDateString(), '2022-10-10');
$this->assertEquals('2022-10-10', $accessory->purchase_date->toDateString());
}
#[Test]
public function willNotCreateNewCategoryWhenCategoryExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['category' => Str::random()]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newAccessories = Accessory::query()
@ -153,11 +147,11 @@ class ImportAccessoriesTest extends ImportDataTestCase
#[Test]
public function willNotCreateNewAccessoryWhenAccessoryWithNameExists(): void
{
$accessory = AccessoryFactory::new()->create(['name' => Str::random()]);
$accessory = Accessory::factory()->create(['name' => Str::random()]);
$importFileBuilder = ImportFileBuilder::times(2)->replace(['itemName' => $accessory->name]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$probablyNewAccessories = Accessory::query()
@ -165,16 +159,16 @@ class ImportAccessoriesTest extends ImportDataTestCase
->get(['name']);
$this->assertCount(1, $probablyNewAccessories);
$this->assertEquals($probablyNewAccessories->first()->name, $accessory->name);
$this->assertEquals($accessory->name, $probablyNewAccessories->first()->name);
}
#[Test]
public function willNotCreateNewCompanyWhenCompanyAlreadyExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['companyName' => Str::random()]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newAccessories = Accessory::query()
@ -188,9 +182,9 @@ class ImportAccessoriesTest extends ImportDataTestCase
public function willNotCreateNewLocationWhenLocationAlreadyExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['location' => Str::random()]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newAccessories = Accessory::query()
@ -204,9 +198,9 @@ class ImportAccessoriesTest extends ImportDataTestCase
public function willNotCreateNewManufacturerWhenManufacturerAlreadyExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['manufacturerName' => $this->faker->company]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newAccessories = Accessory::query()
@ -220,9 +214,9 @@ class ImportAccessoriesTest extends ImportDataTestCase
public function willNotCreateNewSupplierWhenSupplierAlreadyExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['supplierName' => $this->faker->company]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newAccessories = Accessory::query()
@ -236,9 +230,9 @@ class ImportAccessoriesTest extends ImportDataTestCase
public function whenColumnsAreMissingInImportFile(): void
{
$importFileBuilder = ImportFileBuilder::new()->forget(['minimumAmount', 'purchaseCost', 'purchaseDate']);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newAccessory = Accessory::query()
@ -254,9 +248,9 @@ class ImportAccessoriesTest extends ImportDataTestCase
public function whenRequiredColumnsAreMissingInImportFile(): void
{
$importFileBuilder = ImportFileBuilder::new()->forget(['itemName', 'quantity', 'category']);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])
->assertInternalServerError()
->assertExactJson([
@ -277,12 +271,12 @@ class ImportAccessoriesTest extends ImportDataTestCase
#[Test]
public function updateAccessoryFromImport(): void
{
$accessory = AccessoryFactory::new()->create(['name' => Str::random()])->refresh();
$accessory = Accessory::factory()->create(['name' => Str::random()])->refresh();
$importFileBuilder = ImportFileBuilder::new(['itemName' => $accessory->name]);
$row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$updatedAccessory = Accessory::query()->find($accessory->id);
@ -292,30 +286,30 @@ class ImportAccessoriesTest extends ImportDataTestCase
'location_id', 'model_number', 'updated_at'
];
$this->assertEquals($updatedAccessory->name, $row['itemName']);
$this->assertEquals($row['itemName'], $updatedAccessory->name);
$this->assertEquals($row['companyName'], $updatedAccessory->company->name);
$this->assertEquals($updatedAccessory->qty, $row['quantity']);
$this->assertEquals($updatedAccessory->purchase_date->toDateString(), $row['purchaseDate']);
$this->assertEquals($updatedAccessory->purchase_cost, $row['purchaseCost']);
$this->assertEquals($updatedAccessory->order_number, $row['orderNumber']);
$this->assertEquals($updatedAccessory->notes, $row['notes']);
$this->assertEquals($updatedAccessory->category->name, $row['category']);
$this->assertEquals($updatedAccessory->category->category_type, 'accessory');
$this->assertEquals($updatedAccessory->manufacturer->name, $row['manufacturerName']);
$this->assertEquals($updatedAccessory->supplier->name, $row['supplierName']);
$this->assertEquals($updatedAccessory->location->name, $row['location']);
$this->assertEquals($updatedAccessory->model_number, $row['modelNumber']);
$this->assertEquals($row['quantity'], $updatedAccessory->qty);
$this->assertEquals($row['purchaseDate'], $updatedAccessory->purchase_date->toDateString());
$this->assertEquals($row['purchaseCost'], $updatedAccessory->purchase_cost);
$this->assertEquals($row['orderNumber'], $updatedAccessory->order_number);
$this->assertEquals($row['notes'], $updatedAccessory->notes);
$this->assertEquals($row['category'], $updatedAccessory->category->name);
$this->assertEquals('accessory', $updatedAccessory->category->category_type);
$this->assertEquals($row['manufacturerName'], $updatedAccessory->manufacturer->name);
$this->assertEquals($row['supplierName'], $updatedAccessory->supplier->name);
$this->assertEquals($row['location'], $updatedAccessory->location->name);
$this->assertEquals($row['modelNumber'], $updatedAccessory->model_number);
$this->assertEquals(
Arr::except($updatedAccessory->attributesToArray(), $updatedAttributes),
Arr::except($accessory->attributesToArray(), $updatedAttributes),
Arr::except($updatedAccessory->attributesToArray(), $updatedAttributes),
);
}
#[Test]
public function whenImportFileContainsEmptyValues(): void
{
$accessory = AccessoryFactory::new()->create(['name' => Str::random()]);
$accessory = Accessory::factory()->create(['name' => Str::random()]);
$accessory->refresh();
$importFileBuilder = ImportFileBuilder::new([
@ -335,9 +329,9 @@ class ImportAccessoriesTest extends ImportDataTestCase
'modelNumber' => ''
]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])
->assertInternalServerError()
->assertExactJson([
@ -355,14 +349,14 @@ class ImportAccessoriesTest extends ImportDataTestCase
$importFileBuilder->replace(['itemName' => $accessory->name]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$updatedAccessory = clone $accessory;
$updatedAccessory->refresh();
$this->assertEquals($updatedAccessory->toArray(), $accessory->toArray());
$this->assertEquals($accessory->toArray(), $updatedAccessory->toArray());
}
#[Test]
@ -385,9 +379,9 @@ class ImportAccessoriesTest extends ImportDataTestCase
];
$importFileBuilder = new ImportFileBuilder([$row]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse([
'import' => $import->id,
'column-mappings' => [
@ -412,15 +406,15 @@ class ImportAccessoriesTest extends ImportDataTestCase
->where('name', $row['modelNumber'])
->sole();
$this->assertEquals($newAccessory->name, $row['modelNumber']);
$this->assertEquals($newAccessory->model_number, $row['itemName']);
$this->assertEquals($newAccessory->qty, $row['quantity']);
$this->assertEquals($newAccessory->purchase_date->toDateString(), $row['notes']);
$this->assertEquals($newAccessory->purchase_cost, $row['location']);
$this->assertEquals($newAccessory->order_number, $row['companyName']);
$this->assertEquals($newAccessory->notes, $row['purchaseDate']);
$this->assertEquals($newAccessory->category->name, $row['manufacturerName']);
$this->assertEquals($newAccessory->manufacturer->name, $row['category']);
$this->assertEquals($newAccessory->location->name, $row['purchaseCost']);
$this->assertEquals($row['modelNumber'], $newAccessory->name);
$this->assertEquals($row['itemName'], $newAccessory->model_number);
$this->assertEquals($row['quantity'], $newAccessory->qty);
$this->assertEquals($row['notes'], $newAccessory->purchase_date->toDateString());
$this->assertEquals($row['location'], $newAccessory->purchase_cost);
$this->assertEquals($row['companyName'], $newAccessory->order_number);
$this->assertEquals($row['purchaseDate'], $newAccessory->notes);
$this->assertEquals($row['manufacturerName'], $newAccessory->category->name);
$this->assertEquals($row['category'], $newAccessory->manufacturer->name);
$this->assertEquals($row['purchaseCost'], $newAccessory->location->name);
}
}

View file

@ -5,24 +5,23 @@ namespace Tests\Feature\Importing\Api;
use App\Models\Actionlog as ActionLog;
use App\Models\Asset;
use App\Models\CustomField;
use App\Models\Import;
use App\Models\User;
use App\Notifications\CheckoutAssetNotification;
use Carbon\Carbon;
use Database\Factories\AssetFactory;
use Database\Factories\CustomFieldFactory;
use Illuminate\Support\Str;
use Database\Factories\UserFactory;
use Database\Factories\ImportFactory;
use PHPUnit\Framework\Attributes\Test;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Str;
use Illuminate\Testing\TestResponse;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\Support\Importing\AssetsImportFileBuilder as ImportFileBuilder;
use Tests\Support\Importing\CleansUpImportFiles;
class ImportAssetsTest extends ImportDataTestCase
class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsRequirement
{
use CleansUpImportFiles;
use WithFaker;
protected function importFileResponse(array $parameters = []): TestResponse
@ -35,14 +34,9 @@ class ImportAssetsTest extends ImportDataTestCase
}
#[Test]
#[DataProvider('permissionsTestData')]
public function onlyUserWithPermissionCanImportAssets(array|string $permissions): void
public function testRequiresPermission()
{
$permissions = collect((array) $permissions)
->map(fn (string $permission) => [$permission => '1'])
->toJson();
$this->actingAsForApi(UserFactory::new()->create(['permissions' => $permissions]));
$this->actingAsForApi(User::factory()->create());
$this->importFileResponse(['import' => 44])->assertForbidden();
}
@ -50,9 +44,9 @@ class ImportAssetsTest extends ImportDataTestCase
#[Test]
public function userWithImportAssetsPermissionCanImportAssets(): void
{
$this->actingAsForApi(UserFactory::new()->canImport()->create());
$this->actingAsForApi(User::factory()->canImport()->create());
$import = ImportFactory::new()->asset()->create();
$import = Import::factory()->asset()->create();
$this->importFileResponse(['import' => $import->id])->assertOk();
}
@ -64,9 +58,9 @@ class ImportAssetsTest extends ImportDataTestCase
$importFileBuilder = ImportFileBuilder::new();
$row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])
->assertOk()
->assertExactJson([
@ -89,60 +83,60 @@ class ImportAssetsTest extends ImportDataTestCase
$this->assertCount(2, $activityLogs);
$this->assertEquals($activityLogs[0]->action_type, 'checkout');
$this->assertEquals($activityLogs[0]->item_type, Asset::class);
$this->assertEquals($activityLogs[0]->target_id, $assignee->id);
$this->assertEquals($activityLogs[0]->target_type, User::class);
$this->assertEquals($activityLogs[0]->note, 'Checkout from CSV Importer');
$this->assertEquals('checkout', $activityLogs[0]->action_type);
$this->assertEquals(Asset::class, $activityLogs[0]->item_type);
$this->assertEquals($assignee->id, $activityLogs[0]->target_id);
$this->assertEquals(User::class, $activityLogs[0]->target_type);
$this->assertEquals('Checkout from CSV Importer', $activityLogs[0]->note);
$this->assertEquals($activityLogs[1]->action_type, 'create');
$this->assertEquals('create', $activityLogs[1]->action_type);
$this->assertNull($activityLogs[1]->target_id);
$this->assertEquals($activityLogs[1]->item_type, Asset::class);
$this->assertEquals(Asset::class, $activityLogs[1]->item_type);
$this->assertNull($activityLogs[1]->note);
$this->assertNull($activityLogs[1]->target_type);
$this->assertEquals("{$assignee->first_name} {$assignee->last_name}", $row['assigneeFullName']);
$this->assertEquals($assignee->email, $row['assigneeEmail']);
$this->assertEquals($assignee->username, $row['assigneeUsername']);
$this->assertEquals($row['assigneeFullName'], "{$assignee->first_name} {$assignee->last_name}");
$this->assertEquals($row['assigneeEmail'], $assignee->email);
$this->assertEquals($row['assigneeUsername'], $assignee->username);
$this->assertEquals($newAsset->model->category->name, $row['category']);
$this->assertEquals($newAsset->model->manufacturer->name, $row['manufacturerName']);
$this->assertEquals($newAsset->name, $row['itemName']);
$this->assertEquals($newAsset->asset_tag, $row['tag']);
$this->assertEquals($newAsset->model->name, $row['model']);
$this->assertEquals($newAsset->model->model_number, $row['modelNumber']);
$this->assertEquals($newAsset->purchase_date->toDateString(), $row['purchaseDate']);
$this->assertEquals($row['category'], $newAsset->model->category->name);
$this->assertEquals($row['manufacturerName'], $newAsset->model->manufacturer->name);
$this->assertEquals($row['itemName'], $newAsset->name);
$this->assertEquals($row['tag'], $newAsset->asset_tag);
$this->assertEquals($row['model'], $newAsset->model->name);
$this->assertEquals($row['modelNumber'], $newAsset->model->model_number);
$this->assertEquals($row['purchaseDate'], $newAsset->purchase_date->toDateString());
$this->assertNull($newAsset->asset_eol_date);
$this->assertEquals(0, $newAsset->eol_explicit);
$this->assertEquals($newAsset->location_id, $newAsset->rtd_location_id);
$this->assertEquals($newAsset->purchase_cost, $row['purchaseCost']);
$this->assertEquals($row['purchaseCost'], $newAsset->purchase_cost);
$this->assertNull($newAsset->order_number);
$this->assertEquals($newAsset->image, '');
$this->assertEquals('', $newAsset->image);
$this->assertNull($newAsset->user_id);
$this->assertEquals($newAsset->physical, 1);
$this->assertEquals($newAsset->assetStatus->name, $row['status']);
$this->assertEquals($newAsset->archived, 0);
$this->assertEquals($newAsset->warranty_months, $row['warrantyInMonths']);
$this->assertEquals(1, $newAsset->physical);
$this->assertEquals($row['status'], $newAsset->assetStatus->name);
$this->assertEquals(0, $newAsset->archived);
$this->assertEquals($row['warrantyInMonths'], $newAsset->warranty_months);
$this->assertNull($newAsset->deprecate);
$this->assertEquals($newAsset->supplier->name, $row['supplierName']);
$this->assertEquals($newAsset->requestable, 0);
$this->assertEquals($newAsset->defaultLoc->name, $row['location']);
$this->assertEquals($newAsset->accepted, null);
$this->assertEquals(Carbon::parse($newAsset->last_checkout)->toDateString(), now()->toDateString());
$this->assertEquals($newAsset->last_checkin, 0);
$this->assertEquals($newAsset->expected_checkin, 0);
$this->assertEquals($newAsset->company->name, $row['companyName']);
$this->assertEquals($newAsset->assigned_type, User::class);
$this->assertEquals($row['supplierName'], $newAsset->supplier->name);
$this->assertEquals(0, $newAsset->requestable);
$this->assertEquals($row['location'], $newAsset->defaultLoc->name);
$this->assertEquals(null, $newAsset->accepted);
$this->assertEquals(now()->toDateString(), Carbon::parse($newAsset->last_checkout)->toDateString());
$this->assertEquals(0, $newAsset->last_checkin);
$this->assertEquals(0, $newAsset->expected_checkin);
$this->assertEquals($row['companyName'], $newAsset->company->name);
$this->assertEquals(User::class, $newAsset->assigned_type);
$this->assertNull($newAsset->last_audit_date);
$this->assertNull($newAsset->next_audit_date);
$this->assertEquals($newAsset->location->name, $row['location']);
$this->assertEquals($newAsset->checkin_counter, 0);
$this->assertEquals($newAsset->checkout_counter, 1);
$this->assertEquals($newAsset->requests_counter, 0);
$this->assertEquals($newAsset->byod, 0);
$this->assertEquals($row['location'], $newAsset->location->name);
$this->assertEquals(0, $newAsset->checkin_counter);
$this->assertEquals(1, $newAsset->checkout_counter);
$this->assertEquals(0, $newAsset->requests_counter);
$this->assertEquals(0, $newAsset->byod);
//Notes is never read.
//$this->assertEquals($asset->notes, $row['notes']);
// $this->assertEquals($row['notes'], $newAsset->notes);
Notification::assertSentTo($assignee, CheckoutAssetNotification::class);
}
@ -155,9 +149,9 @@ class ImportAssetsTest extends ImportDataTestCase
$importFileBuilder = new ImportFileBuilder([$row]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->importFileResponse(['import' => $import->id])->assertOk();
}
@ -165,11 +159,11 @@ class ImportAssetsTest extends ImportDataTestCase
#[Test]
public function willNotCreateNewAssetWhenAssetWithSameTagAlreadyExists(): void
{
$asset = AssetFactory::new()->create(['asset_tag' => $this->faker->uuid]);
$asset = Asset::factory()->create(['asset_tag' => $this->faker->uuid]);
$importFileBuilder = ImportFileBuilder::times(4)->replace(['tag' => $asset->asset_tag]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])
->assertInternalServerError()
->assertExactJson([
@ -195,9 +189,9 @@ class ImportAssetsTest extends ImportDataTestCase
public function willNotCreateNewCompanyWhenCompanyExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['companyName' => Str::random()]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newAssets = Asset::query()
@ -211,9 +205,9 @@ class ImportAssetsTest extends ImportDataTestCase
public function willNotCreateNewLocationWhenLocationExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['location' => Str::random()]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newAssets = Asset::query()
@ -227,9 +221,9 @@ class ImportAssetsTest extends ImportDataTestCase
public function willNotCreateNewSupplierWhenSupplierExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['supplierName' => $this->faker->company]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newAssets = Asset::query()
@ -243,9 +237,9 @@ class ImportAssetsTest extends ImportDataTestCase
public function willNotCreateNewManufacturerWhenManufacturerExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['manufacturerName' => $this->faker->company]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newAssets = Asset::query()
@ -260,9 +254,9 @@ class ImportAssetsTest extends ImportDataTestCase
public function willNotCreateCategoryWhenCategoryExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['category' => $this->faker->company]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newAssets = Asset::query()
@ -277,9 +271,9 @@ class ImportAssetsTest extends ImportDataTestCase
public function willNotCreateNewAssetModelWhenAssetModelExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['model' => Str::random()]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newAssets = Asset::query()
@ -299,9 +293,9 @@ class ImportAssetsTest extends ImportDataTestCase
'status'
]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newAsset = Asset::query()
@ -309,7 +303,7 @@ class ImportAssetsTest extends ImportDataTestCase
->where('serial', $importFileBuilder->firstRow()['serialNumber'])
->sole();
$this->assertEquals($newAsset->assetStatus->name, 'Ready to Deploy');
$this->assertEquals('Ready to Deploy', $newAsset->assetStatus->name);
$this->assertNull($newAsset->purchase_date);
$this->assertNull($newAsset->purchase_cost);
}
@ -322,17 +316,17 @@ class ImportAssetsTest extends ImportDataTestCase
'purchaseDate' => '2022/10/10'
]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newAsset = Asset::query()
->where('serial', $importFileBuilder->firstRow()['serialNumber'])
->sole();
$this->assertEquals($newAsset->warranty_months, 3);
$this->assertEquals($newAsset->purchase_date->toDateString(), '2022-10-10');
$this->assertEquals(3, $newAsset->warranty_months);
$this->assertEquals('2022-10-10', $newAsset->purchase_date->toDateString());
}
#[Test]
@ -343,9 +337,9 @@ class ImportAssetsTest extends ImportDataTestCase
->replace(['model' => '']);
$rows = $importFileBuilder->all();
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])
->assertInternalServerError()
->assertJson([
@ -385,12 +379,12 @@ class ImportAssetsTest extends ImportDataTestCase
#[Test]
public function updateAssetFromImport(): void
{
$asset = AssetFactory::new()->create()->refresh();
$asset = Asset::factory()->create()->refresh();
$importFileBuilder = ImportFileBuilder::times(1)->replace(['tag' => $asset->asset_tag]);
$row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$updatedAsset = Asset::query()
@ -406,33 +400,33 @@ class ImportAssetsTest extends ImportDataTestCase
'last_checkout', 'requestable', 'updated_at', 'checkout_counter', 'assigned_type'
];
$this->assertEquals("{$assignee->first_name} {$assignee->last_name}", $row['assigneeFullName']);
$this->assertEquals($assignee->email, $row['assigneeEmail']);
$this->assertEquals($assignee->username, $row['assigneeUsername']);
$this->assertEquals($row['assigneeFullName'], "{$assignee->first_name} {$assignee->last_name}");
$this->assertEquals($row['assigneeEmail'], $assignee->email);
$this->assertEquals($row['assigneeUsername'], $assignee->username);
$this->assertEquals($updatedAsset->model->category->name, $row['category']);
$this->assertEquals($updatedAsset->model->manufacturer->name, $row['manufacturerName']);
$this->assertEquals($updatedAsset->name, $row['itemName']);
$this->assertEquals($updatedAsset->asset_tag, $row['tag']);
$this->assertEquals($updatedAsset->model->name, $row['model']);
$this->assertEquals($updatedAsset->model->model_number, $row['modelNumber']);
$this->assertEquals($updatedAsset->purchase_date->toDateString(), $row['purchaseDate']);
$this->assertEquals($updatedAsset->purchase_cost, $row['purchaseCost']);
$this->assertEquals($updatedAsset->assetStatus->name, $row['status']);
$this->assertEquals($updatedAsset->warranty_months, $row['warrantyInMonths']);
$this->assertEquals($updatedAsset->supplier->name, $row['supplierName']);
$this->assertEquals($updatedAsset->defaultLoc->name, $row['location']);
$this->assertEquals($updatedAsset->company->name, $row['companyName']);
$this->assertEquals($updatedAsset->location->name, $row['location']);
$this->assertEquals($updatedAsset->checkout_counter, 1);
$this->assertEquals($updatedAsset->assigned_type, user::class);
$this->assertEquals($row['category'], $updatedAsset->model->category->name);
$this->assertEquals($row['manufacturerName'], $updatedAsset->model->manufacturer->name);
$this->assertEquals($row['itemName'], $updatedAsset->name);
$this->assertEquals($row['tag'], $updatedAsset->asset_tag);
$this->assertEquals($row['model'], $updatedAsset->model->name);
$this->assertEquals($row['modelNumber'], $updatedAsset->model->model_number);
$this->assertEquals($row['purchaseDate'], $updatedAsset->purchase_date->toDateString());
$this->assertEquals($row['purchaseCost'], $updatedAsset->purchase_cost);
$this->assertEquals($row['status'], $updatedAsset->assetStatus->name);
$this->assertEquals($row['warrantyInMonths'], $updatedAsset->warranty_months);
$this->assertEquals($row['supplierName'], $updatedAsset->supplier->name);
$this->assertEquals($row['location'], $updatedAsset->defaultLoc->name);
$this->assertEquals($row['companyName'], $updatedAsset->company->name);
$this->assertEquals($row['location'], $updatedAsset->location->name);
$this->assertEquals(1, $updatedAsset->checkout_counter);
$this->assertEquals(user::class, $updatedAsset->assigned_type);
//RequestAble is always updated regardless of initial value.
//$this->assertEquals($updatedAsset->requestable, $asset->requestable);
// $this->assertEquals($asset->requestable, $updatedAsset->requestable);
$this->assertEquals(
Arr::except($updatedAsset->attributesToArray(), $updatedAttributes),
Arr::except($asset->attributesToArray(), $updatedAttributes),
Arr::except($updatedAsset->attributesToArray(), $updatedAttributes),
);
}
@ -462,9 +456,9 @@ class ImportAssetsTest extends ImportDataTestCase
];
$importFileBuilder = new ImportFileBuilder([$row]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse([
'import' => $import->id,
@ -497,45 +491,45 @@ class ImportAssetsTest extends ImportDataTestCase
$assignee = User::query()->find($asset->assigned_to, ['id', 'first_name', 'last_name', 'email', 'username']);
$this->assertEquals("{$assignee->first_name} {$assignee->last_name}", $row['warrantyInMonths']);
$this->assertEquals($assignee->email, $row['tag']);
$this->assertEquals($assignee->username, $row['location']);
$this->assertEquals($row['warrantyInMonths'], "{$assignee->first_name} {$assignee->last_name}");
$this->assertEquals($row['tag'], $assignee->email);
$this->assertEquals($row['location'], $assignee->username);
$this->assertEquals($asset->model->category->name, $row['modelNumber']);
$this->assertEquals($asset->model->manufacturer->name, $row['assigneeEmail']);
$this->assertEquals($asset->name, $row['model']);
$this->assertEquals($asset->asset_tag, $row['serialNumber']);
$this->assertEquals($asset->model->name, $row['purchaseCost']);
$this->assertEquals($asset->model->model_number, $row['itemName']);
$this->assertEquals($asset->purchase_date->toDateString(), $row['supplierName']);
$this->assertEquals($asset->purchase_cost, $row['companyName']);
$this->assertEquals($asset->assetStatus->name, $row['manufacturerName']);
$this->assertEquals($asset->warranty_months, $row['status']);
$this->assertEquals($asset->supplier->name, $row['assigneeFullName']);
$this->assertEquals($asset->defaultLoc->name, $row['category']);
$this->assertEquals($asset->company->name, $row['purchaseDate']);
$this->assertEquals($asset->location->name, $row['category']);
$this->assertEquals($asset->notes, $row['notes']);
$this->assertEquals($row['modelNumber'], $asset->model->category->name);
$this->assertEquals($row['assigneeEmail'], $asset->model->manufacturer->name);
$this->assertEquals($row['model'], $asset->name);
$this->assertEquals($row['serialNumber'], $asset->asset_tag);
$this->assertEquals($row['purchaseCost'], $asset->model->name);
$this->assertEquals($row['itemName'], $asset->model->model_number);
$this->assertEquals($row['supplierName'], $asset->purchase_date->toDateString());
$this->assertEquals($row['companyName'], $asset->purchase_cost);
$this->assertEquals($row['manufacturerName'], $asset->assetStatus->name);
$this->assertEquals($row['status'], $asset->warranty_months);
$this->assertEquals($row['assigneeFullName'], $asset->supplier->name);
$this->assertEquals($row['category'], $asset->defaultLoc->name);
$this->assertEquals($row['purchaseDate'], $asset->company->name);
$this->assertEquals($row['category'], $asset->location->name);
$this->assertEquals($row['notes'], $asset->notes);
$this->assertNull($asset->asset_eol_date);
$this->assertEquals(0, $asset->eol_explicit);
$this->assertNull($asset->order_number);
$this->assertEquals($asset->image, '');
$this->assertEquals('', $asset->image);
$this->assertNull($asset->user_id);
$this->assertEquals($asset->physical, 1);
$this->assertEquals($asset->archived, 0);
$this->assertEquals(1, $asset->physical);
$this->assertEquals(0, $asset->archived);
$this->assertNull($asset->deprecate);
$this->assertEquals($asset->requestable, 0);
$this->assertEquals($asset->accepted, null);
$this->assertEquals(Carbon::parse($asset->last_checkout)->toDateString(), now()->toDateString());
$this->assertEquals($asset->last_checkin, 0);
$this->assertEquals($asset->expected_checkin, 0);
$this->assertEquals($asset->assigned_type, User::class);
$this->assertEquals(0, $asset->requestable);
$this->assertEquals(null, $asset->accepted);
$this->assertEquals(now()->toDateString(), Carbon::parse($asset->last_checkout)->toDateString());
$this->assertEquals(0, $asset->last_checkin);
$this->assertEquals(0, $asset->expected_checkin);
$this->assertEquals(User::class, $asset->assigned_type);
$this->assertNull($asset->last_audit_date);
$this->assertNull($asset->next_audit_date);
$this->assertEquals($asset->checkin_counter, 0);
$this->assertEquals($asset->checkout_counter, 1);
$this->assertEquals($asset->requests_counter, 0);
$this->assertEquals($asset->byod, 0);
$this->assertEquals(0, $asset->checkin_counter);
$this->assertEquals(1, $asset->checkout_counter);
$this->assertEquals(0, $asset->requests_counter);
$this->assertEquals(0, $asset->byod);
}
#[Test]
@ -550,7 +544,7 @@ class ImportAssetsTest extends ImportDataTestCase
$customField = CustomField::query()->where('name', 'Mac Address')->firstOrNew();
if (!$customField->exists) {
$customField = CustomFieldFactory::new()->macAddress()->create(['db_column' => '_snipeit_mac_address_1']);
$customField = CustomField::factory()->macAddress()->create(['db_column' => '_snipeit_mac_address_1']);
}
if ($customField->field_encrypted) {
@ -558,14 +552,14 @@ class ImportAssetsTest extends ImportDataTestCase
$customField->save();
}
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newAsset = Asset::query()->where('serial', $importFileBuilder->firstRow()['serialNumber'])->sole();
$this->assertEquals($newAsset->getAttribute($customField->db_column), $macAddress);
$this->assertEquals($macAddress, $newAsset->getAttribute($customField->db_column));
}
#[Test]
@ -580,7 +574,7 @@ class ImportAssetsTest extends ImportDataTestCase
$customField = CustomField::query()->where('name', 'Mac Address')->firstOrNew();
if (!$customField->exists) {
$customField = CustomFieldFactory::new()->macAddress()->create();
$customField = CustomField::factory()->macAddress()->create();
}
if (!$customField->field_encrypted) {
@ -588,9 +582,9 @@ class ImportAssetsTest extends ImportDataTestCase
$customField->save();
}
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$asset = Asset::query()->where('serial', $importFileBuilder->firstRow()['serialNumber'])->sole();

View file

@ -4,19 +4,20 @@ namespace Tests\Feature\Importing\Api;
use App\Models\Actionlog as ActionLog;
use App\Models\Component;
use Database\Factories\ComponentFactory;
use Illuminate\Support\Str;
use Database\Factories\UserFactory;
use Database\Factories\ImportFactory;
use PHPUnit\Framework\Attributes\Test;
use App\Models\Import;
use App\Models\User;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Str;
use Illuminate\Testing\TestResponse;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\Support\Importing\CleansUpImportFiles;
use Tests\Support\Importing\ComponentsImportFileBuilder as ImportFileBuilder;
class ImportComponentsTest extends ImportDataTestCase
class ImportComponentsTest extends ImportDataTestCase implements TestsPermissionsRequirement
{
use CleansUpImportFiles;
use WithFaker;
protected function importFileResponse(array $parameters = []): TestResponse
@ -29,14 +30,9 @@ class ImportComponentsTest extends ImportDataTestCase
}
#[Test]
#[DataProvider('permissionsTestData')]
public function onlyUserWithPermissionCanImportComponents(array|string $permissions): void
public function testRequiresPermission()
{
$permissions = collect((array) $permissions)
->map(fn (string $permission) => [$permission => '1'])
->toJson();
$this->actingAsForApi(UserFactory::new()->create(['permissions' => $permissions]));
$this->actingAsForApi(User::factory()->create());
$this->importFileResponse(['import' => 44])->assertForbidden();
}
@ -44,9 +40,9 @@ class ImportComponentsTest extends ImportDataTestCase
#[Test]
public function userWithImportAssetsPermissionCanImportComponents(): void
{
$this->actingAsForApi(UserFactory::new()->canImport()->create());
$this->actingAsForApi(User::factory()->canImport()->create());
$import = ImportFactory::new()->component()->create();
$import = Import::factory()->component()->create();
$this->importFileResponse(['import' => $import->id])->assertOk();
}
@ -58,9 +54,9 @@ class ImportComponentsTest extends ImportDataTestCase
$importFileBuilder = ImportFileBuilder::new();
$row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])
->assertOk()
->assertExactJson([
@ -79,21 +75,21 @@ class ImportComponentsTest extends ImportDataTestCase
->where('item_id', $newComponent->id)
->sole();
$this->assertEquals($activityLog->action_type, 'create');
$this->assertEquals($activityLog->action_source, 'importer');
$this->assertEquals($activityLog->company_id, $newComponent->company->id);
$this->assertEquals('create', $activityLog->action_type);
$this->assertEquals('importer', $activityLog->action_source);
$this->assertEquals($newComponent->company->id, $activityLog->company_id);
$this->assertEquals($newComponent->name, $row['itemName']);
$this->assertEquals($newComponent->company->name, $row['companyName']);
$this->assertEquals($newComponent->category->name, $row['category']);
$this->assertEquals($newComponent->location->name, $row['location']);
$this->assertEquals($row['itemName'], $newComponent->name);
$this->assertEquals($row['companyName'], $newComponent->company->name);
$this->assertEquals($row['category'], $newComponent->category->name);
$this->assertEquals($row['location'], $newComponent->location->name);
$this->assertNull($newComponent->supplier_id);
$this->assertEquals($newComponent->qty, $row['quantity']);
$this->assertEquals($newComponent->order_number, $row['orderNumber']);
$this->assertEquals($newComponent->purchase_date->toDateString(), $row['purchaseDate']);
$this->assertEquals($newComponent->purchase_cost, $row['purchaseCost']);
$this->assertEquals($row['quantity'], $newComponent->qty);
$this->assertEquals($row['orderNumber'], $newComponent->order_number);
$this->assertEquals($row['purchaseDate'], $newComponent->purchase_date->toDateString());
$this->assertEquals($row['purchaseCost'], $newComponent->purchase_cost);
$this->assertNull($newComponent->min_amt);
$this->assertEquals($newComponent->serial, $row['serialNumber']);
$this->assertEquals($row['serialNumber'], $newComponent->serial);
$this->assertNull($newComponent->image);
$this->assertNull($newComponent->notes);
}
@ -106,9 +102,9 @@ class ImportComponentsTest extends ImportDataTestCase
$importFileBuilder = new ImportFileBuilder([$row]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->importFileResponse(['import' => $import->id])->assertOk();
}
@ -116,16 +112,16 @@ class ImportComponentsTest extends ImportDataTestCase
#[Test]
public function willNotCreateNewComponentWhenComponentWithNameAndSerialNumberExists(): void
{
$component = ComponentFactory::new()->create();
$component = Component::factory()->create();
$importFileBuilder = ImportFileBuilder::times(4)->replace([
'itemName' => $component->name,
'serialNumber' => $component->serial
]);
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$probablyNewComponents = Component::query()
@ -141,9 +137,9 @@ class ImportComponentsTest extends ImportDataTestCase
public function willNotCreateNewCompanyWhenCompanyExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['companyName' => Str::random()]);
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newComponents = Component::query()
@ -157,9 +153,9 @@ class ImportComponentsTest extends ImportDataTestCase
public function willNotCreateNewLocationWhenLocationExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['location' => Str::random()]);
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newComponents = Component::query()
@ -173,9 +169,9 @@ class ImportComponentsTest extends ImportDataTestCase
public function willNotCreateNewCategoryWhenCategoryExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['category' => $this->faker->company]);
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newComponents = Component::query()
@ -193,9 +189,9 @@ class ImportComponentsTest extends ImportDataTestCase
->forget(['quantity']);
$row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])
->assertInternalServerError()
@ -222,16 +218,16 @@ class ImportComponentsTest extends ImportDataTestCase
#[Test]
public function updateComponentFromImport(): void
{
$component = ComponentFactory::new()->create();
$component = Component::factory()->create();
$importFileBuilder = ImportFileBuilder::new([
'itemName' => $component->name,
'serialNumber' => $component->serial
]);
$row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$updatedComponent = Component::query()
@ -239,18 +235,18 @@ class ImportComponentsTest extends ImportDataTestCase
->where('serial', $row['serialNumber'])
->sole();
$this->assertEquals($updatedComponent->name, $row['itemName']);
$this->assertEquals($updatedComponent->category->name, $row['category']);
$this->assertEquals($updatedComponent->location->name, $row['location']);
$this->assertEquals($updatedComponent->supplier_id, $component->supplier_id);
$this->assertEquals($updatedComponent->qty, $row['quantity']);
$this->assertEquals($updatedComponent->order_number, $row['orderNumber']);
$this->assertEquals($updatedComponent->purchase_date->toDateString(), $row['purchaseDate']);
$this->assertEquals($updatedComponent->purchase_cost, $row['purchaseCost']);
$this->assertEquals($updatedComponent->min_amt, $component->min_amt);
$this->assertEquals($updatedComponent->serial, $row['serialNumber']);
$this->assertEquals($updatedComponent->image, $component->image);
$this->assertEquals($updatedComponent->notes, $component->notes);
$this->assertEquals($row['itemName'], $updatedComponent->name);
$this->assertEquals($row['category'], $updatedComponent->category->name);
$this->assertEquals($row['location'], $updatedComponent->location->name);
$this->assertEquals($component->supplier_id, $updatedComponent->supplier_id);
$this->assertEquals($row['quantity'], $updatedComponent->qty);
$this->assertEquals($row['orderNumber'], $updatedComponent->order_number);
$this->assertEquals($row['purchaseDate'], $updatedComponent->purchase_date->toDateString());
$this->assertEquals($row['purchaseCost'], $updatedComponent->purchase_cost);
$this->assertEquals($component->min_amt, $updatedComponent->min_amt);
$this->assertEquals($row['serialNumber'], $updatedComponent->serial);
$this->assertEquals($component->image, $updatedComponent->image);
$this->assertEquals($component->notes, $updatedComponent->notes);
}
#[Test]
@ -270,9 +266,9 @@ class ImportComponentsTest extends ImportDataTestCase
];
$importFileBuilder = new ImportFileBuilder([$row]);
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse([
'import' => $import->id,
@ -294,14 +290,14 @@ class ImportComponentsTest extends ImportDataTestCase
->where('serial', $importFileBuilder->firstRow()['category'])
->sole();
$this->assertEquals($newComponent->name, $row['quantity']);
$this->assertEquals($newComponent->category->name, $row['purchaseCost']);
$this->assertEquals($newComponent->location->name, $row['serialNumber']);
$this->assertEquals($row['quantity'], $newComponent->name);
$this->assertEquals($row['purchaseCost'], $newComponent->category->name);
$this->assertEquals($row['serialNumber'], $newComponent->location->name);
$this->assertNull($newComponent->supplier_id);
$this->assertEquals($newComponent->qty, $row['companyName']);
$this->assertEquals($newComponent->order_number, $row['orderNumber']);
$this->assertEquals($newComponent->purchase_date->toDateString(), $row['itemName']);
$this->assertEquals($newComponent->purchase_cost, $row['location']);
$this->assertEquals($row['companyName'], $newComponent->qty);
$this->assertEquals($row['orderNumber'], $newComponent->order_number);
$this->assertEquals($row['itemName'], $newComponent->purchase_date->toDateString());
$this->assertEquals($row['location'], $newComponent->purchase_cost);
$this->assertNull($newComponent->min_amt);
$this->assertNull($newComponent->image);
$this->assertNull($newComponent->notes);

View file

@ -4,19 +4,20 @@ namespace Tests\Feature\Importing\Api;
use App\Models\Actionlog as ActivityLog;
use App\Models\Consumable;
use Database\Factories\ConsumableFactory;
use Illuminate\Support\Str;
use Database\Factories\UserFactory;
use Database\Factories\ImportFactory;
use PHPUnit\Framework\Attributes\Test;
use App\Models\Import;
use App\Models\User;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Str;
use Illuminate\Testing\TestResponse;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\Support\Importing\CleansUpImportFiles;
use Tests\Support\Importing\ConsumablesImportFileBuilder as ImportFileBuilder;
class ImportConsumablesTest extends ImportDataTestCase
class ImportConsumablesTest extends ImportDataTestCase implements TestsPermissionsRequirement
{
use CleansUpImportFiles;
use WithFaker;
protected function importFileResponse(array $parameters = []): TestResponse
@ -29,14 +30,9 @@ class ImportConsumablesTest extends ImportDataTestCase
}
#[Test]
#[DataProvider('permissionsTestData')]
public function onlyUserWithPermissionCanImportConsumables(array|string $permissions): void
public function testRequiresPermission()
{
$permissions = collect((array) $permissions)
->map(fn (string $permission) => [$permission => '1'])
->toJson();
$this->actingAsForApi(UserFactory::new()->create(['permissions' => $permissions]));
$this->actingAsForApi(User::factory()->create());
$this->importFileResponse(['import' => 44])->assertForbidden();
}
@ -44,9 +40,9 @@ class ImportConsumablesTest extends ImportDataTestCase
#[Test]
public function userWithImportAssetsPermissionCanImportConsumables(): void
{
$this->actingAsForApi(UserFactory::new()->canImport()->create());
$this->actingAsForApi(User::factory()->canImport()->create());
$import = ImportFactory::new()->consumable()->create();
$import = Import::factory()->consumable()->create();
$this->importFileResponse(['import' => $import->id])->assertOk();
}
@ -58,9 +54,9 @@ class ImportConsumablesTest extends ImportDataTestCase
$importFileBuilder = ImportFileBuilder::new();
$row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])
->assertOk()
->assertExactJson([
@ -79,22 +75,22 @@ class ImportConsumablesTest extends ImportDataTestCase
->where('item_id', $newConsumable->id)
->sole();
$this->assertEquals($activityLog->action_type, 'create');
$this->assertEquals($activityLog->action_source, 'importer');
$this->assertEquals($activityLog->company_id, $newConsumable->company->id);
$this->assertEquals('create', $activityLog->action_type);
$this->assertEquals('importer', $activityLog->action_source);
$this->assertEquals($newConsumable->company->id, $activityLog->company_id);
$this->assertEquals($newConsumable->name, $row['itemName']);
$this->assertEquals($newConsumable->category->name, $row['category']);
$this->assertEquals($newConsumable->location->name, $row['location']);
$this->assertEquals($newConsumable->company->name, $row['companyName']);
$this->assertEquals($row['itemName'], $newConsumable->name);
$this->assertEquals($row['category'], $newConsumable->category->name);
$this->assertEquals($row['location'], $newConsumable->location->name);
$this->assertEquals($row['companyName'], $newConsumable->company->name);
$this->assertNull($newConsumable->supplier_id);
$this->assertFalse($newConsumable->requestable);
$this->assertNull($newConsumable->image);
$this->assertEquals($newConsumable->order_number, $row['orderNumber']);
$this->assertEquals($newConsumable->purchase_date->toDateString(), $row['purchaseDate']);
$this->assertEquals($newConsumable->purchase_cost, $row['purchaseCost']);
$this->assertEquals($row['orderNumber'], $newConsumable->order_number);
$this->assertEquals($row['purchaseDate'], $newConsumable->purchase_date->toDateString());
$this->assertEquals($row['purchaseCost'], $newConsumable->purchase_cost);
$this->assertNull($newConsumable->min_amt);
$this->assertEquals($newConsumable->model_number, '');
$this->assertEquals('', $newConsumable->model_number);
$this->assertNull($newConsumable->item_number);
$this->assertNull($newConsumable->manufacturer_id);
$this->assertNull($newConsumable->notes);
@ -108,9 +104,9 @@ class ImportConsumablesTest extends ImportDataTestCase
$importFileBuilder = new ImportFileBuilder([$row]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->importFileResponse(['import' => $import->id])->assertOk();
}
@ -118,11 +114,11 @@ class ImportConsumablesTest extends ImportDataTestCase
#[Test]
public function willNotCreateNewConsumableWhenConsumableNameAlreadyExist(): void
{
$consumable = ConsumableFactory::new()->create(['name' => Str::random()]);
$consumable = Consumable::factory()->create(['name' => Str::random()]);
$importFileBuilder = ImportFileBuilder::new(['itemName' => $consumable->name]);
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$probablyNewConsumables = Consumable::query()
@ -137,9 +133,9 @@ class ImportConsumablesTest extends ImportDataTestCase
public function willNotCreateNewCompanyWhenCompanyExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['companyName' => Str::random()]);
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newConsumables = Consumable::query()
@ -153,9 +149,9 @@ class ImportConsumablesTest extends ImportDataTestCase
public function willNotCreateNewLocationWhenLocationExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['location' => Str::random()]);
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newConsumables = Consumable::query()
@ -169,9 +165,9 @@ class ImportConsumablesTest extends ImportDataTestCase
public function willNotCreateNewCategoryWhenCategoryExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['category' => Str::random()]);
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newConsumables = Consumable::query()
@ -187,9 +183,9 @@ class ImportConsumablesTest extends ImportDataTestCase
$importFileBuilder = ImportFileBuilder::new(['category' => ''])->forget(['quantity', 'name']);
$row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])
->assertInternalServerError()
@ -215,13 +211,13 @@ class ImportConsumablesTest extends ImportDataTestCase
#[Test]
public function updateConsumableFromImport(): void
{
$consumable = ConsumableFactory::new()->create(['name' => Str::random()]);
$consumable = Consumable::factory()->create(['name' => Str::random()]);
$importFileBuilder = ImportFileBuilder::new(['itemName' => $consumable->name]);
$row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$updatedConsumable = Consumable::query()
@ -229,13 +225,13 @@ class ImportConsumablesTest extends ImportDataTestCase
->where('name', $importFileBuilder->firstRow()['itemName'])
->sole();
$this->assertEquals($updatedConsumable->name, $row['itemName']);
$this->assertEquals($updatedConsumable->category->name, $row['category']);
$this->assertEquals($updatedConsumable->location->name, $row['location']);
$this->assertEquals($updatedConsumable->company->name, $row['companyName']);
$this->assertEquals($updatedConsumable->order_number, $row['orderNumber']);
$this->assertEquals($updatedConsumable->purchase_date->toDateString(), $row['purchaseDate']);
$this->assertEquals($updatedConsumable->purchase_cost, $row['purchaseCost']);
$this->assertEquals($row['itemName'], $updatedConsumable->name);
$this->assertEquals($row['category'], $updatedConsumable->category->name);
$this->assertEquals($row['location'], $updatedConsumable->location->name);
$this->assertEquals($row['companyName'], $updatedConsumable->company->name);
$this->assertEquals($row['orderNumber'], $updatedConsumable->order_number);
$this->assertEquals($row['purchaseDate'], $updatedConsumable->purchase_date->toDateString());
$this->assertEquals($row['purchaseCost'], $updatedConsumable->purchase_cost);
$this->assertEquals($consumable->supplier_id, $updatedConsumable->supplier_id);
$this->assertEquals($consumable->requestable, $updatedConsumable->requestable);
@ -265,9 +261,9 @@ class ImportConsumablesTest extends ImportDataTestCase
$importFileBuilder = new ImportFileBuilder([$row]);
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse([
'import' => $import->id,
@ -289,19 +285,19 @@ class ImportConsumablesTest extends ImportDataTestCase
->where('name', $importFileBuilder->firstRow()['quantity'])
->sole();
$this->assertEquals($newConsumable->category->name, $row['supplier']);
$this->assertEquals($newConsumable->location->name, $row['purchaseCost']);
$this->assertEquals($newConsumable->company->name, $row['purchaseDate']);
$this->assertEquals($newConsumable->qty, $row['companyName']);
$this->assertEquals($newConsumable->name, $row['quantity']);
$this->assertEquals($row['supplier'], $newConsumable->category->name);
$this->assertEquals($row['purchaseCost'], $newConsumable->location->name);
$this->assertEquals($row['purchaseDate'], $newConsumable->company->name);
$this->assertEquals($row['companyName'], $newConsumable->qty);
$this->assertEquals($row['quantity'], $newConsumable->name);
$this->assertNull($newConsumable->supplier_id);
$this->assertFalse($newConsumable->requestable);
$this->assertNull($newConsumable->image);
$this->assertEquals($newConsumable->order_number, $row['orderNumber']);
$this->assertEquals($newConsumable->purchase_date->toDateString(), $row['itemName']);
$this->assertEquals($newConsumable->purchase_cost, $row['location']);
$this->assertEquals($row['orderNumber'], $newConsumable->order_number);
$this->assertEquals($row['itemName'], $newConsumable->purchase_date->toDateString());
$this->assertEquals($row['location'], $newConsumable->purchase_cost);
$this->assertNull($newConsumable->min_amt);
$this->assertEquals($newConsumable->model_number, '');
$this->assertEquals('', $newConsumable->model_number);
$this->assertNull($newConsumable->item_number);
$this->assertNull($newConsumable->manufacturer_id);
$this->assertNull($newConsumable->notes);

View file

@ -2,8 +2,8 @@
namespace Tests\Feature\Importing\Api;
use Tests\TestCase;
use Illuminate\Testing\TestResponse;
use Tests\TestCase;
abstract class ImportDataTestCase extends TestCase
{
@ -11,26 +11,4 @@ abstract class ImportDataTestCase extends TestCase
{
return $this->postJson(route('api.imports.importFile', $parameters), $parameters);
}
/**
* @todo Add more permissions.
*/
public static function permissionsTestData(): array
{
return [
'`admin`' => ['admin'],
'`reports.view`' => ['reports.view'],
'only `assets` permission' => [
'assets.view',
'assets.create',
'assets.edit',
'assets.delete',
'assets.checkout',
'assets.checkin',
'assets.audit',
'assets.view.requestable',
'assets.view.encrypted_custom_fields'
]
];
}
}

View file

@ -3,19 +3,20 @@
namespace Tests\Feature\Importing\Api;
use App\Models\Actionlog as ActivityLog;
use App\Models\Import;
use App\Models\License;
use Illuminate\Support\Str;
use Database\Factories\UserFactory;
use Database\Factories\ImportFactory;
use Database\Factories\LicenseFactory;
use PHPUnit\Framework\Attributes\Test;
use App\Models\User;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Str;
use Illuminate\Testing\TestResponse;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\Support\Importing\CleansUpImportFiles;
use Tests\Support\Importing\LicensesImportFileBuilder as ImportFileBuilder;
class ImportLicenseTest extends ImportDataTestCase
class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRequirement
{
use CleansUpImportFiles;
use WithFaker;
protected function importFileResponse(array $parameters = []): TestResponse
@ -28,14 +29,9 @@ class ImportLicenseTest extends ImportDataTestCase
}
#[Test]
#[DataProvider('permissionsTestData')]
public function onlyUserWithPermissionCanImportLicenses(array|string $permissions): void
public function testRequiresPermission()
{
$permissions = collect((array) $permissions)
->map(fn (string $permission) => [$permission => '1'])
->toJson();
$this->actingAsForApi(UserFactory::new()->create(['permissions' => $permissions]));
$this->actingAsForApi(User::factory()->create());
$this->importFileResponse(['import' => 44])->assertForbidden();
}
@ -43,9 +39,9 @@ class ImportLicenseTest extends ImportDataTestCase
#[Test]
public function userWithImportAssetsPermissionCanImportLicenses(): void
{
$this->actingAsForApi(UserFactory::new()->canImport()->create());
$this->actingAsForApi(User::factory()->canImport()->create());
$import = ImportFactory::new()->license()->create();
$import = Import::factory()->license()->create();
$this->importFileResponse(['import' => $import->id])->assertOk();
}
@ -55,9 +51,9 @@ class ImportLicenseTest extends ImportDataTestCase
{
$importFileBuilder = ImportFileBuilder::new();
$row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])
->assertOk()
->assertExactJson([
@ -79,22 +75,22 @@ class ImportLicenseTest extends ImportDataTestCase
$this->assertCount(2, $activityLogs);
$this->assertEquals($newLicense->name, $row['licenseName']);
$this->assertEquals($newLicense->serial, $row['serialNumber']);
$this->assertEquals($newLicense->purchase_date->toDateString(), $row['purchaseDate']);
$this->assertEquals($newLicense->purchase_cost, $row['purchaseCost']);
$this->assertEquals($newLicense->order_number, $row['orderNumber']);
$this->assertEquals($newLicense->seats, $row['seats']);
$this->assertEquals($newLicense->notes, $row['notes']);
$this->assertEquals($newLicense->license_name, $row['licensedToName']);
$this->assertEquals($newLicense->license_email, $row['licensedToEmail']);
$this->assertEquals($newLicense->supplier->name, $row['supplierName']);
$this->assertEquals($newLicense->company->name, $row['companyName']);
$this->assertEquals($newLicense->category->name, $row['category']);
$this->assertEquals($newLicense->expiration_date->toDateString(), $row['expirationDate']);
$this->assertEquals($newLicense->maintained, $row['isMaintained'] === 'TRUE');
$this->assertEquals($newLicense->reassignable, $row['isReassignAble'] === 'TRUE');
$this->assertEquals($newLicense->purchase_order, '');
$this->assertEquals($row['licenseName'], $newLicense->name);
$this->assertEquals($row['serialNumber'], $newLicense->serial);
$this->assertEquals($row['purchaseDate'], $newLicense->purchase_date->toDateString());
$this->assertEquals($row['purchaseCost'], $newLicense->purchase_cost);
$this->assertEquals($row['orderNumber'], $newLicense->order_number);
$this->assertEquals($row['seats'], $newLicense->seats);
$this->assertEquals($row['notes'], $newLicense->notes);
$this->assertEquals($row['licensedToName'], $newLicense->license_name);
$this->assertEquals($row['licensedToEmail'], $newLicense->license_email);
$this->assertEquals($row['supplierName'], $newLicense->supplier->name);
$this->assertEquals($row['companyName'], $newLicense->company->name);
$this->assertEquals($row['category'], $newLicense->category->name);
$this->assertEquals($row['expirationDate'], $newLicense->expiration_date->toDateString());
$this->assertEquals($row['isMaintained'] === 'TRUE', $newLicense->maintained);
$this->assertEquals($row['isReassignAble'] === 'TRUE', $newLicense->reassignable);
$this->assertEquals('', $newLicense->purchase_order);
$this->assertNull($newLicense->depreciation_id);
$this->assertNull($newLicense->termination_date);
$this->assertNull($newLicense->deprecate);
@ -109,9 +105,9 @@ class ImportLicenseTest extends ImportDataTestCase
$importFileBuilder = new ImportFileBuilder([$row]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->importFileResponse(['import' => $import->id])->assertOk();
}
@ -119,16 +115,16 @@ class ImportLicenseTest extends ImportDataTestCase
#[Test]
public function willNotCreateNewLicenseWhenNameAndSerialNumberAlreadyExist(): void
{
$license = LicenseFactory::new()->create();
$license = License::factory()->create();
$importFileBuilder = ImportFileBuilder::times(4)->replace([
'itemName' => $license->name,
'serialNumber' => $license->serial
]);
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$probablyNewLicenses = License::query()
@ -146,25 +142,25 @@ class ImportLicenseTest extends ImportDataTestCase
'expirationDate' => '2022/10/10'
]);
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newLicense = License::query()
->where('serial', $importFileBuilder->firstRow()['serialNumber'])
->sole();
$this->assertEquals($newLicense->expiration_date->toDateString(), '2022-10-10');
$this->assertEquals('2022-10-10', $newLicense->expiration_date->toDateString());
}
#[Test]
public function willNotCreateNewCompanyWhenCompanyExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['companyName' => Str::random()]);
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newLicenses = License::query()
@ -178,9 +174,9 @@ class ImportLicenseTest extends ImportDataTestCase
public function willNotCreateNewManufacturerWhenManufacturerExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['manufacturerName' => Str::random()]);
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newLicenses = License::query()
@ -194,9 +190,9 @@ class ImportLicenseTest extends ImportDataTestCase
public function willNotCreateNewCategoryWhenCategoryExists(): void
{
$importFileBuilder = ImportFileBuilder::times(4)->replace(['category' => $this->faker->company]);
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newLicenses = License::query()
@ -214,9 +210,9 @@ class ImportLicenseTest extends ImportDataTestCase
->forget(['seats']);
$row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])
->assertInternalServerError()
@ -242,16 +238,16 @@ class ImportLicenseTest extends ImportDataTestCase
#[Test]
public function updateLicenseFromImport(): void
{
$license = LicenseFactory::new()->create();
$license = License::factory()->create();
$importFileBuilder = ImportFileBuilder::new([
'licenseName' => $license->name,
'serialNumber' => $license->serial
]);
$row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$updatedLicense = License::query()
@ -259,26 +255,26 @@ class ImportLicenseTest extends ImportDataTestCase
->where('serial', $row['serialNumber'])
->sole();
$this->assertEquals($updatedLicense->name, $row['licenseName']);
$this->assertEquals($updatedLicense->serial, $row['serialNumber']);
$this->assertEquals($updatedLicense->purchase_date->toDateString(), $row['purchaseDate']);
$this->assertEquals($updatedLicense->purchase_cost, $row['purchaseCost']);
$this->assertEquals($updatedLicense->order_number, $row['orderNumber']);
$this->assertEquals($updatedLicense->seats, $row['seats']);
$this->assertEquals($updatedLicense->notes, $row['notes']);
$this->assertEquals($updatedLicense->license_name, $row['licensedToName']);
$this->assertEquals($updatedLicense->license_email, $row['licensedToEmail']);
$this->assertEquals($updatedLicense->supplier->name, $row['supplierName']);
$this->assertEquals($updatedLicense->company->name, $row['companyName']);
$this->assertEquals($updatedLicense->category->name, $row['category']);
$this->assertEquals($updatedLicense->expiration_date->toDateString(), $row['expirationDate']);
$this->assertEquals($updatedLicense->maintained, $row['isMaintained'] === 'TRUE');
$this->assertEquals($updatedLicense->reassignable, $row['isReassignAble'] === 'TRUE');
$this->assertEquals($updatedLicense->purchase_order, $license->purchase_order);
$this->assertEquals($updatedLicense->depreciation_id, $license->depreciation_id);
$this->assertEquals($updatedLicense->termination_date, $license->termination_date);
$this->assertEquals($updatedLicense->deprecate, $license->deprecate);
$this->assertEquals($updatedLicense->min_amt, $license->min_amt);
$this->assertEquals($row['licenseName'], $updatedLicense->name);
$this->assertEquals($row['serialNumber'], $updatedLicense->serial);
$this->assertEquals($row['purchaseDate'], $updatedLicense->purchase_date->toDateString());
$this->assertEquals($row['purchaseCost'], $updatedLicense->purchase_cost);
$this->assertEquals($row['orderNumber'], $updatedLicense->order_number);
$this->assertEquals($row['seats'], $updatedLicense->seats);
$this->assertEquals($row['notes'], $updatedLicense->notes);
$this->assertEquals($row['licensedToName'], $updatedLicense->license_name);
$this->assertEquals($row['licensedToEmail'], $updatedLicense->license_email);
$this->assertEquals($row['supplierName'], $updatedLicense->supplier->name);
$this->assertEquals($row['companyName'], $updatedLicense->company->name);
$this->assertEquals($row['category'], $updatedLicense->category->name);
$this->assertEquals($row['expirationDate'], $updatedLicense->expiration_date->toDateString());
$this->assertEquals($row['isMaintained'] === 'TRUE', $updatedLicense->maintained);
$this->assertEquals($row['isReassignAble'] === 'TRUE', $updatedLicense->reassignable);
$this->assertEquals($license->purchase_order, $updatedLicense->purchase_order);
$this->assertEquals($license->depreciation_id, $updatedLicense->depreciation_id);
$this->assertEquals($license->termination_date, $updatedLicense->termination_date);
$this->assertEquals($license->deprecate, $updatedLicense->deprecate);
$this->assertEquals($license->min_amt, $updatedLicense->min_amt);
}
#[Test]
@ -305,9 +301,9 @@ class ImportLicenseTest extends ImportDataTestCase
];
$importFileBuilder = new ImportFileBuilder([$row]);
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse([
'import' => $import->id,
@ -336,22 +332,22 @@ class ImportLicenseTest extends ImportDataTestCase
->where('serial', $row['companyName'])
->sole();
$this->assertEquals($newLicense->name, $row['licenseName']);
$this->assertEquals($newLicense->serial, $row['companyName']);
$this->assertEquals($newLicense->purchase_date->toDateString(), $row['isMaintained']);
$this->assertEquals($newLicense->purchase_cost, $row['isReassignAble']);
$this->assertEquals($newLicense->order_number, $row['licensedToName']);
$this->assertEquals($newLicense->seats, $row['expirationDate']);
$this->assertEquals($newLicense->notes, $row['licensedToEmail']);
$this->assertEquals($newLicense->license_name, $row['seats']);
$this->assertEquals($newLicense->license_email, $row['serialNumber']);
$this->assertEquals($newLicense->supplier->name, $row['category']);
$this->assertEquals($newLicense->company->name, $row['notes']);
$this->assertEquals($newLicense->category->name, $row['manufacturerName']);
$this->assertEquals($newLicense->expiration_date->toDateString(), $row['orderNumber']);
$this->assertEquals($newLicense->maintained, $row['purchaseCost'] === 'TRUE');
$this->assertEquals($newLicense->reassignable, $row['purchaseDate'] === 'TRUE');
$this->assertEquals($newLicense->purchase_order, '');
$this->assertEquals($row['licenseName'], $newLicense->name);
$this->assertEquals($row['companyName'], $newLicense->serial);
$this->assertEquals($row['isMaintained'], $newLicense->purchase_date->toDateString());
$this->assertEquals($row['isReassignAble'], $newLicense->purchase_cost);
$this->assertEquals($row['licensedToName'], $newLicense->order_number);
$this->assertEquals($row['expirationDate'], $newLicense->seats);
$this->assertEquals($row['licensedToEmail'], $newLicense->notes);
$this->assertEquals($row['seats'], $newLicense->license_name);
$this->assertEquals($row['serialNumber'], $newLicense->license_email);
$this->assertEquals($row['category'], $newLicense->supplier->name);
$this->assertEquals($row['notes'], $newLicense->company->name);
$this->assertEquals($row['manufacturerName'], $newLicense->category->name);
$this->assertEquals($row['orderNumber'], $newLicense->expiration_date->toDateString());
$this->assertEquals($row['purchaseCost'] === 'TRUE', $newLicense->maintained);
$this->assertEquals($row['purchaseDate'] === 'TRUE', $newLicense->reassignable);
$this->assertEquals('', $newLicense->purchase_order);
$this->assertNull($newLicense->depreciation_id);
$this->assertNull($newLicense->termination_date);
$this->assertNull($newLicense->deprecate);

View file

@ -2,23 +2,24 @@
namespace Tests\Feature\Importing\Api;
use App\Models\Asset;
use App\Models\Import;
use App\Models\Location;
use App\Models\User;
use Database\Factories\AssetFactory;
use Illuminate\Support\Str;
use Database\Factories\UserFactory;
use Database\Factories\ImportFactory;
use PHPUnit\Framework\Attributes\Test;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Str;
use Illuminate\Testing\TestResponse;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\Support\Importing\CleansUpImportFiles;
use Tests\Support\Importing\UsersImportFileBuilder as ImportFileBuilder;
class ImportUsersTest extends ImportDataTestCase
class ImportUsersTest extends ImportDataTestCase implements TestsPermissionsRequirement
{
use CleansUpImportFiles;
use WithFaker;
protected function importFileResponse(array $parameters = []): TestResponse
@ -31,14 +32,9 @@ class ImportUsersTest extends ImportDataTestCase
}
#[Test]
#[DataProvider('permissionsTestData')]
public function onlyUserWithPermissionCanImportUsers(array|string $permissions): void
public function testRequiresPermission()
{
$permissions = collect((array) $permissions)
->map(fn (string $permission) => [$permission => '1'])
->toJson();
$this->actingAsForApi(UserFactory::new()->create(['permissions' => $permissions]));
$this->actingAsForApi(User::factory()->create());
$this->importFileResponse(['import' => 44])->assertForbidden();
}
@ -46,9 +42,9 @@ class ImportUsersTest extends ImportDataTestCase
#[Test]
public function userWithImportAssetsPermissionCanImportUsers(): void
{
$this->actingAsForApi(UserFactory::new()->canImport()->create());
$this->actingAsForApi(User::factory()->canImport()->create());
$import = ImportFactory::new()->users()->create();
$import = Import::factory()->users()->create();
$this->importFileResponse(['import' => $import->id])->assertOk();
}
@ -60,9 +56,9 @@ class ImportUsersTest extends ImportDataTestCase
$importFileBuilder = ImportFileBuilder::new();
$row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id, 'send-welcome' => 1])
->assertOk()
->assertExactJson([
@ -78,21 +74,21 @@ class ImportUsersTest extends ImportDataTestCase
Notification::assertNothingSent();
$this->assertEquals($newUser->email, $row['email']);
$this->assertEquals($newUser->first_name, $row['firstName']);
$this->assertEquals($newUser->last_name, $row['lastName']);
$this->assertEquals($newUser->employee_num, $row['employeeNumber']);
$this->assertEquals($newUser->company->name, $row['companyName']);
$this->assertEquals($newUser->location->name, $row['location']);
$this->assertEquals($newUser->phone, $row['phoneNumber']);
$this->assertEquals($newUser->jobtitle, $row['position']);
$this->assertEquals($row['email'], $newUser->email);
$this->assertEquals($row['firstName'], $newUser->first_name);
$this->assertEquals($row['lastName'], $newUser->last_name);
$this->assertEquals($row['employeeNumber'], $newUser->employee_num);
$this->assertEquals($row['companyName'], $newUser->company->name);
$this->assertEquals($row['location'], $newUser->location->name);
$this->assertEquals($row['phoneNumber'], $newUser->phone);
$this->assertEquals($row['position'], $newUser->jobtitle);
$this->assertTrue(Hash::isHashed($newUser->password));
$this->assertEquals($newUser->website, '');
$this->assertEquals($newUser->country, '');
$this->assertEquals($newUser->address, '');
$this->assertEquals($newUser->city, '');
$this->assertEquals($newUser->state, '');
$this->assertEquals($newUser->zip, '');
$this->assertEquals('', $newUser->website);
$this->assertEquals('', $newUser->country);
$this->assertEquals('', $newUser->address);
$this->assertEquals('', $newUser->city);
$this->assertEquals('', $newUser->state);
$this->assertEquals('', $newUser->zip);
$this->assertNull($newUser->permissions);
$this->assertNull($newUser->avatar);
$this->assertNull($newUser->notes);
@ -100,15 +96,15 @@ class ImportUsersTest extends ImportDataTestCase
$this->assertNull($newUser->department_id);
$this->assertNull($newUser->two_factor_secret);
$this->assertNull($newUser->idap_import);
$this->assertEquals($newUser->locale, 'en-US');
$this->assertEquals($newUser->show_in_list, 1);
$this->assertEquals($newUser->two_factor_enrolled, 0);
$this->assertEquals($newUser->two_factor_optin, 0);
$this->assertEquals($newUser->remote, 0);
$this->assertEquals($newUser->autoassign_licenses, 0);
$this->assertEquals($newUser->vip, 0);
$this->assertEquals($newUser->enable_sounds, 0);
$this->assertEquals($newUser->enable_confetti, 0);
$this->assertEquals('en-US', $newUser->locale);
$this->assertEquals(1, $newUser->show_in_list);
$this->assertEquals(0, $newUser->two_factor_enrolled);
$this->assertEquals(0, $newUser->two_factor_optin);
$this->assertEquals(0, $newUser->remote);
$this->assertEquals(0, $newUser->autoassign_licenses);
$this->assertEquals(0, $newUser->vip);
$this->assertEquals(0, $newUser->enable_sounds);
$this->assertEquals(0, $newUser->enable_confetti);
$this->assertNull($newUser->created_by);
$this->assertNull($newUser->start_date);
$this->assertNull($newUser->end_date);
@ -118,7 +114,7 @@ class ImportUsersTest extends ImportDataTestCase
$this->assertNull($newUser->last_login);
$this->assertNull($newUser->persist_code);
$this->assertNull($newUser->reset_password_code);
$this->assertEquals($newUser->activated, 0);
$this->assertEquals(0, $newUser->activated);
}
#[Test]
@ -129,9 +125,9 @@ class ImportUsersTest extends ImportDataTestCase
$importFileBuilder = new ImportFileBuilder([$row]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$import = ImportFactory::new()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->importFileResponse(['import' => $import->id])->assertOk();
}
@ -139,11 +135,11 @@ class ImportUsersTest extends ImportDataTestCase
#[Test]
public function willNotCreateNewUserWhenUserWithUserNameAlreadyExist(): void
{
$user = UserFactory::new()->create(['username' => Str::random()]);
$user = User::factory()->create(['username' => Str::random()]);
$importFileBuilder = ImportFileBuilder::times(4)->replace(['username' => $user->username]);
$import = ImportFactory::new()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$probablyNewUsers = User::query()
@ -158,9 +154,9 @@ class ImportUsersTest extends ImportDataTestCase
{
$importFileBuilder = ImportFileBuilder::new()->forget('username');
$row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newUser = User::query()
@ -169,25 +165,25 @@ class ImportUsersTest extends ImportDataTestCase
$generatedUsername = User::generateFormattedNameFromFullName("{$row['firstName']} {$row['lastName']}")['username'];
$this->assertEquals($newUser->username, $generatedUsername);
$this->assertEquals($generatedUsername, $newUser->username);
}
#[Test]
public function willUpdateLocationOfAllAssetsAssignedToUser(): void
{
$user = UserFactory::new()->create(['username' => Str::random()]);
$assetsAssignedToUser = AssetFactory::new()->create(['assigned_to' => $user->id, 'assigned_type' => User::class]);
$user = User::factory()->create(['username' => Str::random()]);
$assetsAssignedToUser = Asset::factory()->create(['assigned_to' => $user->id, 'assigned_type' => User::class]);
$importFileBuilder = ImportFileBuilder::new(['username' => $user->username]);
$import = ImportFactory::new()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$userLocation = Location::query()->where('name', $importFileBuilder->firstRow()['location'])->sole(['id']);
$this->assertEquals(
$assetsAssignedToUser->refresh()->location_id,
$userLocation->id
$userLocation->id,
$assetsAssignedToUser->refresh()->location_id
);
}
@ -195,9 +191,9 @@ class ImportUsersTest extends ImportDataTestCase
public function whenRequiredColumnsAreMissingInImportFile(): void
{
$importFileBuilder = ImportFileBuilder::new(['firstName' => ''])->forget(['username']);
$import = ImportFactory::new()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])
->assertInternalServerError()
@ -223,13 +219,13 @@ class ImportUsersTest extends ImportDataTestCase
#[Test]
public function updateUserFromImport(): void
{
$user = UserFactory::new()->create(['username' => Str::random()])->refresh();
$user = User::factory()->create(['username' => Str::random()])->refresh();
$importFileBuilder = ImportFileBuilder::new(['username' => $user->username]);
$row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$updatedUser = User::query()->with(['company', 'location'])->find($user->id);
@ -238,19 +234,19 @@ class ImportUsersTest extends ImportDataTestCase
'location_id', 'company_id', 'updated_at', 'phone', 'jobtitle'
];
$this->assertEquals($updatedUser->email, $row['email']);
$this->assertEquals($updatedUser->first_name, $row['firstName']);
$this->assertEquals($updatedUser->last_name, $row['lastName']);
$this->assertEquals($updatedUser->employee_num, $row['employeeNumber']);
$this->assertEquals($updatedUser->company->name, $row['companyName']);
$this->assertEquals($updatedUser->location->name, $row['location']);
$this->assertEquals($updatedUser->phone, $row['phoneNumber']);
$this->assertEquals($updatedUser->jobtitle, $row['position']);
$this->assertEquals($row['email'], $updatedUser->email);
$this->assertEquals($row['firstName'], $updatedUser->first_name);
$this->assertEquals($row['lastName'], $updatedUser->last_name);
$this->assertEquals($row['employeeNumber'], $updatedUser->employee_num);
$this->assertEquals($row['companyName'], $updatedUser->company->name);
$this->assertEquals($row['location'], $updatedUser->location->name);
$this->assertEquals($row['phoneNumber'], $updatedUser->phone);
$this->assertEquals($row['position'], $updatedUser->jobtitle);
$this->assertTrue(Hash::isHashed($updatedUser->password));
$this->assertEquals(
Arr::except($updatedUser->attributesToArray(), $updatedAttributes),
Arr::except($user->attributesToArray(), $updatedAttributes),
Arr::except($updatedUser->attributesToArray(), $updatedAttributes),
);
}
@ -271,9 +267,9 @@ class ImportUsersTest extends ImportDataTestCase
];
$importFileBuilder = new ImportFileBuilder([$row]);
$import = ImportFactory::new()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create());
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse([
'import' => $import->id,
@ -295,21 +291,21 @@ class ImportUsersTest extends ImportDataTestCase
->where('username', $row['companyName'])
->sole();
$this->assertEquals($newUser->email, $row['position']);
$this->assertEquals($newUser->first_name, $row['location']);
$this->assertEquals($newUser->last_name, $row['lastName']);
$this->assertEquals($newUser->jobtitle, $row['email']);
$this->assertEquals($newUser->employee_num, $row['phoneNumber']);
$this->assertEquals($newUser->company->name, $row['username']);
$this->assertEquals($newUser->location->name, $row['firstName']);
$this->assertEquals($newUser->phone, $row['employeeNumber']);
$this->assertEquals($row['position'], $newUser->email);
$this->assertEquals($row['location'], $newUser->first_name);
$this->assertEquals($row['lastName'], $newUser->last_name);
$this->assertEquals($row['email'], $newUser->jobtitle);
$this->assertEquals($row['phoneNumber'], $newUser->employee_num);
$this->assertEquals($row['username'], $newUser->company->name);
$this->assertEquals($row['firstName'], $newUser->location->name);
$this->assertEquals($row['employeeNumber'], $newUser->phone);
$this->assertTrue(Hash::isHashed($newUser->password));
$this->assertEquals($newUser->website, '');
$this->assertEquals($newUser->country, '');
$this->assertEquals($newUser->address, '');
$this->assertEquals($newUser->city, '');
$this->assertEquals($newUser->state, '');
$this->assertEquals($newUser->zip, '');
$this->assertEquals('', $newUser->website);
$this->assertEquals('', $newUser->country);
$this->assertEquals('', $newUser->address);
$this->assertEquals('', $newUser->city);
$this->assertEquals('', $newUser->state);
$this->assertEquals('', $newUser->zip);
$this->assertNull($newUser->permissions);
$this->assertNull($newUser->avatar);
$this->assertNull($newUser->notes);
@ -317,15 +313,15 @@ class ImportUsersTest extends ImportDataTestCase
$this->assertNull($newUser->department_id);
$this->assertNull($newUser->two_factor_secret);
$this->assertNull($newUser->idap_import);
$this->assertEquals($newUser->locale, 'en-US');
$this->assertEquals($newUser->show_in_list, 1);
$this->assertEquals($newUser->two_factor_enrolled, 0);
$this->assertEquals($newUser->two_factor_optin, 0);
$this->assertEquals($newUser->remote, 0);
$this->assertEquals($newUser->autoassign_licenses, 0);
$this->assertEquals($newUser->vip, 0);
$this->assertEquals($newUser->enable_sounds, 0);
$this->assertEquals($newUser->enable_confetti, 0);
$this->assertEquals('en-US', $newUser->locale);
$this->assertEquals(1, $newUser->show_in_list);
$this->assertEquals(0, $newUser->two_factor_enrolled);
$this->assertEquals(0, $newUser->two_factor_optin);
$this->assertEquals(0, $newUser->remote);
$this->assertEquals(0, $newUser->autoassign_licenses);
$this->assertEquals(0, $newUser->vip);
$this->assertEquals(0, $newUser->enable_sounds);
$this->assertEquals(0, $newUser->enable_confetti);
$this->assertNull($newUser->created_by);
$this->assertNull($newUser->start_date);
$this->assertNull($newUser->end_date);
@ -335,6 +331,6 @@ class ImportUsersTest extends ImportDataTestCase
$this->assertNull($newUser->last_login);
$this->assertNull($newUser->persist_code);
$this->assertNull($newUser->reset_password_code);
$this->assertEquals($newUser->activated, 0);
$this->assertEquals(0, $newUser->activated);
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Tests\Support\Importing;
use App\Models\Import;
use Illuminate\Support\Facades\Storage;
trait CleansUpImportFiles
{
public function setUp(): void
{
parent::setUp();
Import::created(function (Import $import) {
$this->beforeApplicationDestroyed(function () use ($import) {
Storage::delete('private_uploads/imports/' . $import->file_path);
});
});
}
}