X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fbig_pipe%2Ftests%2Fmodules%2Fbig_pipe_test%2Fsrc%2FBigPipePlaceholderTestCases.php;fp=web%2Fcore%2Fmodules%2Fbig_pipe%2Ftests%2Fmodules%2Fbig_pipe_test%2Fsrc%2FBigPipePlaceholderTestCases.php;h=e3da529ea46d6cda33df3bef2b7e2865ab610ff6;hp=0000000000000000000000000000000000000000;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hpb=aea91e65e895364e460983b890e295aa5d5540a5 diff --git a/web/core/modules/big_pipe/tests/modules/big_pipe_test/src/BigPipePlaceholderTestCases.php b/web/core/modules/big_pipe/tests/modules/big_pipe_test/src/BigPipePlaceholderTestCases.php new file mode 100644 index 000000000..e3da529ea --- /dev/null +++ b/web/core/modules/big_pipe/tests/modules/big_pipe_test/src/BigPipePlaceholderTestCases.php @@ -0,0 +1,406 @@ + 0, + 'contexts' => ['session.exists'], + ]; + $cacheability_depends_on_session_and_nojs_cookie = [ + 'max-age' => 0, + 'contexts' => ['session.exists', 'cookies:big_pipe_nojs'], + ]; + + // 1. Real-world example of HTML placeholder. + $status_messages = new BigPipePlaceholderTestCase( + ['#type' => 'status_messages'], + '', + [ + '#lazy_builder' => [ + 'Drupal\Core\Render\Element\StatusMessages::renderMessages', + [NULL] + ], + ] + ); + $status_messages->bigPipePlaceholderId = 'callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&args%5B0%5D&token=_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA'; + $status_messages->bigPipePlaceholderRenderArray = [ + '#markup' => '', + '#cache' => $cacheability_depends_on_session_and_nojs_cookie, + '#attached' => [ + 'library' => ['big_pipe/big_pipe'], + 'drupalSettings' => [ + 'bigPipePlaceholderIds' => [ + 'callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&args%5B0%5D&token=_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA' => TRUE, + ], + ], + 'big_pipe_placeholders' => [ + 'callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&args%5B0%5D&token=_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA' => $status_messages->placeholderRenderArray, + ], + ], + ]; + $status_messages->bigPipeNoJsPlaceholder = ''; + $status_messages->bigPipeNoJsPlaceholderRenderArray = [ + '#markup' => '', + '#cache' => $cacheability_depends_on_session_and_nojs_cookie, + '#attached' => [ + 'big_pipe_nojs_placeholders' => [ + '' => $status_messages->placeholderRenderArray, + ], + ], + ]; + if ($container && $user) { + $status_messages->embeddedAjaxResponseCommands = [ + [ + 'command' => 'insert', + 'method' => 'replaceWith', + 'selector' => '[data-big-pipe-placeholder-id="callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&args%5B0%5D&token=_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA"]', + 'data' => ' ' . "\n ", + 'settings' => NULL, + ], + ]; + $status_messages->embeddedHtmlResponse = '' . "\n \n"; + } + + // 2. Real-world example of HTML attribute value placeholder: form action. + $form_action = new BigPipePlaceholderTestCase( + $container ? $container->get('form_builder')->getForm('Drupal\big_pipe_test\Form\BigPipeTestForm') : [], + 'form_action_cc611e1d', + [ + '#lazy_builder' => ['form_builder:renderPlaceholderFormAction', []], + ] + ); + $form_action->bigPipeNoJsPlaceholder = 'big_pipe_nojs_placeholder_attribute_safe:form_action_cc611e1d'; + $form_action->bigPipeNoJsPlaceholderRenderArray = [ + '#markup' => 'big_pipe_nojs_placeholder_attribute_safe:form_action_cc611e1d', + '#cache' => $cacheability_depends_on_session_only, + '#attached' => [ + 'big_pipe_nojs_placeholders' => [ + 'big_pipe_nojs_placeholder_attribute_safe:form_action_cc611e1d' => $form_action->placeholderRenderArray, + ], + ], + ]; + if ($container) { + $form_action->embeddedHtmlResponse = '
'Link with CSRF token', + '#type' => 'link', + '#url' => Url::fromRoute('system.theme_set_default'), + ], + 'e88b559cce72c80b687d56b0e2a3a5ae4b66bc0e', + [ + '#lazy_builder' => [ + 'route_processor_csrf:renderPlaceholderCsrfToken', + ['admin/config/user-interface/shortcut/manage/default/add-link-inline'] + ], + ] + ); + $csrf_token->bigPipeNoJsPlaceholder = 'big_pipe_nojs_placeholder_attribute_safe:e88b559cce72c80b687d56b0e2a3a5ae4b66bc0e'; + $csrf_token->bigPipeNoJsPlaceholderRenderArray = [ + '#markup' => 'big_pipe_nojs_placeholder_attribute_safe:e88b559cce72c80b687d56b0e2a3a5ae4b66bc0e', + '#cache' => $cacheability_depends_on_session_only, + '#attached' => [ + 'big_pipe_nojs_placeholders' => [ + 'big_pipe_nojs_placeholder_attribute_safe:e88b559cce72c80b687d56b0e2a3a5ae4b66bc0e' => $csrf_token->placeholderRenderArray, + ], + ], + ]; + if ($container) { + $csrf_token->embeddedHtmlResponse = $container->get('csrf_token')->get('admin/appearance/default'); + } + + // 4. Edge case: custom string to be considered as a placeholder that + // happens to not be valid HTML. + $hello = new BigPipePlaceholderTestCase( + [ + '#markup' => BigPipeMarkup::create(' [ + 'placeholders' => [ + ' ['#lazy_builder' => ['\Drupal\big_pipe_test\BigPipeTestController::helloOrYarhar', []]], + ] + ], + ], + ' [ + 'hello_or_yarhar', + [] + ], + ] + ); + $hello->bigPipeNoJsPlaceholder = 'big_pipe_nojs_placeholder_attribute_safe:<hello'; + $hello->bigPipeNoJsPlaceholderRenderArray = [ + '#markup' => 'big_pipe_nojs_placeholder_attribute_safe:<hello', + '#cache' => $cacheability_depends_on_session_only, + '#attached' => [ + 'big_pipe_nojs_placeholders' => [ + 'big_pipe_nojs_placeholder_attribute_safe:<hello' => $hello->placeholderRenderArray, + ], + ], + ]; + $hello->embeddedHtmlResponse = 'Yarhar llamas forever!'; + + // 5. Edge case: non-#lazy_builder placeholder. + $current_time = new BigPipePlaceholderTestCase( + [ + '#markup' => BigPipeMarkup::create(''), + '#attached' => [ + 'placeholders' => [ + '' => [ + '#pre_render' => [ + '\Drupal\big_pipe_test\BigPipeTestController::currentTime', + ], + ] + ] + ] + ], + '', + [ + '#pre_render' => ['current_time'], + ] + ); + $current_time->bigPipePlaceholderId = 'timecurrent-timetime'; + $current_time->bigPipePlaceholderRenderArray = [ + '#markup' => '', + '#cache' => $cacheability_depends_on_session_and_nojs_cookie, + '#attached' => [ + 'library' => ['big_pipe/big_pipe'], + 'drupalSettings' => [ + 'bigPipePlaceholderIds' => [ + 'timecurrent-timetime' => TRUE, + ], + ], + 'big_pipe_placeholders' => [ + 'timecurrent-timetime' => $current_time->placeholderRenderArray, + ], + ], + ]; + $current_time->embeddedAjaxResponseCommands = [ + [ + 'command' => 'insert', + 'method' => 'replaceWith', + 'selector' => '[data-big-pipe-placeholder-id="timecurrent-timetime"]', + 'data' => '', + 'settings' => NULL, + ], + ]; + $current_time->bigPipeNoJsPlaceholder = ''; + $current_time->bigPipeNoJsPlaceholderRenderArray = [ + '#markup' => '', + '#cache' => $cacheability_depends_on_session_and_nojs_cookie, + '#attached' => [ + 'big_pipe_nojs_placeholders' => [ + '' => $current_time->placeholderRenderArray, + ], + ], + ]; + $current_time->embeddedHtmlResponse = ''; + + // 6. Edge case: #lazy_builder that throws an exception. + $exception = new BigPipePlaceholderTestCase( + [ + '#lazy_builder' => ['\Drupal\big_pipe_test\BigPipeTestController::exception', ['llamas', 'suck']], + '#create_placeholder' => TRUE, + ], + '', + [ + '#lazy_builder' => ['\Drupal\big_pipe_test\BigPipeTestController::exception', ['llamas', 'suck']], + ] + ); + $exception->bigPipePlaceholderId = 'callback=%5CDrupal%5Cbig_pipe_test%5CBigPipeTestController%3A%3Aexception&args%5B0%5D=llamas&args%5B1%5D=suck&token=uhKFNfT4eF449_W-kDQX8E5z4yHyt0-nSHUlwaGAQeU'; + $exception->bigPipePlaceholderRenderArray = [ + '#markup' => '', + '#cache' => $cacheability_depends_on_session_and_nojs_cookie, + '#attached' => [ + 'library' => ['big_pipe/big_pipe'], + 'drupalSettings' => [ + 'bigPipePlaceholderIds' => [ + 'callback=%5CDrupal%5Cbig_pipe_test%5CBigPipeTestController%3A%3Aexception&args%5B0%5D=llamas&args%5B1%5D=suck&token=uhKFNfT4eF449_W-kDQX8E5z4yHyt0-nSHUlwaGAQeU' => TRUE, + ], + ], + 'big_pipe_placeholders' => [ + 'callback=%5CDrupal%5Cbig_pipe_test%5CBigPipeTestController%3A%3Aexception&args%5B0%5D=llamas&args%5B1%5D=suck&token=uhKFNfT4eF449_W-kDQX8E5z4yHyt0-nSHUlwaGAQeU' => $exception->placeholderRenderArray, + ], + ], + ]; + $exception->embeddedAjaxResponseCommands = NULL; + $exception->bigPipeNoJsPlaceholder = ''; + $exception->bigPipeNoJsPlaceholderRenderArray = [ + '#markup' => $exception->bigPipeNoJsPlaceholder, + '#cache' => $cacheability_depends_on_session_and_nojs_cookie, + '#attached' => [ + 'big_pipe_nojs_placeholders' => [ + $exception->bigPipeNoJsPlaceholder => $exception->placeholderRenderArray, + ], + ], + ]; + $exception->embeddedHtmlResponse = NULL; + + // 7. Edge case: response filter throwing an exception for this placeholder. + $embedded_response_exception = new BigPipePlaceholderTestCase( + [ + '#lazy_builder' => ['\Drupal\big_pipe_test\BigPipeTestController::responseException', []], + '#create_placeholder' => TRUE, + ], + '', + [ + '#lazy_builder' => ['\Drupal\big_pipe_test\BigPipeTestController::responseException', []], + ] + ); + $embedded_response_exception->bigPipePlaceholderId = 'callback=%5CDrupal%5Cbig_pipe_test%5CBigPipeTestController%3A%3AresponseException&&token=PxOHfS_QL-T01NjBgu7Z7I04tIwMp6La5vM-mVxezbU'; + $embedded_response_exception->bigPipePlaceholderRenderArray = [ + '#markup' => '', + '#cache' => $cacheability_depends_on_session_and_nojs_cookie, + '#attached' => [ + 'library' => ['big_pipe/big_pipe'], + 'drupalSettings' => [ + 'bigPipePlaceholderIds' => [ + 'callback=%5CDrupal%5Cbig_pipe_test%5CBigPipeTestController%3A%3AresponseException&&token=PxOHfS_QL-T01NjBgu7Z7I04tIwMp6La5vM-mVxezbU' => TRUE, + ], + ], + 'big_pipe_placeholders' => [ + 'callback=%5CDrupal%5Cbig_pipe_test%5CBigPipeTestController%3A%3AresponseException&&token=PxOHfS_QL-T01NjBgu7Z7I04tIwMp6La5vM-mVxezbU' => $embedded_response_exception->placeholderRenderArray, + ], + ], + ]; + $embedded_response_exception->embeddedAjaxResponseCommands = NULL; + $embedded_response_exception->bigPipeNoJsPlaceholder = ''; + $embedded_response_exception->bigPipeNoJsPlaceholderRenderArray = [ + '#markup' => $embedded_response_exception->bigPipeNoJsPlaceholder, + '#cache' => $cacheability_depends_on_session_and_nojs_cookie, + '#attached' => [ + 'big_pipe_nojs_placeholders' => [ + $embedded_response_exception->bigPipeNoJsPlaceholder => $embedded_response_exception->placeholderRenderArray, + ], + ], + ]; + $exception->embeddedHtmlResponse = NULL; + + return [ + 'html' => $status_messages, + 'html_attribute_value' => $form_action, + 'html_attribute_value_subset' => $csrf_token, + 'edge_case__invalid_html' => $hello, + 'edge_case__html_non_lazy_builder' => $current_time, + 'exception__lazy_builder' => $exception, + 'exception__embedded_response' => $embedded_response_exception, + ]; + } + +} + +class BigPipePlaceholderTestCase { + + /** + * The original render array. + * + * @var array + */ + public $renderArray; + + /** + * The expected corresponding placeholder string. + * + * @var string + */ + public $placeholder; + + /** + * The expected corresponding placeholder render array. + * + * @var array + */ + public $placeholderRenderArray; + + /** + * The expected BigPipe placeholder ID. + * + * (Only possible for HTML placeholders.) + * + * @var null|string + */ + public $bigPipePlaceholderId = NULL; + + /** + * The corresponding expected BigPipe placeholder render array. + * + * @var null|array + */ + public $bigPipePlaceholderRenderArray = NULL; + + /** + * The corresponding expected embedded AJAX response. + * + * @var null|array + */ + public $embeddedAjaxResponseCommands = NULL; + + + /** + * The expected BigPipe no-JS placeholder. + * + * (Possible for all placeholders, HTML or non-HTML.) + * + * @var string + */ + public $bigPipeNoJsPlaceholder; + + /** + * The corresponding expected BigPipe no-JS placeholder render array. + * + * @var array + */ + public $bigPipeNoJsPlaceholderRenderArray; + + /** + * The corresponding expected embedded HTML response. + * + * @var string + */ + public $embeddedHtmlResponse; + + public function __construct(array $render_array, $placeholder, array $placeholder_render_array) { + $this->renderArray = $render_array; + $this->placeholder = $placeholder; + $this->placeholderRenderArray = $placeholder_render_array; + } + +}