You are here

public function DiffDynamicEntityReferencePluginTest::testDynamicEntityReferencePlugin in Dynamic Entity Reference 8

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

Tests the DynamicEntityReferenceFieldBuilder plugin.

See also

\Drupal\dynamic_entity_reference\Plugin\diff\Field\DynamicEntityReferenceFieldBuilder

File

tests/src/Functional/DiffDynamicEntityReferencePluginTest.php, line 45

Class

DiffDynamicEntityReferencePluginTest
Tests dynamic entity reference diff plugin.

Namespace

Drupal\Tests\dynamic_entity_reference\Functional

Code

public function testDynamicEntityReferencePlugin() {

  // Add an dynamic entity reference field to the article content type.
  $bundle_path = 'admin/structure/types/manage/article';
  $field_name = 'reference';
  $storage_edit = [
    'settings[exclude_entity_types]' => FALSE,
    'settings[entity_type_ids][]' => [
      'user',
      'node',
    ],
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ];
  $field_edit = [
    'settings[node][handler_settings][target_bundles][article]' => TRUE,
  ];
  $this
    ->fieldUIAddNewField($bundle_path, $field_name, 'Reference', 'dynamic_entity_reference', $storage_edit, $field_edit);

  // Create three article nodes.
  $node1 = $this
    ->drupalCreateNode([
    'type' => 'article',
    'title' => 'Article A',
  ]);
  $node2 = $this
    ->drupalCreateNode([
    'type' => 'article',
    'title' => 'Article B',
  ]);
  $node3 = $this
    ->drupalCreateNode([
    'type' => 'article',
    'title' => 'Article C',
  ]);

  // Reference article B in article A.
  $edit = [
    'field_reference[0][target_type]' => 'node',
    'field_reference[0][target_id]' => 'Article B (' . $node2
      ->id() . ')',
    'field_reference[1][target_type]' => 'user',
    'field_reference[1][target_id]' => 'admin (1)',
    'revision' => TRUE,
  ];
  $this
    ->drupalGet('node/' . $node1
    ->id() . '/edit');
  $button = $this
    ->assertSession()
    ->buttonExists('Add another item');
  $button
    ->click();
  $this
    ->submitForm($edit, t('Save'));

  // Update article A so it points to article C instead of B.
  $edit = [
    'field_reference[0][target_type]' => 'user',
    'field_reference[0][target_id]' => $this->adminUser
      ->label() . ' (' . $this->adminUser
      ->id() . ')',
    'field_reference[1][target_type]' => 'node',
    'field_reference[1][target_id]' => 'Article C (' . $node3
      ->id() . ')',
    'revision' => TRUE,
  ];
  $this
    ->drupalGet('node/' . $node1
    ->id() . '/edit');
  $button = $this
    ->assertSession()
    ->buttonExists('Add another item');
  $button
    ->click();
  $this
    ->submitForm($edit, t('Save'));

  // Check differences between revisions.
  $this
    ->clickLink(t('Revisions'));
  $this
    ->submitForm([], t('Compare selected revisions'));
  $this
    ->assertText('Reference');
  $this
    ->assertText('Article B');
  $this
    ->assertText('Article C');
  $this
    ->assertText('admin');
  $this
    ->assertText($this->adminUser
    ->label());
}