Skip to content

Commit 8345937

Browse files
committed
Add test coverage for escaping show_field JS with blade component.
1 parent dd1ee94 commit 8345937

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

tests/Tags/Form/FormCreateAlpineTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,35 @@ public function it_merges_any_x_data_passed_to_the_tag()
809809
$this->assertStringContainsString($expected, $output);
810810
}
811811

812+
#[Test]
813+
public function it_properly_escapes_show_field_js_in_blade()
814+
{
815+
$output = $this->blade(<<<'EOT'
816+
<s:form:contact js="alpine">
817+
<template x-if="{{ $show_field['message'] }}"></template>
818+
<template x-if="{{ $show_field['my_favourites'] }}"></template>
819+
<template x-if="{{ $show_field['my_favourites.favourite_animals'] }}"></template>
820+
<s:form:fields>
821+
@if ($field['handle'] === 'message')
822+
<template x-if="{{ $field['show_field'] }}"></template>
823+
@endif
824+
</s:form:fields>
825+
</s:form:contact>
826+
EOT
827+
);
828+
829+
preg_match_all('/<template x-if="(.+)"><\/template>/U', $output, $js);
830+
831+
$expected = [
832+
'Statamic.$conditions.showField('.$this->jsonEncode(['if' => ['email' => 'not empty']]).', $data, \'message\')',
833+
'Statamic.$conditions.showField('.$this->jsonEncode(['if' => ['name' => 'not empty']]).', $data, \'my_favourites\')',
834+
'Statamic.$conditions.showField('.$this->jsonEncode(['if' => ['$root.likes_animals' => 'is true']]).', $data, \'my_favourites.favourite_animals\')',
835+
'Statamic.$conditions.showField('.$this->jsonEncode(['if' => ['email' => 'not empty']]).', $data, \'message\')',
836+
];
837+
838+
$this->assertSame($expected, $js[1]);
839+
}
840+
812841
private function jsonEncode($data)
813842
{
814843
return Statamic::modify($data)->toJson()->entities();

tests/Tags/Form/FormTestCase.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Tags\Form;
44

5+
use Illuminate\Support\Facades\Blade;
56
use Statamic\Facades\Blueprint;
67
use Statamic\Facades\Form;
78
use Statamic\Facades\Parse;
@@ -67,9 +68,14 @@ public function post($uri, array $data = [], array $headers = [])
6768
], $headers));
6869
}
6970

70-
protected function tag($tag, $params = [])
71+
protected function tag($string, $context = [])
7172
{
72-
return Parse::template($tag, $params);
73+
return Parse::template($string, $context);
74+
}
75+
76+
protected function blade($string, $context = [])
77+
{
78+
return Blade::render($string, $context);
7379
}
7480

7581
protected function createForm($blueprintContents = null, $handle = null)

0 commit comments

Comments
 (0)