Skip to content

Commit 0984194

Browse files
authored
Merge pull request #15965 from marcusmoore/testing/consumable-ui
Added Consumable UI tests
2 parents dc5dedd + 26264e1 commit 0984194

File tree

4 files changed

+208
-2
lines changed

4 files changed

+208
-2
lines changed

app/Http/Controllers/Consumables/ConsumablesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function create()
5050
{
5151
$this->authorize('create', Consumable::class);
5252

53-
return view('consumables/edit')->with('category_type', 'consumable')
53+
return view('consumables.edit')->with('category_type', 'consumable')
5454
->with('item', new Consumable);
5555
}
5656

tests/Feature/Consumables/Ui/CreateConsumableTest.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,66 @@
22

33
namespace Tests\Feature\Consumables\Ui;
44

5+
use App\Models\Category;
6+
use App\Models\Company;
7+
use App\Models\Location;
8+
use App\Models\Manufacturer;
9+
use App\Models\Supplier;
510
use App\Models\User;
11+
use Tests\Concerns\TestsPermissionsRequirement;
612
use Tests\TestCase;
713

8-
class CreateConsumableTest extends TestCase
14+
class CreateConsumableTest extends TestCase implements TestsPermissionsRequirement
915
{
16+
public function testRequiresPermission()
17+
{
18+
$this->actingAs(User::factory()->create())
19+
->get(route('consumables.create'))
20+
->assertForbidden();
21+
}
22+
23+
public function testCanRenderCreateConsumablePage()
24+
{
25+
$this->actingAs(User::factory()->createConsumables()->create())
26+
->get(route('consumables.create'))
27+
->assertOk()
28+
->assertViewIs('consumables.edit');
29+
}
30+
31+
public function testCanCreateConsumable()
32+
{
33+
$data = [
34+
'company_id' => Company::factory()->create()->id,
35+
'name' => 'My Consumable',
36+
'category_id' => Category::factory()->consumableInkCategory()->create()->id,
37+
'supplier_id' => Supplier::factory()->create()->id,
38+
'manufacturer_id' => Manufacturer::factory()->create()->id,
39+
'location_id' => Location::factory()->create()->id,
40+
'model_number' => '1234',
41+
'item_no' => '5678',
42+
'order_number' => '908',
43+
'purchase_date' => '2024-12-05',
44+
'purchase_cost' => '89.45',
45+
'qty' => '10',
46+
'min_amt' => '1',
47+
'notes' => 'Some Notes',
48+
];
49+
50+
$this->actingAs(User::factory()->createConsumables()->create())
51+
->post(route('consumables.store'), $data + [
52+
'redirect_option' => 'index',
53+
'category_type' => 'consumable',
54+
])
55+
->assertRedirect(route('consumables.index'));
56+
57+
$this->assertDatabaseHas('consumables', $data);
58+
}
59+
1060
public function testPageRenders()
1161
{
1262
$this->actingAs(User::factory()->superuser()->create())
1363
->get(route('consumables.create'))
1464
->assertOk();
65+
1566
}
1667
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Tests\Feature\Consumables\Ui;
4+
5+
use App\Models\Company;
6+
use App\Models\Consumable;
7+
use App\Models\User;
8+
use Tests\TestCase;
9+
10+
class DeleteConsumableTest extends TestCase
11+
{
12+
public function testRequiresPermissionToDeleteConsumable()
13+
{
14+
$this->actingAs(User::factory()->create())
15+
->delete(route('consumables.destroy', Consumable::factory()->create()->id))
16+
->assertForbidden();
17+
}
18+
19+
public function testCannotDeleteConsumableFromAnotherCompany()
20+
{
21+
$this->settings->enableMultipleFullCompanySupport();
22+
23+
[$companyA, $companyB] = Company::factory()->count(2)->create();
24+
25+
$consumableForCompanyA = Consumable::factory()->for($companyA)->create();
26+
$userForCompanyB = User::factory()->deleteConsumables()->for($companyB)->create();
27+
28+
$this->actingAs($userForCompanyB)
29+
->delete(route('consumables.destroy', $consumableForCompanyA->id))
30+
->assertRedirect(route('consumables.index'));
31+
32+
$this->assertNotSoftDeleted($consumableForCompanyA);
33+
}
34+
35+
public function testCanDeleteConsumable()
36+
{
37+
$consumable = Consumable::factory()->create();
38+
39+
$this->actingAs(User::factory()->deleteConsumables()->create())
40+
->delete(route('consumables.destroy', $consumable->id))
41+
->assertRedirect(route('consumables.index'));
42+
43+
$this->assertSoftDeleted($consumable);
44+
}
45+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

Comments
 (0)