Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / big_pipe / tests / src / FunctionalJavascript / BigPipeRegressionTest.php
1 <?php
2
3 namespace Drupal\Tests\big_pipe\FunctionalJavascript;
4
5 use Drupal\big_pipe\Render\BigPipe;
6 use Drupal\big_pipe_regression_test\BigPipeRegressionTestController;
7 use Drupal\comment\CommentInterface;
8 use Drupal\comment\Entity\Comment;
9 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
10 use Drupal\comment\Tests\CommentTestTrait;
11 use Drupal\Core\Url;
12 use Drupal\editor\Entity\Editor;
13 use Drupal\filter\Entity\FilterFormat;
14 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
15 use Drupal\simpletest\ContentTypeCreationTrait;
16 use Drupal\simpletest\NodeCreationTrait;
17
18 /**
19  * BigPipe regression tests.
20  *
21  * @group big_pipe
22  */
23 class BigPipeRegressionTest extends WebDriverTestBase {
24
25   use CommentTestTrait;
26   use ContentTypeCreationTrait;
27   use NodeCreationTrait;
28
29   /**
30    * {@inheritdoc}
31    */
32   public static $modules = [
33     'big_pipe',
34     'big_pipe_regression_test',
35   ];
36
37   /**
38    * {@inheritdoc}
39    */
40   public function setUp() {
41     parent::setUp();
42
43     // Use the big_pipe_test_theme theme.
44     $this->container->get('theme_installer')->install(['big_pipe_test_theme']);
45     $this->container->get('config.factory')->getEditable('system.theme')->set('default', 'big_pipe_test_theme')->save();
46   }
47
48   /**
49    * Ensure comment form works with history and big_pipe modules.
50    *
51    * @see https://www.drupal.org/node/2698811
52    */
53   public function testCommentForm_2698811() {
54     $this->assertTrue($this->container->get('module_installer')->install(['comment', 'history', 'ckeditor'], TRUE), 'Installed modules.');
55
56     // Ensure an `article` node type exists.
57     $this->createContentType(['type' => 'article']);
58     $this->addDefaultCommentField('node', 'article');
59
60     // Enable CKEditor.
61     $format = $this->randomMachineName();
62     FilterFormat::create([
63       'format' => $format,
64       'name' => $this->randomString(),
65       'weight' => 1,
66       'filters' => [],
67     ])->save();
68     $settings['toolbar']['rows'] = [
69       [
70         [
71           'name' => 'Links',
72           'items' => [
73             'DrupalLink',
74             'DrupalUnlink',
75           ],
76         ],
77       ],
78     ];
79     $editor = Editor::create([
80       'format' => $format,
81       'editor' => 'ckeditor',
82     ]);
83     $editor->setSettings($settings);
84     $editor->save();
85
86     $admin_user = $this->drupalCreateUser([
87       'access comments',
88       'post comments',
89       'use text format ' . $format,
90     ]);
91     $this->drupalLogin($admin_user);
92
93     $node = $this->createNode([
94       'type' => 'article',
95       'comment' => CommentItemInterface::OPEN,
96     ]);
97     // Create some comments.
98     foreach (range(1, 5) as $i) {
99       $comment = Comment::create([
100         'status' => CommentInterface::PUBLISHED,
101         'field_name' => 'comment',
102         'entity_type' => 'node',
103         'entity_id' => $node->id(),
104       ]);
105       $comment->save();
106     }
107     $this->drupalGet($node->toUrl()->toString());
108     // Confirm that CKEditor loaded.
109     $javascript = <<<JS
110     (function(){
111       return Object.keys(CKEDITOR.instances).length > 0;
112     }());
113 JS;
114     $this->assertJsCondition($javascript);
115   }
116
117   /**
118    * Ensure BigPipe works despite inline JS containing the string "</body>".
119    *
120    * @see https://www.drupal.org/node/2678662
121    */
122   public function testMultipleClosingBodies_2678662() {
123     $this->assertTrue($this->container->get('module_installer')->install(['render_placeholder_message_test'], TRUE), 'Installed modules.');
124
125     $this->drupalLogin($this->drupalCreateUser());
126     $this->drupalGet(Url::fromRoute('big_pipe_regression_test.2678662'));
127
128     // Confirm that AJAX behaviors were instantiated, if not, this points to a
129     // JavaScript syntax error.
130     $javascript = <<<JS
131     (function(){
132       return Object.keys(Drupal.ajax.instances).length > 0;
133     }());
134 JS;
135     $this->assertJsCondition($javascript);
136
137     // Besides verifying there is no JavaScript syntax error, also verify the
138     // HTML structure.
139     $this->assertSession()
140       ->responseContains(BigPipe::STOP_SIGNAL . "\n\n\n</body></html>", 'The BigPipe stop signal is present just before the closing </body> and </html> tags.');
141     $js_code_until_closing_body_tag = substr(BigPipeRegressionTestController::MARKER_2678662, 0, strpos(BigPipeRegressionTestController::MARKER_2678662, '</body>'));
142     $this->assertSession()
143       ->responseNotContains($js_code_until_closing_body_tag . "\n" . BigPipe::START_SIGNAL, 'The BigPipe start signal does NOT start at the closing </body> tag string in an inline script.');
144   }
145
146   /**
147    * Ensure messages set in placeholders always appear.
148    *
149    * @see https://www.drupal.org/node/2712935
150    */
151   public function testMessages_2712935() {
152     $this->assertTrue($this->container->get('module_installer')->install(['render_placeholder_message_test'], TRUE), 'Installed modules.');
153
154     $this->drupalLogin($this->drupalCreateUser());
155     $messages_markup = '<div role="contentinfo" aria-label="Status message"';
156
157     $test_routes = [
158       // Messages placeholder rendered first.
159       'render_placeholder_message_test.first',
160       // Messages placeholder rendered after one, before another.
161       'render_placeholder_message_test.middle',
162       // Messages placeholder rendered last.
163       'render_placeholder_message_test.last',
164     ];
165
166     $assert = $this->assertSession();
167     foreach ($test_routes as $route) {
168       // Verify that we start off with zero messages queued.
169       $this->drupalGet(Url::fromRoute('render_placeholder_message_test.queued'));
170       $assert->responseNotContains($messages_markup);
171
172       // Verify the test case at this route behaves as expected.
173       $this->drupalGet(Url::fromRoute($route));
174       $assert->elementContains('css', 'p.logged-message:nth-of-type(1)', 'Message: P1');
175       $assert->elementContains('css', 'p.logged-message:nth-of-type(2)', 'Message: P2');
176       $assert->responseContains($messages_markup);
177       $assert->elementExists('css', 'div[aria-label="Status message"] ul');
178       $assert->elementContains('css', 'div[aria-label="Status message"] ul li:nth-of-type(1)', 'P1');
179       $assert->elementContains('css', 'div[aria-label="Status message"] ul li:nth-of-type(2)', 'P2');
180
181       // Verify that we end with all messages printed, hence again zero queued.
182       $this->drupalGet(Url::fromRoute('render_placeholder_message_test.queued'));
183       $assert->responseNotContains($messages_markup);
184     }
185   }
186
187   /**
188    * Ensure default BigPipe placeholder HTML cannot split paragraphs.
189    *
190    * @see https://www.drupal.org/node/2802923
191    */
192   public function testPlaceholderInParagraph_2802923() {
193     $this->drupalLogin($this->drupalCreateUser());
194     $this->drupalGet(Url::fromRoute('big_pipe_regression_test.2802923'));
195
196     $this->assertJsCondition('document.querySelectorAll(\'p\').length === 1');
197   }
198
199 }