7 namespace Drupal\big_pipe_test;
9 use Drupal\big_pipe\Render\BigPipeMarkup;
10 use Drupal\Core\Session\AccountInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * BigPipe placeholder test cases for use in both unit and integration tests.
18 * \Drupal\Tests\big_pipe\Unit\Render\Placeholder\BigPipeStrategyTest
19 * - Integration test for BigPipe with JS on:
20 * \Drupal\Tests\big_pipe\Functional\BigPipeTest::testBigPipe()
21 * - Integration test for BigPipe with JS off:
22 * \Drupal\Tests\big_pipe\Functional\BigPipeTest::testBigPipeNoJs()
24 class BigPipePlaceholderTestCases {
27 * Gets all BigPipe placeholder test cases.
29 * @param \Symfony\Component\DependencyInjection\ContainerInterface|null $container
30 * Optional. Necessary to get the embedded AJAX/HTML responses.
31 * @param \Drupal\Core\Session\AccountInterface|null $user
32 * Optional. Necessary to get the embedded AJAX/HTML responses.
34 * @return \Drupal\big_pipe_test\BigPipePlaceholderTestCase[]
36 public static function cases(ContainerInterface $container = NULL, AccountInterface $user = NULL) {
37 // Define the two types of cacheability that we expect to see. These will be
38 // used in the expectations.
39 $cacheability_depends_on_session_only = [
41 'contexts' => ['session.exists'],
43 $cacheability_depends_on_session_and_nojs_cookie = [
45 'contexts' => ['session.exists', 'cookies:big_pipe_nojs'],
48 // 1. Real-world example of HTML placeholder.
49 $status_messages = new BigPipePlaceholderTestCase(
50 ['#type' => 'status_messages'],
51 '<drupal-render-placeholder callback="Drupal\Core\Render\Element\StatusMessages::renderMessages" arguments="0" token="_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA"></drupal-render-placeholder>',
54 'Drupal\Core\Render\Element\StatusMessages::renderMessages',
59 $status_messages->bigPipePlaceholderId = 'callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&args%5B0%5D&token=_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA';
60 $status_messages->bigPipePlaceholderRenderArray = [
61 '#markup' => '<span data-big-pipe-placeholder-id="callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&args%5B0%5D&token=_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA"></span>',
62 '#cache' => $cacheability_depends_on_session_and_nojs_cookie,
64 'library' => ['big_pipe/big_pipe'],
66 'bigPipePlaceholderIds' => [
67 'callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&args%5B0%5D&token=_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA' => TRUE,
70 'big_pipe_placeholders' => [
71 'callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&args%5B0%5D&token=_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA' => $status_messages->placeholderRenderArray,
75 $status_messages->bigPipeNoJsPlaceholder = '<span data-big-pipe-nojs-placeholder-id="callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&args%5B0%5D&token=_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA"></span>';
76 $status_messages->bigPipeNoJsPlaceholderRenderArray = [
77 '#markup' => '<span data-big-pipe-nojs-placeholder-id="callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&args%5B0%5D&token=_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA"></span>',
78 '#cache' => $cacheability_depends_on_session_and_nojs_cookie,
80 'big_pipe_nojs_placeholders' => [
81 '<span data-big-pipe-nojs-placeholder-id="callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&args%5B0%5D&token=_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA"></span>' => $status_messages->placeholderRenderArray,
85 if ($container && $user) {
86 $status_messages->embeddedAjaxResponseCommands = [
88 'command' => 'insert',
89 'method' => 'replaceWith',
90 'selector' => '[data-big-pipe-placeholder-id="callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&args%5B0%5D&token=_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA"]',
91 'data' => ' <div role="contentinfo" aria-label="Status message" class="messages messages--status">' . "\n" . ' <h2 class="visually-hidden">Status message</h2>' . "\n" . ' Hello from BigPipe!' . "\n" . ' </div>' . "\n ",
95 $status_messages->embeddedHtmlResponse = '<div role="contentinfo" aria-label="Status message" class="messages messages--status">' . "\n" . ' <h2 class="visually-hidden">Status message</h2>' . "\n" . ' Hello from BigPipe!' . "\n" . ' </div>' . "\n \n";
98 // 2. Real-world example of HTML attribute value placeholder: form action.
99 $form_action = new BigPipePlaceholderTestCase(
100 $container ? $container->get('form_builder')->getForm('Drupal\big_pipe_test\Form\BigPipeTestForm') : [],
101 'form_action_cc611e1d',
103 '#lazy_builder' => ['form_builder:renderPlaceholderFormAction', []],
106 $form_action->bigPipeNoJsPlaceholder = 'big_pipe_nojs_placeholder_attribute_safe:form_action_cc611e1d';
107 $form_action->bigPipeNoJsPlaceholderRenderArray = [
108 '#markup' => 'big_pipe_nojs_placeholder_attribute_safe:form_action_cc611e1d',
109 '#cache' => $cacheability_depends_on_session_only,
111 'big_pipe_nojs_placeholders' => [
112 'big_pipe_nojs_placeholder_attribute_safe:form_action_cc611e1d' => $form_action->placeholderRenderArray,
117 $form_action->embeddedHtmlResponse = '<form class="big-pipe-test-form" data-drupal-selector="big-pipe-test-form" action="' . base_path() . 'big_pipe_test"';
120 // 3. Real-world example of HTML attribute value subset placeholder: CSRF
122 $csrf_token = new BigPipePlaceholderTestCase(
124 '#title' => 'Link with CSRF token',
126 '#url' => Url::fromRoute('system.theme_set_default'),
128 'e88b559cce72c80b687d56b0e2a3a5ae4b66bc0e',
131 'route_processor_csrf:renderPlaceholderCsrfToken',
132 ['admin/config/user-interface/shortcut/manage/default/add-link-inline'],
136 $csrf_token->bigPipeNoJsPlaceholder = 'big_pipe_nojs_placeholder_attribute_safe:e88b559cce72c80b687d56b0e2a3a5ae4b66bc0e';
137 $csrf_token->bigPipeNoJsPlaceholderRenderArray = [
138 '#markup' => 'big_pipe_nojs_placeholder_attribute_safe:e88b559cce72c80b687d56b0e2a3a5ae4b66bc0e',
139 '#cache' => $cacheability_depends_on_session_only,
141 'big_pipe_nojs_placeholders' => [
142 'big_pipe_nojs_placeholder_attribute_safe:e88b559cce72c80b687d56b0e2a3a5ae4b66bc0e' => $csrf_token->placeholderRenderArray,
147 $csrf_token->embeddedHtmlResponse = $container->get('csrf_token')->get('admin/appearance/default');
150 // 4. Edge case: custom string to be considered as a placeholder that
151 // happens to not be valid HTML.
152 $hello = new BigPipePlaceholderTestCase(
154 '#markup' => BigPipeMarkup::create('<hello'),
157 '<hello' => ['#lazy_builder' => ['\Drupal\big_pipe_test\BigPipeTestController::helloOrYarhar', []]],
169 $hello->bigPipeNoJsPlaceholder = 'big_pipe_nojs_placeholder_attribute_safe:<hello';
170 $hello->bigPipeNoJsPlaceholderRenderArray = [
171 '#markup' => 'big_pipe_nojs_placeholder_attribute_safe:<hello',
172 '#cache' => $cacheability_depends_on_session_only,
174 'big_pipe_nojs_placeholders' => [
175 'big_pipe_nojs_placeholder_attribute_safe:<hello' => $hello->placeholderRenderArray,
179 $hello->embeddedHtmlResponse = '<marquee>Yarhar llamas forever!</marquee>';
181 // 5. Edge case: non-#lazy_builder placeholder.
182 $current_time = new BigPipePlaceholderTestCase(
184 '#markup' => BigPipeMarkup::create('<time>CURRENT TIME</time>'),
187 '<time>CURRENT TIME</time>' => [
189 '\Drupal\big_pipe_test\BigPipeTestController::currentTime',
195 '<time>CURRENT TIME</time>',
197 '#pre_render' => ['current_time'],
200 $current_time->bigPipePlaceholderId = 'timecurrent-timetime';
201 $current_time->bigPipePlaceholderRenderArray = [
202 '#markup' => '<span data-big-pipe-placeholder-id="timecurrent-timetime"></span>',
203 '#cache' => $cacheability_depends_on_session_and_nojs_cookie,
205 'library' => ['big_pipe/big_pipe'],
206 'drupalSettings' => [
207 'bigPipePlaceholderIds' => [
208 'timecurrent-timetime' => TRUE,
211 'big_pipe_placeholders' => [
212 'timecurrent-timetime' => $current_time->placeholderRenderArray,
216 $current_time->embeddedAjaxResponseCommands = [
218 'command' => 'insert',
219 'method' => 'replaceWith',
220 'selector' => '[data-big-pipe-placeholder-id="timecurrent-timetime"]',
221 'data' => '<time datetime="1991-03-14"></time>',
225 $current_time->bigPipeNoJsPlaceholder = '<span data-big-pipe-nojs-placeholder-id="timecurrent-timetime"></span>';
226 $current_time->bigPipeNoJsPlaceholderRenderArray = [
227 '#markup' => '<span data-big-pipe-nojs-placeholder-id="timecurrent-timetime"></span>',
228 '#cache' => $cacheability_depends_on_session_and_nojs_cookie,
230 'big_pipe_nojs_placeholders' => [
231 '<span data-big-pipe-nojs-placeholder-id="timecurrent-timetime"></span>' => $current_time->placeholderRenderArray,
235 $current_time->embeddedHtmlResponse = '<time datetime="1991-03-14"></time>';
237 // 6. Edge case: #lazy_builder that throws an exception.
238 $exception = new BigPipePlaceholderTestCase(
240 '#lazy_builder' => ['\Drupal\big_pipe_test\BigPipeTestController::exception', ['llamas', 'suck']],
241 '#create_placeholder' => TRUE,
243 '<drupal-render-placeholder callback="\Drupal\big_pipe_test\BigPipeTestController::exception" arguments="0=llamas&1=suck" token="uhKFNfT4eF449_W-kDQX8E5z4yHyt0-nSHUlwaGAQeU"></drupal-render-placeholder>',
245 '#lazy_builder' => ['\Drupal\big_pipe_test\BigPipeTestController::exception', ['llamas', 'suck']],
248 $exception->bigPipePlaceholderId = 'callback=%5CDrupal%5Cbig_pipe_test%5CBigPipeTestController%3A%3Aexception&args%5B0%5D=llamas&args%5B1%5D=suck&token=uhKFNfT4eF449_W-kDQX8E5z4yHyt0-nSHUlwaGAQeU';
249 $exception->bigPipePlaceholderRenderArray = [
250 '#markup' => '<span data-big-pipe-placeholder-id="callback=%5CDrupal%5Cbig_pipe_test%5CBigPipeTestController%3A%3Aexception&args%5B0%5D=llamas&args%5B1%5D=suck&token=uhKFNfT4eF449_W-kDQX8E5z4yHyt0-nSHUlwaGAQeU"></span>',
251 '#cache' => $cacheability_depends_on_session_and_nojs_cookie,
253 'library' => ['big_pipe/big_pipe'],
254 'drupalSettings' => [
255 'bigPipePlaceholderIds' => [
256 'callback=%5CDrupal%5Cbig_pipe_test%5CBigPipeTestController%3A%3Aexception&args%5B0%5D=llamas&args%5B1%5D=suck&token=uhKFNfT4eF449_W-kDQX8E5z4yHyt0-nSHUlwaGAQeU' => TRUE,
259 'big_pipe_placeholders' => [
260 'callback=%5CDrupal%5Cbig_pipe_test%5CBigPipeTestController%3A%3Aexception&args%5B0%5D=llamas&args%5B1%5D=suck&token=uhKFNfT4eF449_W-kDQX8E5z4yHyt0-nSHUlwaGAQeU' => $exception->placeholderRenderArray,
264 $exception->embeddedAjaxResponseCommands = NULL;
265 $exception->bigPipeNoJsPlaceholder = '<span data-big-pipe-nojs-placeholder-id="callback=%5CDrupal%5Cbig_pipe_test%5CBigPipeTestController%3A%3Aexception&args%5B0%5D=llamas&args%5B1%5D=suck&token=uhKFNfT4eF449_W-kDQX8E5z4yHyt0-nSHUlwaGAQeU"></span>';
266 $exception->bigPipeNoJsPlaceholderRenderArray = [
267 '#markup' => $exception->bigPipeNoJsPlaceholder,
268 '#cache' => $cacheability_depends_on_session_and_nojs_cookie,
270 'big_pipe_nojs_placeholders' => [
271 $exception->bigPipeNoJsPlaceholder => $exception->placeholderRenderArray,
275 $exception->embeddedHtmlResponse = NULL;
277 // 7. Edge case: response filter throwing an exception for this placeholder.
278 $embedded_response_exception = new BigPipePlaceholderTestCase(
280 '#lazy_builder' => ['\Drupal\big_pipe_test\BigPipeTestController::responseException', []],
281 '#create_placeholder' => TRUE,
283 '<drupal-render-placeholder callback="\Drupal\big_pipe_test\BigPipeTestController::responseException" arguments="" token="PxOHfS_QL-T01NjBgu7Z7I04tIwMp6La5vM-mVxezbU"></drupal-render-placeholder>',
285 '#lazy_builder' => ['\Drupal\big_pipe_test\BigPipeTestController::responseException', []],
288 $embedded_response_exception->bigPipePlaceholderId = 'callback=%5CDrupal%5Cbig_pipe_test%5CBigPipeTestController%3A%3AresponseException&&token=PxOHfS_QL-T01NjBgu7Z7I04tIwMp6La5vM-mVxezbU';
289 $embedded_response_exception->bigPipePlaceholderRenderArray = [
290 '#markup' => '<span data-big-pipe-placeholder-id="callback=%5CDrupal%5Cbig_pipe_test%5CBigPipeTestController%3A%3AresponseException&&token=PxOHfS_QL-T01NjBgu7Z7I04tIwMp6La5vM-mVxezbU"></span>',
291 '#cache' => $cacheability_depends_on_session_and_nojs_cookie,
293 'library' => ['big_pipe/big_pipe'],
294 'drupalSettings' => [
295 'bigPipePlaceholderIds' => [
296 'callback=%5CDrupal%5Cbig_pipe_test%5CBigPipeTestController%3A%3AresponseException&&token=PxOHfS_QL-T01NjBgu7Z7I04tIwMp6La5vM-mVxezbU' => TRUE,
299 'big_pipe_placeholders' => [
300 'callback=%5CDrupal%5Cbig_pipe_test%5CBigPipeTestController%3A%3AresponseException&&token=PxOHfS_QL-T01NjBgu7Z7I04tIwMp6La5vM-mVxezbU' => $embedded_response_exception->placeholderRenderArray,
304 $embedded_response_exception->embeddedAjaxResponseCommands = NULL;
305 $embedded_response_exception->bigPipeNoJsPlaceholder = '<span data-big-pipe-nojs-placeholder-id="callback=%5CDrupal%5Cbig_pipe_test%5CBigPipeTestController%3A%3AresponseException&&token=PxOHfS_QL-T01NjBgu7Z7I04tIwMp6La5vM-mVxezbU"></span>';
306 $embedded_response_exception->bigPipeNoJsPlaceholderRenderArray = [
307 '#markup' => $embedded_response_exception->bigPipeNoJsPlaceholder,
308 '#cache' => $cacheability_depends_on_session_and_nojs_cookie,
310 'big_pipe_nojs_placeholders' => [
311 $embedded_response_exception->bigPipeNoJsPlaceholder => $embedded_response_exception->placeholderRenderArray,
315 $exception->embeddedHtmlResponse = NULL;
318 'html' => $status_messages,
319 'html_attribute_value' => $form_action,
320 'html_attribute_value_subset' => $csrf_token,
321 'edge_case__invalid_html' => $hello,
322 'edge_case__html_non_lazy_builder' => $current_time,
323 'exception__lazy_builder' => $exception,
324 'exception__embedded_response' => $embedded_response_exception,
330 class BigPipePlaceholderTestCase {
333 * The original render array.
340 * The expected corresponding placeholder string.
347 * The expected corresponding placeholder render array.
351 public $placeholderRenderArray;
354 * The expected BigPipe placeholder ID.
356 * (Only possible for HTML placeholders.)
360 public $bigPipePlaceholderId = NULL;
363 * The corresponding expected BigPipe placeholder render array.
367 public $bigPipePlaceholderRenderArray = NULL;
370 * The corresponding expected embedded AJAX response.
374 public $embeddedAjaxResponseCommands = NULL;
378 * The expected BigPipe no-JS placeholder.
380 * (Possible for all placeholders, HTML or non-HTML.)
384 public $bigPipeNoJsPlaceholder;
387 * The corresponding expected BigPipe no-JS placeholder render array.
391 public $bigPipeNoJsPlaceholderRenderArray;
394 * The corresponding expected embedded HTML response.
398 public $embeddedHtmlResponse;
400 public function __construct(array $render_array, $placeholder, array $placeholder_render_array) {
401 $this->renderArray = $render_array;
402 $this->placeholder = $placeholder;
403 $this->placeholderRenderArray = $placeholder_render_array;