37b00d3b482f27d0168e01e991b936fd570b4e5a
[yaffs-website] / web / core / modules / node / src / Tests / PagePreviewTest.php
1 <?php
2
3 namespace Drupal\node\Tests;
4
5 use Drupal\comment\Tests\CommentTestTrait;
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
7 use Drupal\Component\Utility\Unicode;
8 use Drupal\Core\Language\LanguageInterface;
9 use Drupal\Core\Url;
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;
16
17 /**
18  * Tests the node entity preview functionality.
19  *
20  * @group node
21  */
22 class PagePreviewTest extends NodeTestBase {
23
24   use EntityReferenceTestTrait;
25   use CommentTestTrait;
26
27   /**
28    * Enable the comment, node and taxonomy modules to test on the preview.
29    *
30    * @var array
31    */
32   public static $modules = ['node', 'taxonomy', 'comment', 'image', 'file', 'text', 'node_test', 'menu_ui'];
33
34   /**
35    * The name of the created field.
36    *
37    * @var string
38    */
39   protected $fieldName;
40
41   protected function setUp() {
42     parent::setUp();
43     $this->addDefaultCommentField('node', 'page');
44
45     $web_user = $this->drupalCreateUser(['edit own page content', 'create page content', 'administer menu']);
46     $this->drupalLogin($web_user);
47
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,
54       'help' => '',
55     ]);
56     $vocabulary->save();
57
58     $this->vocabulary = $vocabulary;
59
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,
66     ]);
67     $term->save();
68
69     $this->term = $term;
70
71     // Create an image field.
72     FieldStorageConfig::create([
73       'field_name' => 'field_image',
74       'entity_type' => 'node',
75       'type' => 'image',
76       'settings' => [],
77       'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED,
78     ])->save();
79
80     $field_config = FieldConfig::create([
81       'field_name' => 'field_image',
82       'label' => 'Images',
83       'entity_type' => 'node',
84       'bundle' => 'page',
85       'required' => FALSE,
86       'settings' => [],
87     ]);
88     $field_config->save();
89
90     // Create a field.
91     $this->fieldName = Unicode::strtolower($this->randomMachineName());
92     $handler_settings = [
93       'target_bundles' => [
94         $this->vocabulary->id() => $this->vocabulary->id(),
95       ],
96       'auto_create' => TRUE,
97     ];
98     $this->createEntityReferenceField('node', 'page', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
99
100     entity_get_form_display('node', 'page', 'default')
101       ->setComponent($this->fieldName, [
102         'type' => 'entity_reference_autocomplete_tags',
103       ])
104       ->save();
105
106     // Show on default display and teaser.
107     entity_get_display('node', 'page', 'default')
108       ->setComponent($this->fieldName, [
109         'type' => 'entity_reference_label',
110       ])
111       ->save();
112     entity_get_display('node', 'page', 'teaser')
113       ->setComponent($this->fieldName, [
114         'type' => 'entity_reference_label',
115       ])
116       ->save();
117
118     entity_get_form_display('node', 'page', 'default')
119       ->setComponent('field_image', [
120         'type' => 'image_image',
121         'settings' => [],
122       ])
123       ->save();
124
125     entity_get_display('node', 'page', 'default')
126       ->setComponent('field_image')
127       ->save();
128
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,
134       'type' => 'text',
135       'settings' => [
136         'max_length' => 50,
137       ]
138     ]);
139     $field_storage->save();
140     FieldConfig::create([
141       'field_storage' => $field_storage,
142       'bundle' => 'page',
143     ])->save();
144
145     entity_get_form_display('node', 'page', 'default')
146       ->setComponent('field_test_multi', [
147         'type' => 'text_textfield',
148       ])
149       ->save();
150
151     entity_get_display('node', 'page', 'default')
152       ->setComponent('field_test_multi', [
153         'type' => 'string',
154       ])
155       ->save();
156   }
157
158   /**
159    * Checks the node preview functionality.
160    */
161   public function testPagePreview() {
162     $title_key = 'title[0][value]';
163     $body_key = 'body[0][value]';
164     $term_key = $this->fieldName . '[target_id]';
165
166     // Fill in node creation form and preview node.
167     $edit = [];
168     $edit[$title_key] = '<em>' . $this->randomMachineName(8) . '</em>';
169     $edit[$body_key] = $this->randomMachineName(16);
170     $edit[$term_key] = $this->term->getName();
171
172     // Upload an image.
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'));
176
177     // Add an alt tag and preview the node.
178     $this->drupalPostForm(NULL, ['field_image[0][alt]' => 'Picture of llamas'], t('Preview'));
179
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'));
186
187     // Get the UUID.
188     $url = parse_url($this->getUrl());
189     $paths = explode('/', $url['path']);
190     $view_mode = array_pop($paths);
191     $uuid = array_pop($paths);
192
193     // Switch view mode. We'll remove the body from the teaser view mode.
194     entity_get_display('node', 'page', 'teaser')
195       ->removeComponent('body')
196       ->save();
197
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.');
202
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]');
213
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'));
221
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.');
227
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]);
232
233     // Check the term was displayed on the saved node.
234     $this->drupalGet('node/' . $node->id());
235     $this->assertText($edit[$term_key], 'Term displayed.');
236
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.');
240
241     // Check with two new terms on the edit form, additionally to the existing
242     // one.
243     $edit = [];
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);
254
255     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
256
257     // Check with one more new term, keeping old terms, removing the existing
258     // one.
259     $edit = [];
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);
270
271     // Check that editing an existing node after it has been previewed and not
272     // saved doesn't remember the previous changes.
273     $edit = [
274       $title_key => $this->randomMachineName(8),
275     ];
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.');
285
286     // Check with required preview.
287     $node_type = NodeType::load('page');
288     $node_type->setPreviewMode(DRUPAL_REQUIRED);
289     $node_type->save();
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');
295
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);
308
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);
318
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';
325
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][]');
332
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');
338     $edit = [
339       'title[0][value]' => $title,
340       'field_test_multi[0][value]' => $example_text_1,
341     ];
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')]);
353     $edit = [
354       'field_test_multi[1][value]' => $example_text_2,
355       'field_test_multi[2][value]' => $example_text_3,
356     ];
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);
364
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);
370
371     // Edit again, change the menu_ui settings and click on preview.
372     $this->drupalGet('node/' . $node->id() . '/edit');
373     $edit = [
374       'menu[enabled]' => TRUE,
375       'menu[title]' => 'Changed title',
376     ];
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');
381
382     // Save, change the title while saving and make sure that it is correctly
383     // saved.
384     $edit = [
385       'menu[enabled]' => TRUE,
386       'menu[title]' => 'Second title change',
387     ];
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');
391
392   }
393
394   /**
395    * Checks the node preview functionality, when using revisions.
396    */
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);
404     $node_type->save();
405
406     // Fill in node creation form and preview node.
407     $edit = [];
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'));
413
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.');
419
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.');
425
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.');
428
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]);
433
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);
441
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');
446   }
447
448   /**
449    * Checks the node preview accessible for simultaneous node editing.
450    */
451   public function testSimultaneousPreview() {
452     $title_key = 'title[0][value]';
453     $node = $this->drupalCreateNode([]);
454
455     $edit = [$title_key => 'New page title'];
456     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview'));
457     $this->assertText($edit[$title_key]);
458
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.');
463
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]);
468   }
469
470 }