|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Consumables\Ui; |
| 4 | + |
| 5 | +use App\Models\Category; |
| 6 | +use App\Models\Company; |
| 7 | +use App\Models\Consumable; |
| 8 | +use App\Models\Location; |
| 9 | +use App\Models\Manufacturer; |
| 10 | +use App\Models\Supplier; |
| 11 | +use App\Models\User; |
| 12 | +use Tests\TestCase; |
| 13 | + |
| 14 | +class UpdateConsumableTest extends TestCase |
| 15 | +{ |
| 16 | + public function testRequiresPermissionToSeeEditConsumablePage() |
| 17 | + { |
| 18 | + $this->actingAs(User::factory()->create()) |
| 19 | + ->get(route('consumables.edit', Consumable::factory()->create())) |
| 20 | + ->assertForbidden(); |
| 21 | + } |
| 22 | + |
| 23 | + public function testDoesNotShowEditConsumablePageFromAnotherCompany() |
| 24 | + { |
| 25 | + $this->settings->enableMultipleFullCompanySupport(); |
| 26 | + |
| 27 | + [$companyA, $companyB] = Company::factory()->count(2)->create(); |
| 28 | + $consumableForCompanyA = Consumable::factory()->for($companyA)->create(); |
| 29 | + $userForCompanyB = User::factory()->editConsumables()->for($companyB)->create(); |
| 30 | + |
| 31 | + $this->actingAs($userForCompanyB) |
| 32 | + ->get(route('consumables.edit', $consumableForCompanyA->id)) |
| 33 | + ->assertRedirect(route('consumables.index')); |
| 34 | + } |
| 35 | + |
| 36 | + public function testEditConsumablePageRenders() |
| 37 | + { |
| 38 | + $this->actingAs(User::factory()->editConsumables()->create()) |
| 39 | + ->get(route('consumables.edit', Consumable::factory()->create())) |
| 40 | + ->assertOk() |
| 41 | + ->assertViewIs('consumables.edit'); |
| 42 | + } |
| 43 | + |
| 44 | + public function testCannotUpdateConsumableBelongingToAnotherCompany() |
| 45 | + { |
| 46 | + $this->settings->enableMultipleFullCompanySupport(); |
| 47 | + |
| 48 | + [$companyA, $companyB] = Company::factory()->count(2)->create(); |
| 49 | + |
| 50 | + $consumableForCompanyA = Consumable::factory()->for($companyA)->create(); |
| 51 | + $userForCompanyB = User::factory()->editConsumables()->for($companyB)->create(); |
| 52 | + |
| 53 | + $this->actingAs($userForCompanyB) |
| 54 | + ->put(route('consumables.update', $consumableForCompanyA->id), [ |
| 55 | + // |
| 56 | + ]) |
| 57 | + ->assertForbidden(); |
| 58 | + } |
| 59 | + |
| 60 | + public function testCannotSetQuantityToAmountLowerThanWhatIsCheckedOut() |
| 61 | + { |
| 62 | + $user = User::factory()->createConsumables()->editConsumables()->create(); |
| 63 | + $consumable = Consumable::factory()->create(['qty' => 2]); |
| 64 | + |
| 65 | + $consumable->users()->attach($consumable->id, ['consumable_id' => $consumable->id, 'assigned_to' => $user->id]); |
| 66 | + $consumable->users()->attach($consumable->id, ['consumable_id' => $consumable->id, 'assigned_to' => $user->id]); |
| 67 | + |
| 68 | + $this->assertEquals(2, $consumable->numCheckedOut()); |
| 69 | + |
| 70 | + $this->actingAs($user) |
| 71 | + ->put(route('consumables.update', $consumable->id), [ |
| 72 | + 'qty' => 1, |
| 73 | + 'redirect_option' => 'index', |
| 74 | + 'category_type' => 'consumable', |
| 75 | + ]) |
| 76 | + ->assertSessionHasErrors('qty'); |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + public function testCanUpdateConsumable() |
| 81 | + { |
| 82 | + $consumable = Consumable::factory()->create(); |
| 83 | + |
| 84 | + $data = [ |
| 85 | + 'company_id' => Company::factory()->create()->id, |
| 86 | + 'name' => 'My Consumable', |
| 87 | + 'category_id' => Category::factory()->consumableInkCategory()->create()->id, |
| 88 | + 'supplier_id' => Supplier::factory()->create()->id, |
| 89 | + 'manufacturer_id' => Manufacturer::factory()->create()->id, |
| 90 | + 'location_id' => Location::factory()->create()->id, |
| 91 | + 'model_number' => '8765', |
| 92 | + 'item_no' => '5678', |
| 93 | + 'order_number' => '908', |
| 94 | + 'purchase_date' => '2024-12-05', |
| 95 | + 'purchase_cost' => '89.45', |
| 96 | + 'qty' => '9', |
| 97 | + 'min_amt' => '7', |
| 98 | + 'notes' => 'Some Notes', |
| 99 | + ]; |
| 100 | + |
| 101 | + $this->actingAs(User::factory()->createConsumables()->editConsumables()->create()) |
| 102 | + ->put(route('consumables.update', $consumable->id), $data + [ |
| 103 | + 'redirect_option' => 'index', |
| 104 | + 'category_type' => 'consumable', |
| 105 | + ]) |
| 106 | + ->assertRedirect(route('consumables.index')); |
| 107 | + |
| 108 | + $this->assertDatabaseHas('consumables', $data); |
| 109 | + } |
| 110 | +} |
0 commit comments