3 namespace Drupal\Tests\Core\Render\Placeholder;
5 use Drupal\Core\Render\Placeholder\ChainedPlaceholderStrategy;
6 use Drupal\Tests\UnitTestCase;
9 * @coversDefaultClass \Drupal\Core\Render\Placeholder\ChainedPlaceholderStrategy
12 class ChainedPlaceholderStrategyTest extends UnitTestCase {
15 * @covers ::addPlaceholderStrategy
16 * @covers ::processPlaceholders
18 * @dataProvider providerProcessPlaceholders
20 public function testProcessPlaceholders($strategies, $placeholders, $result) {
21 $chained_placeholder_strategy = new ChainedPlaceholderStrategy();
23 foreach ($strategies as $strategy) {
24 $chained_placeholder_strategy->addPlaceholderStrategy($strategy);
27 $this->assertEquals($result, $chained_placeholder_strategy->processPlaceholders($placeholders));
31 * Provides a list of render strategies, placeholders and results.
35 public function providerProcessPlaceholders() {
38 // Empty placeholders.
39 $data['empty placeholders'] = [[], [], []];
41 // Placeholder removing strategy.
43 'remove-me' => ['#markup' => 'I-am-a-llama-that-will-be-removed-sad-face.'],
46 $prophecy = $this->prophesize('\Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface');
47 $prophecy->processPlaceholders($placeholders)->willReturn([]);
48 $dev_null_strategy = $prophecy->reveal();
50 $data['placeholder removing strategy'] = [[$dev_null_strategy], $placeholders, []];
52 // Fake Single Flush strategy.
54 '67890' => ['#markup' => 'special-placeholder'],
57 $prophecy = $this->prophesize('\Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface');
58 $prophecy->processPlaceholders($placeholders)->willReturn($placeholders);
59 $single_flush_strategy = $prophecy->reveal();
61 $data['fake single flush strategy'] = [[$single_flush_strategy], $placeholders, $placeholders];
65 '12345' => ['#markup' => 'special-placeholder-for-esi'],
68 '12345' => ['#markup' => '<esi:include src="/fragment/12345" />'],
71 $prophecy = $this->prophesize('\Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface');
72 $prophecy->processPlaceholders($placeholders)->willReturn($result);
73 $esi_strategy = $prophecy->reveal();
75 $data['fake esi strategy'] = [[$esi_strategy], $placeholders, $result];
77 // ESI + SingleFlush strategy (ESI replaces all).
78 $prophecy = $this->prophesize('\Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface');
79 $prophecy->processPlaceholders($placeholders)->willReturn($result);
80 $esi_strategy = $prophecy->reveal();
82 $prophecy = $this->prophesize('\Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface');
83 $prophecy->processPlaceholders($placeholders)->shouldNotBeCalled();
84 $prophecy->processPlaceholders($result)->shouldNotBeCalled();
85 $prophecy->processPlaceholders([])->shouldNotBeCalled();
86 $single_flush_strategy = $prophecy->reveal();
88 $data['fake esi and single_flush strategy - esi replaces all'] = [[$esi_strategy, $single_flush_strategy], $placeholders, $result];
90 // ESI + SingleFlush strategy (mixed).
92 '12345' => ['#markup' => 'special-placeholder-for-ESI'],
93 '67890' => ['#markup' => 'special-placeholder'],
94 'foo' => ['#markup' => 'bar'],
98 '12345' => ['#markup' => '<esi:include src="/fragment/12345" />'],
102 '67890' => ['#markup' => 'special-placeholder'],
103 'foo' => ['#markup' => 'bar'],
106 $result = $esi_result + $normal_result;
108 $prophecy = $this->prophesize('\Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface');
109 $prophecy->processPlaceholders($placeholders)->willReturn($esi_result);
110 $esi_strategy = $prophecy->reveal();
112 $prophecy = $this->prophesize('\Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface');
113 $prophecy->processPlaceholders($normal_result)->willReturn($normal_result);
114 $single_flush_strategy = $prophecy->reveal();
116 $data['fake esi and single_flush strategy - mixed'] = [[$esi_strategy, $single_flush_strategy], $placeholders, $result];
122 * @covers ::processPlaceholders
124 public function testProcessPlaceholdersNoStrategies() {
125 // Placeholders but no strategies defined.
127 'assert-me' => ['#markup' => 'I-am-a-llama-that-will-lead-to-an-assertion-by-the-chained-placeholder-strategy.'],
130 $chained_placeholder_strategy = new ChainedPlaceholderStrategy();
131 $this->setExpectedException(\AssertionError::class, 'At least one placeholder strategy must be present; by default the fallback strategy \Drupal\Core\Render\Placeholder\SingleFlushStrategy is always present.');
132 $chained_placeholder_strategy->processPlaceholders($placeholders);
136 * @covers ::processPlaceholders
138 public function testProcessPlaceholdersWithRoguePlaceholderStrategy() {
139 // Placeholders but no strategies defined.
141 'assert-me' => ['#markup' => 'llama'],
145 'assert-me' => ['#markup' => 'llama'],
146 'new-placeholder' => ['#markup' => 'rogue llama'],
149 $prophecy = $this->prophesize('\Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface');
150 $prophecy->processPlaceholders($placeholders)->willReturn($result);
151 $rogue_strategy = $prophecy->reveal();
153 $chained_placeholder_strategy = new ChainedPlaceholderStrategy();
154 $chained_placeholder_strategy->addPlaceholderStrategy($rogue_strategy);
155 $this->setExpectedException(\AssertionError::class, 'Processed placeholders must be a subset of all placeholders.');
156 $chained_placeholder_strategy->processPlaceholders($placeholders);