diff --git a/app/Http/Controllers/Consumables/ConsumablesController.php b/app/Http/Controllers/Consumables/ConsumablesController.php index 98141f27832f..47a9ad71b43a 100644 --- a/app/Http/Controllers/Consumables/ConsumablesController.php +++ b/app/Http/Controllers/Consumables/ConsumablesController.php @@ -50,7 +50,7 @@ public function create() { $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.php new file mode 100644 index 000000000000..75e11fdca376 --- /dev/null +++ b/tests/Feature/Consumables/Ui/CreateConsumableTest.php @@ -0,0 +1,59 @@ +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.php new file mode 100644 index 000000000000..4406ed530a8a --- /dev/null +++ b/tests/Feature/Consumables/Ui/DeleteConsumableTest.php @@ -0,0 +1,45 @@ +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.php new file mode 100644 index 000000000000..66fc9a11f316 --- /dev/null +++ b/tests/Feature/Consumables/Ui/UpdateConsumableTest.php @@ -0,0 +1,110 @@ +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); + } +}