feat(sales): validate PDF timezone parameter
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Sale\Requests;
|
||||
|
||||
use App\Domains\Shared\Rules\ValidTimezone;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class AdminAppSaleModificationPdfRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @return array<string, list<string>> */
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'timezone' => ['required', 'string', new ValidTimezone],
|
||||
];
|
||||
}
|
||||
}
|
||||
17
app/Domains/Sale/Requests/AdminAppSalePdfRequest.php
Normal file
17
app/Domains/Sale/Requests/AdminAppSalePdfRequest.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Sale\Requests;
|
||||
|
||||
use App\Domains\Shared\Rules\ValidTimezone;
|
||||
|
||||
class AdminAppSalePdfRequest extends AdminAppSaleIndexRequest
|
||||
{
|
||||
/** @return array<string, list<string>> */
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
...parent::rules(),
|
||||
'timezone' => ['required', 'string', new ValidTimezone],
|
||||
];
|
||||
}
|
||||
}
|
||||
24
app/Domains/Shared/Rules/ValidTimezone.php
Normal file
24
app/Domains/Shared/Rules/ValidTimezone.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Shared\Rules;
|
||||
|
||||
use Closure;
|
||||
use DateTimeZone;
|
||||
use Exception;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
|
||||
class ValidTimezone implements ValidationRule
|
||||
{
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
new DateTimeZone($value);
|
||||
} catch (Exception) {
|
||||
$fail(__('validation.timezone'));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,7 @@ return [
|
||||
'required_with' => 'The :attribute field is required when :values is present.',
|
||||
'required_without' => 'The :attribute field is required when :values is not present.',
|
||||
'string' => 'The :attribute must be a string.',
|
||||
'timezone' => 'The timezone sent by the browser is invalid.',
|
||||
'unique' => 'The :attribute has already been taken.',
|
||||
'url' => 'The :attribute must be a valid URL.',
|
||||
'uuid' => 'The :attribute must be a valid UUID.',
|
||||
@@ -52,6 +53,7 @@ return [
|
||||
'password_confirmation' => 'password confirmation',
|
||||
'telefono' => 'phone number',
|
||||
'tenant_codigo' => 'tenant',
|
||||
'timezone' => 'timezone',
|
||||
'variant_id' => 'variant',
|
||||
],
|
||||
];
|
||||
|
||||
@@ -37,6 +37,7 @@ return [
|
||||
'required_with' => 'El campo :attribute es obligatorio cuando :values está presente.',
|
||||
'required_without' => 'El campo :attribute es obligatorio cuando :values no está presente.',
|
||||
'string' => ':Attribute debe ser texto.',
|
||||
'timezone' => 'La zona horaria enviada por el navegador no es válida.',
|
||||
'unique' => 'El :attribute ya está en uso.',
|
||||
'url' => ':Attribute debe ser una URL válida.',
|
||||
'uuid' => ':Attribute debe ser un UUID válido.',
|
||||
@@ -52,6 +53,7 @@ return [
|
||||
'password_confirmation' => 'confirmación de contraseña',
|
||||
'telefono' => 'teléfono',
|
||||
'tenant_codigo' => 'tenant',
|
||||
'timezone' => 'zona horaria',
|
||||
'variant_id' => 'variante',
|
||||
],
|
||||
];
|
||||
|
||||
55
tests/Unit/Sale/AdminAppSalePdfRequestTest.php
Normal file
55
tests/Unit/Sale/AdminAppSalePdfRequestTest.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Sale;
|
||||
|
||||
use App\Domains\Sale\Requests\AdminAppSaleModificationPdfRequest;
|
||||
use App\Domains\Sale\Requests\AdminAppSalePdfRequest;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AdminAppSalePdfRequestTest extends TestCase
|
||||
{
|
||||
public function test_pdf_requests_accept_php_timezone_identifiers_including_browser_aliases(): void
|
||||
{
|
||||
$requests = [
|
||||
new AdminAppSalePdfRequest,
|
||||
new AdminAppSaleModificationPdfRequest,
|
||||
];
|
||||
|
||||
foreach ($requests as $request) {
|
||||
$this->assertTrue(Validator::make([
|
||||
'timezone' => 'America/Argentina/Buenos_Aires',
|
||||
], $request->rules())->passes());
|
||||
|
||||
$this->assertTrue(Validator::make([
|
||||
'timezone' => 'America/Buenos_Aires',
|
||||
], $request->rules())->passes());
|
||||
|
||||
$this->assertFalse(Validator::make([
|
||||
'timezone' => 'Invalid/Timezone',
|
||||
], $request->rules())->passes());
|
||||
|
||||
$this->assertFalse(Validator::make([], $request->rules())->passes());
|
||||
}
|
||||
}
|
||||
|
||||
public function test_invalid_timezone_message_uses_the_application_locale(): void
|
||||
{
|
||||
$request = new AdminAppSalePdfRequest;
|
||||
|
||||
App::setLocale('es');
|
||||
$spanishValidator = Validator::make(['timezone' => 'Invalid/Timezone'], $request->rules());
|
||||
$this->assertSame(
|
||||
'La zona horaria enviada por el navegador no es válida.',
|
||||
$spanishValidator->errors()->first('timezone'),
|
||||
);
|
||||
|
||||
App::setLocale('en');
|
||||
$englishValidator = Validator::make(['timezone' => 'Invalid/Timezone'], $request->rules());
|
||||
$this->assertSame(
|
||||
'The timezone sent by the browser is invalid.',
|
||||
$englishValidator->errors()->first('timezone'),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user