- membuat config baru untuk secret key - implementasi test - menyesuaikan validasi - kasih return type
144 lines
4.4 KiB
PHP
144 lines
4.4 KiB
PHP
<?php
|
|
|
|
use App\Livewire\Forms\Studio\Setting\GeneralForm;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Livewire\Livewire;
|
|
|
|
function createGeneralForm()
|
|
{
|
|
// Create a minimal Livewire component for testing
|
|
$component = new class extends \Livewire\Component
|
|
{
|
|
public GeneralForm $form;
|
|
|
|
public function mount()
|
|
{
|
|
$this->form = new GeneralForm($this, 'form');
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return '<div></div>';
|
|
}
|
|
};
|
|
|
|
$component->mount();
|
|
|
|
return $component->form;
|
|
}
|
|
|
|
it('has correct validation rules', function () {
|
|
$form = createGeneralForm();
|
|
$rules = $form->rules();
|
|
|
|
expect($rules)->toHaveKey('site_name');
|
|
expect($rules)->toHaveKey('site_tagline');
|
|
expect($rules)->toHaveKey('site_description');
|
|
expect($rules)->toHaveKey('maintenance_mode');
|
|
|
|
expect($rules['site_name'])->toContain('required');
|
|
expect($rules['site_name'])->toContain('string');
|
|
expect($rules['site_name'])->toContain('max:100');
|
|
expect($rules['site_name'])->toContain('min:1');
|
|
|
|
expect($rules['site_tagline'])->toContain('required');
|
|
expect($rules['site_tagline'])->toContain('string');
|
|
expect($rules['site_tagline'])->toContain('max:150');
|
|
expect($rules['site_tagline'])->toContain('min:1');
|
|
|
|
expect($rules['site_description'])->toContain('required');
|
|
expect($rules['site_description'])->toContain('string');
|
|
expect($rules['site_description'])->toContain('min:10');
|
|
expect($rules['site_description'])->toContain('max:500');
|
|
|
|
expect($rules['maintenance_mode'])->toContain('required');
|
|
expect($rules['maintenance_mode'])->toContain('boolean');
|
|
});
|
|
|
|
it('validates required fields', function () {
|
|
$form = createGeneralForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'site_name' => '',
|
|
'site_tagline' => '',
|
|
'site_description' => '',
|
|
'maintenance_mode' => false,
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('site_name'))->toBeTrue();
|
|
expect($validator->errors()->has('site_tagline'))->toBeTrue();
|
|
expect($validator->errors()->has('site_description'))->toBeTrue();
|
|
});
|
|
|
|
it('validates minimum lengths', function () {
|
|
$form = createGeneralForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'site_name' => 'A',
|
|
'site_tagline' => 'B',
|
|
'site_description' => 'Short',
|
|
'maintenance_mode' => false,
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('site_description'))->toBeTrue();
|
|
});
|
|
|
|
it('validates maximum lengths', function () {
|
|
$form = createGeneralForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'site_name' => str_repeat('A', 101),
|
|
'site_tagline' => str_repeat('B', 151),
|
|
'site_description' => 'Valid description',
|
|
'maintenance_mode' => false,
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('site_name'))->toBeTrue();
|
|
expect($validator->errors()->has('site_tagline'))->toBeTrue();
|
|
});
|
|
|
|
it('validates maintenance mode boolean', function () {
|
|
$form = createGeneralForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'site_name' => 'Valid Name',
|
|
'site_tagline' => 'Valid Tagline',
|
|
'site_description' => 'Valid description with proper length',
|
|
'maintenance_mode' => 'not_boolean',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('maintenance_mode'))->toBeTrue();
|
|
});
|
|
|
|
it('passes validation with valid data', function () {
|
|
$form = createGeneralForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'site_name' => 'Valid Site Name',
|
|
'site_tagline' => 'Valid Tagline',
|
|
'site_description' => 'Valid description with proper length for the site',
|
|
'maintenance_mode' => true,
|
|
], $rules);
|
|
|
|
expect($validator->passes())->toBeTrue();
|
|
});
|
|
|
|
it('has correct validation attributes', function () {
|
|
$form = createGeneralForm();
|
|
$attributes = $form->validationAttributes();
|
|
|
|
expect($attributes)->toHaveKey('site_name', 'nama situs');
|
|
expect($attributes)->toHaveKey('site_tagline', 'tagline situs');
|
|
expect($attributes)->toHaveKey('site_description', 'deskripsi situs');
|
|
expect($attributes)->toHaveKey('maintenance_mode', 'mode maintenance');
|
|
});
|