Skip to content

Commit

Permalink
Change allowed symbol on custom keyword from _ to - (#971)
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix authored May 3, 2024
1 parent f8e0766 commit 0039e0f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
4 changes: 2 additions & 2 deletions app/Http/Requests/StoreUrlRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Http\Requests;

use App\Rules\StrAlphaUnderscore;
use App\Rules\AlphaNumHyphen;
use App\Rules\Url\DomainBlacklist;
use Illuminate\Foundation\Http\FormRequest;

Expand All @@ -27,7 +27,7 @@ public function rules()
{
return [
'long_url' => ['required', 'url', 'max:65535', new DomainBlacklist],
'custom_key' => ['nullable', 'max:20', new StrAlphaUnderscore, 'unique:urls,keyword'],
'custom_key' => ['nullable', 'max:20', new AlphaNumHyphen, 'unique:urls,keyword'],
];
}

Expand Down
2 changes: 1 addition & 1 deletion app/Livewire/Validation/ValidateCustomKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function rules()
return [
'keyword' => [
"min:$minLen", "max:$maxLen", 'unique:App\Models\Url', 'lowercase:field',
new \App\Rules\StrAlphaUnderscore,
new \App\Rules\AlphaNumHyphen,
new \App\Rules\Url\KeywordBlacklist,
],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

/**
* The field under validation may have alpha-numeric characters, as well as
* underscores.
* hyphen.
*/
class StrAlphaUnderscore implements ValidationRule
class AlphaNumHyphen implements ValidationRule
{
/**
* Run the validation rule.
Expand All @@ -17,10 +17,10 @@ class StrAlphaUnderscore implements ValidationRule
*/
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
$rule = preg_match('/^[\pL\pM\pN_]+$/u', $value);
$rule = preg_match('/^[\pL\pM\pN-]+$/u', $value);

if ($rule === false || $rule === 0) {
$fail('The :attribute may only contain letters, numbers and underscores.');
$fail('The :attribute may only contain letters, numbers and hyphens.');
}
}
}
18 changes: 11 additions & 7 deletions tests/Feature/FrontPage/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ public function testCustomKeyValidation(): void
$component = Livewire::test(ValidateCustomKeyword::class);

$component->assertStatus(200)
->set('keyword', '!')
->assertHasErrors('keyword')
->set('keyword', 'FOO')
->assertHasErrors('keyword')
->set('keyword', 'admin')
->assertHasErrors('keyword')
->set('keyword', 'foo_bar')
->set('keyword', 'foobar')
->assertHasNoErrors('keyword')
->set('keyword', '123456')
->assertHasNoErrors('keyword')
->set('keyword', 'foo-b4r')
->assertHasNoErrors('keyword');

$component
->set('keyword', 'FOOBAR')
->assertHasErrors('keyword')
->set('keyword', 'admin') // Dashboard route
->assertHasErrors('keyword');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@

namespace Tests\Unit\Rule;

use App\Rules\StrAlphaUnderscore;
use App\Rules\AlphaNumHyphen;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Tests\Support\Helper;
use Tests\TestCase;

class StrAlphaUnderscoreTest extends TestCase
class AlphaNumHyphenTest extends TestCase
{
#[Test]
#[Group('u-rule')]
public function strAlphaUnderscorePass(): void
public function AlphaNumHyphenPass(): void
{
$val = Helper::validator(['foo' => 'foo_bar'], ['foo' => new StrAlphaUnderscore]);
$val = Helper::validator(['foo' => 'foo-bar'], ['foo' => new AlphaNumHyphen]);

$this->assertTrue($val->passes());
$this->assertSame([], $val->messages()->messages());
}

#[Test]
#[Group('u-rule')]
public function strAlphaUnderscoreFail(): void
public function AlphaNumHyphenFail(): void
{
$val = Helper::validator(['foo' => 'foo-bar'], ['foo' => new StrAlphaUnderscore]);
$val = Helper::validator(['foo' => 'foo_bar'], ['foo' => new AlphaNumHyphen]);
$this->assertTrue($val->fails());

$val = Helper::validator(['foo' => 'fo0_b@r'], ['foo' => new StrAlphaUnderscore]);
$val = Helper::validator(['foo' => 'fo0-b@r'], ['foo' => new AlphaNumHyphen]);
$this->assertTrue($val->fails());

$this->assertSame([
'foo' => ['The foo may only contain letters, numbers and underscores.'],
'foo' => ['The foo may only contain letters, numbers and hyphens.'],
], $val->messages()->messages());
}
}

0 comments on commit 0039e0f

Please sign in to comment.