201 lines
6.4 KiB
PHP
201 lines
6.4 KiB
PHP
<?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.',
|
|
]);
|
|
});
|