22307708fdf2af6a363a6537a61e32a42ff1ba91
[yaffs-website] / web / core / modules / big_pipe / tests / modules / big_pipe_test / src / BigPipeTestController.php
1 <?php
2
3 namespace Drupal\big_pipe_test;
4
5 use Drupal\big_pipe\Render\BigPipeMarkup;
6 use Drupal\big_pipe\Tests\BigPipePlaceholderTestCases;
7 use Drupal\big_pipe_test\EventSubscriber\BigPipeTestSubscriber;
8
9 class BigPipeTestController {
10
11   /**
12    * Returns a all BigPipe placeholder test case render arrays.
13    *
14    * @return array
15    */
16   public function test() {
17     $has_session = \Drupal::service('session_configuration')->hasSession(\Drupal::requestStack()->getMasterRequest());
18
19     $build = [];
20
21     $cases = BigPipePlaceholderTestCases::cases(\Drupal::getContainer());
22
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.
25     if ($has_session) {
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!');
29     }
30     $build['html'] = $cases['html']->renderArray;
31
32     // 2. HTML attribute value placeholder: form action.
33     $build['html_attribute_value'] = $cases['html_attribute_value']->renderArray;
34
35     // 3. HTML attribute value subset placeholder: CSRF token in link.
36     $build['html_attribute_value_subset'] = $cases['html_attribute_value_subset']->renderArray;
37
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;
41
42     // 5. Edge case: non-#lazy_builder placeholder.
43     $build['edge_case__html_non_lazy_builder'] = $cases['edge_case__html_non_lazy_builder']->renderArray;
44
45     // 6. Exception: #lazy_builder that throws an exception.
46     $build['exception__lazy_builder'] = $cases['exception__lazy_builder']->renderArray;
47
48     // 7. Exception: placeholder that causes response filter to throw exception.
49     $build['exception__embedded_response'] = $cases['exception__embedded_response']->renderArray;
50
51     return $build;
52   }
53
54   /**
55    * @return array
56    */
57   public static function nope() {
58     return ['#markup' => '<p>Nope.</p>'];
59   }
60
61   /**
62    * A page with multiple occurrences of the same placeholder.
63    *
64    * @see \Drupal\big_pipe\Tests\BigPipeTest::testBigPipeMultipleOccurrencePlaceholders()
65    *
66    * @return array
67    */
68   public function multiOccurrence() {
69     return [
70       'item1' => [
71         '#lazy_builder' => [static::class . '::counter', []],
72         '#create_placeholder' => TRUE,
73       ],
74       'item2' => [
75         '#lazy_builder' => [static::class . '::counter', []],
76         '#create_placeholder' => TRUE,
77       ],
78       'item3' => [
79         '#lazy_builder' => [static::class . '::counter', []],
80         '#create_placeholder' => TRUE,
81       ],
82     ];
83   }
84
85   /**
86    * #lazy_builder callback; builds <time> markup with current time.
87    *
88    * Note: does not actually use current time, that would complicate testing.
89    *
90    * @return array
91    */
92   public static function currentTime() {
93     return [
94       '#markup' => '<time datetime="' . date('Y-m-d', 668948400) . '"></time>',
95       '#cache' => ['max-age' => 0]
96     ];
97   }
98
99   /**
100    * #lazy_builder callback; says "hello" or "yarhar".
101    *
102    * @return array
103    */
104   public static function helloOrYarhar() {
105     return [
106       '#markup' => BigPipeMarkup::create('<marquee>Yarhar llamas forever!</marquee>'),
107       '#cache' => [
108         'max-age' => 0,
109         'tags' => ['cache_tag_set_in_lazy_builder'],
110       ],
111     ];
112   }
113
114   /**
115    * #lazy_builder callback; throws exception.
116    *
117    * @throws \Exception
118    */
119   public static function exception() {
120     throw new \Exception('You are not allowed to say llamas are not cool!');
121   }
122
123   /**
124    * #lazy_builder callback; returns content that will trigger an exception.
125    *
126    * @see \Drupal\big_pipe_test\EventSubscriber\BigPipeTestSubscriber::onRespondTriggerException()
127    *
128    * @return array
129    */
130   public static function responseException() {
131     return ['#plain_text' => BigPipeTestSubscriber::CONTENT_TRIGGER_EXCEPTION];
132   }
133
134   /**
135    * #lazy_builder callback; returns the current count.
136    *
137    * @see \Drupal\big_pipe\Tests\BigPipeTest::testBigPipeMultipleOccurrencePlaceholders()
138    *
139    * @return array
140    *   The render array.
141    */
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.
147     static $count;
148
149     if (!isset($count)) {
150       $count = 0;
151     }
152
153     $count++;
154
155     return [
156       '#markup' => BigPipeMarkup::create("<p>The count is $count.</p>"),
157       '#cache' => ['max-age' => 0],
158     ];
159   }
160
161 }