Skip to content

Commit

Permalink
Validate data provided in purchase form
Browse files Browse the repository at this point in the history
  • Loading branch information
budziam committed Sep 20, 2023
1 parent d0de5b9 commit c652540
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 4 deletions.
10 changes: 8 additions & 2 deletions includes/Http/Controllers/Api/Shop/PaymentResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use App\Http\Responses\ApiResponse;
use App\Http\Responses\ErrorApiResponse;
use App\Http\Validation\Rules\EnumRule;
use App\Http\Validation\Rules\FullNameRule;
use App\Http\Validation\Rules\MaxLengthRule;
use App\Http\Validation\Rules\PostalCodeRule;
use App\Http\Validation\Rules\RequiredRule;
use App\Http\Validation\Validator;
use App\Models\Purchase;
Expand Down Expand Up @@ -45,10 +47,14 @@ public function post(
"method" => [new EnumRule(PaymentMethod::class)],
"payment_platform_id" => [],
"sms_code" => [],
"billing_address_name" => [...$billingRequiredRule, new MaxLengthRule(128)],
"billing_address_name" => [
...$billingRequiredRule,
new FullNameRule(),
new MaxLengthRule(128),
],
"billing_address_vat_id" => [new MaxLengthRule(128)],
"billing_address_street" => [...$billingRequiredRule, new MaxLengthRule(128)],
"billing_address_postal_code" => [...$billingRequiredRule, new MaxLengthRule(128)],
"billing_address_postal_code" => [...$billingRequiredRule, new PostalCodeRule()],
"billing_address_city" => [...$billingRequiredRule, new MaxLengthRule(128)],
"remember_billing_address" => [],
]);
Expand Down
6 changes: 4 additions & 2 deletions includes/Http/Controllers/Api/Shop/UserProfileResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
namespace App\Http\Controllers\Api\Shop;

use App\Http\Responses\SuccessApiResponse;
use App\Http\Validation\Rules\FullNameRule;
use App\Http\Validation\Rules\MaxLengthRule;
use App\Http\Validation\Rules\PostalCodeRule;
use App\Http\Validation\Rules\RequiredRule;
use App\Http\Validation\Rules\SteamIdRule;
use App\Http\Validation\Rules\UniqueSteamIdRule;
Expand Down Expand Up @@ -57,10 +59,10 @@ public function put(
"forename" => [],
"surname" => [],
"steam_id" => [new SteamIdRule(), new UniqueSteamIdRule($user->getId())],
"billing_address_name" => [new MaxLengthRule(128)],
"billing_address_name" => [new FullNameRule(), new MaxLengthRule(128)],
"billing_address_vat_id" => [new MaxLengthRule(128)],
"billing_address_street" => [new MaxLengthRule(128)],
"billing_address_postal_code" => [new MaxLengthRule(128)],
"billing_address_postal_code" => [new PostalCodeRule(), new MaxLengthRule(128)],
"billing_address_city" => [new MaxLengthRule(128)],
]
);
Expand Down
13 changes: 13 additions & 0 deletions includes/Http/Validation/Rules/FullNameRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace App\Http\Validation\Rules;

use App\Exceptions\ValidationException;
use App\Http\Validation\BaseRule;

class FullNameRule extends RegexRule
{
public function __construct(string $pattern = "/^\p{L}+\s\p{L}+$/u")
{
parent::__construct($pattern);
}
}
13 changes: 13 additions & 0 deletions includes/Http/Validation/Rules/PostalCodeRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace App\Http\Validation\Rules;

use App\Exceptions\ValidationException;
use App\Http\Validation\BaseRule;

class PostalCodeRule extends RegexRule
{
public function __construct(string $pattern = "/^\d{2}-\d{3}$/")
{
parent::__construct($pattern);
}
}
23 changes: 23 additions & 0 deletions includes/Http/Validation/Rules/RegexRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace App\Http\Validation\Rules;

use App\Exceptions\ValidationException;
use App\Http\Validation\BaseRule;

class RegexRule extends BaseRule
{
private string $pattern;

public function __construct(string $pattern)
{
parent::__construct();
$this->pattern = $pattern;
}

public function validate($attribute, $value, array $data): void
{
if (!preg_match($this->pattern, $value)) {
throw new ValidationException($this->lang->t("field_regex_warn"));
}
}
}
49 changes: 49 additions & 0 deletions tests/Unit/Http/Validation/FullNameRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Tests\Unit\Http\Validation;

use App\Exceptions\ValidationException;
use App\Http\Validation\Rules\FullNameRule;
use App\Http\Validation\Rules\PostalCodeRule;
use Tests\Psr4\TestCases\UnitTestCase;

class FullNameRuleTest extends UnitTestCase
{
public function validFullNames(): array
{
return [["John Wick"], ["John Johny"], ["Michał Nowak"], ["Zażółć GęśląJaźń"]];
}

/**
* @test
* @dataProvider validFullNames
*/
public function passes_validation(string $fullName)
{
// given
$postalCodeRule = new FullNameRule();

// when
$postalCodeRule->validate("", $fullName, []);
}

public function invalidFullNames(): array
{
return [["xyz"], ["to jestem ja"], [""], [null]];
}

/**
* @test
* @dataProvider invalidFullNames
*/
public function fails_validation(?string $fullName)
{
// given
$this->expectException(ValidationException::class);

$postalCodeRule = new FullNameRule();

// when
$postalCodeRule->validate("", $fullName, []);
}
}
48 changes: 48 additions & 0 deletions tests/Unit/Http/Validation/PostalCodeRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Tests\Unit\Http\Validation;

use App\Exceptions\ValidationException;
use App\Http\Validation\Rules\PostalCodeRule;
use Tests\Psr4\TestCases\UnitTestCase;

class PostalCodeRuleTest extends UnitTestCase
{
public function validPostalCodes(): array
{
return [["00-000"], ["01-960"]];
}

/**
* @test
* @dataProvider validPostalCodes
*/
public function passes_validation(string $postalCode)
{
// given
$postalCodeRule = new PostalCodeRule();

// when
$postalCodeRule->validate("", $postalCode, []);
}

public function invalidPostalCodes(): array
{
return [["abc"], ["0000"], [""], [null]];
}

/**
* @test
* @dataProvider invalidPostalCodes
*/
public function fails_validation(?string $postalCode)
{
// given
$this->expectException(ValidationException::class);

$postalCodeRule = new PostalCodeRule();

// when
$postalCodeRule->validate("", $postalCode, []);
}
}
1 change: 1 addition & 0 deletions translations/english/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"field_length_min_warn" => "Field must consist of min. {1} chars.",
"field_must_be_number" => "Field must contain a number.",
"field_no_empty" => "Field cannot be empty.",
"field_regex_warn" => "Field value does not meet requirements.",
"firstname" => "Firstname",
"forever" => "Forever",
"form_wrong_filled" => "Not all the form fields were filled correctly.",
Expand Down
1 change: 1 addition & 0 deletions translations/polish/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"field_length_min_warn" => "Pole musi się składać z co najmniej {1} znaków.",
"field_must_be_number" => "W polu musi się znajdować liczba.",
"field_no_empty" => "Pole nie może być puste.",
"field_regex_warn" => "Wprowadzona wartość nie spełnia wymagań.",
"firstname" => "Imię",
"forever" => "Na zawsze",
"form_wrong_filled" => "Nie wszystkie pola formularza zostały prawidłowo wypełnione.",
Expand Down

0 comments on commit c652540

Please sign in to comment.