You are here

public function DynamicEntityReferenceTest::testNodePreview in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/DynamicEntityReferenceTest.php \Drupal\Tests\dynamic_entity_reference\Functional\DynamicEntityReferenceTest::testNodePreview()

Tests node preview of dynamic entity reference field.

File

tests/src/Functional/DynamicEntityReferenceTest.php, line 522

Class

DynamicEntityReferenceTest
Ensures that Dynamic Entity References field works correctly.

Namespace

Drupal\Tests\dynamic_entity_reference\Functional

Code

public function testNodePreview() {
  $assert_session = $this
    ->assertSession();
  \Drupal::service('module_installer')
    ->install([
    'taxonomy',
    'node',
  ]);
  $this
    ->drupalCreateContentType([
    'type' => 'article',
  ]);
  $this->permissions = [
    'access content',
    'administer nodes',
    'administer node fields',
    'create article content',
  ];
  $this->adminUser = $this
    ->drupalCreateUser($this->permissions);
  $vocabulary = Vocabulary::create([
    'name' => $this
      ->randomMachineName(),
    'vid' => mb_strtolower($this
      ->randomMachineName()),
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $vocabulary
    ->save();
  $term = Term::create([
    'name' => $this
      ->randomMachineName(),
    'vid' => $vocabulary
      ->id(),
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $term
    ->save();
  $this
    ->drupalLogin($this->adminUser);

  // Add a new dynamic entity reference field.
  $this
    ->drupalGet('admin/structure/types/manage/article/fields/add-field');
  $edit = [
    'label' => 'DER',
    'field_name' => 'der',
    'new_storage_type' => 'dynamic_entity_reference',
  ];
  $this
    ->submitForm($edit, t('Save and continue'));
  $this
    ->submitForm([
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    'settings[exclude_entity_types]' => FALSE,
    'settings[entity_type_ids][]' => [
      'taxonomy_term',
      'entity_test_label',
    ],
  ], t('Save field settings'));
  $edit = [
    'settings[entity_test_label][handler_settings][target_bundles][entity_test_label]' => 'entity_test_label',
    'settings[taxonomy_term][handler_settings][target_bundles][' . $vocabulary
      ->id() . ']' => $vocabulary
      ->id(),
    'settings[taxonomy_term][handler_settings][auto_create]' => TRUE,
  ];
  $this
    ->submitForm($edit, t('Save settings'));
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();

  // Test the node preview for existing term.
  $this
    ->drupalGet('node/add/article');
  $assert_session
    ->fieldExists('field_der[0][target_id]');
  $assert_session
    ->fieldExists('field_der[0][target_type]');
  $title = $this
    ->randomMachineName();
  $edit = [
    'field_der[0][target_id]' => $term
      ->label() . ' (' . $term
      ->id() . ')',
    'field_der[0][target_type]' => 'taxonomy_term',
    'title[0][value]' => $title,
    'uid[0][target_id]' => $this->adminUser
      ->label() . ' (' . $this->adminUser
      ->id() . ')',
  ];
  $this
    ->submitForm($edit, t('Preview'));
  $assert_session
    ->pageTextContains($title);
  $assert_session
    ->pageTextContains($term
    ->label());

  // Back to node add page.
  $this
    ->clickLink('Back to content editing');
  $assert_session
    ->fieldValueEquals('field_der[0][target_id]', $term
    ->label() . ' (' . $term
    ->id() . ')');

  // Test the node preview for new term.
  $this
    ->drupalGet('node/add/article');
  $assert_session
    ->fieldExists('field_der[0][target_id]');
  $assert_session
    ->fieldExists('field_der[0][target_type]');
  $new_term = $this
    ->randomMachineName();
  $edit = [
    'field_der[0][target_id]' => $new_term,
    'field_der[0][target_type]' => 'taxonomy_term',
    'title[0][value]' => $title,
    'uid[0][target_id]' => $this->adminUser
      ->label() . ' (' . $this->adminUser
      ->id() . ')',
  ];
  $this
    ->submitForm($edit, t('Preview'));
  $assert_session
    ->pageTextContains($title);
  $assert_session
    ->pageTextContains($new_term);

  // Back to node add page.
  $this
    ->clickLink('Back to content editing');
  $assert_session
    ->fieldValueEquals('field_der[0][target_id]', $new_term);
}