- membuat config baru untuk secret key - implementasi test - menyesuaikan validasi - kasih return type
351 lines
11 KiB
PHP
351 lines
11 KiB
PHP
<?php
|
|
|
|
use App\Livewire\Forms\Studio\Setting\SeoForm;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Livewire\Livewire;
|
|
|
|
function createSeoForm()
|
|
{
|
|
// Create a minimal Livewire component for testing
|
|
$component = new class extends \Livewire\Component
|
|
{
|
|
public SeoForm $form;
|
|
|
|
public function mount()
|
|
{
|
|
$this->form = new SeoForm($this, 'form');
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return '<div></div>';
|
|
}
|
|
};
|
|
|
|
$component->mount();
|
|
|
|
return $component->form;
|
|
}
|
|
|
|
it('has correct validation rules', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
|
|
expect($rules)->toHaveKey('meta_description');
|
|
expect($rules)->toHaveKey('meta_keywords');
|
|
expect($rules)->toHaveKey('meta_author');
|
|
expect($rules)->toHaveKey('og_image');
|
|
expect($rules)->toHaveKey('og_title');
|
|
expect($rules)->toHaveKey('og_description');
|
|
expect($rules)->toHaveKey('og_type');
|
|
expect($rules)->toHaveKey('twitter_card');
|
|
expect($rules)->toHaveKey('canonical_url');
|
|
|
|
expect($rules['meta_description'])->toContain('required');
|
|
expect($rules['meta_description'])->toContain('string');
|
|
expect($rules['meta_description'])->toContain('min:10');
|
|
expect($rules['meta_description'])->toContain('max:160');
|
|
|
|
expect($rules['meta_keywords'])->toContain('required');
|
|
expect($rules['meta_keywords'])->toContain('string');
|
|
expect($rules['meta_keywords'])->toContain('min:3');
|
|
expect($rules['meta_keywords'])->toContain('max:255');
|
|
|
|
expect($rules['meta_author'])->toContain('required');
|
|
expect($rules['meta_author'])->toContain('string');
|
|
expect($rules['meta_author'])->toContain('min:1');
|
|
expect($rules['meta_author'])->toContain('max:150');
|
|
|
|
expect($rules['og_image'])->toContain('nullable');
|
|
expect($rules['og_image'])->toContain('string');
|
|
expect($rules['og_image'])->toContain('max:500');
|
|
expect($rules['og_image'])->toContain('url');
|
|
|
|
expect($rules['og_title'])->toContain('nullable');
|
|
expect($rules['og_title'])->toContain('string');
|
|
expect($rules['og_title'])->toContain('max:255');
|
|
|
|
expect($rules['og_description'])->toContain('nullable');
|
|
expect($rules['og_description'])->toContain('string');
|
|
expect($rules['og_description'])->toContain('max:300');
|
|
|
|
expect($rules['og_type'])->toContain('nullable');
|
|
expect($rules['og_type'])->toContain('string');
|
|
expect($rules['og_type'])->toContain('max:50');
|
|
expect($rules['og_type'])->toContain('in:website,article,product');
|
|
|
|
expect($rules['twitter_card'])->toContain('nullable');
|
|
expect($rules['twitter_card'])->toContain('string');
|
|
expect($rules['twitter_card'])->toContain('max:50');
|
|
expect($rules['twitter_card'])->toContain('in:summary,summary_large_image');
|
|
|
|
expect($rules['canonical_url'])->toContain('nullable');
|
|
expect($rules['canonical_url'])->toContain('string');
|
|
expect($rules['canonical_url'])->toContain('max:500');
|
|
expect($rules['canonical_url'])->toContain('url');
|
|
});
|
|
|
|
it('validates required fields', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'meta_description' => '',
|
|
'meta_keywords' => '',
|
|
'meta_author' => '',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('meta_description'))->toBeTrue();
|
|
expect($validator->errors()->has('meta_keywords'))->toBeTrue();
|
|
expect($validator->errors()->has('meta_author'))->toBeTrue();
|
|
});
|
|
|
|
it('validates meta description length', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'meta_description' => 'Short',
|
|
'meta_keywords' => 'keywords',
|
|
'meta_author' => 'Author',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('meta_description'))->toBeTrue();
|
|
});
|
|
|
|
it('validates meta description max length', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'meta_description' => str_repeat('a', 161),
|
|
'meta_keywords' => 'keywords',
|
|
'meta_author' => 'Author',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('meta_description'))->toBeTrue();
|
|
});
|
|
|
|
it('validates meta keywords minimum length', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'meta_description' => 'Valid meta description with proper length',
|
|
'meta_keywords' => 'ab',
|
|
'meta_author' => 'Author',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('meta_keywords'))->toBeTrue();
|
|
});
|
|
|
|
it('validates meta author max length', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'meta_description' => 'Valid meta description with proper length',
|
|
'meta_keywords' => 'valid keywords',
|
|
'meta_author' => str_repeat('a', 151),
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('meta_author'))->toBeTrue();
|
|
});
|
|
|
|
it('validates og_image url format and max length', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'meta_description' => 'Valid meta description with proper length',
|
|
'meta_keywords' => 'valid keywords',
|
|
'meta_author' => 'Author',
|
|
'og_image' => 'not-a-url',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('og_image'))->toBeTrue();
|
|
});
|
|
|
|
it('validates og_image max length', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'meta_description' => 'Valid meta description with proper length',
|
|
'meta_keywords' => 'valid keywords',
|
|
'meta_author' => 'Author',
|
|
'og_image' => 'https://example.com/'.str_repeat('a', 501),
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('og_image'))->toBeTrue();
|
|
});
|
|
|
|
it('validates og_title max length', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'meta_description' => 'Valid meta description with proper length',
|
|
'meta_keywords' => 'valid keywords',
|
|
'meta_author' => 'Author',
|
|
'og_title' => str_repeat('a', 256),
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('og_title'))->toBeTrue();
|
|
});
|
|
|
|
it('validates og_description max length', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'meta_description' => 'Valid meta description with proper length',
|
|
'meta_keywords' => 'valid keywords',
|
|
'meta_author' => 'Author',
|
|
'og_description' => str_repeat('a', 301),
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('og_description'))->toBeTrue();
|
|
});
|
|
|
|
it('validates og_type enum values', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'meta_description' => 'Valid meta description with proper length',
|
|
'meta_keywords' => 'valid keywords',
|
|
'meta_author' => 'Author',
|
|
'og_type' => 'invalid_type',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('og_type'))->toBeTrue();
|
|
});
|
|
|
|
it('accepts valid og_type values', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
$validTypes = ['website', 'article', 'product'];
|
|
|
|
foreach ($validTypes as $type) {
|
|
$validator = Validator::make([
|
|
'meta_description' => 'Valid meta description with proper length',
|
|
'meta_keywords' => 'valid keywords',
|
|
'meta_author' => 'Author',
|
|
'og_type' => $type,
|
|
], $rules);
|
|
|
|
expect($validator->passes())->toBeTrue();
|
|
}
|
|
});
|
|
|
|
it('validates twitter_card enum values', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'meta_description' => 'Valid meta description with proper length',
|
|
'meta_keywords' => 'valid keywords',
|
|
'meta_author' => 'Author',
|
|
'twitter_card' => 'invalid_card',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('twitter_card'))->toBeTrue();
|
|
});
|
|
|
|
it('accepts valid twitter_card values', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
$validCards = ['summary', 'summary_large_image'];
|
|
|
|
foreach ($validCards as $card) {
|
|
$validator = Validator::make([
|
|
'meta_description' => 'Valid meta description with proper length',
|
|
'meta_keywords' => 'valid keywords',
|
|
'meta_author' => 'Author',
|
|
'twitter_card' => $card,
|
|
], $rules);
|
|
|
|
expect($validator->passes())->toBeTrue();
|
|
}
|
|
});
|
|
|
|
it('validates canonical_url url format and max length', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'meta_description' => 'Valid meta description with proper length',
|
|
'meta_keywords' => 'valid keywords',
|
|
'meta_author' => 'Author',
|
|
'canonical_url' => 'not-a-url',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('canonical_url'))->toBeTrue();
|
|
});
|
|
|
|
it('accepts nullable fields', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'meta_description' => 'Valid meta description with proper length',
|
|
'meta_keywords' => 'valid keywords',
|
|
'meta_author' => 'Author',
|
|
'og_image' => null,
|
|
'og_title' => null,
|
|
'og_description' => null,
|
|
'og_type' => null,
|
|
'twitter_card' => null,
|
|
'canonical_url' => null,
|
|
], $rules);
|
|
|
|
expect($validator->passes())->toBeTrue();
|
|
});
|
|
|
|
it('passes validation with valid data', function () {
|
|
$form = createSeoForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'meta_description' => 'Valid meta description for SEO purposes',
|
|
'meta_keywords' => 'keyword1, keyword2, keyword3',
|
|
'meta_author' => 'Valid Author',
|
|
'og_image' => 'https://example.com/image.jpg',
|
|
'og_title' => 'Valid OG Title',
|
|
'og_description' => 'Valid OG Description',
|
|
'og_type' => 'article',
|
|
'twitter_card' => 'summary_large_image',
|
|
'canonical_url' => 'https://example.com/page',
|
|
], $rules);
|
|
|
|
expect($validator->passes())->toBeTrue();
|
|
});
|
|
|
|
it('has correct validation attributes', function () {
|
|
$form = createSeoForm();
|
|
$attributes = $form->validationAttributes();
|
|
|
|
expect($attributes)->toHaveKey('meta_description', 'meta deskripsi');
|
|
expect($attributes)->toHaveKey('meta_keywords', 'meta kata kunci');
|
|
expect($attributes)->toHaveKey('meta_author', 'meta penulis');
|
|
expect($attributes)->toHaveKey('og_image', 'gambar Open Graph');
|
|
expect($attributes)->toHaveKey('og_title', 'judul Open Graph');
|
|
expect($attributes)->toHaveKey('og_description', 'deskripsi Open Graph');
|
|
expect($attributes)->toHaveKey('og_type', 'tipe Open Graph');
|
|
expect($attributes)->toHaveKey('twitter_card', 'kartu Twitter');
|
|
expect($attributes)->toHaveKey('canonical_url', 'URL kanonis');
|
|
});
|