mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-10-31 14:07:32 -04:00 
			
		
		
		
	Refactor for validation
This commit is contained in:
		
							parent
							
								
									516533c374
								
							
						
					
					
						commit
						b58e5f5bb7
					
				| @ -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 | ||||
|  | ||||
| @ -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'; | ||||
|             } | ||||
|  | ||||
| @ -1,59 +0,0 @@ | ||||
| <?php | ||||
| /** | ||||
|  * Credit Ninja (https://creditninja.com). | ||||
|  * | ||||
|  * @link https://github.com/creditninja/creditninja source repository | ||||
|  * | ||||
|  * @copyright Copyright (c) 2022. Credit Ninja LLC (https://creditninja.com) | ||||
|  * | ||||
|  * @license https://www.elastic.co/licensing/elastic-license | ||||
|  */ | ||||
| 
 | ||||
| namespace App\Http\ValidationRules\Client; | ||||
| 
 | ||||
| use App\Models\Country; | ||||
| use Illuminate\Contracts\Validation\Rule; | ||||
| 
 | ||||
| /** | ||||
|  * Class UniqueCreditNumberRule. | ||||
|  */ | ||||
| class CountryCodeExistsRule implements Rule | ||||
| { | ||||
|     public function __construct() | ||||
|     { | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $attribute | ||||
|      * @param mixed $value | ||||
|      * @return bool | ||||
|      */ | ||||
|     public function passes($attribute, $value) | ||||
|     { | ||||
|         return $this->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; | ||||
|     } | ||||
| } | ||||
| @ -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]); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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'); | ||||
|     } | ||||
| } | ||||
|  | ||||
							
								
								
									
										68
									
								
								tests/Integration/Validation/ValidCompanyQuantityTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								tests/Integration/Validation/ValidCompanyQuantityTest.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,68 @@ | ||||
| <?php | ||||
| /** | ||||
|  * Invoice Ninja (https://invoiceninja.com). | ||||
|  * | ||||
|  * @link https://github.com/invoiceninja/invoiceninja source repository | ||||
|  * | ||||
|  * @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com) | ||||
|  * | ||||
|  * @license https://www.elastic.co/licensing/elastic-license | ||||
|  */ | ||||
| 
 | ||||
| namespace Tests\Integration\Validation; | ||||
| 
 | ||||
| use Tests\TestCase; | ||||
| use App\Models\Company; | ||||
| use Tests\MockUnitData; | ||||
| use Illuminate\Support\Facades\Validator; | ||||
| use App\Http\ValidationRules\Company\ValidCompanyQuantity; | ||||
| 
 | ||||
| /** | ||||
|  * @test | ||||
|  */ | ||||
| class ValidCompanyQuantityTest extends TestCase | ||||
| { | ||||
|     use MockUnitData; | ||||
| 
 | ||||
|     protected function setUp(): void | ||||
|     { | ||||
|         parent::setUp(); | ||||
| 
 | ||||
|         $this->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()); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										84
									
								
								tests/Integration/Validation/ValidSubdomainTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								tests/Integration/Validation/ValidSubdomainTest.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,84 @@ | ||||
| <?php | ||||
| /** | ||||
|  * Invoice Ninja (https://invoiceninja.com). | ||||
|  * | ||||
|  * @link https://github.com/invoiceninja/invoiceninja source repository | ||||
|  * | ||||
|  * @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com) | ||||
|  * | ||||
|  * @license https://www.elastic.co/licensing/elastic-license | ||||
|  */ | ||||
| 
 | ||||
| namespace Tests\Integration\Validation; | ||||
| 
 | ||||
| use Tests\TestCase; | ||||
| use App\Models\Company; | ||||
| use Tests\MockUnitData; | ||||
| use Illuminate\Support\Facades\Validator; | ||||
| use App\Http\ValidationRules\Company\ValidCompanyQuantity; | ||||
| use App\Http\ValidationRules\Company\ValidSubdomain; | ||||
| 
 | ||||
| /** | ||||
|  * @test | ||||
|  */ | ||||
| class ValidSubdomainTest extends TestCase | ||||
| { | ||||
|     use MockUnitData; | ||||
| 
 | ||||
|     protected function setUp(): void | ||||
|     { | ||||
|         parent::setUp(); | ||||
|     } | ||||
| 
 | ||||
|     /** @test */ | ||||
|     public function testCheckValidSubdomainName() | ||||
|     { | ||||
|          | ||||
|         $data = ['subdomain' => '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()); | ||||
| 
 | ||||
|        | ||||
|     } | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user