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