A free open source IT asset/license management system
Add consumable ui tests
Marcus Moored763feb8035a
diff --git a/app/Http/Controllers/Consumables/ConsumablesController.php b/app/Http/Controllers/Consumables/ConsumablesController.phpindex 98141f2783..47a9ad71b4 100644--- a/app/Http/Controllers/Consumables/ConsumablesController.php+++ b/app/Http/Controllers/Consumables/ConsumablesController.php@@ -50,7 +50,7 @@ class ConsumablesController extends Controller{$this->authorize('create', Consumable::class);- return view('consumables/edit')->with('category_type', 'consumable')+ return view('consumables.edit')->with('category_type', 'consumable')->with('item', new Consumable);}diff --git a/tests/Feature/Consumables/Ui/CreateConsumableTest.php b/tests/Feature/Consumables/Ui/CreateConsumableTest.phpnew file mode 100644index 0000000000..75e11fdca3--- /dev/null+++ b/tests/Feature/Consumables/Ui/CreateConsumableTest.php@@ -0,0 +1,59 @@+<?php++namespace Tests\Feature\Consumables\Ui;++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\Concerns\TestsPermissionsRequirement;+use Tests\TestCase;++class CreateConsumableTest extends TestCase implements TestsPermissionsRequirement+{+ public function testRequiresPermission()+ {+ $this->actingAs(User::factory()->create())+ ->get(route('consumables.create'))+ ->assertForbidden();+ }++ public function testCanRenderCreateConsumablePage()+ {+ $this->actingAs(User::factory()->createConsumables()->create())+ ->get(route('consumables.create'))+ ->assertOk()+ ->assertViewIs('consumables.edit');+ }++ public function testCanCreateConsumable()+ {+ $data = [+ 'company_id' => Company::factory()->create()->id,+ 'name' => 'My Consumable',+ 'category_id' => Category::factory()->consumableInkCategory()->create()->id,+ 'supplier_id' => Supplier::factory()->create()->id,+ 'manufacturer_id' => Manufacturer::factory()->create()->id,+ 'location_id' => Location::factory()->create()->id,+ 'model_number' => '1234',+ 'item_no' => '5678',+ 'order_number' => '908',+ 'purchase_date' => '2024-12-05',+ 'purchase_cost' => '89.45',+ 'qty' => '10',+ 'min_amt' => '1',+ 'notes' => 'Some Notes',+ ];++ $this->actingAs(User::factory()->createConsumables()->create())+ ->post(route('consumables.store'), $data + [+ 'redirect_option' => 'index',+ 'category_type' => 'consumable',+ ])+ ->assertRedirect(route('consumables.index'));++ $this->assertDatabaseHas('consumables', $data);+ }+}diff --git a/tests/Feature/Consumables/Ui/DeleteConsumableTest.php b/tests/Feature/Consumables/Ui/DeleteConsumableTest.phpnew file mode 100644index 0000000000..4406ed530a--- /dev/null+++ b/tests/Feature/Consumables/Ui/DeleteConsumableTest.php@@ -0,0 +1,45 @@+<?php++namespace Tests\Feature\Consumables\Ui;++use App\Models\Company;+use App\Models\Consumable;+use App\Models\User;+use Tests\TestCase;++class DeleteConsumableTest extends TestCase+{+ public function testRequiresPermissionToDeleteConsumable()+ {+ $this->actingAs(User::factory()->create())+ ->delete(route('consumables.destroy', Consumable::factory()->create()->id))+ ->assertForbidden();+ }++ public function testCannotDeleteConsumableFromAnotherCompany()+ {+ $this->settings->enableMultipleFullCompanySupport();++ [$companyA, $companyB] = Company::factory()->count(2)->create();++ $consumableForCompanyA = Consumable::factory()->for($companyA)->create();+ $userForCompanyB = User::factory()->deleteConsumables()->for($companyB)->create();++ $this->actingAs($userForCompanyB)+ ->delete(route('consumables.destroy', $consumableForCompanyA->id))+ ->assertRedirect(route('consumables.index'));++ $this->assertNotSoftDeleted($consumableForCompanyA);+ }++ public function testCanDeleteConsumable()+ {+ $consumable = Consumable::factory()->create();++ $this->actingAs(User::factory()->deleteConsumables()->create())+ ->delete(route('consumables.destroy', $consumable->id))+ ->assertRedirect(route('consumables.index'));++ $this->assertSoftDeleted($consumable);+ }+}diff --git a/tests/Feature/Consumables/Ui/UpdateConsumableTest.php b/tests/Feature/Consumables/Ui/UpdateConsumableTest.phpnew file mode 100644index 0000000000..66fc9a11f3--- /dev/null+++ b/tests/Feature/Consumables/Ui/UpdateConsumableTest.php@@ -0,0 +1,110 @@+<?php++namespace Tests\Feature\Consumables\Ui;++use App\Models\Category;+use App\Models\Company;+use App\Models\Consumable;+use App\Models\Location;+use App\Models\Manufacturer;+use App\Models\Supplier;+use App\Models\User;+use Tests\TestCase;++class UpdateConsumableTest extends TestCase+{+ public function testRequiresPermissionToSeeEditConsumablePage()+ {+ $this->actingAs(User::factory()->create())+ ->get(route('consumables.edit', Consumable::factory()->create()))+ ->assertForbidden();+ }++ public function testDoesNotShowEditConsumablePageFromAnotherCompany()+ {+ $this->settings->enableMultipleFullCompanySupport();++ [$companyA, $companyB] = Company::factory()->count(2)->create();+ $consumableForCompanyA = Consumable::factory()->for($companyA)->create();+ $userForCompanyB = User::factory()->editConsumables()->for($companyB)->create();++ $this->actingAs($userForCompanyB)+ ->get(route('consumables.edit', $consumableForCompanyA->id))+ ->assertRedirect(route('consumables.index'));+ }++ public function testEditConsumablePageRenders()+ {+ $this->actingAs(User::factory()->editConsumables()->create())+ ->get(route('consumables.edit', Consumable::factory()->create()))+ ->assertOk()+ ->assertViewIs('consumables.edit');+ }++ public function testCannotUpdateConsumableBelongingToAnotherCompany()+ {+ $this->settings->enableMultipleFullCompanySupport();++ [$companyA, $companyB] = Company::factory()->count(2)->create();++ $consumableForCompanyA = Consumable::factory()->for($companyA)->create();+ $userForCompanyB = User::factory()->editConsumables()->for($companyB)->create();++ $this->actingAs($userForCompanyB)+ ->put(route('consumables.update', $consumableForCompanyA->id), [+ //+ ])+ ->assertForbidden();+ }++ public function testCannotSetQuantityToAmountLowerThanWhatIsCheckedOut()+ {+ $user = User::factory()->createConsumables()->editConsumables()->create();+ $consumable = Consumable::factory()->create(['qty' => 2]);++ $consumable->users()->attach($consumable->id, ['consumable_id' => $consumable->id, 'assigned_to' => $user->id]);+ $consumable->users()->attach($consumable->id, ['consumable_id' => $consumable->id, 'assigned_to' => $user->id]);++ $this->assertEquals(2, $consumable->numCheckedOut());++ $this->actingAs($user)+ ->put(route('consumables.update', $consumable->id), [+ 'qty' => 1,+ 'redirect_option' => 'index',+ 'category_type' => 'consumable',+ ])+ ->assertSessionHasErrors('qty');++ }++ public function testCanUpdateConsumable()+ {+ $consumable = Consumable::factory()->create();++ $data = [+ 'company_id' => Company::factory()->create()->id,+ 'name' => 'My Consumable',+ 'category_id' => Category::factory()->consumableInkCategory()->create()->id,+ 'supplier_id' => Supplier::factory()->create()->id,+ 'manufacturer_id' => Manufacturer::factory()->create()->id,+ 'location_id' => Location::factory()->create()->id,+ 'model_number' => '8765',+ 'item_no' => '5678',+ 'order_number' => '908',+ 'purchase_date' => '2024-12-05',+ 'purchase_cost' => '89.45',+ 'qty' => '9',+ 'min_amt' => '7',+ 'notes' => 'Some Notes',+ ];++ $this->actingAs(User::factory()->createConsumables()->editConsumables()->create())+ ->put(route('consumables.update', $consumable->id), $data + [+ 'redirect_option' => 'index',+ 'category_type' => 'consumable',+ ])+ ->assertRedirect(route('consumables.index'));++ $this->assertDatabaseHas('consumables', $data);+ }+}