diff --git a/app/Liquid/Filters/Data.php b/app/Liquid/Filters/Data.php index 2bbb5a9..4437032 100644 --- a/app/Liquid/Filters/Data.php +++ b/app/Liquid/Filters/Data.php @@ -63,30 +63,4 @@ class Data extends FiltersProvider return $grouped; } - - /** - * Return a random element from an array - * - * @param array $array The array to sample from - * @return mixed A random element from the array - */ - public function sample(array $array): mixed - { - if (empty($array)) { - return null; - } - - return $array[array_rand($array)]; - } - - /** - * Parse a JSON string into a PHP value - * - * @param string $json The JSON string to parse - * @return mixed The parsed JSON value - */ - public function parse_json(string $json): mixed - { - return json_decode($json, true); - } } diff --git a/app/Liquid/Filters/Date.php b/app/Liquid/Filters/Date.php deleted file mode 100644 index 2f730ac..0000000 --- a/app/Liquid/Filters/Date.php +++ /dev/null @@ -1,25 +0,0 @@ -subDays($days)->toDateString(); - } -} diff --git a/app/Models/Plugin.php b/app/Models/Plugin.php index 0dceddf..72da730 100644 --- a/app/Models/Plugin.php +++ b/app/Models/Plugin.php @@ -4,7 +4,6 @@ namespace App\Models; use App\Liquid\FileSystems\InlineTemplatesFileSystem; use App\Liquid\Filters\Data; -use App\Liquid\Filters\Date; use App\Liquid\Filters\Localization; use App\Liquid\Filters\Numbers; use App\Liquid\Filters\StringMarkup; @@ -216,7 +215,6 @@ class Plugin extends Model { $replacements = [ 'date: "%N"' => 'date: "u"', - 'date: "%u"' => 'date: "u"', '%-m/%-d/%Y' => 'm/d/Y', ]; @@ -273,12 +271,11 @@ class Plugin extends Model ); // Register all custom filters - $environment->filterRegistry->register(Data::class); - $environment->filterRegistry->register(Date::class); - $environment->filterRegistry->register(Localization::class); $environment->filterRegistry->register(Numbers::class); + $environment->filterRegistry->register(Data::class); $environment->filterRegistry->register(StringMarkup::class); $environment->filterRegistry->register(Uniqueness::class); + $environment->filterRegistry->register(Localization::class); // Register the template tag for inline templates $environment->tagRegistry->register(TemplateTag::class); diff --git a/resources/views/livewire/plugins/recipe.blade.php b/resources/views/livewire/plugins/recipe.blade.php index 9226af6..7a73358 100644 --- a/resources/views/livewire/plugins/recipe.blade.php +++ b/resources/views/livewire/plugins/recipe.blade.php @@ -574,7 +574,7 @@ HTML; $fieldKey = $field['keyname'] ?? $field['key'] ?? $field['name']; $currentValue = $configuration[$fieldKey] ?? ''; @endphp -
+
@if($field['field_type'] === 'author_bio') @continue @endif @@ -587,35 +587,14 @@ HTML; - @elseif($field['field_type'] === 'text') - - @elseif($field['field_type'] === 'code') - @elseif($field['field_type'] === 'password') @@ -633,7 +611,6 @@ HTML; label="{{ $field['name'] }}" wire:model="configuration.{{ $fieldKey }}" description="{{ $field['description'] ?? '' }}" - descriptionTrailing="{{ $field['help_text'] ?? '' }}" > @foreach(timezone_identifiers_list() as $timezone) @@ -645,7 +622,6 @@ HTML; type="number" label="{{ $field['name'] }}" description="{{ $field['description'] ?? $field['name'] }}" - descriptionTrailing="{{ $field['help_text'] ?? '' }}" wire:model="configuration.{{ $fieldKey }}" value="{{ $currentValue }}" /> @@ -653,7 +629,6 @@ HTML; @@ -662,16 +637,6 @@ HTML; type="date" label="{{ $field['name'] }}" description="{{ $field['description'] ?? $field['name'] }}" - descriptionTrailing="{{ $field['help_text'] ?? '' }}" - wire:model="configuration.{{ $fieldKey }}" - value="{{ $currentValue }}" - /> - @elseif($field['field_type'] === 'time') - @@ -681,7 +646,6 @@ HTML; label="{{ $field['name'] }}" wire:model="configuration.{{ $fieldKey }}" description="{{ $field['description'] ?? '' }}" - descriptionTrailing="{{ $field['help_text'] ?? '' }}" > @if(isset($field['options']) && is_array($field['options'])) @foreach($field['options'] as $option) @@ -700,7 +664,6 @@ HTML; label="{{ $field['name'] }}" wire:model="configuration.{{ $fieldKey }}" description="{{ $field['description'] ?? '' }}" - descriptionTrailing="{{ $field['help_text'] ?? '' }}" > @if(isset($field['options']) && is_array($field['options'])) @@ -724,7 +687,6 @@ HTML; label="{{ $field['name'] }}" wire:model="configuration.{{ $fieldKey }}" description="{{ $field['description'] ?? '' }}" - descriptionTrailing="{{ $field['help_text'] ?? '' }}" wire:init="loadXhrSelectOptions('{{ $fieldKey }}', '{{ $field['endpoint'] }}')" > @@ -760,7 +722,7 @@ HTML; wire:click="searchXhrSelect('{{ $fieldKey }}', '{{ $field['endpoint'] }}')" icon="magnifying-glass"/> - {{ $field['help_text'] ?? '' }} + @if((isset($xhrSelectOptions[$fieldKey]) && is_array($xhrSelectOptions[$fieldKey]) && count($xhrSelectOptions[$fieldKey]) > 0) || !empty($currentValue)) @else - Field type "{{ $field['field_type'] }}" not yet supported +

{{ $field['name'] }}: Field type "{{ $field['field_type'] }}" not yet supported

@endif
@endforeach diff --git a/tests/Feature/PluginInlineTemplatesTest.php b/tests/Feature/PluginInlineTemplatesTest.php index fb35344..ce83d9d 100644 --- a/tests/Feature/PluginInlineTemplatesTest.php +++ b/tests/Feature/PluginInlineTemplatesTest.php @@ -1,15 +1,23 @@ create([ - 'name' => 'Test Plugin', - 'markup_language' => 'liquid', - 'render_markup' => <<<'LIQUID' + public function test_plugin_with_inline_templates(): void + { + $plugin = Plugin::factory()->create([ + 'name' => 'Test Plugin', + 'markup_language' => 'liquid', + 'render_markup' => <<<'LIQUID' {% assign min = 1 %} {% assign max = facts | size %} {% assign diff = max | minus: min %} @@ -31,6 +39,7 @@ test('renders plugin with inline templates', function () { {% template title_bar %}
+ {{ trmnl.plugin_settings.instance_name }} {{ instance }}
@@ -50,31 +59,32 @@ test('renders plugin with inline templates', function () { %}
LIQUID - , - 'data_payload' => [ - 'facts' => ['Fact 1', 'Fact 2', 'Fact 3'], - ], - ]); + , + 'data_payload' => [ + 'facts' => ['Fact 1', 'Fact 2', 'Fact 3'], + ], + ]); - $result = $plugin->render('full'); + $result = $plugin->render('full'); - // Should render both templates - // Check for any of the facts (since random number generation is non-deterministic) - $this->assertTrue( - str_contains($result, 'Fact 1') || - str_contains($result, 'Fact 2') || - str_contains($result, 'Fact 3') - ); - $this->assertStringContainsString('Test Plugin', $result); - $this->assertStringContainsString('Please try to enjoy each fact equally', $result); - $this->assertStringContainsString('class="view view--full"', $result); -}); + // Should render both templates + // Check for any of the facts (since random number generation is non-deterministic) + $this->assertTrue( + str_contains($result, 'Fact 1') || + str_contains($result, 'Fact 2') || + str_contains($result, 'Fact 3') + ); + $this->assertStringContainsString('Test Plugin', $result); + $this->assertStringContainsString('Please try to enjoy each fact equally', $result); + $this->assertStringContainsString('class="view view--full"', $result); + } -test('renders plugin with inline templates using with syntax', function () { - $plugin = Plugin::factory()->create([ - 'name' => 'Test Plugin', - 'markup_language' => 'liquid', - 'render_markup' => <<<'LIQUID' + public function test_plugin_with_inline_templates_using_with_syntax(): void + { + $plugin = Plugin::factory()->create([ + 'name' => 'Test Plugin', + 'markup_language' => 'liquid', + 'render_markup' => <<<'LIQUID' {% assign min = 1 %} {% assign max = facts | size %} {% assign diff = max | minus: min %} @@ -116,30 +126,31 @@ test('renders plugin with inline templates using with syntax', function () { %} LIQUID - , - 'data_payload' => [ - 'facts' => ['Fact 1', 'Fact 2', 'Fact 3'], - ], - ]); + , + 'data_payload' => [ + 'facts' => ['Fact 1', 'Fact 2', 'Fact 3'], + ], + ]); - $result = $plugin->render('full'); + $result = $plugin->render('full'); - // Should render both templates - // Check for any of the facts (since random number generation is non-deterministic) - $this->assertTrue( - str_contains($result, 'Fact 1') || - str_contains($result, 'Fact 2') || - str_contains($result, 'Fact 3') - ); - $this->assertStringContainsString('Test Plugin', $result); - $this->assertStringContainsString('Please try to enjoy each fact equally', $result); - $this->assertStringContainsString('class="view view--full"', $result); -}); + // Should render both templates + // Check for any of the facts (since random number generation is non-deterministic) + $this->assertTrue( + str_contains($result, 'Fact 1') || + str_contains($result, 'Fact 2') || + str_contains($result, 'Fact 3') + ); + $this->assertStringContainsString('Test Plugin', $result); + $this->assertStringContainsString('Please try to enjoy each fact equally', $result); + $this->assertStringContainsString('class="view view--full"', $result); + } -test('renders plugin with simple inline template', function () { - $plugin = Plugin::factory()->create([ - 'markup_language' => 'liquid', - 'render_markup' => <<<'LIQUID' + public function test_plugin_with_simple_inline_template(): void + { + $plugin = Plugin::factory()->create([ + 'markup_language' => 'liquid', + 'render_markup' => <<<'LIQUID' {% template simple %}

{{ title }}

@@ -152,20 +163,21 @@ test('renders plugin with simple inline template', function () { content: "This is a test" %} LIQUID - , - ]); + , + ]); - $result = $plugin->render('full'); + $result = $plugin->render('full'); - $this->assertStringContainsString('Hello World', $result); - $this->assertStringContainsString('This is a test', $result); - $this->assertStringContainsString('class="simple"', $result); -}); + $this->assertStringContainsString('Hello World', $result); + $this->assertStringContainsString('This is a test', $result); + $this->assertStringContainsString('class="simple"', $result); + } -test('renders plugin with liquid filter find_by', function () { - $plugin = Plugin::factory()->create([ - 'markup_language' => 'liquid', - 'render_markup' => <<<'LIQUID' + public function test_plugin_with_find_by_filter(): void + { + $plugin = Plugin::factory()->create([ + 'markup_language' => 'liquid', + 'render_markup' => <<<'LIQUID' {% template user_info %}

{{ user.name }}

@@ -176,65 +188,68 @@ test('renders plugin with liquid filter find_by', function () { {% assign found_user = collection | find_by: 'name', 'Ryan' %} {% render "user_info", user: found_user %} LIQUID - , - 'data_payload' => [ - 'collection' => [ - ['name' => 'Ryan', 'age' => 35], - ['name' => 'Sara', 'age' => 29], - ['name' => 'Jimbob', 'age' => 29], + , + 'data_payload' => [ + 'collection' => [ + ['name' => 'Ryan', 'age' => 35], + ['name' => 'Sara', 'age' => 29], + ['name' => 'Jimbob', 'age' => 29], + ], ], - ], - ]); + ]); - $result = $plugin->render('full'); + $result = $plugin->render('full'); - // Should render the user info for Ryan - $this->assertStringContainsString('Ryan', $result); - $this->assertStringContainsString('Age: 35', $result); - $this->assertStringContainsString('class="user"', $result); -}); + // Should render the user info for Ryan + $this->assertStringContainsString('Ryan', $result); + $this->assertStringContainsString('Age: 35', $result); + $this->assertStringContainsString('class="user"', $result); + } -test('renders plugin with liquid filter find_by and fallback', function () { - $plugin = Plugin::factory()->create([ - 'markup_language' => 'liquid', - 'render_markup' => <<<'LIQUID' + public function test_plugin_with_find_by_filter_and_fallback(): void + { + $plugin = Plugin::factory()->create([ + 'markup_language' => 'liquid', + 'render_markup' => <<<'LIQUID' {{ collection | find_by: 'name', 'ronak', 'Not Found' }} LIQUID - , - 'data_payload' => [ - 'collection' => [ - ['name' => 'Ryan', 'age' => 35], - ['name' => 'Sara', 'age' => 29], - ['name' => 'Jimbob', 'age' => 29], + , + 'data_payload' => [ + 'collection' => [ + ['name' => 'Ryan', 'age' => 35], + ['name' => 'Sara', 'age' => 29], + ['name' => 'Jimbob', 'age' => 29], + ], ], - ], - ]); + ]); - $result = $plugin->render('full'); + $result = $plugin->render('full'); - // Should return the fallback value - $this->assertStringContainsString('Not Found', $result); -}); + // Should return the fallback value + $this->assertStringContainsString('Not Found', $result); + } -test('renders plugin with liquid filter group_by', function () { - $plugin = Plugin::factory()->create([ - 'markup_language' => 'liquid', - 'render_markup' => <<<'LIQUID' + public function test_plugin_with_group_by_filter(): void + { + $plugin = Plugin::factory()->create([ + 'markup_language' => 'liquid', + 'render_markup' => <<<'LIQUID' {{ collection | group_by: 'age' | json }} LIQUID - , - 'data_payload' => [ - 'collection' => [ - ['name' => 'Ryan', 'age' => 35], - ['name' => 'Sara', 'age' => 29], - ['name' => 'Jimbob', 'age' => 29], + , + 'data_payload' => [ + 'collection' => [ + ['name' => 'Ryan', 'age' => 35], + ['name' => 'Sara', 'age' => 29], + ['name' => 'Jimbob', 'age' => 29], + ], ], - ], - ]); + ]); - $result = $plugin->render('full'); + $result = $plugin->render('full'); - // Should output JSON representation of grouped data - $this->assertStringContainsString('"35":[{"name":"Ryan","age":35}]', $result); - $this->assertStringContainsString('"29":[{"name":"Sara","age":29},{"name":"Jimbob","age":29}]', $result); -}); + // Should output JSON representation of grouped data + $this->assertStringContainsString('"35":[{"name":"Ryan","age":35}]', $result); + $this->assertStringContainsString('"29":[{"name":"Sara","age":29},{"name":"Jimbob","age":29}]', $result); + } +} diff --git a/tests/Unit/Liquid/Filters/DataTest.php b/tests/Unit/Liquid/Filters/DataTest.php index bdf649f..8145088 100644 --- a/tests/Unit/Liquid/Filters/DataTest.php +++ b/tests/Unit/Liquid/Filters/DataTest.php @@ -237,91 +237,3 @@ test('group_by filter handles mixed data types as keys', function () { '' => [['name' => 'Alice', 'active' => null]], // PHP converts null keys to empty string ]); }); - -test('sample filter returns a random element from array', function () { - $filter = new Data(); - $array = ['1', '2', '3', '4', '5']; - - $result = $filter->sample($array); - expect($result)->toBeIn($array); -}); - -test('sample filter returns a random element from string array', function () { - $filter = new Data(); - $array = ['cat', 'dog']; - - $result = $filter->sample($array); - expect($result)->toBeIn($array); -}); - -test('sample filter returns null for empty array', function () { - $filter = new Data(); - $array = []; - - $result = $filter->sample($array); - expect($result)->toBeNull(); -}); - -test('sample filter returns the only element from single element array', function () { - $filter = new Data(); - $array = ['single']; - - $result = $filter->sample($array); - expect($result)->toBe('single'); -}); - -test('sample filter works with mixed data types', function () { - $filter = new Data(); - $array = [1, 'string', true, null, ['nested']]; - - $result = $filter->sample($array); - expect($result)->toBeIn($array); -}); - -test('parse_json filter parses JSON string to array', function () { - $filter = new Data(); - $jsonString = '[{"a":1,"b":"c"},"d"]'; - - $result = $filter->parse_json($jsonString); - expect($result)->toBe([['a' => 1, 'b' => 'c'], 'd']); -}); - -test('parse_json filter parses simple JSON object', function () { - $filter = new Data(); - $jsonString = '{"name":"John","age":30,"city":"New York"}'; - - $result = $filter->parse_json($jsonString); - expect($result)->toBe(['name' => 'John', 'age' => 30, 'city' => 'New York']); -}); - -test('parse_json filter parses JSON array', function () { - $filter = new Data(); - $jsonString = '["apple","banana","cherry"]'; - - $result = $filter->parse_json($jsonString); - expect($result)->toBe(['apple', 'banana', 'cherry']); -}); - -test('parse_json filter parses nested JSON structure', function () { - $filter = new Data(); - $jsonString = '{"users":[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}],"total":2}'; - - $result = $filter->parse_json($jsonString); - expect($result)->toBe([ - 'users' => [ - ['id' => 1, 'name' => 'Alice'], - ['id' => 2, 'name' => 'Bob'], - ], - 'total' => 2, - ]); -}); - -test('parse_json filter handles primitive values', function () { - $filter = new Data(); - - expect($filter->parse_json('"hello"'))->toBe('hello'); - expect($filter->parse_json('123'))->toBe(123); - expect($filter->parse_json('true'))->toBe(true); - expect($filter->parse_json('false'))->toBe(false); - expect($filter->parse_json('null'))->toBe(null); -}); diff --git a/tests/Unit/Liquid/Filters/DateTest.php b/tests/Unit/Liquid/Filters/DateTest.php deleted file mode 100644 index 5813e10..0000000 --- a/tests/Unit/Liquid/Filters/DateTest.php +++ /dev/null @@ -1,32 +0,0 @@ -subDays(3)->toDateString(); - - expect($filter->days_ago(3))->toBe($threeDaysAgo); -}); - -test('days_ago filter handles string input', function () { - $filter = new Date(); - $fiveDaysAgo = Carbon::now()->subDays(5)->toDateString(); - - expect($filter->days_ago('5'))->toBe($fiveDaysAgo); -}); - -test('days_ago filter with zero days returns today', function () { - $filter = new Date(); - $today = Carbon::now()->toDateString(); - - expect($filter->days_ago(0))->toBe($today); -}); - -test('days_ago filter with large number works correctly', function () { - $filter = new Date(); - $hundredDaysAgo = Carbon::now()->subDays(100)->toDateString(); - - expect($filter->days_ago(100))->toBe($hundredDaysAgo); -}); diff --git a/tests/Unit/Liquid/InlineTemplatesTest.php b/tests/Unit/Liquid/InlineTemplatesTest.php index e3685b8..bf68ddf 100644 --- a/tests/Unit/Liquid/InlineTemplatesTest.php +++ b/tests/Unit/Liquid/InlineTemplatesTest.php @@ -6,15 +6,9 @@ namespace Tests\Unit\Liquid; use App\Liquid\FileSystems\InlineTemplatesFileSystem; use App\Liquid\Filters\Data; -use App\Liquid\Filters\Date; -use App\Liquid\Filters\Localization; -use App\Liquid\Filters\Numbers; -use App\Liquid\Filters\StringMarkup; -use App\Liquid\Filters\Uniqueness; use App\Liquid\Tags\TemplateTag; use Keepsuit\Liquid\Environment; use Keepsuit\Liquid\Exceptions\LiquidException; -use Keepsuit\Liquid\Extensions\StandardExtension; use Keepsuit\Liquid\Tags\RenderTag; use PHPUnit\Framework\TestCase; @@ -30,17 +24,11 @@ class InlineTemplatesTest extends TestCase $this->fileSystem = new InlineTemplatesFileSystem(); $this->environment = new Environment( - fileSystem: $this->fileSystem, - extensions: [new StandardExtension()] + fileSystem: $this->fileSystem ); $this->environment->tagRegistry->register(TemplateTag::class); $this->environment->tagRegistry->register(RenderTag::class); $this->environment->filterRegistry->register(Data::class); - $this->environment->filterRegistry->register(Date::class); - $this->environment->filterRegistry->register(Localization::class); - $this->environment->filterRegistry->register(Numbers::class); - $this->environment->filterRegistry->register(StringMarkup::class); - $this->environment->filterRegistry->register(Uniqueness::class); } public function test_template_tag_registers_template(): void @@ -308,35 +296,4 @@ LIQUID // Should not throw an error and should return empty string $this->assertEquals('', $result); } - - public function test_quotes_template_with_modulo_filter(): void - { - $template = $this->environment->parseString(<<<'LIQUID' -{% assign quotes_array = quotes[trmnl.plugin_settings.custom_fields_values.language] %} -{% assign random_index = 'now' | date: '%s' | modulo: quotes_array.size %} -{{ quotes_array[random_index] }} -LIQUID - ); - - $context = $this->environment->newRenderContext( - data: [ - 'quotes' => [ - 'english' => ['Demo Quote'], - 'german' => ['Demo Zitat'], - ], - 'trmnl' => [ - 'plugin_settings' => [ - 'custom_fields_values' => [ - 'language' => 'english', - ], - ], - ], - ] - ); - - $result = $template->render($context); - // Should render a quote from the english array - $this->assertStringContainsString('Demo Quote', $result); - $this->assertStringNotContainsString('Demo Zitat', $result); - } }