Skip to content

Commit e906d25

Browse files
authored
Merge pull request #16973 from spencerrlongg/bug/sc-29245
Adds Form Request for Creating Departments
2 parents 5d8905c + 1b397cd commit e906d25

File tree

4 files changed

+113
-13
lines changed

4 files changed

+113
-13
lines changed

app/Http/Controllers/Api/DepartmentsController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Helpers\Helper;
66
use App\Http\Controllers\Controller;
7+
use App\Http\Requests\StoreDepartmentRequest;
78
use App\Http\Transformers\DepartmentsTransformer;
89
use App\Http\Transformers\SelectlistTransformer;
910
use App\Models\Department;
@@ -94,11 +95,10 @@ public function index(Request $request) : JsonResponse | array
9495
* @since [v4.0]
9596
* @param \App\Http\Requests\ImageUploadRequest $request
9697
*/
97-
public function store(ImageUploadRequest $request) : JsonResponse
98+
public function store(StoreDepartmentRequest $request): JsonResponse
9899
{
99-
$this->authorize('create', Department::class);
100100
$department = new Department;
101-
$department->fill($request->all());
101+
$department->fill($request->validated());
102102
$department = $request->handleImages($department);
103103

104104
$department->created_by = auth()->id();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use App\Models\Department;
6+
use Illuminate\Foundation\Http\FormRequest;
7+
use Illuminate\Support\Facades\Gate;
8+
9+
class StoreDepartmentRequest extends ImageUploadRequest
10+
{
11+
/**
12+
* Determine if the user is authorized to make this request.
13+
*/
14+
public function authorize(): bool
15+
{
16+
return Gate::allows('create', new Department);
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
23+
*/
24+
public function rules(): array
25+
{
26+
$modelRules = (new Department)->getRules();
27+
28+
return array_merge(
29+
$modelRules,
30+
);
31+
}
32+
}

app/Models/Department.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ class Department extends SnipeModel
3131
];
3232

3333
protected $rules = [
34-
'name' => 'required|max:255|is_unique_department',
35-
'location_id' => 'numeric|nullable',
36-
'company_id' => 'numeric|nullable',
37-
'manager_id' => 'numeric|nullable',
34+
'name' => 'required|max:255|is_unique_department',
35+
'location_id' => 'numeric|nullable|exists:locations,id',
36+
'company_id' => 'numeric|nullable|exists:companies,id',
37+
'manager_id' => 'numeric|nullable|exists:users,id',
38+
'phone' => 'string|max:255|nullable',
39+
'fax' => 'string|max:255|nullable',
40+
'notes' => 'string|max:255|nullable',
3841
];
3942

4043
/**

tests/Feature/Departments/Api/CreateDepartmentsTest.php

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Tests\Feature\Departments\Api;
44

55
use App\Models\AssetModel;
6+
use App\Models\Company;
67
use App\Models\Department;
78
use App\Models\Category;
9+
use App\Models\Location;
810
use App\Models\User;
911
use Illuminate\Testing\Fluent\AssertableJson;
1012
use Tests\TestCase;
@@ -13,30 +15,93 @@ class CreateDepartmentsTest extends TestCase
1315
{
1416

1517

16-
public function testRequiresPermissionToCreateDepartment()
18+
public function test_requires_permission_to_create_department()
1719
{
1820
$this->actingAsForApi(User::factory()->create())
1921
->postJson(route('api.departments.store'))
2022
->assertForbidden();
2123
}
2224

23-
public function testCanCreateDepartment()
25+
public function test_can_create_department_with_all_fields()
2426
{
25-
$response = $this->actingAsForApi(User::factory()->superuser()->create())
27+
$company = Company::factory()->create();
28+
$location = Location::factory()->create();
29+
$manager = User::factory()->create();
30+
$user = User::factory()->superuser()->create();
31+
$response = $this->actingAsForApi($user)
2632
->postJson(route('api.departments.store'), [
27-
'name' => 'Test Department',
28-
'notes' => 'Test Note',
33+
'name' => 'Test Department',
34+
'company_id' => $company->id,
35+
'location_id' => $location->id,
36+
'manager_id' => $manager->id,
37+
'notes' => 'Test Note',
38+
'phone' => '1234567890',
39+
'fax' => '1234567890',
2940
])
3041
->assertOk()
3142
->assertStatusMessageIs('success')
32-
->assertStatus(200)
3343
->json();
3444

3545
$this->assertTrue(Department::where('name', 'Test Department')->exists());
3646

3747
$department = Department::find($response['payload']['id']);
3848
$this->assertEquals('Test Department', $department->name);
3949
$this->assertEquals('Test Note', $department->notes);
50+
51+
$this->assertDatabaseHas('departments', [
52+
'name' => 'Test Department',
53+
'company_id' => $company->id,
54+
'location_id' => $location->id,
55+
'manager_id' => $manager->id,
56+
'notes' => 'Test Note',
57+
'phone' => '1234567890',
58+
'fax' => '1234567890',
59+
'created_by' => $user->id,
60+
]);
61+
}
62+
63+
public function test_name_required_for_department()
64+
{
65+
$response = $this->actingAsForApi(User::factory()->superuser()->create())
66+
->postJson(route('api.departments.store'), [
67+
'company_id' => Company::factory()->create()->id,
68+
])
69+
->assertOk()
70+
->assertStatusMessageIs('error');
71+
}
72+
73+
public function test_only_name_required_for_department()
74+
{
75+
$response = $this->actingAsForApi(User::factory()->superuser()->create())
76+
->postJson(route('api.departments.store'), [
77+
'name' => 'Test Department',
78+
])
79+
->assertOk()
80+
->assertStatusMessageIs('success');
4081
}
4182

83+
public function test_arrays_not_allowed_for_numeric_fields()
84+
{
85+
$response = $this->actingAsForApi(User::factory()->superuser()->create())
86+
->postJson(route('api.departments.store'), [
87+
'name' => 'Test Department',
88+
'company_id' => [1, 2],
89+
])
90+
->assertOk()
91+
->assertStatusMessageIs('error');
92+
}
93+
94+
public function test_arrays_of_strings_are_not_allowed_for_numeric_fields()
95+
{
96+
$response = $this->actingAsForApi(User::factory()->superuser()->create())
97+
->postJson(route('api.departments.store'), [
98+
'name' => 'Test Department',
99+
'company_id' => ['1', '2'],
100+
])
101+
->assertOk()
102+
->assertStatusMessageIs('error');
103+
}
104+
105+
106+
42107
}

0 commit comments

Comments
 (0)