3 namespace Drupal\big_pipe_test;
5 use Drupal\big_pipe\Render\BigPipeMarkup;
6 use Drupal\big_pipe\Tests\BigPipePlaceholderTestCases;
7 use Drupal\big_pipe_test\EventSubscriber\BigPipeTestSubscriber;
9 class BigPipeTestController {
12 * Returns a all BigPipe placeholder test case render arrays.
16 public function test() {
17 $has_session = \Drupal::service('session_configuration')->hasSession(\Drupal::requestStack()->getMasterRequest());
21 $cases = BigPipePlaceholderTestCases::cases(\Drupal::getContainer());
23 // 1. HTML placeholder: status messages. Drupal renders those automatically,
24 // so all that we need to do in this controller is set a message.
26 // Only set a message if a session already exists, otherwise we always
27 // trigger a session, which means we can't test no-session requests.
28 drupal_set_message('Hello from BigPipe!');
30 $build['html'] = $cases['html']->renderArray;
32 // 2. HTML attribute value placeholder: form action.
33 $build['html_attribute_value'] = $cases['html_attribute_value']->renderArray;
35 // 3. HTML attribute value subset placeholder: CSRF token in link.
36 $build['html_attribute_value_subset'] = $cases['html_attribute_value_subset']->renderArray;
38 // 4. Edge case: custom string to be considered as a placeholder that
39 // happens to not be valid HTML.
40 $build['edge_case__invalid_html'] = $cases['edge_case__invalid_html']->renderArray;
42 // 5. Edge case: non-#lazy_builder placeholder.
43 $build['edge_case__html_non_lazy_builder'] = $cases['edge_case__html_non_lazy_builder']->renderArray;
45 // 6. Exception: #lazy_builder that throws an exception.
46 $build['exception__lazy_builder'] = $cases['exception__lazy_builder']->renderArray;
48 // 7. Exception: placeholder that causes response filter to throw exception.
49 $build['exception__embedded_response'] = $cases['exception__embedded_response']->renderArray;
57 public static function nope() {
58 return ['#markup' => '<p>Nope.</p>'];
62 * A page with multiple occurrences of the same placeholder.
64 * @see \Drupal\big_pipe\Tests\BigPipeTest::testBigPipeMultipleOccurrencePlaceholders()
68 public function multiOccurrence() {
71 '#lazy_builder' => [static::class . '::counter', []],
72 '#create_placeholder' => TRUE,
75 '#lazy_builder' => [static::class . '::counter', []],
76 '#create_placeholder' => TRUE,
79 '#lazy_builder' => [static::class . '::counter', []],
80 '#create_placeholder' => TRUE,
86 * #lazy_builder callback; builds <time> markup with current time.
88 * Note: does not actually use current time, that would complicate testing.
92 public static function currentTime() {
94 '#markup' => '<time datetime="' . date('Y-m-d', 668948400) . '"></time>',
95 '#cache' => ['max-age' => 0]
100 * #lazy_builder callback; says "hello" or "yarhar".
104 public static function helloOrYarhar() {
106 '#markup' => BigPipeMarkup::create('<marquee>Yarhar llamas forever!</marquee>'),
109 'tags' => ['cache_tag_set_in_lazy_builder'],
115 * #lazy_builder callback; throws exception.
119 public static function exception() {
120 throw new \Exception('You are not allowed to say llamas are not cool!');
124 * #lazy_builder callback; returns content that will trigger an exception.
126 * @see \Drupal\big_pipe_test\EventSubscriber\BigPipeTestSubscriber::onRespondTriggerException()
130 public static function responseException() {
131 return ['#plain_text' => BigPipeTestSubscriber::CONTENT_TRIGGER_EXCEPTION];
135 * #lazy_builder callback; returns the current count.
137 * @see \Drupal\big_pipe\Tests\BigPipeTest::testBigPipeMultipleOccurrencePlaceholders()
142 public static function counter() {
143 // Lazy builders are not allowed to build their own state like this function
144 // does, but in this case we're intentionally doing that for testing
145 // purposes: so we can ensure that each lazy builder is only ever called
146 // once with the same parameters.
149 if (!isset($count)) {
156 '#markup' => BigPipeMarkup::create("<p>The count is $count.</p>"),
157 '#cache' => ['max-age' => 0],