This commit is contained in:
Meghdad committed 2026-07-10 04:20:13 +03:30
commit 8fe27cfa5e
52 files changed
+5299

No files matched your search

+157
View File
@@ -0,0 +1,157 @@
<?php
use MeghdadFadaee\NovaFormCard\Tests\Fixtures\SerializableCard;
use MeghdadFadaee\NovaFormCard\Tests\Fixtures\DependentCard;
it('serializes supported Nova fields for the card component', function (): void {
$serialized = $this->serializeFormCard(new SerializableCard());
$form = $serialized['formCard'];
expect($form['title'])->toBe('Invite User')
->and($form['endpoint'])->toBe('/nova-vendor/nova-form-card/submit')
->and($form['submitText'])->toBe('Submit')
->and($form['canSubmit'])->toBeTrue()
->and($form['token'])->toBeString()->not->toBeEmpty();
$types = collect($form['fields'])->pluck('formCardType', 'attribute')->all();
$components = collect($form['fields'])->pluck('component', 'attribute')->all();
expect($types)->not->toHaveKey('hidden_secret');
expect($types)->toMatchArray([
'email' => 'text',
'message' => 'textarea',
'role' => 'select',
'active' => 'boolean',
'seats' => 'number',
'starts_at' => 'date',
'starts_at_time' => 'date-time',
'work_email' => 'email',
'website' => 'url',
'password' => 'password',
'password_confirmation' => 'password-confirmation',
'tracking_code' => 'hidden',
'record_id' => 'id',
'price' => 'currency',
'brand_color' => 'color',
'payload' => 'code',
'country' => 'country',
'timezone' => 'timezone',
'tags' => 'multi-select',
'permissions' => 'boolean-group',
'metadata' => 'key-value',
'slug' => 'slug',
'api_token' => 'token',
'attachment' => 'file',
'avatar' => 'image',
'profile_photo' => 'avatar',
'voice_note' => 'audio',
'invite_summary' => 'heading',
'queue_label' => 'line',
'invite_stats' => 'stack',
'invite_trend' => 'sparkline',
'health' => 'badge',
'import_status' => 'status',
'sender_email' => 'gravatar',
'sender_name' => 'ui-avatar',
])->and($components)->toMatchArray([
'email' => 'text-field',
'message' => 'textarea-field',
'role' => 'select-field',
'active' => 'boolean-field',
'seats' => 'text-field',
'starts_at' => 'date-field',
'starts_at_time' => 'date-time-field',
'work_email' => 'email-field',
'website' => 'url-field',
'password' => 'password-field',
'password_confirmation' => 'password-field',
'tracking_code' => 'hidden-field',
'record_id' => 'id-field',
'price' => 'currency-field',
'brand_color' => 'color-field',
'payload' => 'code-field',
'country' => 'select-field',
'timezone' => 'select-field',
'tags' => 'multi-select-field',
'permissions' => 'boolean-group-field',
'metadata' => 'key-value-field',
'slug' => 'slug-field',
'api_token' => 'text-field',
'attachment' => 'file-field',
'avatar' => 'file-field',
'profile_photo' => 'file-field',
'voice_note' => 'audio-field',
'invite_summary' => 'heading-field',
'queue_label' => 'line-field',
'invite_stats' => 'stack-field',
'invite_trend' => 'sparkline-field',
'health' => 'badge-field',
'import_status' => 'status-field',
'sender_email' => 'file-field',
'sender_name' => 'file-field',
]);
$displayOnly = collect($form['fields'])->pluck('displayOnly', 'attribute')->all();
expect($displayOnly)->toMatchArray([
'password' => false,
'password_confirmation' => false,
'invite_summary' => true,
'queue_label' => true,
'invite_stats' => true,
'invite_trend' => true,
'health' => true,
'import_status' => true,
'sender_email' => true,
'sender_name' => true,
]);
$email = collect($form['fields'])->firstWhere('attribute', 'email');
$role = collect($form['fields'])->firstWhere('attribute', 'role');
$passwordConfirmation = collect($form['fields'])->firstWhere('attribute', 'password_confirmation');
$line = collect($form['fields'])->firstWhere('attribute', 'queue_label');
$badge = collect($form['fields'])->firstWhere('attribute', 'health');
$status = collect($form['fields'])->firstWhere('attribute', 'import_status');
$stack = collect($form['fields'])->firstWhere('attribute', 'invite_stats');
expect($email['required'])->toBeTrue()
->and($email['value'])->toBe('taylor@example.com')
->and($role['options'])->toContain([
'label' => 'Admin',
'value' => 'admin',
])
->and($badge['label'])->toBe('OK')
->and($passwordConfirmation['type'])->toBe('password')
->and($line['type'])->toBe('line')
->and($status['type'])->toBe('success')
->and($stack['lines'])->toHaveCount(2);
});
it('serializes and syncs Nova dependent fields', function (): void {
$serialized = $this->serializeFormCard(new DependentCard());
$form = $serialized['formCard'];
$email = collect($form['fields'])->firstWhere('attribute', 'email');
expect($form['syncEndpoint'])->toBe('/nova-vendor/nova-form-card/sync')
->and($email['visible'])->toBeFalse()
->and($email['dependsOn'])->toHaveKey('role')
->and($email['dependentComponentKey'])->toBeString()->not->toBeEmpty();
$this->patchJson($form['syncEndpoint'].'?'.http_build_query([
'token' => $form['token'],
'editing' => 'true',
'editMode' => 'create',
'field' => 'email',
'component' => $email['dependentComponentKey'],
]), [
'email' => '',
'role' => 'admin',
])
->assertOk()
->assertJson([
'attribute' => 'email',
'visible' => true,
'type' => 'text',
]);
});
+200
View File
@@ -0,0 +1,200 @@
<?php
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Hash;
use MeghdadFadaee\NovaFormCard\Tests\Fixtures\AdditionalFieldsCard;
use MeghdadFadaee\NovaFormCard\Tests\Fixtures\CreationRulesCard;
use MeghdadFadaee\NovaFormCard\Tests\Fixtures\FailingCard;
use MeghdadFadaee\NovaFormCard\Tests\Fixtures\FileUploadCard;
use MeghdadFadaee\NovaFormCard\Tests\Fixtures\SuccessfulCard;
use MeghdadFadaee\NovaFormCard\Tests\Fixtures\UnauthorizedCard;
beforeEach(function (): void {
SuccessfulCard::$handled = [];
FileUploadCard::$handled = [];
AdditionalFieldsCard::$handled = [];
});
it('rejects unauthorized form card submissions', function (): void {
$token = $this->tokenFor(new UnauthorizedCard());
$this->postJson('/nova-vendor/nova-form-card/submit', [
'token' => $token,
'fields' => [
'email' => 'taylor@example.com',
],
])->assertForbidden();
});
it('validates submitted values using Nova field rules', function (): void {
$token = $this->tokenFor(new SuccessfulCard());
$this->postJson('/nova-vendor/nova-form-card/submit', [
'token' => $token,
'fields' => [
'email' => 'not-an-email',
],
])
->assertStatus(422)
->assertJsonValidationErrors(['email']);
});
it('validates submitted values using Nova creation rules', function (): void {
$token = $this->tokenFor(new CreationRulesCard());
$this->postJson('/nova-vendor/nova-form-card/submit', [
'token' => $token,
'fields' => [
'email' => 'taylor@example.com',
],
])
->assertStatus(422)
->assertJsonValidationErrors(['invite_code'])
->assertJsonMissingValidationErrors(['hidden_secret']);
});
it('ignores hidden fields during submit validation', function (): void {
$token = $this->tokenFor(new CreationRulesCard());
$this->postJson('/nova-vendor/nova-form-card/submit', [
'token' => $token,
'fields' => [
'email' => 'taylor@example.com',
'invite_code' => 'NOVA',
],
])
->assertOk()
->assertJson([
'type' => 'success',
'message' => 'Created.',
]);
});
it('passes validated field values to handle and returns a success response', function (): void {
$token = $this->tokenFor(new SuccessfulCard());
$this->postJson('/nova-vendor/nova-form-card/submit', [
'token' => $token,
'fields' => [
'email' => 'taylor@example.com',
],
])
->assertOk()
->assertJson([
'type' => 'success',
'message' => 'Invitation sent.',
]);
expect(SuccessfulCard::$handled)->toBe([
'email' => 'taylor@example.com',
]);
});
it('passes uploaded file fields to handle', function (): void {
$token = $this->tokenFor(new FileUploadCard());
$this->post('/nova-vendor/nova-form-card/submit', [
'token' => $token,
'fields' => [
'attachment' => UploadedFile::fake()->create('terms.pdf', 12, 'application/pdf'),
'avatar' => UploadedFile::fake()->image('avatar.jpg'),
'voice_note' => UploadedFile::fake()->create('voice.mp3', 12, 'audio/mpeg'),
],
])
->assertOk()
->assertJson([
'type' => 'success',
'message' => 'Files received.',
]);
expect(FileUploadCard::$handled)->toBe([
'attachment' => 'terms.pdf',
'avatar' => 'avatar.jpg',
'voice_note' => 'voice.mp3',
]);
});
it('passes additional supported field values to handle', function (): void {
$token = $this->tokenFor(new AdditionalFieldsCard());
$this->postJson('/nova-vendor/nova-form-card/submit', [
'token' => $token,
'fields' => [
'work_email' => 'person@example.com',
'website' => 'https://example.com',
'secret' => 'super-secret',
'secret_confirmation' => 'super-secret',
'tracking_code' => 'abc123',
'record_id' => '123',
'starts_at_time' => '2026-07-10T10:30:00',
'price' => '12.34',
'brand_color' => '#ff0000',
'payload' => '{"enabled":true}',
'country' => 'IR',
'timezone' => 'Asia/Tehran',
'tags' => '["news","events"]',
'permissions' => '{"read":true,"write":false}',
'metadata' => '{"source":"nova"}',
'slug' => 'person-example',
'api_token' => 'secret-token',
],
])
->assertOk()
->assertJson([
'type' => 'success',
'message' => 'Additional fields received.',
]);
expect(AdditionalFieldsCard::$handled)->toMatchArray([
'work_email' => 'person@example.com',
'website' => 'https://example.com',
'tracking_code' => 'abc123',
'record_id' => '123',
'starts_at_time' => '2026-07-10T10:30:00',
'price' => '12.34',
'brand_color' => '#ff0000',
'payload' => ['enabled' => true],
'country' => 'IR',
'timezone' => 'Asia/Tehran',
'tags' => ['news', 'events'],
'permissions' => ['read' => true, 'write' => false],
'metadata' => ['source' => 'nova'],
'slug' => 'person-example',
'api_token' => 'secret-token',
]);
expect(Hash::check('super-secret', AdditionalFieldsCard::$handled['secret']))->toBeTrue()
->and(AdditionalFieldsCard::$handled)->not->toHaveKey('secret_confirmation');
});
it('validates password confirmation fields', function (): void {
$token = $this->tokenFor(new AdditionalFieldsCard());
$this->postJson('/nova-vendor/nova-form-card/submit', [
'token' => $token,
'fields' => [
'work_email' => 'person@example.com',
'website' => 'https://example.com',
'secret' => 'super-secret',
'secret_confirmation' => 'different-secret',
],
])
->assertStatus(422)
->assertJsonValidationErrors(['secret_confirmation']);
});
it('returns structured error responses from handle', function (): void {
$token = $this->tokenFor(new FailingCard());
$this->postJson('/nova-vendor/nova-form-card/submit', [
'token' => $token,
'fields' => [
'email' => 'taylor@example.com',
],
])
->assertOk()
->assertJson([
'type' => 'error',
'message' => 'No invitation was sent.',
]);
});