Chris@17: addDefaultCommentField('node', 'page'); Chris@17: Chris@17: $web_user = $this->drupalCreateUser(['edit own page content', 'create page content', 'administer menu']); Chris@17: $this->drupalLogin($web_user); Chris@17: Chris@17: // Add a vocabulary so we can test different view modes. Chris@17: $vocabulary = Vocabulary::create([ Chris@17: 'name' => $this->randomMachineName(), Chris@17: 'description' => $this->randomMachineName(), Chris@17: 'vid' => $this->randomMachineName(), Chris@17: 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, Chris@17: 'help' => '', Chris@17: ]); Chris@17: $vocabulary->save(); Chris@17: Chris@17: $this->vocabulary = $vocabulary; Chris@17: Chris@17: // Add a term to the vocabulary. Chris@17: $term = Term::create([ Chris@17: 'name' => $this->randomMachineName(), Chris@17: 'description' => $this->randomMachineName(), Chris@17: 'vid' => $this->vocabulary->id(), Chris@17: 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, Chris@17: ]); Chris@17: $term->save(); Chris@17: Chris@17: $this->term = $term; Chris@17: Chris@17: // Create an image field. Chris@17: FieldStorageConfig::create([ Chris@17: 'field_name' => 'field_image', Chris@17: 'entity_type' => 'node', Chris@17: 'type' => 'image', Chris@17: 'settings' => [], Chris@17: 'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED, Chris@17: ])->save(); Chris@17: Chris@17: $field_config = FieldConfig::create([ Chris@17: 'field_name' => 'field_image', Chris@17: 'label' => 'Images', Chris@17: 'entity_type' => 'node', Chris@17: 'bundle' => 'page', Chris@17: 'required' => FALSE, Chris@17: 'settings' => [], Chris@17: ]); Chris@17: $field_config->save(); Chris@17: Chris@17: // Create a field. Chris@17: $this->fieldName = mb_strtolower($this->randomMachineName()); Chris@17: $handler_settings = [ Chris@17: 'target_bundles' => [ Chris@17: $this->vocabulary->id() => $this->vocabulary->id(), Chris@17: ], Chris@17: 'auto_create' => TRUE, Chris@17: ]; Chris@17: $this->createEntityReferenceField('node', 'page', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); Chris@17: Chris@17: entity_get_form_display('node', 'page', 'default') Chris@17: ->setComponent($this->fieldName, [ Chris@17: 'type' => 'entity_reference_autocomplete_tags', Chris@17: ]) Chris@17: ->save(); Chris@17: Chris@17: // Show on default display and teaser. Chris@17: entity_get_display('node', 'page', 'default') Chris@17: ->setComponent($this->fieldName, [ Chris@17: 'type' => 'entity_reference_label', Chris@17: ]) Chris@17: ->save(); Chris@17: entity_get_display('node', 'page', 'teaser') Chris@17: ->setComponent($this->fieldName, [ Chris@17: 'type' => 'entity_reference_label', Chris@17: ]) Chris@17: ->save(); Chris@17: Chris@17: entity_get_form_display('node', 'page', 'default') Chris@17: ->setComponent('field_image', [ Chris@17: 'type' => 'image_image', Chris@17: 'settings' => [], Chris@17: ]) Chris@17: ->save(); Chris@17: Chris@17: entity_get_display('node', 'page', 'default') Chris@17: ->setComponent('field_image') Chris@17: ->save(); Chris@17: Chris@17: // Create a multi-value text field. Chris@17: $field_storage = FieldStorageConfig::create([ Chris@17: 'field_name' => 'field_test_multi', Chris@17: 'entity_type' => 'node', Chris@17: 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, Chris@17: 'type' => 'text', Chris@17: 'settings' => [ Chris@17: 'max_length' => 50, Chris@17: ], Chris@17: ]); Chris@17: $field_storage->save(); Chris@17: FieldConfig::create([ Chris@17: 'field_storage' => $field_storage, Chris@17: 'bundle' => 'page', Chris@17: ])->save(); Chris@17: Chris@17: entity_get_form_display('node', 'page', 'default') Chris@17: ->setComponent('field_test_multi', [ Chris@17: 'type' => 'text_textfield', Chris@17: ]) Chris@17: ->save(); Chris@17: Chris@17: entity_get_display('node', 'page', 'default') Chris@17: ->setComponent('field_test_multi', [ Chris@17: 'type' => 'string', Chris@17: ]) Chris@17: ->save(); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Checks the node preview functionality. Chris@17: */ Chris@17: public function testPagePreview() { Chris@17: $title_key = 'title[0][value]'; Chris@17: $body_key = 'body[0][value]'; Chris@17: $term_key = $this->fieldName . '[target_id]'; Chris@17: Chris@17: // Fill in node creation form and preview node. Chris@17: $edit = []; Chris@17: $edit[$title_key] = '' . $this->randomMachineName(8) . ''; Chris@17: $edit[$body_key] = $this->randomMachineName(16); Chris@17: $edit[$term_key] = $this->term->getName(); Chris@17: Chris@17: // Upload an image. Chris@17: $test_image = current($this->drupalGetTestFiles('image', 39325)); Chris@17: $edit['files[field_image_0][]'] = \Drupal::service('file_system')->realpath($test_image->uri); Chris@17: $this->drupalPostForm('node/add/page', $edit, t('Upload')); Chris@17: Chris@17: // Add an alt tag and preview the node. Chris@17: $this->drupalPostForm(NULL, ['field_image[0][alt]' => 'Picture of llamas'], t('Preview')); Chris@17: Chris@17: // Check that the preview is displaying the title, body and term. Chris@17: $expected_title = $edit[$title_key] . ' | Drupal'; Chris@17: $this->assertSession()->titleEquals($expected_title); Chris@17: $this->assertEscaped($edit[$title_key], 'Title displayed and escaped.'); Chris@17: $this->assertText($edit[$body_key], 'Body displayed.'); Chris@17: $this->assertText($edit[$term_key], 'Term displayed.'); Chris@17: $this->assertLink(t('Back to content editing')); Chris@17: Chris@17: // Check that we see the class of the node type on the body element. Chris@17: $body_class_element = $this->xpath("//body[contains(@class, 'page-node-type-page')]"); Chris@17: $this->assertTrue(!empty($body_class_element), 'Node type body class found.'); Chris@17: Chris@17: // Get the UUID. Chris@17: $url = parse_url($this->getUrl()); Chris@17: $paths = explode('/', $url['path']); Chris@17: $view_mode = array_pop($paths); Chris@17: $uuid = array_pop($paths); Chris@17: Chris@17: // Switch view mode. We'll remove the body from the teaser view mode. Chris@17: entity_get_display('node', 'page', 'teaser') Chris@17: ->removeComponent('body') Chris@17: ->save(); Chris@17: Chris@17: $view_mode_edit = ['view_mode' => 'teaser']; Chris@17: $this->drupalPostForm('node/preview/' . $uuid . '/full', $view_mode_edit, t('Switch')); Chris@17: $this->assertRaw('view-mode-teaser', 'View mode teaser class found.'); Chris@17: $this->assertNoText($edit[$body_key], 'Body not displayed.'); Chris@17: Chris@17: // Check that the title, body and term fields are displayed with the Chris@17: // values after going back to the content edit page. Chris@17: $this->clickLink(t('Back to content editing')); Chris@17: $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.'); Chris@17: $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.'); Chris@17: $this->assertFieldByName($term_key, $edit[$term_key], 'Term field displayed.'); Chris@17: $this->assertFieldByName('field_image[0][alt]', 'Picture of llamas'); Chris@17: $this->getSession()->getPage()->pressButton('Add another item'); Chris@17: $this->assertFieldByName('field_test_multi[0][value]'); Chris@17: $this->assertFieldByName('field_test_multi[1][value]'); Chris@17: Chris@17: // Return to page preview to check everything is as expected. Chris@17: $this->drupalPostForm(NULL, [], t('Preview')); Chris@17: $this->assertSession()->titleEquals($expected_title); Chris@17: $this->assertEscaped($edit[$title_key], 'Title displayed and escaped.'); Chris@17: $this->assertText($edit[$body_key], 'Body displayed.'); Chris@17: $this->assertText($edit[$term_key], 'Term displayed.'); Chris@17: $this->assertLink(t('Back to content editing')); Chris@17: Chris@17: // Assert the content is kept when reloading the page. Chris@17: $this->drupalGet('node/add/page', ['query' => ['uuid' => $uuid]]); Chris@17: $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.'); Chris@17: $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.'); Chris@17: $this->assertFieldByName($term_key, $edit[$term_key], 'Term field displayed.'); Chris@17: Chris@17: // Save the node - this is a new POST, so we need to upload the image. Chris@17: $this->drupalPostForm('node/add/page', $edit, t('Upload')); Chris@17: $this->drupalPostForm(NULL, ['field_image[0][alt]' => 'Picture of llamas'], t('Save')); Chris@17: $node = $this->drupalGetNodeByTitle($edit[$title_key]); Chris@17: Chris@17: // Check the term was displayed on the saved node. Chris@17: $this->drupalGet('node/' . $node->id()); Chris@17: $this->assertText($edit[$term_key], 'Term displayed.'); Chris@17: Chris@17: // Check the term appears again on the edit form. Chris@17: $this->drupalGet('node/' . $node->id() . '/edit'); Chris@17: $this->assertFieldByName($term_key, $edit[$term_key] . ' (' . $this->term->id() . ')', 'Term field displayed.'); Chris@17: Chris@17: // Check with two new terms on the edit form, additionally to the existing Chris@17: // one. Chris@17: $edit = []; Chris@17: $newterm1 = $this->randomMachineName(8); Chris@17: $newterm2 = $this->randomMachineName(8); Chris@17: $edit[$term_key] = $this->term->getName() . ', ' . $newterm1 . ', ' . $newterm2; Chris@17: $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview')); Chris@17: $this->assertRaw('>' . $newterm1 . '<', 'First new term displayed.'); Chris@17: $this->assertRaw('>' . $newterm2 . '<', 'Second new term displayed.'); Chris@17: // The first term should be displayed as link, the others not. Chris@17: $this->assertLink($this->term->getName()); Chris@17: $this->assertNoLink($newterm1); Chris@17: $this->assertNoLink($newterm2); Chris@17: Chris@17: $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); Chris@17: Chris@17: // Check with one more new term, keeping old terms, removing the existing Chris@17: // one. Chris@17: $edit = []; Chris@17: $newterm3 = $this->randomMachineName(8); Chris@17: $edit[$term_key] = $newterm1 . ', ' . $newterm3 . ', ' . $newterm2; Chris@17: $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview')); Chris@17: $this->assertRaw('>' . $newterm1 . '<', 'First existing term displayed.'); Chris@17: $this->assertRaw('>' . $newterm2 . '<', 'Second existing term displayed.'); Chris@17: $this->assertRaw('>' . $newterm3 . '<', 'Third new term displayed.'); Chris@17: $this->assertNoText($this->term->getName()); Chris@17: $this->assertLink($newterm1); Chris@17: $this->assertLink($newterm2); Chris@17: $this->assertNoLink($newterm3); Chris@17: Chris@17: // Check that editing an existing node after it has been previewed and not Chris@17: // saved doesn't remember the previous changes. Chris@17: $edit = [ Chris@17: $title_key => $this->randomMachineName(8), Chris@17: ]; Chris@17: $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview')); Chris@17: $this->assertText($edit[$title_key], 'New title displayed.'); Chris@17: $this->clickLink(t('Back to content editing')); Chris@17: $this->assertFieldByName($title_key, $edit[$title_key], 'New title value displayed.'); Chris@17: // Navigate away from the node without saving. Chris@17: $this->drupalGet(''); Chris@17: // Go back to the edit form, the title should have its initial value. Chris@17: $this->drupalGet('node/' . $node->id() . '/edit'); Chris@17: $this->assertFieldByName($title_key, $node->label(), 'Correct title value displayed.'); Chris@17: Chris@17: // Check with required preview. Chris@17: $node_type = NodeType::load('page'); Chris@17: $node_type->setPreviewMode(DRUPAL_REQUIRED); Chris@17: $node_type->save(); Chris@17: $this->drupalGet('node/add/page'); Chris@17: $this->assertNoRaw('edit-submit'); Chris@17: $this->drupalPostForm('node/add/page', [$title_key => 'Preview'], t('Preview')); Chris@17: $this->clickLink(t('Back to content editing')); Chris@17: $this->assertRaw('edit-submit'); Chris@17: Chris@17: // Check that destination is remembered when clicking on preview. When going Chris@17: // back to the edit form and clicking save, we should go back to the Chris@17: // original destination, if set. Chris@17: $destination = 'node'; Chris@17: $this->drupalPostForm($node->toUrl('edit-form'), [], t('Preview'), ['query' => ['destination' => $destination]]); Chris@17: $parameters = ['node_preview' => $node->uuid(), 'view_mode_id' => 'full']; Chris@17: $options = ['absolute' => TRUE, 'query' => ['destination' => $destination]]; Chris@17: $this->assertUrl(Url::fromRoute('entity.node.preview', $parameters, $options)); Chris@17: $this->drupalPostForm(NULL, ['view_mode' => 'teaser'], t('Switch')); Chris@17: $this->clickLink(t('Back to content editing')); Chris@17: $this->drupalPostForm(NULL, [], t('Save')); Chris@17: $this->assertUrl($destination); Chris@17: Chris@17: // Check that preview page works as expected without a destination set. Chris@17: $this->drupalPostForm($node->toUrl('edit-form'), [], t('Preview')); Chris@17: $parameters = ['node_preview' => $node->uuid(), 'view_mode_id' => 'full']; Chris@17: $this->assertUrl(Url::fromRoute('entity.node.preview', $parameters, ['absolute' => TRUE])); Chris@17: $this->drupalPostForm(NULL, ['view_mode' => 'teaser'], t('Switch')); Chris@17: $this->clickLink(t('Back to content editing')); Chris@17: $this->drupalPostForm(NULL, [], t('Save')); Chris@17: $this->assertUrl($node->toUrl()); Chris@17: $this->assertResponse(200); Chris@17: Chris@17: /** @var \Drupal\Core\File\FileSystemInterface $file_system */ Chris@17: $file_system = \Drupal::service('file_system'); Chris@17: // Assert multiple items can be added and are not lost when previewing. Chris@17: $test_image_1 = current($this->drupalGetTestFiles('image', 39325)); Chris@17: $edit_image_1['files[field_image_0][]'] = $file_system->realpath($test_image_1->uri); Chris@17: $test_image_2 = current($this->drupalGetTestFiles('image', 39325)); Chris@17: $edit_image_2['files[field_image_1][]'] = $file_system->realpath($test_image_2->uri); Chris@17: $edit['field_image[0][alt]'] = 'Alt 1'; Chris@17: Chris@17: $this->drupalPostForm('node/add/page', $edit_image_1, t('Upload')); Chris@17: $this->drupalPostForm(NULL, $edit, t('Preview')); Chris@17: $this->clickLink(t('Back to content editing')); Chris@17: $this->assertFieldByName('files[field_image_1][]'); Chris@17: $this->drupalPostForm(NULL, $edit_image_2, t('Upload')); Chris@17: $this->assertNoFieldByName('files[field_image_1][]'); Chris@17: Chris@17: $title = 'node_test_title'; Chris@17: $example_text_1 = 'example_text_preview_1'; Chris@17: $example_text_2 = 'example_text_preview_2'; Chris@17: $example_text_3 = 'example_text_preview_3'; Chris@17: $this->drupalGet('node/add/page'); Chris@17: $edit = [ Chris@17: 'title[0][value]' => $title, Chris@17: 'field_test_multi[0][value]' => $example_text_1, Chris@17: ]; Chris@17: $this->assertRaw('Storage is not set'); Chris@17: $this->drupalPostForm(NULL, $edit, t('Preview')); Chris@17: $this->clickLink(t('Back to content editing')); Chris@17: $this->assertRaw('Storage is set'); Chris@17: $this->assertFieldByName('field_test_multi[0][value]'); Chris@17: $this->drupalPostForm(NULL, [], t('Save')); Chris@17: $this->assertText('Basic page ' . $title . ' has been created.'); Chris@17: $node = $this->drupalGetNodeByTitle($title); Chris@17: $this->drupalGet('node/' . $node->id() . '/edit'); Chris@17: $this->getSession()->getPage()->pressButton('Add another item'); Chris@17: $this->getSession()->getPage()->pressButton('Add another item'); Chris@17: $edit = [ Chris@17: 'field_test_multi[1][value]' => $example_text_2, Chris@17: 'field_test_multi[2][value]' => $example_text_3, Chris@17: ]; Chris@17: $this->drupalPostForm(NULL, $edit, t('Preview')); Chris@17: $this->clickLink(t('Back to content editing')); Chris@17: $this->drupalPostForm(NULL, $edit, t('Preview')); Chris@17: $this->clickLink(t('Back to content editing')); Chris@17: $this->assertFieldByName('field_test_multi[0][value]', $example_text_1); Chris@17: $this->assertFieldByName('field_test_multi[1][value]', $example_text_2); Chris@17: $this->assertFieldByName('field_test_multi[2][value]', $example_text_3); Chris@17: Chris@17: // Now save the node and make sure all values got saved. Chris@17: $this->drupalPostForm(NULL, [], t('Save')); Chris@17: $this->assertText($example_text_1); Chris@17: $this->assertText($example_text_2); Chris@17: $this->assertText($example_text_3); Chris@17: Chris@17: // Edit again, change the menu_ui settings and click on preview. Chris@17: $this->drupalGet('node/' . $node->id() . '/edit'); Chris@17: $edit = [ Chris@17: 'menu[enabled]' => TRUE, Chris@17: 'menu[title]' => 'Changed title', Chris@17: ]; Chris@17: $this->drupalPostForm(NULL, $edit, t('Preview')); Chris@17: $this->clickLink(t('Back to content editing')); Chris@17: $this->assertFieldChecked('edit-menu-enabled', 'Menu option is still checked'); Chris@17: $this->assertFieldByName('menu[title]', 'Changed title', 'Menu link title is correct after preview'); Chris@17: Chris@17: // Save, change the title while saving and make sure that it is correctly Chris@17: // saved. Chris@17: $edit = [ Chris@17: 'menu[enabled]' => TRUE, Chris@17: 'menu[title]' => 'Second title change', Chris@17: ]; Chris@17: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@17: $this->drupalGet('node/' . $node->id() . '/edit'); Chris@17: $this->assertFieldByName('menu[title]', 'Second title change', 'Menu link title is correct after saving'); Chris@17: Chris@17: } Chris@17: Chris@17: /** Chris@17: * Checks the node preview functionality, when using revisions. Chris@17: */ Chris@17: public function testPagePreviewWithRevisions() { Chris@17: $title_key = 'title[0][value]'; Chris@17: $body_key = 'body[0][value]'; Chris@17: $term_key = $this->fieldName . '[target_id]'; Chris@17: // Force revision on "Basic page" content. Chris@17: $node_type = NodeType::load('page'); Chris@17: $node_type->setNewRevision(TRUE); Chris@17: $node_type->save(); Chris@17: Chris@17: // Fill in node creation form and preview node. Chris@17: $edit = []; Chris@17: $edit[$title_key] = $this->randomMachineName(8); Chris@17: $edit[$body_key] = $this->randomMachineName(16); Chris@17: $edit[$term_key] = $this->term->id(); Chris@17: $edit['revision_log[0][value]'] = $this->randomString(32); Chris@17: $this->drupalPostForm('node/add/page', $edit, t('Preview')); Chris@17: Chris@17: // Check that the preview is displaying the title, body and term. Chris@17: $this->assertTitle(t('@title | Drupal', ['@title' => $edit[$title_key]]), 'Basic page title is preview.'); Chris@17: $this->assertText($edit[$title_key], 'Title displayed.'); Chris@17: $this->assertText($edit[$body_key], 'Body displayed.'); Chris@17: $this->assertText($edit[$term_key], 'Term displayed.'); Chris@17: Chris@17: // Check that the title and body fields are displayed with the correct Chris@17: // values after going back to the content edit page. Chris@17: $this->clickLink(t('Back to content editing')); $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.'); Chris@17: $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.'); Chris@17: $this->assertFieldByName($term_key, $edit[$term_key], 'Term field displayed.'); Chris@17: Chris@17: // Check that the revision log field has the correct value. Chris@17: $this->assertFieldByName('revision_log[0][value]', $edit['revision_log[0][value]'], 'Revision log field displayed.'); Chris@17: Chris@17: // Save the node after coming back from the preview page so we can create a Chris@17: // pending revision for it. Chris@17: $this->drupalPostForm(NULL, [], t('Save')); Chris@17: $node = $this->drupalGetNodeByTitle($edit[$title_key]); Chris@17: Chris@17: // Check that previewing a pending revision of a node works. This can not be Chris@17: // accomplished through the UI so we have to use API calls. Chris@17: // @todo Change this test to use the UI when we will be able to create Chris@17: // pending revisions in core. Chris@17: // @see https://www.drupal.org/node/2725533 Chris@17: $node->setNewRevision(TRUE); Chris@17: $node->isDefaultRevision(FALSE); Chris@17: Chris@17: /** @var \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver */ Chris@17: $controller_resolver = \Drupal::service('controller_resolver'); Chris@17: $node_preview_controller = $controller_resolver->getControllerFromDefinition('\Drupal\node\Controller\NodePreviewController::view'); Chris@17: $node_preview_controller($node, 'full'); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Checks the node preview accessible for simultaneous node editing. Chris@17: */ Chris@17: public function testSimultaneousPreview() { Chris@17: $title_key = 'title[0][value]'; Chris@17: $node = $this->drupalCreateNode([]); Chris@17: Chris@17: $edit = [$title_key => 'New page title']; Chris@17: $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview')); Chris@17: $this->assertText($edit[$title_key]); Chris@17: Chris@17: $user2 = $this->drupalCreateUser(['edit any page content']); Chris@17: $this->drupalLogin($user2); Chris@17: $this->drupalGet('node/' . $node->id() . '/edit'); Chris@17: $this->assertFieldByName($title_key, $node->label(), 'No title leaked from previous user.'); Chris@17: Chris@17: $edit2 = [$title_key => 'Another page title']; Chris@17: $this->drupalPostForm('node/' . $node->id() . '/edit', $edit2, t('Preview')); Chris@18: $this->assertUrl(Url::fromRoute('entity.node.preview', ['node_preview' => $node->uuid(), 'view_mode_id' => 'full'], ['absolute' => TRUE])->toString()); Chris@17: $this->assertText($edit2[$title_key]); Chris@17: } Chris@17: Chris@17: }