Chris@17
|
1 <?php
|
Chris@17
|
2
|
Chris@17
|
3 namespace Drupal\Tests\node\Functional;
|
Chris@17
|
4
|
Chris@17
|
5 use Drupal\comment\Tests\CommentTestTrait;
|
Chris@17
|
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
Chris@17
|
7 use Drupal\Core\Language\LanguageInterface;
|
Chris@17
|
8 use Drupal\Core\Url;
|
Chris@17
|
9 use Drupal\field\Entity\FieldConfig;
|
Chris@17
|
10 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@17
|
11 use Drupal\node\Entity\NodeType;
|
Chris@17
|
12 use Drupal\taxonomy\Entity\Term;
|
Chris@17
|
13 use Drupal\taxonomy\Entity\Vocabulary;
|
Chris@17
|
14 use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
|
Chris@17
|
15 use Drupal\Tests\TestFileCreationTrait;
|
Chris@17
|
16
|
Chris@17
|
17 /**
|
Chris@17
|
18 * Tests the node entity preview functionality.
|
Chris@17
|
19 *
|
Chris@17
|
20 * @group node
|
Chris@17
|
21 */
|
Chris@17
|
22 class PagePreviewTest extends NodeTestBase {
|
Chris@17
|
23
|
Chris@17
|
24 use EntityReferenceTestTrait;
|
Chris@17
|
25 use CommentTestTrait;
|
Chris@17
|
26 use TestFileCreationTrait {
|
Chris@17
|
27 getTestFiles as drupalGetTestFiles;
|
Chris@17
|
28 }
|
Chris@17
|
29
|
Chris@17
|
30 /**
|
Chris@17
|
31 * Enable the comment, node and taxonomy modules to test on the preview.
|
Chris@17
|
32 *
|
Chris@17
|
33 * @var array
|
Chris@17
|
34 */
|
Chris@17
|
35 public static $modules = ['node', 'taxonomy', 'comment', 'image', 'file', 'text', 'node_test', 'menu_ui'];
|
Chris@17
|
36
|
Chris@17
|
37 /**
|
Chris@17
|
38 * The name of the created field.
|
Chris@17
|
39 *
|
Chris@17
|
40 * @var string
|
Chris@17
|
41 */
|
Chris@17
|
42 protected $fieldName;
|
Chris@17
|
43
|
Chris@17
|
44 protected function setUp() {
|
Chris@17
|
45 parent::setUp();
|
Chris@17
|
46 $this->addDefaultCommentField('node', 'page');
|
Chris@17
|
47
|
Chris@17
|
48 $web_user = $this->drupalCreateUser(['edit own page content', 'create page content', 'administer menu']);
|
Chris@17
|
49 $this->drupalLogin($web_user);
|
Chris@17
|
50
|
Chris@17
|
51 // Add a vocabulary so we can test different view modes.
|
Chris@17
|
52 $vocabulary = Vocabulary::create([
|
Chris@17
|
53 'name' => $this->randomMachineName(),
|
Chris@17
|
54 'description' => $this->randomMachineName(),
|
Chris@17
|
55 'vid' => $this->randomMachineName(),
|
Chris@17
|
56 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
|
Chris@17
|
57 'help' => '',
|
Chris@17
|
58 ]);
|
Chris@17
|
59 $vocabulary->save();
|
Chris@17
|
60
|
Chris@17
|
61 $this->vocabulary = $vocabulary;
|
Chris@17
|
62
|
Chris@17
|
63 // Add a term to the vocabulary.
|
Chris@17
|
64 $term = Term::create([
|
Chris@17
|
65 'name' => $this->randomMachineName(),
|
Chris@17
|
66 'description' => $this->randomMachineName(),
|
Chris@17
|
67 'vid' => $this->vocabulary->id(),
|
Chris@17
|
68 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
|
Chris@17
|
69 ]);
|
Chris@17
|
70 $term->save();
|
Chris@17
|
71
|
Chris@17
|
72 $this->term = $term;
|
Chris@17
|
73
|
Chris@17
|
74 // Create an image field.
|
Chris@17
|
75 FieldStorageConfig::create([
|
Chris@17
|
76 'field_name' => 'field_image',
|
Chris@17
|
77 'entity_type' => 'node',
|
Chris@17
|
78 'type' => 'image',
|
Chris@17
|
79 'settings' => [],
|
Chris@17
|
80 'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED,
|
Chris@17
|
81 ])->save();
|
Chris@17
|
82
|
Chris@17
|
83 $field_config = FieldConfig::create([
|
Chris@17
|
84 'field_name' => 'field_image',
|
Chris@17
|
85 'label' => 'Images',
|
Chris@17
|
86 'entity_type' => 'node',
|
Chris@17
|
87 'bundle' => 'page',
|
Chris@17
|
88 'required' => FALSE,
|
Chris@17
|
89 'settings' => [],
|
Chris@17
|
90 ]);
|
Chris@17
|
91 $field_config->save();
|
Chris@17
|
92
|
Chris@17
|
93 // Create a field.
|
Chris@17
|
94 $this->fieldName = mb_strtolower($this->randomMachineName());
|
Chris@17
|
95 $handler_settings = [
|
Chris@17
|
96 'target_bundles' => [
|
Chris@17
|
97 $this->vocabulary->id() => $this->vocabulary->id(),
|
Chris@17
|
98 ],
|
Chris@17
|
99 'auto_create' => TRUE,
|
Chris@17
|
100 ];
|
Chris@17
|
101 $this->createEntityReferenceField('node', 'page', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
|
Chris@17
|
102
|
Chris@17
|
103 entity_get_form_display('node', 'page', 'default')
|
Chris@17
|
104 ->setComponent($this->fieldName, [
|
Chris@17
|
105 'type' => 'entity_reference_autocomplete_tags',
|
Chris@17
|
106 ])
|
Chris@17
|
107 ->save();
|
Chris@17
|
108
|
Chris@17
|
109 // Show on default display and teaser.
|
Chris@17
|
110 entity_get_display('node', 'page', 'default')
|
Chris@17
|
111 ->setComponent($this->fieldName, [
|
Chris@17
|
112 'type' => 'entity_reference_label',
|
Chris@17
|
113 ])
|
Chris@17
|
114 ->save();
|
Chris@17
|
115 entity_get_display('node', 'page', 'teaser')
|
Chris@17
|
116 ->setComponent($this->fieldName, [
|
Chris@17
|
117 'type' => 'entity_reference_label',
|
Chris@17
|
118 ])
|
Chris@17
|
119 ->save();
|
Chris@17
|
120
|
Chris@17
|
121 entity_get_form_display('node', 'page', 'default')
|
Chris@17
|
122 ->setComponent('field_image', [
|
Chris@17
|
123 'type' => 'image_image',
|
Chris@17
|
124 'settings' => [],
|
Chris@17
|
125 ])
|
Chris@17
|
126 ->save();
|
Chris@17
|
127
|
Chris@17
|
128 entity_get_display('node', 'page', 'default')
|
Chris@17
|
129 ->setComponent('field_image')
|
Chris@17
|
130 ->save();
|
Chris@17
|
131
|
Chris@17
|
132 // Create a multi-value text field.
|
Chris@17
|
133 $field_storage = FieldStorageConfig::create([
|
Chris@17
|
134 'field_name' => 'field_test_multi',
|
Chris@17
|
135 'entity_type' => 'node',
|
Chris@17
|
136 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
|
Chris@17
|
137 'type' => 'text',
|
Chris@17
|
138 'settings' => [
|
Chris@17
|
139 'max_length' => 50,
|
Chris@17
|
140 ],
|
Chris@17
|
141 ]);
|
Chris@17
|
142 $field_storage->save();
|
Chris@17
|
143 FieldConfig::create([
|
Chris@17
|
144 'field_storage' => $field_storage,
|
Chris@17
|
145 'bundle' => 'page',
|
Chris@17
|
146 ])->save();
|
Chris@17
|
147
|
Chris@17
|
148 entity_get_form_display('node', 'page', 'default')
|
Chris@17
|
149 ->setComponent('field_test_multi', [
|
Chris@17
|
150 'type' => 'text_textfield',
|
Chris@17
|
151 ])
|
Chris@17
|
152 ->save();
|
Chris@17
|
153
|
Chris@17
|
154 entity_get_display('node', 'page', 'default')
|
Chris@17
|
155 ->setComponent('field_test_multi', [
|
Chris@17
|
156 'type' => 'string',
|
Chris@17
|
157 ])
|
Chris@17
|
158 ->save();
|
Chris@17
|
159 }
|
Chris@17
|
160
|
Chris@17
|
161 /**
|
Chris@17
|
162 * Checks the node preview functionality.
|
Chris@17
|
163 */
|
Chris@17
|
164 public function testPagePreview() {
|
Chris@17
|
165 $title_key = 'title[0][value]';
|
Chris@17
|
166 $body_key = 'body[0][value]';
|
Chris@17
|
167 $term_key = $this->fieldName . '[target_id]';
|
Chris@17
|
168
|
Chris@17
|
169 // Fill in node creation form and preview node.
|
Chris@17
|
170 $edit = [];
|
Chris@17
|
171 $edit[$title_key] = '<em>' . $this->randomMachineName(8) . '</em>';
|
Chris@17
|
172 $edit[$body_key] = $this->randomMachineName(16);
|
Chris@17
|
173 $edit[$term_key] = $this->term->getName();
|
Chris@17
|
174
|
Chris@17
|
175 // Upload an image.
|
Chris@17
|
176 $test_image = current($this->drupalGetTestFiles('image', 39325));
|
Chris@17
|
177 $edit['files[field_image_0][]'] = \Drupal::service('file_system')->realpath($test_image->uri);
|
Chris@17
|
178 $this->drupalPostForm('node/add/page', $edit, t('Upload'));
|
Chris@17
|
179
|
Chris@17
|
180 // Add an alt tag and preview the node.
|
Chris@17
|
181 $this->drupalPostForm(NULL, ['field_image[0][alt]' => 'Picture of llamas'], t('Preview'));
|
Chris@17
|
182
|
Chris@17
|
183 // Check that the preview is displaying the title, body and term.
|
Chris@17
|
184 $expected_title = $edit[$title_key] . ' | Drupal';
|
Chris@17
|
185 $this->assertSession()->titleEquals($expected_title);
|
Chris@17
|
186 $this->assertEscaped($edit[$title_key], 'Title displayed and escaped.');
|
Chris@17
|
187 $this->assertText($edit[$body_key], 'Body displayed.');
|
Chris@17
|
188 $this->assertText($edit[$term_key], 'Term displayed.');
|
Chris@17
|
189 $this->assertLink(t('Back to content editing'));
|
Chris@17
|
190
|
Chris@17
|
191 // Check that we see the class of the node type on the body element.
|
Chris@17
|
192 $body_class_element = $this->xpath("//body[contains(@class, 'page-node-type-page')]");
|
Chris@17
|
193 $this->assertTrue(!empty($body_class_element), 'Node type body class found.');
|
Chris@17
|
194
|
Chris@17
|
195 // Get the UUID.
|
Chris@17
|
196 $url = parse_url($this->getUrl());
|
Chris@17
|
197 $paths = explode('/', $url['path']);
|
Chris@17
|
198 $view_mode = array_pop($paths);
|
Chris@17
|
199 $uuid = array_pop($paths);
|
Chris@17
|
200
|
Chris@17
|
201 // Switch view mode. We'll remove the body from the teaser view mode.
|
Chris@17
|
202 entity_get_display('node', 'page', 'teaser')
|
Chris@17
|
203 ->removeComponent('body')
|
Chris@17
|
204 ->save();
|
Chris@17
|
205
|
Chris@17
|
206 $view_mode_edit = ['view_mode' => 'teaser'];
|
Chris@17
|
207 $this->drupalPostForm('node/preview/' . $uuid . '/full', $view_mode_edit, t('Switch'));
|
Chris@17
|
208 $this->assertRaw('view-mode-teaser', 'View mode teaser class found.');
|
Chris@17
|
209 $this->assertNoText($edit[$body_key], 'Body not displayed.');
|
Chris@17
|
210
|
Chris@17
|
211 // Check that the title, body and term fields are displayed with the
|
Chris@17
|
212 // values after going back to the content edit page.
|
Chris@17
|
213 $this->clickLink(t('Back to content editing'));
|
Chris@17
|
214 $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
|
Chris@17
|
215 $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
|
Chris@17
|
216 $this->assertFieldByName($term_key, $edit[$term_key], 'Term field displayed.');
|
Chris@17
|
217 $this->assertFieldByName('field_image[0][alt]', 'Picture of llamas');
|
Chris@17
|
218 $this->getSession()->getPage()->pressButton('Add another item');
|
Chris@17
|
219 $this->assertFieldByName('field_test_multi[0][value]');
|
Chris@17
|
220 $this->assertFieldByName('field_test_multi[1][value]');
|
Chris@17
|
221
|
Chris@17
|
222 // Return to page preview to check everything is as expected.
|
Chris@17
|
223 $this->drupalPostForm(NULL, [], t('Preview'));
|
Chris@17
|
224 $this->assertSession()->titleEquals($expected_title);
|
Chris@17
|
225 $this->assertEscaped($edit[$title_key], 'Title displayed and escaped.');
|
Chris@17
|
226 $this->assertText($edit[$body_key], 'Body displayed.');
|
Chris@17
|
227 $this->assertText($edit[$term_key], 'Term displayed.');
|
Chris@17
|
228 $this->assertLink(t('Back to content editing'));
|
Chris@17
|
229
|
Chris@17
|
230 // Assert the content is kept when reloading the page.
|
Chris@17
|
231 $this->drupalGet('node/add/page', ['query' => ['uuid' => $uuid]]);
|
Chris@17
|
232 $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
|
Chris@17
|
233 $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
|
Chris@17
|
234 $this->assertFieldByName($term_key, $edit[$term_key], 'Term field displayed.');
|
Chris@17
|
235
|
Chris@17
|
236 // Save the node - this is a new POST, so we need to upload the image.
|
Chris@17
|
237 $this->drupalPostForm('node/add/page', $edit, t('Upload'));
|
Chris@17
|
238 $this->drupalPostForm(NULL, ['field_image[0][alt]' => 'Picture of llamas'], t('Save'));
|
Chris@17
|
239 $node = $this->drupalGetNodeByTitle($edit[$title_key]);
|
Chris@17
|
240
|
Chris@17
|
241 // Check the term was displayed on the saved node.
|
Chris@17
|
242 $this->drupalGet('node/' . $node->id());
|
Chris@17
|
243 $this->assertText($edit[$term_key], 'Term displayed.');
|
Chris@17
|
244
|
Chris@17
|
245 // Check the term appears again on the edit form.
|
Chris@17
|
246 $this->drupalGet('node/' . $node->id() . '/edit');
|
Chris@17
|
247 $this->assertFieldByName($term_key, $edit[$term_key] . ' (' . $this->term->id() . ')', 'Term field displayed.');
|
Chris@17
|
248
|
Chris@17
|
249 // Check with two new terms on the edit form, additionally to the existing
|
Chris@17
|
250 // one.
|
Chris@17
|
251 $edit = [];
|
Chris@17
|
252 $newterm1 = $this->randomMachineName(8);
|
Chris@17
|
253 $newterm2 = $this->randomMachineName(8);
|
Chris@17
|
254 $edit[$term_key] = $this->term->getName() . ', ' . $newterm1 . ', ' . $newterm2;
|
Chris@17
|
255 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview'));
|
Chris@17
|
256 $this->assertRaw('>' . $newterm1 . '<', 'First new term displayed.');
|
Chris@17
|
257 $this->assertRaw('>' . $newterm2 . '<', 'Second new term displayed.');
|
Chris@17
|
258 // The first term should be displayed as link, the others not.
|
Chris@17
|
259 $this->assertLink($this->term->getName());
|
Chris@17
|
260 $this->assertNoLink($newterm1);
|
Chris@17
|
261 $this->assertNoLink($newterm2);
|
Chris@17
|
262
|
Chris@17
|
263 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
|
Chris@17
|
264
|
Chris@17
|
265 // Check with one more new term, keeping old terms, removing the existing
|
Chris@17
|
266 // one.
|
Chris@17
|
267 $edit = [];
|
Chris@17
|
268 $newterm3 = $this->randomMachineName(8);
|
Chris@17
|
269 $edit[$term_key] = $newterm1 . ', ' . $newterm3 . ', ' . $newterm2;
|
Chris@17
|
270 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview'));
|
Chris@17
|
271 $this->assertRaw('>' . $newterm1 . '<', 'First existing term displayed.');
|
Chris@17
|
272 $this->assertRaw('>' . $newterm2 . '<', 'Second existing term displayed.');
|
Chris@17
|
273 $this->assertRaw('>' . $newterm3 . '<', 'Third new term displayed.');
|
Chris@17
|
274 $this->assertNoText($this->term->getName());
|
Chris@17
|
275 $this->assertLink($newterm1);
|
Chris@17
|
276 $this->assertLink($newterm2);
|
Chris@17
|
277 $this->assertNoLink($newterm3);
|
Chris@17
|
278
|
Chris@17
|
279 // Check that editing an existing node after it has been previewed and not
|
Chris@17
|
280 // saved doesn't remember the previous changes.
|
Chris@17
|
281 $edit = [
|
Chris@17
|
282 $title_key => $this->randomMachineName(8),
|
Chris@17
|
283 ];
|
Chris@17
|
284 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview'));
|
Chris@17
|
285 $this->assertText($edit[$title_key], 'New title displayed.');
|
Chris@17
|
286 $this->clickLink(t('Back to content editing'));
|
Chris@17
|
287 $this->assertFieldByName($title_key, $edit[$title_key], 'New title value displayed.');
|
Chris@17
|
288 // Navigate away from the node without saving.
|
Chris@17
|
289 $this->drupalGet('<front>');
|
Chris@17
|
290 // Go back to the edit form, the title should have its initial value.
|
Chris@17
|
291 $this->drupalGet('node/' . $node->id() . '/edit');
|
Chris@17
|
292 $this->assertFieldByName($title_key, $node->label(), 'Correct title value displayed.');
|
Chris@17
|
293
|
Chris@17
|
294 // Check with required preview.
|
Chris@17
|
295 $node_type = NodeType::load('page');
|
Chris@17
|
296 $node_type->setPreviewMode(DRUPAL_REQUIRED);
|
Chris@17
|
297 $node_type->save();
|
Chris@17
|
298 $this->drupalGet('node/add/page');
|
Chris@17
|
299 $this->assertNoRaw('edit-submit');
|
Chris@17
|
300 $this->drupalPostForm('node/add/page', [$title_key => 'Preview'], t('Preview'));
|
Chris@17
|
301 $this->clickLink(t('Back to content editing'));
|
Chris@17
|
302 $this->assertRaw('edit-submit');
|
Chris@17
|
303
|
Chris@17
|
304 // Check that destination is remembered when clicking on preview. When going
|
Chris@17
|
305 // back to the edit form and clicking save, we should go back to the
|
Chris@17
|
306 // original destination, if set.
|
Chris@17
|
307 $destination = 'node';
|
Chris@17
|
308 $this->drupalPostForm($node->toUrl('edit-form'), [], t('Preview'), ['query' => ['destination' => $destination]]);
|
Chris@17
|
309 $parameters = ['node_preview' => $node->uuid(), 'view_mode_id' => 'full'];
|
Chris@17
|
310 $options = ['absolute' => TRUE, 'query' => ['destination' => $destination]];
|
Chris@17
|
311 $this->assertUrl(Url::fromRoute('entity.node.preview', $parameters, $options));
|
Chris@17
|
312 $this->drupalPostForm(NULL, ['view_mode' => 'teaser'], t('Switch'));
|
Chris@17
|
313 $this->clickLink(t('Back to content editing'));
|
Chris@17
|
314 $this->drupalPostForm(NULL, [], t('Save'));
|
Chris@17
|
315 $this->assertUrl($destination);
|
Chris@17
|
316
|
Chris@17
|
317 // Check that preview page works as expected without a destination set.
|
Chris@17
|
318 $this->drupalPostForm($node->toUrl('edit-form'), [], t('Preview'));
|
Chris@17
|
319 $parameters = ['node_preview' => $node->uuid(), 'view_mode_id' => 'full'];
|
Chris@17
|
320 $this->assertUrl(Url::fromRoute('entity.node.preview', $parameters, ['absolute' => TRUE]));
|
Chris@17
|
321 $this->drupalPostForm(NULL, ['view_mode' => 'teaser'], t('Switch'));
|
Chris@17
|
322 $this->clickLink(t('Back to content editing'));
|
Chris@17
|
323 $this->drupalPostForm(NULL, [], t('Save'));
|
Chris@17
|
324 $this->assertUrl($node->toUrl());
|
Chris@17
|
325 $this->assertResponse(200);
|
Chris@17
|
326
|
Chris@17
|
327 /** @var \Drupal\Core\File\FileSystemInterface $file_system */
|
Chris@17
|
328 $file_system = \Drupal::service('file_system');
|
Chris@17
|
329 // Assert multiple items can be added and are not lost when previewing.
|
Chris@17
|
330 $test_image_1 = current($this->drupalGetTestFiles('image', 39325));
|
Chris@17
|
331 $edit_image_1['files[field_image_0][]'] = $file_system->realpath($test_image_1->uri);
|
Chris@17
|
332 $test_image_2 = current($this->drupalGetTestFiles('image', 39325));
|
Chris@17
|
333 $edit_image_2['files[field_image_1][]'] = $file_system->realpath($test_image_2->uri);
|
Chris@17
|
334 $edit['field_image[0][alt]'] = 'Alt 1';
|
Chris@17
|
335
|
Chris@17
|
336 $this->drupalPostForm('node/add/page', $edit_image_1, t('Upload'));
|
Chris@17
|
337 $this->drupalPostForm(NULL, $edit, t('Preview'));
|
Chris@17
|
338 $this->clickLink(t('Back to content editing'));
|
Chris@17
|
339 $this->assertFieldByName('files[field_image_1][]');
|
Chris@17
|
340 $this->drupalPostForm(NULL, $edit_image_2, t('Upload'));
|
Chris@17
|
341 $this->assertNoFieldByName('files[field_image_1][]');
|
Chris@17
|
342
|
Chris@17
|
343 $title = 'node_test_title';
|
Chris@17
|
344 $example_text_1 = 'example_text_preview_1';
|
Chris@17
|
345 $example_text_2 = 'example_text_preview_2';
|
Chris@17
|
346 $example_text_3 = 'example_text_preview_3';
|
Chris@17
|
347 $this->drupalGet('node/add/page');
|
Chris@17
|
348 $edit = [
|
Chris@17
|
349 'title[0][value]' => $title,
|
Chris@17
|
350 'field_test_multi[0][value]' => $example_text_1,
|
Chris@17
|
351 ];
|
Chris@17
|
352 $this->assertRaw('Storage is not set');
|
Chris@17
|
353 $this->drupalPostForm(NULL, $edit, t('Preview'));
|
Chris@17
|
354 $this->clickLink(t('Back to content editing'));
|
Chris@17
|
355 $this->assertRaw('Storage is set');
|
Chris@17
|
356 $this->assertFieldByName('field_test_multi[0][value]');
|
Chris@17
|
357 $this->drupalPostForm(NULL, [], t('Save'));
|
Chris@17
|
358 $this->assertText('Basic page ' . $title . ' has been created.');
|
Chris@17
|
359 $node = $this->drupalGetNodeByTitle($title);
|
Chris@17
|
360 $this->drupalGet('node/' . $node->id() . '/edit');
|
Chris@17
|
361 $this->getSession()->getPage()->pressButton('Add another item');
|
Chris@17
|
362 $this->getSession()->getPage()->pressButton('Add another item');
|
Chris@17
|
363 $edit = [
|
Chris@17
|
364 'field_test_multi[1][value]' => $example_text_2,
|
Chris@17
|
365 'field_test_multi[2][value]' => $example_text_3,
|
Chris@17
|
366 ];
|
Chris@17
|
367 $this->drupalPostForm(NULL, $edit, t('Preview'));
|
Chris@17
|
368 $this->clickLink(t('Back to content editing'));
|
Chris@17
|
369 $this->drupalPostForm(NULL, $edit, t('Preview'));
|
Chris@17
|
370 $this->clickLink(t('Back to content editing'));
|
Chris@17
|
371 $this->assertFieldByName('field_test_multi[0][value]', $example_text_1);
|
Chris@17
|
372 $this->assertFieldByName('field_test_multi[1][value]', $example_text_2);
|
Chris@17
|
373 $this->assertFieldByName('field_test_multi[2][value]', $example_text_3);
|
Chris@17
|
374
|
Chris@17
|
375 // Now save the node and make sure all values got saved.
|
Chris@17
|
376 $this->drupalPostForm(NULL, [], t('Save'));
|
Chris@17
|
377 $this->assertText($example_text_1);
|
Chris@17
|
378 $this->assertText($example_text_2);
|
Chris@17
|
379 $this->assertText($example_text_3);
|
Chris@17
|
380
|
Chris@17
|
381 // Edit again, change the menu_ui settings and click on preview.
|
Chris@17
|
382 $this->drupalGet('node/' . $node->id() . '/edit');
|
Chris@17
|
383 $edit = [
|
Chris@17
|
384 'menu[enabled]' => TRUE,
|
Chris@17
|
385 'menu[title]' => 'Changed title',
|
Chris@17
|
386 ];
|
Chris@17
|
387 $this->drupalPostForm(NULL, $edit, t('Preview'));
|
Chris@17
|
388 $this->clickLink(t('Back to content editing'));
|
Chris@17
|
389 $this->assertFieldChecked('edit-menu-enabled', 'Menu option is still checked');
|
Chris@17
|
390 $this->assertFieldByName('menu[title]', 'Changed title', 'Menu link title is correct after preview');
|
Chris@17
|
391
|
Chris@17
|
392 // Save, change the title while saving and make sure that it is correctly
|
Chris@17
|
393 // saved.
|
Chris@17
|
394 $edit = [
|
Chris@17
|
395 'menu[enabled]' => TRUE,
|
Chris@17
|
396 'menu[title]' => 'Second title change',
|
Chris@17
|
397 ];
|
Chris@17
|
398 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@17
|
399 $this->drupalGet('node/' . $node->id() . '/edit');
|
Chris@17
|
400 $this->assertFieldByName('menu[title]', 'Second title change', 'Menu link title is correct after saving');
|
Chris@17
|
401
|
Chris@17
|
402 }
|
Chris@17
|
403
|
Chris@17
|
404 /**
|
Chris@17
|
405 * Checks the node preview functionality, when using revisions.
|
Chris@17
|
406 */
|
Chris@17
|
407 public function testPagePreviewWithRevisions() {
|
Chris@17
|
408 $title_key = 'title[0][value]';
|
Chris@17
|
409 $body_key = 'body[0][value]';
|
Chris@17
|
410 $term_key = $this->fieldName . '[target_id]';
|
Chris@17
|
411 // Force revision on "Basic page" content.
|
Chris@17
|
412 $node_type = NodeType::load('page');
|
Chris@17
|
413 $node_type->setNewRevision(TRUE);
|
Chris@17
|
414 $node_type->save();
|
Chris@17
|
415
|
Chris@17
|
416 // Fill in node creation form and preview node.
|
Chris@17
|
417 $edit = [];
|
Chris@17
|
418 $edit[$title_key] = $this->randomMachineName(8);
|
Chris@17
|
419 $edit[$body_key] = $this->randomMachineName(16);
|
Chris@17
|
420 $edit[$term_key] = $this->term->id();
|
Chris@17
|
421 $edit['revision_log[0][value]'] = $this->randomString(32);
|
Chris@17
|
422 $this->drupalPostForm('node/add/page', $edit, t('Preview'));
|
Chris@17
|
423
|
Chris@17
|
424 // Check that the preview is displaying the title, body and term.
|
Chris@17
|
425 $this->assertTitle(t('@title | Drupal', ['@title' => $edit[$title_key]]), 'Basic page title is preview.');
|
Chris@17
|
426 $this->assertText($edit[$title_key], 'Title displayed.');
|
Chris@17
|
427 $this->assertText($edit[$body_key], 'Body displayed.');
|
Chris@17
|
428 $this->assertText($edit[$term_key], 'Term displayed.');
|
Chris@17
|
429
|
Chris@17
|
430 // Check that the title and body fields are displayed with the correct
|
Chris@17
|
431 // values after going back to the content edit page.
|
Chris@17
|
432 $this->clickLink(t('Back to content editing')); $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
|
Chris@17
|
433 $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
|
Chris@17
|
434 $this->assertFieldByName($term_key, $edit[$term_key], 'Term field displayed.');
|
Chris@17
|
435
|
Chris@17
|
436 // Check that the revision log field has the correct value.
|
Chris@17
|
437 $this->assertFieldByName('revision_log[0][value]', $edit['revision_log[0][value]'], 'Revision log field displayed.');
|
Chris@17
|
438
|
Chris@17
|
439 // Save the node after coming back from the preview page so we can create a
|
Chris@17
|
440 // pending revision for it.
|
Chris@17
|
441 $this->drupalPostForm(NULL, [], t('Save'));
|
Chris@17
|
442 $node = $this->drupalGetNodeByTitle($edit[$title_key]);
|
Chris@17
|
443
|
Chris@17
|
444 // Check that previewing a pending revision of a node works. This can not be
|
Chris@17
|
445 // accomplished through the UI so we have to use API calls.
|
Chris@17
|
446 // @todo Change this test to use the UI when we will be able to create
|
Chris@17
|
447 // pending revisions in core.
|
Chris@17
|
448 // @see https://www.drupal.org/node/2725533
|
Chris@17
|
449 $node->setNewRevision(TRUE);
|
Chris@17
|
450 $node->isDefaultRevision(FALSE);
|
Chris@17
|
451
|
Chris@17
|
452 /** @var \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver */
|
Chris@17
|
453 $controller_resolver = \Drupal::service('controller_resolver');
|
Chris@17
|
454 $node_preview_controller = $controller_resolver->getControllerFromDefinition('\Drupal\node\Controller\NodePreviewController::view');
|
Chris@17
|
455 $node_preview_controller($node, 'full');
|
Chris@17
|
456 }
|
Chris@17
|
457
|
Chris@17
|
458 /**
|
Chris@17
|
459 * Checks the node preview accessible for simultaneous node editing.
|
Chris@17
|
460 */
|
Chris@17
|
461 public function testSimultaneousPreview() {
|
Chris@17
|
462 $title_key = 'title[0][value]';
|
Chris@17
|
463 $node = $this->drupalCreateNode([]);
|
Chris@17
|
464
|
Chris@17
|
465 $edit = [$title_key => 'New page title'];
|
Chris@17
|
466 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview'));
|
Chris@17
|
467 $this->assertText($edit[$title_key]);
|
Chris@17
|
468
|
Chris@17
|
469 $user2 = $this->drupalCreateUser(['edit any page content']);
|
Chris@17
|
470 $this->drupalLogin($user2);
|
Chris@17
|
471 $this->drupalGet('node/' . $node->id() . '/edit');
|
Chris@17
|
472 $this->assertFieldByName($title_key, $node->label(), 'No title leaked from previous user.');
|
Chris@17
|
473
|
Chris@17
|
474 $edit2 = [$title_key => 'Another page title'];
|
Chris@17
|
475 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit2, t('Preview'));
|
Chris@18
|
476 $this->assertUrl(Url::fromRoute('entity.node.preview', ['node_preview' => $node->uuid(), 'view_mode_id' => 'full'], ['absolute' => TRUE])->toString());
|
Chris@17
|
477 $this->assertText($edit2[$title_key]);
|
Chris@17
|
478 }
|
Chris@17
|
479
|
Chris@17
|
480 }
|