- membuat config baru untuk secret key - implementasi test - menyesuaikan validasi - kasih return type
118 lines
3.5 KiB
PHP
118 lines
3.5 KiB
PHP
<?php
|
|
|
|
use App\Livewire\Forms\Studio\Setting\SocialForm;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Livewire\Livewire;
|
|
|
|
function createSocialForm()
|
|
{
|
|
// Create a minimal Livewire component for testing
|
|
$component = new class extends \Livewire\Component
|
|
{
|
|
public SocialForm $form;
|
|
|
|
public function mount()
|
|
{
|
|
$this->form = new SocialForm($this, 'form');
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return '<div></div>';
|
|
}
|
|
};
|
|
|
|
$component->mount();
|
|
|
|
return $component->form;
|
|
}
|
|
|
|
it('has correct validation rules', function () {
|
|
$form = createSocialForm();
|
|
$rules = $form->rules();
|
|
|
|
expect($rules)->toHaveKey('facebook');
|
|
expect($rules)->toHaveKey('instagram');
|
|
expect($rules)->toHaveKey('youtube');
|
|
expect($rules)->toHaveKey('tiktok');
|
|
expect($rules)->toHaveKey('whatsapp');
|
|
expect($rules)->toHaveKey('telegram');
|
|
|
|
foreach (['facebook', 'instagram', 'youtube', 'tiktok', 'whatsapp', 'telegram'] as $field) {
|
|
expect($rules[$field])->toContain('nullable');
|
|
expect($rules[$field])->toContain('url');
|
|
expect($rules[$field])->toContain('max:255');
|
|
}
|
|
});
|
|
|
|
it('validates nullable fields accept null values', function () {
|
|
$form = createSocialForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'facebook' => null,
|
|
'instagram' => null,
|
|
'youtube' => null,
|
|
'tiktok' => null,
|
|
'whatsapp' => null,
|
|
'telegram' => null,
|
|
], $rules);
|
|
|
|
expect($validator->passes())->toBeTrue();
|
|
});
|
|
|
|
it('validates url format for social media fields', function () {
|
|
$form = createSocialForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'facebook' => 'not-a-url',
|
|
'instagram' => 'https://instagram.com/valid',
|
|
'youtube' => 'invalid-url',
|
|
'tiktok' => 'https://tiktok.com/@valid',
|
|
'whatsapp' => 'not-a-url',
|
|
'telegram' => 'https://t.me/valid',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('facebook'))->toBeTrue();
|
|
expect($validator->errors()->has('youtube'))->toBeTrue();
|
|
expect($validator->errors()->has('whatsapp'))->toBeTrue();
|
|
expect($validator->errors()->has('instagram'))->toBeFalse();
|
|
expect($validator->errors()->has('tiktok'))->toBeFalse();
|
|
expect($validator->errors()->has('telegram'))->toBeFalse();
|
|
});
|
|
|
|
it('validates url max length', function () {
|
|
$form = createSocialForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'facebook' => 'https://facebook.com/'.str_repeat('a', 256),
|
|
'instagram' => 'https://instagram.com/valid',
|
|
'youtube' => 'https://youtube.com/valid',
|
|
'tiktok' => 'https://tiktok.com/@valid',
|
|
'whatsapp' => 'https://wa.me/valid',
|
|
'telegram' => 'https://t.me/valid',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('facebook'))->toBeTrue();
|
|
});
|
|
|
|
it('accepts valid urls', function () {
|
|
$form = createSocialForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'facebook' => 'https://facebook.com/page',
|
|
'instagram' => 'https://instagram.com/page',
|
|
'youtube' => 'https://youtube.com/channel',
|
|
'tiktok' => 'https://tiktok.com/@user',
|
|
'whatsapp' => 'https://wa.me/123456789',
|
|
'telegram' => 'https://t.me/channel',
|
|
], $rules);
|
|
|
|
expect($validator->passes())->toBeTrue();
|
|
});
|