function ContentTmgmtEntitySourceUiTest::testEmbeddedReferences

Tests the embedded references.

File

sources/content/tests/src/Functional/ContentTmgmtEntitySourceUiTest.php, line 610

Class

ContentTmgmtEntitySourceUiTest
Content entity source UI tests.

Namespace

Drupal\Tests\tmgmt_content\Functional

Code

function testEmbeddedReferences() {

  // Create 4 field storages, 3 for nodes, 1 for users (not translatable
  // target).
  $field1 = FieldStorageConfig::create(array(
    'field_name' => 'field1',
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'cardinality' => -1,
    'settings' => array(
      'target_type' => 'node',
    ),
  ));
  $field1
    ->save();
  $field2 = FieldStorageConfig::create(array(
    'field_name' => 'field2',
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'cardinality' => -1,
    'settings' => array(
      'target_type' => 'node',
    ),
  ));
  $field2
    ->save();
  $field3 = FieldStorageConfig::create(array(
    'field_name' => 'field3',
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'cardinality' => -1,
    'settings' => array(
      'target_type' => 'node',
    ),
  ));
  $field3
    ->save();
  $field4 = FieldStorageConfig::create(array(
    'field_name' => 'field4',
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'cardinality' => -1,
    'settings' => array(
      'target_type' => 'user',
    ),
  ));
  $field4
    ->save();
  $this
    ->createNodeType('untranslatable', 'Untranslatable', FALSE);

  // There are two node types, article (translatable) and untranslatable, with
  // the following field configuration:
  // Untranslatable Field 1 on article and untranslatable: Available
  // Untranslatable Field 2 on untranslatable: Not Available
  // Translatable Field 3 on article: Available
  // Untranslatable Field 4 (user reference) on article: Not available.
  FieldConfig::create(array(
    'field_storage' => $field1,
    'bundle' => 'article',
    'label' => 'Field 1',
    'translatable' => FALSE,
    'settings' => array(),
  ))
    ->save();
  FieldConfig::create(array(
    'field_storage' => $field1,
    'bundle' => 'untranslatable',
    'label' => 'Field 1',
    'translatable' => FALSE,
    'settings' => array(),
  ))
    ->save();
  FieldConfig::create(array(
    'field_storage' => $field2,
    'bundle' => 'untranslatable',
    'label' => 'Field 2',
    'translatable' => FALSE,
    'settings' => array(),
  ))
    ->save();
  FieldConfig::create(array(
    'field_storage' => $field3,
    'bundle' => 'article',
    'label' => 'Field 3',
    'translatable' => TRUE,
    'settings' => array(),
  ))
    ->save();
  FieldConfig::create(array(
    'field_storage' => $field4,
    'bundle' => 'article',
    'label' => 'Field 4',
    'translatable' => FALSE,
    'settings' => array(),
  ))
    ->save();
  EntityViewDisplay::load('node.article.default')
    ->setComponent('field1', [
    'type' => 'entity_reference_entity_view',
    'settings' => [
      'view_mode' => 'teaser',
    ],
  ])
    ->save();
  $this
    ->drupalGet('admin/tmgmt/settings');

  // Field 1 and 3 should be available, enable them.
  $checked_reference_fields = array(
    'embedded_fields[node][field1]' => TRUE,
    'embedded_fields[node][field3]' => TRUE,
  );

  // The node about translatable fields should be shown exactly once.
  $this
    ->assertSession()
    ->pageTextContainsOnce('Note: This is a translatable field, embedding this will add a translation on the existing reference.');

  // String fields, field 2 and 4 as well as the node type und uid reference
  // should not show up.
  $this
    ->assertSession()
    ->fieldNotExists('embedded_fields[node][title]');
  $this
    ->assertSession()
    ->fieldNotExists('embedded_fields[node][uid]');
  $this
    ->assertSession()
    ->fieldNotExists('embedded_fields[node][field2]');
  $this
    ->assertSession()
    ->fieldNotExists('embedded_fields[node][field4]');
  $this
    ->assertSession()
    ->fieldNotExists('embedded_fields[node][type]');
  $this
    ->submitForm($checked_reference_fields, 'Save configuration');

  // Check if the save was successful.
  $this
    ->assertSession()
    ->pageTextContains('The configuration options have been saved.');
  $this
    ->assertSession()
    ->checkboxChecked('edit-embedded-fields-node-field1');
  $this
    ->assertSession()
    ->checkboxChecked('edit-embedded-fields-node-field3');

  // Create translatable child node.
  $edit = [
    'title' => 'Child title',
    'type' => 'article',
    'langcode' => 'en',
  ];
  $child_node = $this
    ->createNode($edit);

  // Create translatable parent node.
  $edit = [
    'title' => 'Parent title',
    'type' => 'article',
    'langcode' => 'en',
  ];
  $edit['field1'][]['target_id'] = $child_node
    ->id();
  $parent_node = $this
    ->createNode($edit);

  // Create a translation job.
  $job = $this
    ->createJob('en', 'de');
  $job->translator = $this->default_translator
    ->id();
  $job
    ->save();
  $job_item = tmgmt_job_item_create('content', $parent_node
    ->getEntityTypeId(), $parent_node
    ->id(), array(
    'tjid' => $job
      ->id(),
  ));
  $job_item
    ->save();
  $job
    ->requestTranslation();

  // Visit preview page.
  $this
    ->drupalGet(URL::fromRoute('entity.tmgmt_job_item.canonical', [
    'tmgmt_job_item' => $job_item
      ->id(),
  ]));
  $this
    ->clickLink('Preview');

  // Check if parent and child nodes are translated.
  $this
    ->assertSession()
    ->pageTextContains('de(de-ch): ' . $parent_node
    ->getTitle());
  $this
    ->assertSession()
    ->pageTextContains('de(de-ch): ' . $parent_node->body->value);
  $this
    ->assertSession()
    ->pageTextContains('de(de-ch): ' . $child_node
    ->getTitle());
  $this
    ->assertSession()
    ->pageTextContains('de(de-ch): ' . $child_node->body->value);
}