Skip to content

[5.x] Corrects Antlers error logging with PHP nodes #11800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/View/Antlers/Language/Runtime/GlobalRuntimeState.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public static function mergeTagRuntimeAssignments($assignments)

public static function resetGlobalState()
{
self::$templateFileStack = [];
self::$shareVariablesTemplateTrigger = '';
self::$layoutVariables = [];
self::$containsLayout = false;
Expand Down
6 changes: 4 additions & 2 deletions src/View/Antlers/Language/Runtime/NodeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,9 @@ public function reduce($processNodes)
'User content Antlers PHP tag.'
);
} else {
Log::warning('PHP Node evaluated in user content: '.$node->name->name, [
$logContent = $node->rawStart.$node->innerContent().$node->rawEnd;

Log::warning('PHP Node evaluated in user content: '.$logContent, [
'file' => GlobalRuntimeState::$currentExecutionFile,
'trace' => GlobalRuntimeState::$templateFileStack,
'content' => $node->innerContent(),
Expand Down Expand Up @@ -2456,7 +2458,7 @@ protected function evaluatePhp($buffer)

protected function evaluateAntlersPhpNode(PhpExecutionNode $node)
{
if (! GlobalRuntimeState::$allowPhpInContent == false && GlobalRuntimeState::$isEvaluatingUserData) {
if (! GlobalRuntimeState::$allowPhpInContent && GlobalRuntimeState::$isEvaluatingUserData) {
return StringUtilities::sanitizePhp($node->content);
}

Expand Down
80 changes: 78 additions & 2 deletions tests/Antlers/Runtime/PhpEnabledTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace Tests\Antlers\Runtime;

use Illuminate\Support\Facades\Log;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Fields\Field;
use Statamic\Fields\Fieldtype;
use Statamic\Fields\Value;
use Statamic\Fieldtypes\Text;
use Statamic\View\Antlers\Language\Runtime\GlobalRuntimeState;
use Statamic\View\Antlers\Language\Runtime\RuntimeConfiguration;
use Statamic\View\Antlers\Language\Utilities\StringUtilities;
use Tests\Antlers\ParserTestCase;
Expand Down Expand Up @@ -513,8 +517,8 @@ public function test_php_node_assignments_within_loops()
public function test_assignments_from_php_nodes()
{
$template = <<<'EOT'
{{?
$value_one = 100;
{{?
$value_one = 100;
$value_two = 0;
?}}

Expand All @@ -533,4 +537,76 @@ public function test_assignments_from_php_nodes()
$this->assertStringContainsString('<value_one: 1125>', $result);
$this->assertStringContainsString('<value_two: 1025>', $result);
}

public function test_disabled_php_echo_node_inside_user_values()
{
$textFieldtype = new Text();
$field = new Field('text_field', [
'type' => 'text',
'antlers' => true,
]);

$textContent = <<<'TEXT'
Text: {{$ Str::upper('hello, world.') $}}
TEXT;

$textFieldtype->setField($field);
$value = new Value($textContent, 'text_field', $textFieldtype);

Log::shouldReceive('warning')
->once()
->with("PHP Node evaluated in user content: {{\$ Str::upper('hello, world.') \$}}", [
'file' => null,
'trace' => [],
'content' => " Str::upper('hello, world.') ",
]);

$result = $this->renderString('{{ text_field }}', ['text_field' => $value]);

$this->assertSame('Text: ', $result);

GlobalRuntimeState::$allowPhpInContent = true;

$result = $this->renderString('{{ text_field }}', ['text_field' => $value]);

$this->assertSame('Text: HELLO, WORLD.', $result);

GlobalRuntimeState::$allowPhpInContent = false;
}

public function test_disabled_php_node_inside_user_values()
{
$textFieldtype = new Text();
$field = new Field('text_field', [
'type' => 'text',
'antlers' => true,
]);

$textContent = <<<'TEXT'
Text: {{? echo Str::upper('hello, world.') ?}}
TEXT;

$textFieldtype->setField($field);
$value = new Value($textContent, 'text_field', $textFieldtype);

Log::shouldReceive('warning')
->once()
->with("PHP Node evaluated in user content: {{? echo Str::upper('hello, world.') ?}}", [
'file' => null,
'trace' => [],
'content' => " echo Str::upper('hello, world.') ",
]);

$result = $this->renderString('{{ text_field }}', ['text_field' => $value]);

$this->assertSame('Text: ', $result);

GlobalRuntimeState::$allowPhpInContent = true;

$result = $this->renderString('{{ text_field }}', ['text_field' => $value]);

$this->assertSame('Text: HELLO, WORLD.', $result);

GlobalRuntimeState::$allowPhpInContent = false;
}
}