|
8 | 8 | use Illuminate\Support\Facades\Bus; |
9 | 9 | use Illuminate\Support\Facades\Event; |
10 | 10 | use Illuminate\Support\Facades\Storage; |
| 11 | +use Illuminate\Support\MessageBag; |
| 12 | +use Illuminate\Support\ViewErrorBag; |
11 | 13 | use Illuminate\Validation\ValidationException; |
12 | 14 | use PHPUnit\Framework\Attributes\DataProvider; |
13 | 15 | use PHPUnit\Framework\Attributes\Test; |
@@ -1386,6 +1388,51 @@ public function it_only_renders_old_input_for_the_submitted_form() |
1386 | 1388 |
|
1387 | 1389 | $output = $this->tag('{{ form:newsletter }}{{ fields }}[{{ handle }}={{ value }}]{{ /fields }}{{ /form:newsletter }}'); |
1388 | 1390 | $this->assertStringContainsString('[email=]', $output); |
| 1391 | + |
| 1392 | + $output = $this->tag('{{ form:contact }}{{ fields }}[{{ handle }}={{ old ?? "null" }}]{{ /fields }}{{ /form:contact }}'); |
| 1393 | + $this->assertStringContainsString('[email=not-an-email]', $output); |
| 1394 | + |
| 1395 | + $output = $this->tag('{{ form:newsletter }}{{ fields }}[{{ handle }}={{ old ?? "null" }}]{{ /fields }}{{ /form:newsletter }}'); |
| 1396 | + $this->assertStringContainsString('[email=null]', $output); |
| 1397 | + } |
| 1398 | + |
| 1399 | + #[Test] |
| 1400 | + public function it_renders_old_input_flashed_without_form_errors() |
| 1401 | + { |
| 1402 | + // Simulates old input flashed by something other than a form submission, |
| 1403 | + // for example a failed login or a custom controller redirecting with input. |
| 1404 | + $this->withSession(['_old_input' => ['name' => 'Frodo Braggins']]); |
| 1405 | + request()->setLaravelSession(app('session.store')); |
| 1406 | + |
| 1407 | + $output = $this->tag('{{ form:contact }}{{ fields }}[{{ handle }}={{ old ?? "null" }}]{{ /fields }}{{ /form:contact }}'); |
| 1408 | + |
| 1409 | + $this->assertStringContainsString('[name=Frodo Braggins]', $output); |
| 1410 | + $this->assertStringContainsString('[email=null]', $output); |
| 1411 | + $this->assertStringContainsString('[message=null]', $output); |
| 1412 | + |
| 1413 | + $output = $this->tag('{{ form:contact }}{{ fields }}[{{ handle }}={{ value }}]{{ /fields }}{{ /form:contact }}'); |
| 1414 | + |
| 1415 | + $this->assertStringContainsString('[name=Frodo Braggins]', $output); |
| 1416 | + $this->assertStringContainsString('[email=]', $output); |
| 1417 | + } |
| 1418 | + |
| 1419 | + #[Test] |
| 1420 | + public function it_renders_old_input_flashed_with_a_non_form_error_bag() |
| 1421 | + { |
| 1422 | + // Simulates old input flashed by something other than a form submission, |
| 1423 | + // while a non-form error bag (e.g. from a failed login) is also in the session. |
| 1424 | + $this->withSession([ |
| 1425 | + '_old_input' => ['name' => 'Frodo Braggins', 'email' => 'flashed@example.com'], |
| 1426 | + 'errors' => (new ViewErrorBag)->put('default', new MessageBag(['email' => 'Invalid.'])), |
| 1427 | + ]); |
| 1428 | + request()->setLaravelSession(app('session.store')); |
| 1429 | + |
| 1430 | + $output = $this->tag('{{ form:contact }}{{ fields }}[{{ handle }}={{ old ?? "null" }}]{{ /fields }}{{ /form:contact }}'); |
| 1431 | + |
| 1432 | + // The old input can't be attributed to another form submission, so it's rendered. |
| 1433 | + $this->assertStringContainsString('[name=Frodo Braggins]', $output); |
| 1434 | + $this->assertStringContainsString('[email=flashed@example.com]', $output); |
| 1435 | + $this->assertStringContainsString('[message=null]', $output); |
1389 | 1436 | } |
1390 | 1437 |
|
1391 | 1438 | #[Test] |
|
0 commit comments