Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Render / Placeholder / ChainedPlaceholderStrategyTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Render\Placeholder;
4
5 use Drupal\Core\Render\Placeholder\ChainedPlaceholderStrategy;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Core\Render\Placeholder\ChainedPlaceholderStrategy
10  * @group Render
11  */
12 class ChainedPlaceholderStrategyTest extends UnitTestCase {
13
14   /**
15    * @covers ::addPlaceholderStrategy
16    * @covers ::processPlaceholders
17    *
18    * @dataProvider providerProcessPlaceholders
19    */
20   public function testProcessPlaceholders($strategies, $placeholders, $result) {
21     $chained_placeholder_strategy = new ChainedPlaceholderStrategy();
22
23     foreach ($strategies as $strategy) {
24       $chained_placeholder_strategy->addPlaceholderStrategy($strategy);
25     }
26
27     $this->assertEquals($result, $chained_placeholder_strategy->processPlaceholders($placeholders));
28   }
29
30   /**
31    * Provides a list of render strategies, placeholders and results.
32    *
33    * @return array
34    */
35   public function providerProcessPlaceholders() {
36     $data = [];
37
38     // Empty placeholders.
39     $data['empty placeholders'] = [[], [], []];
40
41     // Placeholder removing strategy.
42     $placeholders = [
43       'remove-me' => ['#markup' => 'I-am-a-llama-that-will-be-removed-sad-face.'],
44     ];
45
46     $prophecy = $this->prophesize('\Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface');
47     $prophecy->processPlaceholders($placeholders)->willReturn([]);
48     $dev_null_strategy = $prophecy->reveal();
49
50     $data['placeholder removing strategy'] = [[$dev_null_strategy], $placeholders, []];
51
52     // Fake Single Flush strategy.
53     $placeholders = [
54       '67890' => ['#markup' => 'special-placeholder'],
55     ];
56
57     $prophecy = $this->prophesize('\Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface');
58     $prophecy->processPlaceholders($placeholders)->willReturn($placeholders);
59     $single_flush_strategy = $prophecy->reveal();
60
61     $data['fake single flush strategy'] = [[$single_flush_strategy], $placeholders, $placeholders];
62
63     // Fake ESI strategy.
64     $placeholders = [
65       '12345' => ['#markup' => 'special-placeholder-for-esi'],
66     ];
67     $result = [
68       '12345' => ['#markup' => '<esi:include src="/fragment/12345" />'],
69     ];
70
71     $prophecy = $this->prophesize('\Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface');
72     $prophecy->processPlaceholders($placeholders)->willReturn($result);
73     $esi_strategy = $prophecy->reveal();
74
75     $data['fake esi strategy'] = [[$esi_strategy], $placeholders, $result];
76
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();
81
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();
87
88     $data['fake esi and single_flush strategy - esi replaces all'] = [[$esi_strategy, $single_flush_strategy], $placeholders, $result];
89
90     // ESI + SingleFlush strategy (mixed).
91     $placeholders = [
92       '12345' => ['#markup' => 'special-placeholder-for-ESI'],
93       '67890' => ['#markup' => 'special-placeholder'],
94       'foo' => ['#markup' => 'bar'],
95     ];
96
97     $esi_result = [
98       '12345' => ['#markup' => '<esi:include src="/fragment/12345" />'],
99     ];
100
101     $normal_result = [
102       '67890' => ['#markup' => 'special-placeholder'],
103       'foo' => ['#markup' => 'bar'],
104     ];
105
106     $result = $esi_result + $normal_result;
107
108     $prophecy = $this->prophesize('\Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface');
109     $prophecy->processPlaceholders($placeholders)->willReturn($esi_result);
110     $esi_strategy = $prophecy->reveal();
111
112     $prophecy = $this->prophesize('\Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface');
113     $prophecy->processPlaceholders($normal_result)->willReturn($normal_result);
114     $single_flush_strategy = $prophecy->reveal();
115
116     $data['fake esi and single_flush strategy - mixed'] = [[$esi_strategy, $single_flush_strategy], $placeholders, $result];
117
118     return $data;
119   }
120
121   /**
122    * @covers ::processPlaceholders
123    */
124   public function testProcessPlaceholdersNoStrategies() {
125     // Placeholders but no strategies defined.
126     $placeholders = [
127       'assert-me' => ['#markup' => 'I-am-a-llama-that-will-lead-to-an-assertion-by-the-chained-placeholder-strategy.'],
128     ];
129
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);
133   }
134
135   /**
136    * @covers ::processPlaceholders
137    */
138   public function testProcessPlaceholdersWithRoguePlaceholderStrategy() {
139     // Placeholders but no strategies defined.
140     $placeholders = [
141       'assert-me' => ['#markup' => 'llama'],
142     ];
143
144     $result = [
145       'assert-me' => ['#markup' => 'llama'],
146       'new-placeholder' => ['#markup' => 'rogue llama'],
147     ];
148
149     $prophecy = $this->prophesize('\Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface');
150     $prophecy->processPlaceholders($placeholders)->willReturn($result);
151     $rogue_strategy = $prophecy->reveal();
152
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);
157   }
158
159 }