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

[5.x] Update deprecated contract usages in custom validation rules #9780

Merged
merged 10 commits into from
Mar 25, 2024
2 changes: 2 additions & 0 deletions resources/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
'handle' => 'Must contain only lowercase letters and numbers with underscores as separators.',
'slug' => 'Must contain only letters and numbers with dashes or underscores as separators.',
'code_fieldtype_rulers' => 'This is invalid.',
'composer_package' => 'Must be a valid composer package name (eg. hasselhoff/kung-fury).',
'date_fieldtype_date_required' => 'Date is required.',
'date_fieldtype_end_date_invalid' => 'Not a valid end date.',
'date_fieldtype_end_date_required' => 'End date is required.',
Expand All @@ -166,6 +167,7 @@
'date_fieldtype_time_required' => 'Time is required.',
'duplicate_field_handle' => 'A field with a handle of :handle already exists.',
'duplicate_uri' => 'Duplicate URI :value',
'email_available' => 'A user with this email already exists.',
'one_site_without_origin' => 'At least one site must not have an origin.',
'options_require_keys' => 'All options must have keys.',
'origin_cannot_be_disabled' => 'Cannot select a disabled origin.',
Expand Down
28 changes: 7 additions & 21 deletions src/Rules/ComposerPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,15 @@

namespace Statamic\Rules;

use Illuminate\Contracts\Validation\Rule;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class ComposerPackage implements Rule
class ComposerPackage implements ValidationRule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
return preg_match("/^[^\/\s]+\/[^\/\s]+$/", $value);
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'Please enter a valid composer package name (eg. hasselhoff/kung-fury).';
if (! preg_match("/^[^\/\s]+\/[^\/\s]+$/", $value)) {
$fail('statamic::validation.composer_package')->translate();
}
}
}
28 changes: 7 additions & 21 deletions src/Rules/EmailAvailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,16 @@

namespace Statamic\Rules;

use Illuminate\Contracts\Validation\Rule;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Statamic\Facades\User;

class EmailAvailable implements Rule
class EmailAvailable implements ValidationRule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
return User::query()->where('email', trim($value))->count() === 0;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'A user with this email already exists.';
if (User::query()->where('email', trim($value))->count() !== 0) {
$fail('statamic::validation.email_available')->translate();
}
}
}
4 changes: 2 additions & 2 deletions tests/Console/Commands/MakeAddonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public function it_can_generate_an_addon()
public function it_cannot_make_addon_with_invalid_composer_package_name()
{
$this->artisan('statamic:make:addon', ['addon' => 'deaths-tar-vulnerability'])
->expectsOutput('Please enter a valid composer package name (eg. hasselhoff/kung-fury).');
->expectsOutput(trans('statamic::validation.composer_package'));

$this->artisan('statamic:make:addon', ['addon' => 'some/path/deaths-tar-vulnerability'])
->expectsOutput('Please enter a valid composer package name (eg. hasselhoff/kung-fury).');
->expectsOutput(trans('statamic::validation.composer_package'));

$this->assertFileDoesNotExist(base_path('addons/erso/deaths-tar-vulnerability'));
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Rules/ComposerPackageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Tests\Rules;

use Statamic\Rules\ComposerPackage;
use Tests\TestCase;

class ComposerPackageTest extends TestCase
{
use ValidatesCustomRule;

protected static $customRule = ComposerPackage::class;

/** @test */
public function it_validates_handles()
{
$this->assertPasses('the-hasselhoff/kung-fury');
$this->assertPasses('blastoff12345/chocolate-ship67890');

$this->assertFails('not a package');
$this->assertFails('not-a-package');
$this->assertFails(' vendor/not-a-package');
$this->assertFails('vendor/not-a-package ');
$this->assertFails('vendor /not-a-package');
$this->assertFails('vendor/ not-a-package');
}

/** @test */
public function it_outputs_helpful_validation_error()
{
$this->assertValidationErrorOutput(trans('statamic::validation.composer_package'), 'not-a-package');
}
}
44 changes: 44 additions & 0 deletions tests/Rules/EmailAvailableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tests\Rules;

use Statamic\Facades\User;
use Statamic\Rules\EmailAvailable;
use Tests\TestCase;

class EmailAvailableTest extends TestCase
{
use ValidatesCustomRule;

protected static $customRule = EmailAvailable::class;

public function setUp(): void
{
parent::setUp();

User::make()->email('frodo@lotr.com')->save();
}

public function tearDown(): void
{
User::all()->each->delete();

parent::tearDown();
}

/** @test */
public function it_validates_handles()
{
$this->assertPasses('gandalf@lotr.com');
$this->assertPasses('aragorn@lotr.com');
$this->assertPasses('samwise@lotr.com');

$this->assertFails('frodo@lotr.com');
}

/** @test */
public function it_outputs_helpful_validation_error()
{
$this->assertValidationErrorOutput(trans('statamic::validation.email_available'), 'frodo@lotr.com');
}
}
Loading