3 namespace Drupal\node\Tests;
5 use Drupal\comment\Tests\CommentTestTrait;
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
7 use Drupal\Component\Utility\Unicode;
8 use Drupal\Core\Language\LanguageInterface;
10 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
11 use Drupal\field\Entity\FieldConfig;
12 use Drupal\field\Entity\FieldStorageConfig;
13 use Drupal\node\Entity\NodeType;
14 use Drupal\taxonomy\Entity\Term;
15 use Drupal\taxonomy\Entity\Vocabulary;
18 * Tests the node entity preview functionality.
22 class PagePreviewTest extends NodeTestBase {
24 use EntityReferenceTestTrait;
28 * Enable the comment, node and taxonomy modules to test on the preview.
32 public static $modules = ['node', 'taxonomy', 'comment', 'image', 'file', 'text', 'node_test', 'menu_ui'];
35 * The name of the created field.
41 protected function setUp() {
43 $this->addDefaultCommentField('node', 'page');
45 $web_user = $this->drupalCreateUser(['edit own page content', 'create page content', 'administer menu']);
46 $this->drupalLogin($web_user);
48 // Add a vocabulary so we can test different view modes.
49 $vocabulary = Vocabulary::create([
50 'name' => $this->randomMachineName(),
51 'description' => $this->randomMachineName(),
52 'vid' => $this->randomMachineName(),
53 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
58 $this->vocabulary = $vocabulary;
60 // Add a term to the vocabulary.
61 $term = Term::create([
62 'name' => $this->randomMachineName(),
63 'description' => $this->randomMachineName(),
64 'vid' => $this->vocabulary->id(),
65 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
71 // Create an image field.
72 FieldStorageConfig::create([
73 'field_name' => 'field_image',
74 'entity_type' => 'node',
77 'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED,
80 $field_config = FieldConfig::create([
81 'field_name' => 'field_image',
83 'entity_type' => 'node',
88 $field_config->save();
91 $this->fieldName = Unicode::strtolower($this->randomMachineName());
94 $this->vocabulary->id() => $this->vocabulary->id(),
96 'auto_create' => TRUE,
98 $this->createEntityReferenceField('node', 'page', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
100 entity_get_form_display('node', 'page', 'default')
101 ->setComponent($this->fieldName, [
102 'type' => 'entity_reference_autocomplete_tags',
106 // Show on default display and teaser.
107 entity_get_display('node', 'page', 'default')
108 ->setComponent($this->fieldName, [
109 'type' => 'entity_reference_label',
112 entity_get_display('node', 'page', 'teaser')
113 ->setComponent($this->fieldName, [
114 'type' => 'entity_reference_label',
118 entity_get_form_display('node', 'page', 'default')
119 ->setComponent('field_image', [
120 'type' => 'image_image',
125 entity_get_display('node', 'page', 'default')
126 ->setComponent('field_image')
129 // Create a multi-value text field.
130 $field_storage = FieldStorageConfig::create([
131 'field_name' => 'field_test_multi',
132 'entity_type' => 'node',
133 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
139 $field_storage->save();
140 FieldConfig::create([
141 'field_storage' => $field_storage,
145 entity_get_form_display('node', 'page', 'default')
146 ->setComponent('field_test_multi', [
147 'type' => 'text_textfield',
151 entity_get_display('node', 'page', 'default')
152 ->setComponent('field_test_multi', [
159 * Checks the node preview functionality.
161 public function testPagePreview() {
162 $title_key = 'title[0][value]';
163 $body_key = 'body[0][value]';
164 $term_key = $this->fieldName . '[target_id]';
166 // Fill in node creation form and preview node.
168 $edit[$title_key] = '<em>' . $this->randomMachineName(8) . '</em>';
169 $edit[$body_key] = $this->randomMachineName(16);
170 $edit[$term_key] = $this->term->getName();
173 $test_image = current($this->drupalGetTestFiles('image', 39325));
174 $edit['files[field_image_0][]'] = drupal_realpath($test_image->uri);
175 $this->drupalPostForm('node/add/page', $edit, t('Upload'));
177 // Add an alt tag and preview the node.
178 $this->drupalPostForm(NULL, ['field_image[0][alt]' => 'Picture of llamas'], t('Preview'));
180 // Check that the preview is displaying the title, body and term.
181 $this->assertTitle(t('@title | Drupal', ['@title' => $edit[$title_key]]), 'Basic page title is preview.');
182 $this->assertEscaped($edit[$title_key], 'Title displayed and escaped.');
183 $this->assertText($edit[$body_key], 'Body displayed.');
184 $this->assertText($edit[$term_key], 'Term displayed.');
185 $this->assertLink(t('Back to content editing'));
188 $url = parse_url($this->getUrl());
189 $paths = explode('/', $url['path']);
190 $view_mode = array_pop($paths);
191 $uuid = array_pop($paths);
193 // Switch view mode. We'll remove the body from the teaser view mode.
194 entity_get_display('node', 'page', 'teaser')
195 ->removeComponent('body')
198 $view_mode_edit = ['view_mode' => 'teaser'];
199 $this->drupalPostForm('node/preview/' . $uuid . '/full', $view_mode_edit, t('Switch'));
200 $this->assertRaw('view-mode-teaser', 'View mode teaser class found.');
201 $this->assertNoText($edit[$body_key], 'Body not displayed.');
203 // Check that the title, body and term fields are displayed with the
204 // values after going back to the content edit page.
205 $this->clickLink(t('Back to content editing'));
206 $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
207 $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
208 $this->assertFieldByName($term_key, $edit[$term_key], 'Term field displayed.');
209 $this->assertFieldByName('field_image[0][alt]', 'Picture of llamas');
210 $this->drupalPostAjaxForm(NULL, [], ['field_test_multi_add_more' => t('Add another item')], NULL, [], [], 'node-page-form');
211 $this->assertFieldByName('field_test_multi[0][value]');
212 $this->assertFieldByName('field_test_multi[1][value]');
214 // Return to page preview to check everything is as expected.
215 $this->drupalPostForm(NULL, [], t('Preview'));
216 $this->assertTitle(t('@title | Drupal', ['@title' => $edit[$title_key]]), 'Basic page title is preview.');
217 $this->assertEscaped($edit[$title_key], 'Title displayed and escaped.');
218 $this->assertText($edit[$body_key], 'Body displayed.');
219 $this->assertText($edit[$term_key], 'Term displayed.');
220 $this->assertLink(t('Back to content editing'));
222 // Assert the content is kept when reloading the page.
223 $this->drupalGet('node/add/page', ['query' => ['uuid' => $uuid]]);
224 $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
225 $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
226 $this->assertFieldByName($term_key, $edit[$term_key], 'Term field displayed.');
228 // Save the node - this is a new POST, so we need to upload the image.
229 $this->drupalPostForm('node/add/page', $edit, t('Upload'));
230 $this->drupalPostForm(NULL, ['field_image[0][alt]' => 'Picture of llamas'], t('Save'));
231 $node = $this->drupalGetNodeByTitle($edit[$title_key]);
233 // Check the term was displayed on the saved node.
234 $this->drupalGet('node/' . $node->id());
235 $this->assertText($edit[$term_key], 'Term displayed.');
237 // Check the term appears again on the edit form.
238 $this->drupalGet('node/' . $node->id() . '/edit');
239 $this->assertFieldByName($term_key, $edit[$term_key] . ' (' . $this->term->id() . ')', 'Term field displayed.');
241 // Check with two new terms on the edit form, additionally to the existing
244 $newterm1 = $this->randomMachineName(8);
245 $newterm2 = $this->randomMachineName(8);
246 $edit[$term_key] = $this->term->getName() . ', ' . $newterm1 . ', ' . $newterm2;
247 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview'));
248 $this->assertRaw('>' . $newterm1 . '<', 'First new term displayed.');
249 $this->assertRaw('>' . $newterm2 . '<', 'Second new term displayed.');
250 // The first term should be displayed as link, the others not.
251 $this->assertLink($this->term->getName());
252 $this->assertNoLink($newterm1);
253 $this->assertNoLink($newterm2);
255 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
257 // Check with one more new term, keeping old terms, removing the existing
260 $newterm3 = $this->randomMachineName(8);
261 $edit[$term_key] = $newterm1 . ', ' . $newterm3 . ', ' . $newterm2;
262 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview'));
263 $this->assertRaw('>' . $newterm1 . '<', 'First existing term displayed.');
264 $this->assertRaw('>' . $newterm2 . '<', 'Second existing term displayed.');
265 $this->assertRaw('>' . $newterm3 . '<', 'Third new term displayed.');
266 $this->assertNoText($this->term->getName());
267 $this->assertLink($newterm1);
268 $this->assertLink($newterm2);
269 $this->assertNoLink($newterm3);
271 // Check that editing an existing node after it has been previewed and not
272 // saved doesn't remember the previous changes.
274 $title_key => $this->randomMachineName(8),
276 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview'));
277 $this->assertText($edit[$title_key], 'New title displayed.');
278 $this->clickLink(t('Back to content editing'));
279 $this->assertFieldByName($title_key, $edit[$title_key], 'New title value displayed.');
280 // Navigate away from the node without saving.
281 $this->drupalGet('<front>');
282 // Go back to the edit form, the title should have its initial value.
283 $this->drupalGet('node/' . $node->id() . '/edit');
284 $this->assertFieldByName($title_key, $node->label(), 'Correct title value displayed.');
286 // Check with required preview.
287 $node_type = NodeType::load('page');
288 $node_type->setPreviewMode(DRUPAL_REQUIRED);
290 $this->drupalGet('node/add/page');
291 $this->assertNoRaw('edit-submit');
292 $this->drupalPostForm('node/add/page', [$title_key => 'Preview'], t('Preview'));
293 $this->clickLink(t('Back to content editing'));
294 $this->assertRaw('edit-submit');
296 // Check that destination is remembered when clicking on preview. When going
297 // back to the edit form and clicking save, we should go back to the
298 // original destination, if set.
299 $destination = 'node';
300 $this->drupalPostForm($node->toUrl('edit-form'), [], t('Preview'), ['query' => ['destination' => $destination]]);
301 $parameters = ['node_preview' => $node->uuid(), 'view_mode_id' => 'full'];
302 $options = ['absolute' => TRUE, 'query' => ['destination' => $destination]];
303 $this->assertUrl(Url::fromRoute('entity.node.preview', $parameters, $options));
304 $this->drupalPostForm(NULL, ['view_mode' => 'teaser'], t('Switch'));
305 $this->clickLink(t('Back to content editing'));
306 $this->drupalPostForm(NULL, [], t('Save'));
307 $this->assertUrl($destination);
309 // Check that preview page works as expected without a destination set.
310 $this->drupalPostForm($node->toUrl('edit-form'), [], t('Preview'));
311 $parameters = ['node_preview' => $node->uuid(), 'view_mode_id' => 'full'];
312 $this->assertUrl(Url::fromRoute('entity.node.preview', $parameters, ['absolute' => TRUE]));
313 $this->drupalPostForm(NULL, ['view_mode' => 'teaser'], t('Switch'));
314 $this->clickLink(t('Back to content editing'));
315 $this->drupalPostForm(NULL, [], t('Save'));
316 $this->assertUrl($node->toUrl());
317 $this->assertResponse(200);
319 // Assert multiple items can be added and are not lost when previewing.
320 $test_image_1 = current($this->drupalGetTestFiles('image', 39325));
321 $edit_image_1['files[field_image_0][]'] = drupal_realpath($test_image_1->uri);
322 $test_image_2 = current($this->drupalGetTestFiles('image', 39325));
323 $edit_image_2['files[field_image_1][]'] = drupal_realpath($test_image_2->uri);
324 $edit['field_image[0][alt]'] = 'Alt 1';
326 $this->drupalPostForm('node/add/page', $edit_image_1, t('Upload'));
327 $this->drupalPostForm(NULL, $edit, t('Preview'));
328 $this->clickLink(t('Back to content editing'));
329 $this->assertFieldByName('files[field_image_1][]');
330 $this->drupalPostForm(NULL, $edit_image_2, t('Upload'));
331 $this->assertNoFieldByName('files[field_image_1][]');
333 $title = 'node_test_title';
334 $example_text_1 = 'example_text_preview_1';
335 $example_text_2 = 'example_text_preview_2';
336 $example_text_3 = 'example_text_preview_3';
337 $this->drupalGet('node/add/page');
339 'title[0][value]' => $title,
340 'field_test_multi[0][value]' => $example_text_1,
342 $this->assertRaw('Storage is not set');
343 $this->drupalPostForm(NULL, $edit, t('Preview'));
344 $this->clickLink(t('Back to content editing'));
345 $this->assertRaw('Storage is set');
346 $this->assertFieldByName('field_test_multi[0][value]');
347 $this->drupalPostForm(NULL, [], t('Save'));
348 $this->assertText('Basic page ' . $title . ' has been created.');
349 $node = $this->drupalGetNodeByTitle($title);
350 $this->drupalGet('node/' . $node->id() . '/edit');
351 $this->drupalPostAjaxForm(NULL, [], ['field_test_multi_add_more' => t('Add another item')]);
352 $this->drupalPostAjaxForm(NULL, [], ['field_test_multi_add_more' => t('Add another item')]);
354 'field_test_multi[1][value]' => $example_text_2,
355 'field_test_multi[2][value]' => $example_text_3,
357 $this->drupalPostForm(NULL, $edit, t('Preview'));
358 $this->clickLink(t('Back to content editing'));
359 $this->drupalPostForm(NULL, $edit, t('Preview'));
360 $this->clickLink(t('Back to content editing'));
361 $this->assertFieldByName('field_test_multi[0][value]', $example_text_1);
362 $this->assertFieldByName('field_test_multi[1][value]', $example_text_2);
363 $this->assertFieldByName('field_test_multi[2][value]', $example_text_3);
365 // Now save the node and make sure all values got saved.
366 $this->drupalPostForm(NULL, [], t('Save'));
367 $this->assertText($example_text_1);
368 $this->assertText($example_text_2);
369 $this->assertText($example_text_3);
371 // Edit again, change the menu_ui settings and click on preview.
372 $this->drupalGet('node/' . $node->id() . '/edit');
374 'menu[enabled]' => TRUE,
375 'menu[title]' => 'Changed title',
377 $this->drupalPostForm(NULL, $edit, t('Preview'));
378 $this->clickLink(t('Back to content editing'));
379 $this->assertFieldChecked('edit-menu-enabled', 'Menu option is still checked');
380 $this->assertFieldByName('menu[title]', 'Changed title', 'Menu link title is correct after preview');
382 // Save, change the title while saving and make sure that it is correctly
385 'menu[enabled]' => TRUE,
386 'menu[title]' => 'Second title change',
388 $this->drupalPostForm(NULL, $edit, t('Save'));
389 $this->drupalGet('node/' . $node->id() . '/edit');
390 $this->assertFieldByName('menu[title]', 'Second title change', 'Menu link title is correct after saving');
395 * Checks the node preview functionality, when using revisions.
397 public function testPagePreviewWithRevisions() {
398 $title_key = 'title[0][value]';
399 $body_key = 'body[0][value]';
400 $term_key = $this->fieldName . '[target_id]';
401 // Force revision on "Basic page" content.
402 $node_type = NodeType::load('page');
403 $node_type->setNewRevision(TRUE);
406 // Fill in node creation form and preview node.
408 $edit[$title_key] = $this->randomMachineName(8);
409 $edit[$body_key] = $this->randomMachineName(16);
410 $edit[$term_key] = $this->term->id();
411 $edit['revision_log[0][value]'] = $this->randomString(32);
412 $this->drupalPostForm('node/add/page', $edit, t('Preview'));
414 // Check that the preview is displaying the title, body and term.
415 $this->assertTitle(t('@title | Drupal', ['@title' => $edit[$title_key]]), 'Basic page title is preview.');
416 $this->assertText($edit[$title_key], 'Title displayed.');
417 $this->assertText($edit[$body_key], 'Body displayed.');
418 $this->assertText($edit[$term_key], 'Term displayed.');
420 // Check that the title and body fields are displayed with the correct
421 // values after going back to the content edit page.
422 $this->clickLink(t('Back to content editing')); $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
423 $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
424 $this->assertFieldByName($term_key, $edit[$term_key], 'Term field displayed.');
426 // Check that the revision log field has the correct value.
427 $this->assertFieldByName('revision_log[0][value]', $edit['revision_log[0][value]'], 'Revision log field displayed.');
429 // Save the node after coming back from the preview page so we can create a
430 // forward revision for it.
431 $this->drupalPostForm(NULL, [], t('Save'));
432 $node = $this->drupalGetNodeByTitle($edit[$title_key]);
434 // Check that previewing a forward revision of a node works. This can not be
435 // accomplished through the UI so we have to use API calls.
436 // @todo Change this test to use the UI when we will be able to create
437 // forward revisions in core.
438 // @see https://www.drupal.org/node/2725533
439 $node->setNewRevision(TRUE);
440 $node->isDefaultRevision(FALSE);
442 /** @var \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver */
443 $controller_resolver = \Drupal::service('controller_resolver');
444 $node_preview_controller = $controller_resolver->getControllerFromDefinition('\Drupal\node\Controller\NodePreviewController::view');
445 $node_preview_controller($node, 'full');
449 * Checks the node preview accessible for simultaneous node editing.
451 public function testSimultaneousPreview() {
452 $title_key = 'title[0][value]';
453 $node = $this->drupalCreateNode([]);
455 $edit = [$title_key => 'New page title'];
456 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview'));
457 $this->assertText($edit[$title_key]);
459 $user2 = $this->drupalCreateUser(['edit any page content']);
460 $this->drupalLogin($user2);
461 $this->drupalGet('node/' . $node->id() . '/edit');
462 $this->assertFieldByName($title_key, $node->label(), 'No title leaked from previous user.');
464 $edit2 = [$title_key => 'Another page title'];
465 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit2, t('Preview'));
466 $this->assertUrl(\Drupal::url('entity.node.preview', ['node_preview' => $node->uuid(), 'view_mode_id' => 'full'], ['absolute' => TRUE]));
467 $this->assertText($edit2[$title_key]);