diff --git a/app/Http/Requests/Client/StoreClientRequest.php b/app/Http/Requests/Client/StoreClientRequest.php index 3262593f60..def4080a3c 100644 --- a/app/Http/Requests/Client/StoreClientRequest.php +++ b/app/Http/Requests/Client/StoreClientRequest.php @@ -13,13 +13,11 @@ namespace App\Http\Requests\Client; use App\DataMapper\ClientSettings; use App\Http\Requests\Request; -use App\Http\ValidationRules\Client\CountryCodeExistsRule; use App\Http\ValidationRules\Ninja\CanStoreClientsRule; use App\Http\ValidationRules\ValidClientGroupSettingsRule; use App\Models\Client; use App\Models\GroupSetting; use App\Utils\Traits\MakesHash; -use Illuminate\Support\Facades\Cache; use Illuminate\Validation\Rule; class StoreClientRequest extends Request diff --git a/app/Http/Requests/Company/StoreCompanyRequest.php b/app/Http/Requests/Company/StoreCompanyRequest.php index 6db6cce58c..442eceec2c 100644 --- a/app/Http/Requests/Company/StoreCompanyRequest.php +++ b/app/Http/Requests/Company/StoreCompanyRequest.php @@ -50,7 +50,7 @@ class StoreCompanyRequest extends Request $rules['portal_domain'] = 'sometimes|url'; } else { if (Ninja::isHosted()) { - $rules['subdomain'] = ['nullable', 'regex:/^[a-zA-Z0-9][a-zA-Z0-9.-]+[a-zA-Z0-9]$/', new ValidSubdomain()]; + $rules['subdomain'] = ['nullable', 'regex:/^[a-zA-Z0-9-]{1,63}$/', new ValidSubdomain()]; } else { $rules['subdomain'] = 'nullable|alpha_num'; } diff --git a/app/Http/ValidationRules/Client/CountryCodeExistsRule.php b/app/Http/ValidationRules/Client/CountryCodeExistsRule.php deleted file mode 100644 index 7b3a0b8abb..0000000000 --- a/app/Http/ValidationRules/Client/CountryCodeExistsRule.php +++ /dev/null @@ -1,59 +0,0 @@ -checkIfCodeExists($value); //if it exists, return false! - } - - /** - * @return string - */ - public function message() - { - return 'Country code does not exist'; - } - - /** - * @return bool - */ - private function checkIfCodeExists($value): bool - { - $country = Country::where('iso_3166_2', $value) - ->orWhere('iso_3166_3', $value) - ->exists(); - - if ($country) { - return true; - } - - return false; - } -} diff --git a/app/Http/ValidationRules/Company/ValidCompanyQuantity.php b/app/Http/ValidationRules/Company/ValidCompanyQuantity.php index ce472598fb..7f67d2b46b 100644 --- a/app/Http/ValidationRules/Company/ValidCompanyQuantity.php +++ b/app/Http/ValidationRules/Company/ValidCompanyQuantity.php @@ -12,36 +12,26 @@ namespace App\Http\ValidationRules\Company; use App\Utils\Ninja; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; /** * Class ValidCompanyQuantity. */ -class ValidCompanyQuantity implements Rule +class ValidCompanyQuantity implements ValidationRule { - /** - * @param string $attribute - * @param mixed $value - * @return bool - */ - public function passes($attribute, $value) + + public function validate(string $attribute, mixed $value, Closure $fail): void { - if (config('ninja.testvars.travis')) { - return true; - } + $message = ctrans('texts.company_limit_reached', ['limit' => Ninja::isSelfHost() ? 10 : auth()->user()->company()->account->hosted_company_count]); - if (Ninja::isSelfHost()) { - return auth()->user()->company()->account->companies->count() < 10; - } + $test = Ninja::isSelfHost() ? + auth()->user()->company()->account->companies->count() < 10 : + (auth()->user()->account->isPaid() || auth()->user()->account->isTrial()) && auth()->user()->company()->account->companies->count() < 10 ; - return (auth()->user()->account->isPaid() || auth()->user()->account->isTrial()) && auth()->user()->company()->account->companies->count() < 10 ; + if (!$test) { + $fail($message); + } } - /** - * @return string - */ - public function message() - { - return ctrans('texts.company_limit_reached', ['limit' => Ninja::isSelfHost() ? 10 : auth()->user()->company()->account->hosted_company_count]); - } } diff --git a/app/Http/ValidationRules/Company/ValidSubdomain.php b/app/Http/ValidationRules/Company/ValidSubdomain.php index 48555e49c0..26aeb32349 100644 --- a/app/Http/ValidationRules/Company/ValidSubdomain.php +++ b/app/Http/ValidationRules/Company/ValidSubdomain.php @@ -12,31 +12,22 @@ namespace App\Http\ValidationRules\Company; use App\Libraries\MultiDB; -use Illuminate\Contracts\Validation\Rule; +use Closure; +use Illuminate\Contracts\Validation\ValidationRule; /** - * Class ValidCompanyQuantity. + * Class ValidSubdomain. */ -class ValidSubdomain implements Rule +class ValidSubdomain implements ValidationRule { - public function __construct() + public function validate(string $attribute, mixed $value, Closure $fail): void { - } - public function passes($attribute, $value) - { - if (empty($value)) { - return true; + if(empty($value)) + return; + + if (!MultiDB::checkDomainAvailable($value)) { + $fail(ctrans('texts.subdomain_taken')); } - - return MultiDB::checkDomainAvailable($value); - } - - /** - * @return string - */ - public function message() - { - return ctrans('texts.subdomain_taken'); } } diff --git a/tests/Integration/Validation/ValidCompanyQuantityTest.php b/tests/Integration/Validation/ValidCompanyQuantityTest.php new file mode 100644 index 0000000000..dec9a978bd --- /dev/null +++ b/tests/Integration/Validation/ValidCompanyQuantityTest.php @@ -0,0 +1,68 @@ +makeTestData(); + + } + + /** @test */ + public function testCompanyQuantityValidation() + { + auth()->login($this->user, true); + + $data =[]; + $rules = ['name' => [new ValidCompanyQuantity()]]; + + $validator = Validator::make($data, $rules); + + $this->assertTrue($validator->passes()); + } + + + /** @test */ + public function testCompanyQuantityValidationFails() + { + + auth()->login($this->user, true); + auth()->user()->setCompany($this->company); + + $data =['name' => 'bob']; + $rules = ['name' => [new ValidCompanyQuantity()]]; + + Company::factory()->count(10)->create([ + 'account_id' => auth()->user()->account->id, + ]); + + $validator = Validator::make($data, $rules); + + $this->assertFalse($validator->passes()); + } + +} diff --git a/tests/Integration/Validation/ValidSubdomainTest.php b/tests/Integration/Validation/ValidSubdomainTest.php new file mode 100644 index 0000000000..0a801f014d --- /dev/null +++ b/tests/Integration/Validation/ValidSubdomainTest.php @@ -0,0 +1,84 @@ + 'invoiceyninjay']; + $rules = ['subdomain' => ['nullable', 'regex:/^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/',new ValidSubdomain()]]; + + $validator = Validator::make($data, $rules); + + $this->assertTrue($validator->passes()); + + } + + public function testCheckEmptyValidSubdomainName() + { + + $data = ['subdomain' => '']; + $rules = ['subdomain' => ['nullable', 'regex:/^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/',new ValidSubdomain()]]; + + $validator = Validator::make($data, $rules); + + $this->assertTrue($validator->passes()); + + } + + public function testCheckEmpty2ValidSubdomainName() + { + + $data = ['subdomain' => ' ']; + $rules = ['subdomain' => ['nullable', 'regex:/^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/',new ValidSubdomain()]]; + + $validator = Validator::make($data, $rules); + + $this->assertTrue($validator->passes()); + + } + + /** @test */ + public function testCheckInValidSubdomainName() + { + + $data = ['subdomain' => 'domain.names']; + $rules = ['subdomain' => ['nullable', 'regex:/^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/',new ValidSubdomain()]]; + + $validator = Validator::make($data, $rules); + + $this->assertFalse($validator->passes()); + + + } + +}