Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Accessory UI tests #15964

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/Http/Controllers/Accessories/AccessoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down Expand Up @@ -100,7 +100,7 @@ public function edit($accessoryId = null) : View | RedirectResponse

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'));
Expand Down Expand Up @@ -236,7 +236,7 @@ public function show($accessoryID = null) : View | RedirectResponse
$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]));
Expand Down
14 changes: 12 additions & 2 deletions tests/Feature/Accessories/Ui/AccessoriesIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@
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');
}

public function testPageRenders()
{
$this->actingAs(User::factory()->superuser()->create())
Expand Down
55 changes: 55 additions & 0 deletions tests/Feature/Accessories/Ui/DeleteAccessoryTest.php
Original file line number Diff line number Diff line change
@@ -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');
}
}
33 changes: 33 additions & 0 deletions tests/Feature/Accessories/Ui/ShowAccessoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,48 @@
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]);
}

public function testPageRenders()
{
$this->actingAs(User::factory()->superuser()->create())
->get(route('accessories.show', Accessory::factory()->create()->id))
->assertOk();

}
}
127 changes: 127 additions & 0 deletions tests/Feature/Accessories/Ui/UpdateAccessoryTest.php
Original file line number Diff line number Diff line change
@@ -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',
]);
}
}
Loading