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