Skip to content

A free open source IT asset/license management system

Added tests and test support

snipe13fd43c45ca4
diff --git a/tests/Feature/Importing/Api/ImportLocationsTest.php b/tests/Feature/Importing/Api/ImportLocationsTest.php
new file mode 100644
index 0000000000..88eeb5fa18
--- /dev/null
+++ b/tests/Feature/Importing/Api/ImportLocationsTest.php
@@ -0,0 +1,136 @@
+<?php
+
+namespace Tests\Feature\Importing\Api;
+
+use App\Models\Location;
+use App\Models\User;
+use App\Models\Import;
+use Illuminate\Foundation\Testing\WithFaker;
+use Illuminate\Support\Arr;
+use Illuminate\Testing\TestResponse;
+use PHPUnit\Framework\Attributes\Test;
+use Tests\Concerns\TestsPermissionsRequirement;
+use Tests\Support\Importing\CleansUpImportFiles;
+use Tests\Support\Importing\LocationsImportFileBuilder as ImportFileBuilder;
+
+class ImportLocationsTest extends ImportDataTestCase implements TestsPermissionsRequirement
+{
+ use CleansUpImportFiles;
+ use WithFaker;
+
+ protected function importFileResponse(array $parameters = []): TestResponse
+ {
+ if (!array_key_exists('import-type', $parameters)) {
+ $parameters['import-type'] = 'location';
+ }
+
+ return parent::importFileResponse($parameters);
+ }
+
+ #[Test]
+ public function testRequiresPermission()
+ {
+ $this->actingAsForApi(User::factory()->create());
+
+ $this->importFileResponse(['import' => 44])->assertForbidden();
+ }
+
+ #[Test]
+ public function importLocation(): void
+ {
+ $importFileBuilder = ImportFileBuilder::new();
+ $row = $importFileBuilder->firstRow();
+ $import = Import::factory()->locations()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create());
+ $this->importFileResponse(['import' => $import->id, 'send-welcome' => 0])
+ ->assertOk()
+ ->assertExactJson([
+ 'payload' => null,
+ 'status' => 'success',
+ 'messages' => ['redirect_url' => route('locations.index')]
+ ]);
+
+ $newLocation = Location::query()
+ ->where('name', $row['name'])
+ ->sole();
+
+ $this->assertEquals($row['name'], $newLocation->name);
+
+ }
+
+ #[Test]
+ public function willIgnoreUnknownColumnsWhenFileContainsUnknownColumns(): void
+ {
+ $row = ImportFileBuilder::new()->definition();
+ $row['unknownColumnInCsvFile'] = 'foo';
+
+ $importFileBuilder = new ImportFileBuilder([$row]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create());
+
+ $import = Import::factory()->locations()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
+
+ $this->importFileResponse(['import' => $import->id])->assertOk();
+ }
+
+
+ #[Test]
+ public function whenRequiredColumnsAreMissingInImportFile(): void
+ {
+ $importFileBuilder = ImportFileBuilder::new(['name' => '']);
+ $import = Import::factory()->locations()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create());
+
+ $this->importFileResponse(['import' => $import->id])
+ ->assertInternalServerError()
+ ->assertExactJson([
+ 'status' => 'import-errors',
+ 'payload' => null,
+ 'messages' => [
+ '' => [
+ 'Location ""' => [
+ 'name' =>
+ ['The name field is required.'],
+ ],
+ ]
+
+ ]
+ ]);
+
+ $newLocation = Location::query()
+ ->where('name', $importFileBuilder->firstRow()['name'])
+ ->get();
+
+ $this->assertCount(0, $newLocation);
+ }
+
+
+ #[Test]
+ public function updateLocationFromImport(): void
+ {
+ $location = Location::factory()->create()->refresh();
+ $importFileBuilder = ImportFileBuilder::new(['name' => $location->name, 'phone' => $location->phone]);
+
+ $row = $importFileBuilder->firstRow();
+ $import = Import::factory()->locations()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create());
+ $this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
+
+ $updatedLocation = Location::query()->find($location->id);
+ $updatedAttributes = [
+ 'name',
+ 'phone',
+ ];
+
+ $this->assertEquals($row['name'], $updatedLocation->name);
+
+ $this->assertEquals(
+ Arr::except($location->attributesToArray(), array_merge($updatedAttributes, $location->getDates())),
+ Arr::except($updatedLocation->attributesToArray(), array_merge($updatedAttributes, $location->getDates())),
+ );
+ }
+
+}
diff --git a/tests/Feature/Importing/Api/ImportSuppliersTest.php b/tests/Feature/Importing/Api/ImportSuppliersTest.php
new file mode 100644
index 0000000000..c9d0b9c72e
--- /dev/null
+++ b/tests/Feature/Importing/Api/ImportSuppliersTest.php
@@ -0,0 +1,141 @@
+<?php
+
+namespace Tests\Feature\Importing\Api;
+
+use App\Models\Supplier;
+use App\Models\User;
+use App\Models\Import;
+use Illuminate\Foundation\Testing\WithFaker;
+use Illuminate\Support\Arr;
+use Illuminate\Support\Str;
+use Illuminate\Testing\TestResponse;
+use PHPUnit\Framework\Attributes\Test;
+use Tests\Concerns\TestsPermissionsRequirement;
+use Tests\Support\Importing\CleansUpImportFiles;
+use Tests\Support\Importing\SuppliersImportFileBuilder as ImportFileBuilder;
+
+class ImportSuppliersTest extends ImportDataTestCase implements TestsPermissionsRequirement
+{
+ use CleansUpImportFiles;
+ use WithFaker;
+
+ protected function importFileResponse(array $parameters = []): TestResponse
+ {
+ if (!array_key_exists('import-type', $parameters)) {
+ $parameters['import-type'] = 'supplier';
+ }
+
+ return parent::importFileResponse($parameters);
+ }
+
+ #[Test]
+ public function testRequiresPermission()
+ {
+ $this->actingAsForApi(User::factory()->create());
+
+ $this->importFileResponse(['import' => 44])->assertForbidden();
+ }
+
+ #[Test]
+ public function importSupplier(): void
+ {
+ $importFileBuilder = ImportFileBuilder::new();
+ $row = $importFileBuilder->firstRow();
+ $import = Import::factory()->suppliers()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create());
+ $this->importFileResponse(['import' => $import->id, 'send-welcome' => 0])
+ ->assertOk()
+ ->assertExactJson([
+ 'payload' => null,
+ 'status' => 'success',
+ 'messages' => ['redirect_url' => route('suppliers.index')]
+ ]);
+
+ $newSupplier = Supplier::query()
+ ->where('name', $row['name'])
+ ->sole();
+
+ $this->assertEquals($row['name'], $newSupplier->name);
+
+ }
+
+ #[Test]
+ public function willIgnoreUnknownColumnsWhenFileContainsUnknownColumns(): void
+ {
+ $row = ImportFileBuilder::new()->definition();
+ $row['unknownColumnInCsvFile'] = 'foo';
+
+ $importFileBuilder = new ImportFileBuilder([$row]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create());
+
+ $import = Import::factory()->suppliers()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
+
+ $this->importFileResponse(['import' => $import->id])->assertOk();
+ }
+
+
+ #[Test]
+ public function whenRequiredColumnsAreMissingInImportFile(): void
+ {
+ $importFileBuilder = ImportFileBuilder::new(['name' => '']);
+ $import = Import::factory()->suppliers()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create());
+
+ $this->importFileResponse(['import' => $import->id])
+ ->assertInternalServerError()
+ ->assertExactJson([
+ 'status' => 'import-errors',
+ 'payload' => null,
+ 'messages' => [
+ '' => [
+ 'Supplier ""' => [
+ 'name' =>
+ ['The name field is required.'],
+ ],
+ ]
+
+ ]
+ ]);
+
+ $newSupplier = Supplier::query()
+ ->where('name', $importFileBuilder->firstRow()['name'])
+ ->get();
+
+ $this->assertCount(0, $newSupplier);
+ }
+
+
+ #[Test]
+ public function updateSupplierFromImport(): void
+ {
+ $supplier = Supplier::factory()->create()->refresh();
+ $importFileBuilder = ImportFileBuilder::new(['name' => $supplier->name, 'url' => $supplier->url, 'phone' => $supplier->phone, 'fax' => $supplier->fax, 'contact' => $supplier->contact, 'email' => $supplier->email]);
+
+ $row = $importFileBuilder->firstRow();
+ $import = Import::factory()->suppliers()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create());
+ $this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
+
+ $updatedSupplier = Supplier::query()->find($supplier->id);
+ $updatedAttributes = [
+ 'name',
+ 'url',
+ 'phone',
+ 'fax',
+ 'contact',
+ 'email',
+ ];
+
+ $this->assertEquals($row['name'], $updatedSupplier->name);
+
+ $this->assertEquals(
+ Arr::except($supplier->attributesToArray(), array_merge($updatedAttributes, $supplier->getDates())),
+ Arr::except($updatedSupplier->attributesToArray(), array_merge($updatedAttributes, $supplier->getDates())),
+ );
+ }
+
+}
diff --git a/tests/Support/Importing/LocationsImportFileBuilder.php b/tests/Support/Importing/LocationsImportFileBuilder.php
new file mode 100644
index 0000000000..59b7dcc416
--- /dev/null
+++ b/tests/Support/Importing/LocationsImportFileBuilder.php
@@ -0,0 +1,58 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Tests\Support\Importing;
+
+use Illuminate\Support\Str;
+
+/**
+ * Build a users import file at runtime for testing.
+ *
+ * @template Row of array{
+ * companyName?: string,
+ * email?: string,
+ * employeeNumber?: int,
+ * firstName?: string,
+ * lastName?: string,
+ * location?: string,
+ * phoneNumber?: string,
+ * position?: string,
+ * username?: string,
+ * }
+ *
+ * @extends FileBuilder<Row>
+ */
+class LocationsImportFileBuilder extends FileBuilder
+{
+ /**
+ * @inheritdoc
+ */
+ protected function getDictionary(): array
+ {
+ return [
+ 'name' => 'name',
+ 'phone' => 'Phone',
+ 'address' => 'address',
+ 'address2' => 'address2',
+ 'city' => 'city',
+ 'state' => 'state',
+ 'country' => 'country',
+ 'zip' => 'zip',
+ 'notes' => 'notes',
+ ];
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function definition(): array
+ {
+ $faker = fake();
+
+ return [
+ 'name' => $faker->company,
+ 'phone' => $faker->phoneNumber,
+ ];
+ }
+}
diff --git a/tests/Support/Importing/SuppliersImportFileBuilder.php b/tests/Support/Importing/SuppliersImportFileBuilder.php
new file mode 100644
index 0000000000..7666174993
--- /dev/null
+++ b/tests/Support/Importing/SuppliersImportFileBuilder.php
@@ -0,0 +1,66 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Tests\Support\Importing;
+
+use Illuminate\Support\Str;
+
+/**
+ * Build a users import file at runtime for testing.
+ *
+ * @template Row of array{
+ * companyName?: string,
+ * email?: string,
+ * employeeNumber?: int,
+ * firstName?: string,
+ * lastName?: string,
+ * location?: string,
+ * phoneNumber?: string,
+ * position?: string,
+ * username?: string,
+ * }
+ *
+ * @extends FileBuilder<Row>
+ */
+class SuppliersImportFileBuilder extends FileBuilder
+{
+ /**
+ * @inheritdoc
+ */
+ protected function getDictionary(): array
+ {
+ return [
+ 'name' => 'name',
+ 'email' => 'email',
+ 'contact' => 'contact',
+ 'phone' => 'Phone',
+ 'address' => 'address',
+ 'address2' => 'address2',
+ 'city' => 'city',
+ 'state' => 'state',
+ 'country' => 'country',
+ 'zip' => 'zip',
+ 'notes' => 'notes',
+ 'fax' => 'fax',
+ 'url' => 'url',
+ ];
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function definition(): array
+ {
+ $faker = fake();
+
+ return [
+ 'name' => $faker->company,
+ 'email' => Str::random(32) . "@{$faker->freeEmailDomain}",
+ 'contact' => $faker->firstName,
+ 'phone' => $faker->phoneNumber,
+ 'fax' => $faker->phoneNumber,
+ 'url' => $faker->url(),
+ ];
+ }
+}