From 58fabe2704c64289ea371d20257a53f5fb9a5308 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 10 Mar 2024 13:01:30 +1100 Subject: [PATCH] Validation for project budgeted hours --- .../Requests/Project/StoreProjectRequest.php | 4 + .../Requests/Project/UpdateProjectRequest.php | 6 + tests/Feature/ProjectApiTest.php | 106 ++++++++++++++++++ tests/MockAccountData.php | 5 + 4 files changed, 121 insertions(+) diff --git a/app/Http/Requests/Project/StoreProjectRequest.php b/app/Http/Requests/Project/StoreProjectRequest.php index 8f8e4760df0c..3bc1bca1bc52 100644 --- a/app/Http/Requests/Project/StoreProjectRequest.php +++ b/app/Http/Requests/Project/StoreProjectRequest.php @@ -44,6 +44,7 @@ class StoreProjectRequest extends Request $rules['name'] = 'required'; $rules['client_id'] = 'required|exists:clients,id,company_id,'.$user->company()->id; + $rules['budgeted_hours'] = 'sometimes|numeric'; if (isset($this->number)) { $rules['number'] = Rule::unique('projects')->where('company_id', $user->company()->id); @@ -74,6 +75,9 @@ class StoreProjectRequest extends Request $input['color'] = ''; } + if(array_key_exists('budgeted_hours', $input) && empty($input['budgeted_hours'])) + $input['budgeted_hours'] = 0; + $this->replace($input); } diff --git a/app/Http/Requests/Project/UpdateProjectRequest.php b/app/Http/Requests/Project/UpdateProjectRequest.php index e68c90383790..cbcf6882cf89 100644 --- a/app/Http/Requests/Project/UpdateProjectRequest.php +++ b/app/Http/Requests/Project/UpdateProjectRequest.php @@ -45,6 +45,8 @@ class UpdateProjectRequest extends Request $rules['number'] = Rule::unique('projects')->where('company_id', $user->company()->id)->ignore($this->project->id); } + $rules['budgeted_hours'] = 'sometimes|numeric'; + if ($this->file('documents') && is_array($this->file('documents'))) { $rules['documents.*'] = $this->file_validation; } elseif ($this->file('documents')) { @@ -73,6 +75,10 @@ class UpdateProjectRequest extends Request if (array_key_exists('color', $input) && is_null($input['color'])) { $input['color'] = ''; } + + if(array_key_exists('budgeted_hours', $input) && empty($input['budgeted_hours'])) { + $input['budgeted_hours'] = 0; + } $this->replace($input); } diff --git a/tests/Feature/ProjectApiTest.php b/tests/Feature/ProjectApiTest.php index f65abaae0dec..3f2b6a5ad797 100644 --- a/tests/Feature/ProjectApiTest.php +++ b/tests/Feature/ProjectApiTest.php @@ -29,6 +29,8 @@ class ProjectApiTest extends TestCase use DatabaseTransactions; use MockAccountData; + protected $faker; + protected function setUp() :void { parent::setUp(); @@ -42,6 +44,110 @@ class ProjectApiTest extends TestCase Model::reguard(); } + public function testProjectValidationForBudgetedHoursPut() + { + + $data = $this->project->toArray(); + $data['budgeted_hours'] = "aa"; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->putJson("/api/v1/projects/{$this->project->hashed_id}", $data); + + $response->assertStatus(422); + + } + + public function testProjectValidationForBudgetedHoursPutNull() + { + + $data = $this->project->toArray(); + $data['budgeted_hours'] = null; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->putJson("/api/v1/projects/{$this->project->hashed_id}", $data); + + $response->assertStatus(200); + + } + + + public function testProjectValidationForBudgetedHoursPutEmpty() + { + + $data = $this->project->toArray(); + $data['budgeted_hours'] = ""; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->putJson("/api/v1/projects/{$this->project->hashed_id}", $data); + + $response->assertStatus(200); + + } + + + public function testProjectValidationForBudgetedHours() + { + + $data = [ + 'name' => $this->faker->firstName(), + 'client_id' => $this->client->hashed_id, + 'number' => 'duplicate', + 'budgeted_hours' => null + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/projects', $data); + + $response->assertStatus(200); + + } + + public function testProjectValidationForBudgetedHours2() + { + + $data = [ + 'name' => $this->faker->firstName(), + 'client_id' => $this->client->hashed_id, + 'number' => 'duplicate', + 'budgeted_hours' => "a" + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/projects', $data); + + $response->assertStatus(422); + + } + + public function testProjectValidationForBudgetedHours3() + { + + $data = [ + 'name' => $this->faker->firstName(), + 'client_id' => $this->client->hashed_id, + 'number' => 'duplicate', + 'budgeted_hours' => "" + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/projects', $data); + + $response->assertStatus(200); + + } + public function testProjectGetFilter() { $response = $this->withHeaders([ diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index 959224548d7e..99eaf980bdb8 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -72,6 +72,11 @@ trait MockAccountData use MakesHash; use GeneratesCounter; + /** + * @var + */ + public $project; + /** * @var */