3 namespace Drupal\big_pipe_test;
5 use Drupal\big_pipe\Render\BigPipeMarkup;
6 use Drupal\big_pipe_test\EventSubscriber\BigPipeTestSubscriber;
8 class BigPipeTestController {
11 * Returns a all BigPipe placeholder test case render arrays.
15 public function test() {
16 $has_session = \Drupal::service('session_configuration')->hasSession(\Drupal::requestStack()->getMasterRequest());
20 $cases = BigPipePlaceholderTestCases::cases(\Drupal::getContainer());
22 // 1. HTML placeholder: status messages. Drupal renders those automatically,
23 // so all that we need to do in this controller is set a message.
25 // Only set a message if a session already exists, otherwise we always
26 // trigger a session, which means we can't test no-session requests.
27 \Drupal::messenger()->addStatus('Hello from BigPipe!');
29 $build['html'] = $cases['html']->renderArray;
31 // 2. HTML attribute value placeholder: form action.
32 $build['html_attribute_value'] = $cases['html_attribute_value']->renderArray;
34 // 3. HTML attribute value subset placeholder: CSRF token in link.
35 $build['html_attribute_value_subset'] = $cases['html_attribute_value_subset']->renderArray;
37 // 4. Edge case: custom string to be considered as a placeholder that
38 // happens to not be valid HTML.
39 $build['edge_case__invalid_html'] = $cases['edge_case__invalid_html']->renderArray;
41 // 5. Edge case: non-#lazy_builder placeholder.
42 $build['edge_case__html_non_lazy_builder'] = $cases['edge_case__html_non_lazy_builder']->renderArray;
44 // 6. Exception: #lazy_builder that throws an exception.
45 $build['exception__lazy_builder'] = $cases['exception__lazy_builder']->renderArray;
47 // 7. Exception: placeholder that causes response filter to throw exception.
48 $build['exception__embedded_response'] = $cases['exception__embedded_response']->renderArray;
56 public static function nope() {
57 return ['#markup' => '<p>Nope.</p>'];
61 * A page with multiple occurrences of the same placeholder.
63 * @see \Drupal\Tests\big_pipe\Functional\BigPipeTest::testBigPipeMultiOccurrencePlaceholders()
67 public function multiOccurrence() {
70 '#lazy_builder' => [static::class . '::counter', []],
71 '#create_placeholder' => TRUE,
74 '#lazy_builder' => [static::class . '::counter', []],
75 '#create_placeholder' => TRUE,
78 '#lazy_builder' => [static::class . '::counter', []],
79 '#create_placeholder' => TRUE,
85 * #lazy_builder callback; builds <time> markup with current time.
87 * Note: does not actually use current time, that would complicate testing.
91 public static function currentTime() {
93 '#markup' => '<time datetime="' . date('Y-m-d', 668948400) . '"></time>',
94 '#cache' => ['max-age' => 0],
99 * #lazy_builder callback; says "hello" or "yarhar".
103 public static function helloOrYarhar() {
105 '#markup' => BigPipeMarkup::create('<marquee>Yarhar llamas forever!</marquee>'),
108 'tags' => ['cache_tag_set_in_lazy_builder'],
114 * #lazy_builder callback; throws exception.
118 public static function exception() {
119 throw new \Exception('You are not allowed to say llamas are not cool!');
123 * #lazy_builder callback; returns content that will trigger an exception.
125 * @see \Drupal\big_pipe_test\EventSubscriber\BigPipeTestSubscriber::onRespondTriggerException()
129 public static function responseException() {
130 return ['#plain_text' => BigPipeTestSubscriber::CONTENT_TRIGGER_EXCEPTION];
134 * #lazy_builder callback; returns the current count.
136 * @see \Drupal\Tests\big_pipe\Functional\BigPipeTest::testBigPipeMultiOccurrencePlaceholders()
141 public static function counter() {
142 // Lazy builders are not allowed to build their own state like this function
143 // does, but in this case we're intentionally doing that for testing
144 // purposes: so we can ensure that each lazy builder is only ever called
145 // once with the same parameters.
148 if (!isset($count)) {
155 '#markup' => BigPipeMarkup::create("<p>The count is $count.</p>"),
156 '#cache' => ['max-age' => 0],