|
| 1 | +<?php |
| 2 | + |
| 3 | +use Twig\Environment; |
| 4 | +use Twig\Loader\ArrayLoader; |
| 5 | + |
| 6 | +/** |
| 7 | + * Regression coverage for getgrav/grav#4256. |
| 8 | + * |
| 9 | + * Twig's own parser refuses a `block` definition nested under an `if`, so the |
| 10 | + * getgrav/Twig fork carries a patch that makes `if` transparent when a child |
| 11 | + * template's body is cleaned up. Grav themes and plugins rely on it: the form |
| 12 | + * plugin's forms/default/field.html.twig wraps its whole body — the `extends` |
| 13 | + * tag included — in a single `{% if not field.validate.ignore %}`. |
| 14 | + * |
| 15 | + * Upstream 3.27 replaced the recursive Parser::filterBodyNodes() with the flat |
| 16 | + * Parser::cleanupBodyForChildTemplates(), and re-homing the fork patch onto it |
| 17 | + * lost the case where that `if` IS the body rather than one node inside it. The |
| 18 | + * block definitions were then rendered in place as well as through the parent, |
| 19 | + * so every form field printed its attributes as text above the field. |
| 20 | + * |
| 21 | + * These tests pin the parser behaviour the fork has to keep providing. They are |
| 22 | + * deliberately free of Grav bootstrapping so they stay a reliable canary for the |
| 23 | + * vendored Twig itself. |
| 24 | + */ |
| 25 | +class TwigConditionalBlockTest extends \PHPUnit\Framework\TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @param array<string, string> $templates |
| 29 | + * @return string |
| 30 | + */ |
| 31 | + protected function render(array $templates, string $name = 'child.twig'): string |
| 32 | + { |
| 33 | + $twig = new Environment(new ArrayLoader($templates), [ |
| 34 | + 'autoescape' => 'html', |
| 35 | + 'cache' => false, |
| 36 | + 'debug' => true, |
| 37 | + ]); |
| 38 | + |
| 39 | + return $twig->render($name); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * The whole child body is one `if`, with `extends` inside it. This is the |
| 44 | + * shape the form plugin uses, and the one #4256 broke. |
| 45 | + */ |
| 46 | + public function testBlocksUnderAnIfWrappingTheWholeBodyAreNotRenderedInPlace() |
| 47 | + { |
| 48 | + $output = $this->render([ |
| 49 | + 'layout.twig' => '<div {% block outer %}{% endblock %}><input {% block inner %}{% endblock %} /></div>', |
| 50 | + 'child.twig' => <<<TWIG |
| 51 | +{% if true %} |
| 52 | +{% extends "layout.twig" %} |
| 53 | +{% block outer %}data-a="1"{% endblock %} |
| 54 | +{% block inner %}class="c"{% endblock %} |
| 55 | +{% endif %} |
| 56 | +TWIG, |
| 57 | + ]); |
| 58 | + |
| 59 | + $this->assertSame('<div data-a="1"><input class="c" /></div>', trim($output)); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * The same shape one level deeper: a grandchild overriding a block and |
| 64 | + * calling parent(), as forms/fields/text/text.html.twig does. |
| 65 | + */ |
| 66 | + public function testParentResolvesThroughAConditionalChildTemplate() |
| 67 | + { |
| 68 | + $output = $this->render([ |
| 69 | + 'layout.twig' => '<input {% block attrs %}{% endblock %} />', |
| 70 | + 'middle.twig' => <<<TWIG |
| 71 | +{% if true %} |
| 72 | +{% extends "layout.twig" %} |
| 73 | +{% block attrs %}class="c"{% endblock %} |
| 74 | +{% endif %} |
| 75 | +TWIG, |
| 76 | + 'child.twig' => <<<TWIG |
| 77 | +{% extends "middle.twig" %} |
| 78 | +{% block attrs %}type="text" {{ parent() }}{% endblock %} |
| 79 | +TWIG, |
| 80 | + ]); |
| 81 | + |
| 82 | + $this->assertSame('<input type="text" class="c" />', trim($output)); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * The condition only decides whether the definition also renders in place; |
| 87 | + * the override itself is registered at compile time either way. A false |
| 88 | + * condition must therefore still override the parent, and still not leak. |
| 89 | + */ |
| 90 | + public function testAFalseConditionStillRegistersTheOverrideWithoutRenderingInPlace() |
| 91 | + { |
| 92 | + $output = $this->render([ |
| 93 | + 'layout.twig' => '[{% block content %}DEFAULT{% endblock %}]', |
| 94 | + 'child.twig' => <<<TWIG |
| 95 | +{% extends "layout.twig" %} |
| 96 | +{% if false %} |
| 97 | +{% block content %}OVERRIDE{% endblock %} |
| 98 | +{% endif %} |
| 99 | +TWIG, |
| 100 | + ]); |
| 101 | + |
| 102 | + $this->assertSame('[OVERRIDE]', trim($output)); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * The pre-existing case: the `if` is one node among others at the root of |
| 107 | + * the child. Guards against a fix for the above regressing this direction. |
| 108 | + */ |
| 109 | + public function testBlocksUnderAnIfAlongsideOtherRootNodesAreNotRenderedInPlace() |
| 110 | + { |
| 111 | + $output = $this->render([ |
| 112 | + 'layout.twig' => '[{% block first %}{% endblock %}][{% block second %}{% endblock %}]', |
| 113 | + 'child.twig' => <<<TWIG |
| 114 | +{% extends "layout.twig" %} |
| 115 | +{% block first %}FOO{% endblock %} |
| 116 | +{% if true %} |
| 117 | +{% block second %}BAR{% endblock %} |
| 118 | +{% endif %} |
| 119 | +TWIG, |
| 120 | + ]); |
| 121 | + |
| 122 | + $this->assertSame('[FOO][BAR]', trim($output)); |
| 123 | + } |
| 124 | +} |
0 commit comments