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

Handle badly formatted last_audit_date in StoreAssetRequest #14488

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 19 additions & 5 deletions app/Http/Requests/StoreAssetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Asset;
use App\Models\Company;
use Carbon\Carbon;
use Carbon\Exceptions\InvalidFormatException;
use Illuminate\Support\Facades\Gate;

class StoreAssetRequest extends ImageUploadRequest
Expand All @@ -28,11 +29,7 @@ public function prepareForValidation(): void
? Company::getIdForCurrentUser($this->company_id)
: $this->company_id;

if ($this->input('last_audit_date')) {
$this->merge([
'last_audit_date' => Carbon::parse($this->input('last_audit_date'))->startOfDay()->format('Y-m-d H:i:s'),
]);
}
$this->parseLastAuditDate();

$this->merge([
'asset_tag' => $this->asset_tag ?? Asset::autoincrement_asset(),
Expand All @@ -55,4 +52,21 @@ public function rules(): array

return $rules;
}

private function parseLastAuditDate(): void
{
if ($this->input('last_audit_date')) {
try {
$lastAuditDate = Carbon::parse($this->input('last_audit_date'));

$this->merge([
'last_audit_date' => $lastAuditDate->startOfDay()->format('Y-m-d H:i:s'),
]);
} catch (InvalidFormatException $e) {
// we don't need to do anything here...
// we'll keep the provided date in an
// invalid format so validation picks it up later
}
}
}
}
14 changes: 14 additions & 0 deletions tests/Feature/Api/Assets/AssetStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ public function testLastAuditDateCanBeNull()
$this->assertNull($asset->last_audit_date);
}

public function testNonDateUsedForLastAuditDateReturnsValidationError()
{
$response = $this->actingAsForApi(User::factory()->superuser()->create())
->postJson(route('api.assets.store'), [
'last_audit_date' => 'this-is-not-valid',
'asset_tag' => '1234',
'model_id' => AssetModel::factory()->create()->id,
'status_id' => Statuslabel::factory()->create()->id,
])
->assertStatusMessageIs('error');

$this->assertNotNull($response->json('messages.last_audit_date'));
}

public function testArchivedDepreciateAndPhysicalCanBeNull()
{
$model = AssetModel::factory()->ipadModel()->create();
Expand Down