Skip to content

A free open source IT asset/license management system

Add accessory ui tests

Marcus Moored3bfa752518f
diff --git a/app/Http/Controllers/Accessories/AccessoriesController.php b/app/Http/Controllers/Accessories/AccessoriesController.php
index 8c66c9a3b2..f7ddd53940 100755
--- a/app/Http/Controllers/Accessories/AccessoriesController.php
+++ b/app/Http/Controllers/Accessories/AccessoriesController.php
@@ -31,7 +31,7 @@ class AccessoriesController extends Controller
public function index() : View
{
$this->authorize('index', Accessory::class);
- return view('accessories/index');
+ return view('accessories.index');
}
/**
@@ -100,7 +100,7 @@ class AccessoriesController extends Controller
if ($item = Accessory::find($accessoryId)) {
$this->authorize($item);
- return view('accessories/edit', compact('item'))->with('category_type', 'accessory');
+ return view('accessories.edit', compact('item'))->with('category_type', 'accessory');
}
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.does_not_exist'));
@@ -236,7 +236,7 @@ class AccessoriesController extends Controller
$accessory = Accessory::withCount('checkouts as checkouts_count')->find($accessoryID);
$this->authorize('view', $accessory);
if (isset($accessory->id)) {
- return view('accessories/view', compact('accessory'));
+ return view('accessories.view', compact('accessory'));
}
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.does_not_exist', ['id' => $accessoryID]));
diff --git a/tests/Feature/Accessories/Ui/AccessoriesIndexTest.php b/tests/Feature/Accessories/Ui/AccessoriesIndexTest.php
index ea355f9a9d..3b98fb4cd3 100644
--- a/tests/Feature/Accessories/Ui/AccessoriesIndexTest.php
+++ b/tests/Feature/Accessories/Ui/AccessoriesIndexTest.php
@@ -3,14 +3,23 @@
namespace Tests\Feature\Accessories\Ui;
use App\Models\User;
+use Tests\Concerns\TestsPermissionsRequirement;
use Tests\TestCase;
-class AccessoriesIndexTest extends TestCase
+class AccessoriesIndexTest extends TestCase implements TestsPermissionsRequirement
{
- public function testPermissionRequiredToViewAccessoryList()
+ public function testRequiresPermission()
{
$this->actingAs(User::factory()->create())
->get(route('accessories.index'))
->assertForbidden();
}
+
+ public function testRendersAccessoriesIndexPage()
+ {
+ $this->actingAs(User::factory()->viewAccessories()->create())
+ ->get(route('accessories.index'))
+ ->assertOk()
+ ->assertViewIs('accessories.index');
+ }
}
diff --git a/tests/Feature/Accessories/Ui/DeleteAccessoryTest.php b/tests/Feature/Accessories/Ui/DeleteAccessoryTest.php
new file mode 100644
index 0000000000..80cf960911
--- /dev/null
+++ b/tests/Feature/Accessories/Ui/DeleteAccessoryTest.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace Tests\Feature\Accessories\Ui;
+
+use App\Models\Accessory;
+use App\Models\Company;
+use App\Models\User;
+use Tests\TestCase;
+
+class DeleteAccessoryTest extends TestCase
+{
+ public function testRequiresPermissionToDeleteAccessory()
+ {
+ $this->actingAs(User::factory()->create())
+ ->delete(route('accessories.destroy', Accessory::factory()->create()->id))
+ ->assertForbidden();
+ }
+
+ public function testCannotDeleteAccessoryFromAnotherCompany()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $accessoryForCompanyA = Accessory::factory()->for($companyA)->create();
+ $userForCompanyB = User::factory()->for($companyB)->deleteAccessories()->create();
+
+ $this->actingAs($userForCompanyB)->delete(route('accessories.destroy', $accessoryForCompanyA->id));
+
+ $this->assertFalse($accessoryForCompanyA->refresh()->trashed(), 'Accessory should not be deleted');
+ }
+
+ public function testCannotDeleteAccessoryThatHasCheckouts()
+ {
+ $accessory = Accessory::factory()->checkedOutToUser()->create();
+
+ $this->actingAs(User::factory()->deleteAccessories()->create())
+ ->delete(route('accessories.destroy', $accessory->id))
+ ->assertSessionHas('error')
+ ->assertRedirect(route('accessories.index'));
+
+ $this->assertFalse($accessory->refresh()->trashed(), 'Accessory should not be deleted');
+ }
+
+ public function testCanDeleteAccessory()
+ {
+
+ $accessory = Accessory::factory()->create();
+
+ $this->actingAs(User::factory()->deleteAccessories()->create())
+ ->delete(route('accessories.destroy', $accessory->id))
+ ->assertRedirect(route('accessories.index'));
+
+ $this->assertTrue($accessory->refresh()->trashed(), 'Accessory should be deleted');
+ }
+}
diff --git a/tests/Feature/Accessories/Ui/ShowAccessoryTest.php b/tests/Feature/Accessories/Ui/ShowAccessoryTest.php
new file mode 100644
index 0000000000..e75ac05a9f
--- /dev/null
+++ b/tests/Feature/Accessories/Ui/ShowAccessoryTest.php
@@ -0,0 +1,42 @@
+<?php
+
+namespace Tests\Feature\Accessories\Ui;
+
+use App\Models\Accessory;
+use App\Models\Company;
+use App\Models\User;
+use Tests\TestCase;
+
+class ShowAccessoryTest extends TestCase
+{
+ public function testRequiresPermissionToViewAccessory()
+ {
+ $this->actingAs(User::factory()->create())
+ ->get(route('accessories.show', Accessory::factory()->create()->id))
+ ->assertForbidden();
+ }
+
+ public function testCannotViewAccessoryFromAnotherCompany()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $accessoryForCompanyA = Accessory::factory()->for($companyA)->create();
+ $userForCompanyB = User::factory()->for($companyB)->viewAccessories()->create();
+
+ $this->actingAs($userForCompanyB)
+ ->get(route('accessories.show', $accessoryForCompanyA->id))
+ ->assertForbidden();
+ }
+
+ public function testCanViewAccessory()
+ {
+ $accessory = Accessory::factory()->create();
+
+ $this->actingAs(User::factory()->viewAccessories()->create())
+ ->get(route('accessories.show', $accessory->id))
+ ->assertOk()
+ ->assertViewIs('accessories.view')
+ ->assertViewHas(['accessory' => $accessory]);
+ }
+}
diff --git a/tests/Feature/Accessories/Ui/UpdateAccessoryTest.php b/tests/Feature/Accessories/Ui/UpdateAccessoryTest.php
new file mode 100644
index 0000000000..1c6fe8a496
--- /dev/null
+++ b/tests/Feature/Accessories/Ui/UpdateAccessoryTest.php
@@ -0,0 +1,127 @@
+<?php
+
+namespace Tests\Feature\Accessories\Ui;
+
+use App\Models\Accessory;
+use App\Models\Category;
+use App\Models\Company;
+use App\Models\Location;
+use App\Models\Manufacturer;
+use App\Models\Supplier;
+use App\Models\User;
+use Tests\TestCase;
+
+class UpdateAccessoryTest extends TestCase
+{
+ public function testRequiresPermissionToSeeEditAccessoryPage()
+ {
+ $this->actingAs(User::factory()->create())
+ ->get(route('accessories.edit', Accessory::factory()->create()->id))
+ ->assertForbidden();
+ }
+
+ public function testEditAccessoryPageRenders()
+ {
+ $this->actingAs(User::factory()->editAccessories()->create())
+ ->get(route('accessories.edit', Accessory::factory()->create()->id))
+ ->assertOk()
+ ->assertViewIs('accessories.edit');
+ }
+
+ public function testDoesNotShowEditAccessoryPageFromAnotherCompany()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $accessoryForCompanyA = Accessory::factory()->for($companyA)->create();
+ $userForCompanyB = User::factory()->for($companyB)->editAccessories()->create();
+
+ $this->actingAs($userForCompanyB)
+ ->get(route('accessories.edit', $accessoryForCompanyA->id))
+ ->assertRedirect(route('accessories.index'));
+ }
+
+ public function testCannotSetQuantityToAmountLowerThanWhatIsCheckedOut()
+ {
+ $accessory = Accessory::factory()->create(['qty' => 2]);
+ $accessory->checkouts()->create(['assigned_to' => User::factory()->create()->id, 'qty' => 1]);
+ $accessory->checkouts()->create(['assigned_to' => User::factory()->create()->id, 'qty' => 1]);
+
+ $this->assertEquals(2, $accessory->checkouts->count());
+
+ $this->actingAs(User::factory()->editAccessories()->create())
+ ->put(route('accessories.update', $accessory), [
+ 'redirect_option' => 'index',
+ 'company_id' => (string) $accessory->company_id,
+ 'name' => $accessory->name,
+ 'category_id' => (string) $accessory->category_id,
+ 'supplier_id' => (string) $accessory->supplier_id,
+ 'manufacturer_id' => (string) $accessory->manufacturer_id,
+ 'location_id' => (string) $accessory->location_id,
+ 'model_number' => $accessory->model_number,
+ 'order_number' => $accessory->order_number,
+ 'purchase_date' => $accessory->purchase_date,
+ 'purchase_cost' => $accessory->purchase_cost,
+ 'min_amt' => $accessory->min_amt,
+ 'notes' => $accessory->notes,
+ // the important part...
+ // try to lower the qty to 1 when there are 2 checked out
+ 'qty' => '1',
+ ]);
+ }
+
+ public function testCanUpdateAccessory()
+ {
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ [$categoryA, $categoryB] = Category::factory()->count(2)->create();
+ [$supplierA, $supplierB] = Supplier::factory()->count(2)->create();
+ [$manufacturerA, $manufacturerB] = Manufacturer::factory()->count(2)->create();
+ [$locationA, $locationB] = Location::factory()->count(2)->create();
+
+ $accessory = Accessory::factory()
+ ->for($companyA)
+ ->for($categoryA)
+ ->for($supplierA)
+ ->for($manufacturerA)
+ ->for($locationA)
+ ->create([
+ 'min_amt' => 1,
+ 'qty' => 5
+ ]);
+
+ $this->actingAs(User::factory()->editAccessories()->create())
+ ->put(route('accessories.update', $accessory), [
+ 'redirect_option' => 'index',
+ 'company_id' => (string) $companyB->id,
+ 'name' => 'Changed Name',
+ 'category_id' => (string) $categoryB->id,
+ 'supplier_id' => (string) $supplierB->id,
+ 'manufacturer_id' => (string) $manufacturerB->id,
+ 'location_id' => (string) $locationB->id,
+ 'model_number' => 'changed 1234',
+ 'order_number' => 'changed 5678',
+ 'purchase_date' => '2024-10-11',
+ 'purchase_cost' => '83.52',
+ 'qty' => '7',
+ 'min_amt' => '10',
+ 'notes' => 'A new note',
+ ])
+ ->assertRedirect(route('accessories.index'));
+
+ $this->assertDatabaseHas('accessories', [
+ 'company_id' => $companyB->id,
+ 'name' => 'Changed Name',
+ 'category_id' => $categoryB->id,
+ 'supplier_id' => $supplierB->id,
+ 'manufacturer_id' => $manufacturerB->id,
+ 'location_id' => $locationB->id,
+ 'model_number' => 'changed 1234',
+ 'order_number' => 'changed 5678',
+ 'purchase_date' => '2024-10-11',
+ 'purchase_cost' => '83.52',
+ 'qty' => '7',
+ 'min_amt' => '10',
+ 'notes' => 'A new note',
+ ]);
+ }
+}