Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Nov 28, 2024
1 parent 70efef2 commit ec49f57
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tests/Tags/Form/FormCreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\AssetContainer;
use Statamic\Facades\Form;
use Statamic\Facades\Site;
use Statamic\Statamic;

class FormCreateTest extends FormTestCase
Expand Down Expand Up @@ -53,6 +55,16 @@ public function it_renders_form_with_redirects_to_anchor()
$this->assertStringContainsString('<input type="hidden" name="_error_redirect" value="http://localhost#form" />', $output);
}

#[Test]
public function it_renders_original_url()
{
Request::swap(Request::create('http://localhost/contact'));

$output = $this->tag('{{ form:contact }}');

$this->assertStringContainsString('<input type="hidden" name="_original_url" value="http://localhost/contact" />', $output);
}

#[Test]
public function it_dynamically_renders_fields_array()
{
Expand Down Expand Up @@ -804,6 +816,53 @@ public function it_can_render_an_inline_error_when_multiple_rules_fail()
$this->assertEquals($expectedInline, $inlineErrors[1]);
}

#[Test]
public function it_uses_original_url_to_localize_error_messages()
{
trans()->addLines([
'validation.required' => 'Das :attribute Feld ist erforderlich.',
], 'de');

Site::setSites([
'default' => [
'name' => 'English',
'url' => '/',
'locale' => 'en_US',
],
'german' => [
'name' => 'German',
'url' => '/de/',
'locale' => 'de_DE',
],
]);

$this->assertEmpty(Form::find('contact')->submissions());

$this
->post('/!/forms/contact', [
'_original_url' => 'http://localhost/de/contact',
'email' => '[email protected]',
'message' => '',
])
->assertSessionHasErrors(['message'], null, 'form.contact')
->assertLocation('/');

$this->assertEmpty(Form::find('contact')->submissions());

$output = $this->tag(<<<'EOT'
{{ form:contact }}
{{ errors }}
<p class="error">{{ value }}</p>
{{ /errors }}
<p class="inline-error">{{ error:name }}</p>
{{ /form:contact }}
EOT
);

$this->assertStringContainsString('<p class="error">Das Message Feld ist erforderlich.</p>', $output);
$this->assertStringNotContainsString('<p class="error">The Message field is required.</p>', $output);
}

#[Test]
public function it_fetches_form_data()
{
Expand Down

0 comments on commit ec49f57

Please sign in to comment.