Compare commits

..

No commits in common. "f777e850b19fc8860df494f438b3ff032008a136" and "58fad59301c22e39990ecfc305dff686e0d1d8cd" have entirely different histories.

8 changed files with 128 additions and 368 deletions

View file

@ -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);
}
}

View file

@ -1,25 +0,0 @@
<?php
namespace App\Liquid\Filters;
use Carbon\Carbon;
use Keepsuit\Liquid\Filters\FiltersProvider;
/**
* Data filters for Liquid templates
*/
class Date extends FiltersProvider
{
/**
* Calculate a date that is a specified number of days in the past
*
* @param int|string $num The number of days to subtract
* @return string The date in Y-m-d format
*/
public function days_ago(int|string $num): string
{
$days = (int) $num;
return Carbon::now()->subDays($days)->toDateString();
}
}

View file

@ -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);

View file

@ -574,7 +574,7 @@ HTML;
$fieldKey = $field['keyname'] ?? $field['key'] ?? $field['name'];
$currentValue = $configuration[$fieldKey] ?? '';
@endphp
<div class="mb-4">
<div class="mb-8">
@if($field['field_type'] === 'author_bio')
@continue
@endif
@ -587,35 +587,14 @@ HTML;
<flux:input
label="{{ $field['name'] }}"
description="{{ $field['description'] ?? '' }}"
descriptionTrailing="{{ $field['help_text'] ?? '' }}"
wire:model="configuration.{{ $fieldKey }}"
value="{{ $currentValue }}"
/>
@elseif($field['field_type'] === 'text')
<flux:textarea
label="{{ $field['name'] }}"
description="{{ $field['description'] ?? '' }}"
descriptionTrailing="{{ $field['help_text'] ?? '' }}"
wire:model="configuration.{{ $fieldKey }}"
value="{{ $currentValue }}"
/>
@elseif($field['field_type'] === 'code')
<flux:textarea
label="{{ $field['name'] }}"
description="{{ $field['description'] ?? '' }}"
descriptionTrailing="{{ $field['help_text'] ?? '' }}"
rows="{{ $field['rows'] ?? 3 }}"
placeholder="{{ $field['placeholder'] ?? null }}"
wire:model="configuration.{{ $fieldKey }}"
value="{{ $currentValue }}"
class="font-mono"
/>
@elseif($field['field_type'] === 'password')
<flux:input
type="password"
label="{{ $field['name'] }}"
description="{{ $field['description'] ?? '' }}"
descriptionTrailing="{{ $field['help_text'] ?? '' }}"
wire:model="configuration.{{ $fieldKey }}"
value="{{ $currentValue }}"
viewable
@ -624,7 +603,6 @@ HTML;
<flux:input
label="{{ $field['name'] }}"
description="{{ $field['description'] ?? '' }}"
descriptionTrailing="{{ $field['help_text'] ?? '' }}"
value="{{ $field['value'] }}"
copyable
/>
@ -633,7 +611,6 @@ HTML;
label="{{ $field['name'] }}"
wire:model="configuration.{{ $fieldKey }}"
description="{{ $field['description'] ?? '' }}"
descriptionTrailing="{{ $field['help_text'] ?? '' }}"
>
<option value="">Select timezone...</option>
@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;
<flux:checkbox
label="{{ $field['name'] }}"
description="{{ $field['description'] ?? $field['name'] }}"
descriptionTrailing="{{ $field['help_text'] ?? '' }}"
wire:model="configuration.{{ $fieldKey }}"
:checked="$currentValue"
/>
@ -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')
<flux:input
type="time"
label="{{ $field['name'] }}"
description="{{ $field['description'] ?? $field['name'] }}"
descriptionTrailing="{{ $field['help_text'] ?? '' }}"
wire:model="configuration.{{ $fieldKey }}"
value="{{ $currentValue }}"
/>
@ -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'] ?? '' }}"
>
<option value="">Select {{ $field['name'] }}...</option>
@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'] }}')"
>
<option value="">Select {{ $field['name'] }}...</option>
@ -760,7 +722,7 @@ HTML;
wire:click="searchXhrSelect('{{ $fieldKey }}', '{{ $field['endpoint'] }}')"
icon="magnifying-glass"/>
</flux:input.group>
<flux:description>{{ $field['help_text'] ?? '' }}</flux:description>
@if((isset($xhrSelectOptions[$fieldKey]) && is_array($xhrSelectOptions[$fieldKey]) && count($xhrSelectOptions[$fieldKey]) > 0) || !empty($currentValue))
<flux:select
wire:model="configuration.{{ $fieldKey }}"
@ -791,7 +753,7 @@ HTML;
@endif
</div>
@else
<flux:callout variant="warning">Field type "{{ $field['field_type'] }}" not yet supported</flux:callout>
<p>{{ $field['name'] }}: Field type "{{ $field['field_type'] }}" not yet supported</p>
@endif
</div>
@endforeach

View file

@ -1,15 +1,23 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Models\Plugin;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
uses(RefreshDatabase::class);
class PluginInlineTemplatesTest extends TestCase
{
use RefreshDatabase;
test('renders plugin with inline templates', function () {
$plugin = Plugin::factory()->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 %}
<div class="title_bar">
<img class="image" src="https://res.jwq.lol/img/lumon.svg">
<span class="title">{{ trmnl.plugin_settings.instance_name }}</span>
<span class="instance">{{ instance }}</span>
</div>
@ -50,31 +59,32 @@ test('renders plugin with inline templates', function () {
%}
</div>
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 () {
%}
</div>
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 %}
<div class="simple">
<h1>{{ title }}</h1>
@ -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 %}
<div class="user">
<h2>{{ user.name }}</h2>
@ -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);
}
}

View file

@ -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);
});

View file

@ -1,32 +0,0 @@
<?php
use App\Liquid\Filters\Date;
use Carbon\Carbon;
test('days_ago filter returns correct date', function () {
$filter = new Date();
$threeDaysAgo = Carbon::now()->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);
});

View file

@ -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);
}
}