public function ContentEntitySourceUnitTest::testEmbeddedReferences

Test extraction and saving translation for embedded references.

File

sources/content/tests/src/Kernel/ContentEntitySourceUnitTest.php, line 529

Class

ContentEntitySourceUnitTest
Content entity Source unit tests.

Namespace

Drupal\Tests\tmgmt_content\Kernel

Code

public function testEmbeddedReferences() {
  $field1 = FieldStorageConfig::create(array(
    'field_name' => 'field1',
    'entity_type' => $this->entityTypeId,
    'type' => 'entity_reference',
    'cardinality' => -1,
    'settings' => array(
      'target_type' => $this->entityTypeId,
    ),
  ));
  $field1
    ->save();
  $field2 = FieldStorageConfig::create(array(
    'field_name' => 'field2',
    'entity_type' => $this->entityTypeId,
    'type' => 'entity_reference',
    'cardinality' => -1,
    'settings' => array(
      'target_type' => $this->entityTypeId,
    ),
  ));
  $field2
    ->save();

  // Create field instances on the content type.
  FieldConfig::create(array(
    'field_storage' => $field1,
    'bundle' => $this->entityTypeId,
    'label' => 'Field 1',
    'settings' => array(),
  ))
    ->save();
  FieldConfig::create(array(
    'field_storage' => $field2,
    'bundle' => $this->entityTypeId,
    'label' => 'Field 2',
    'translatable' => FALSE,
    'settings' => array(),
  ))
    ->save();
  $this
    ->config('tmgmt_content.settings')
    ->set('embedded_fields.' . $this->entityTypeId . '.field1', TRUE)
    ->save();

  // Create test entities that can be referenced, the first 5 with en
  // then two with cs as source language.
  $referenced_entities = [];
  for ($i = 0; $i < 7; $i++) {
    $referenced_values = [
      'langcode' => $i < 5 ? 'en' : 'cs',
      'user_id' => 1,
      'name' => 'Referenced entity #' . $i,
    ];
    $referenced_entities[$i] = EntityTestMul::create($referenced_values);
    $referenced_entities[$i]
      ->save();
  }

  // Add a translation for one of the cs entities.
  $referenced_entities[5]
    ->addTranslation('en', [
    'name' => 'EN entity #5',
  ]);
  $referenced_entities[5]
    ->save();

  // Create an english test entity.
  $values = array(
    'langcode' => 'en',
    'user_id' => 1,
    'name' => $this
      ->randomString(),
    'field1' => [
      $referenced_entities[0],
      $referenced_entities[1],
      $referenced_entities[2],
      $referenced_entities[5],
      $referenced_entities[6],
    ],
    'field2' => $referenced_entities[4],
  );
  $entity_test = EntityTestMul::create($values);
  $entity_test
    ->save();
  $job = tmgmt_job_create('en', 'de');
  $job->translator = 'test_translator';
  $job
    ->save();
  $job_item = tmgmt_job_item_create('content', $this->entityTypeId, $entity_test
    ->id(), array(
    'tjid' => $job
      ->id(),
  ));
  $job_item
    ->save();
  $source_plugin = $this->container
    ->get('plugin.manager.tmgmt.source')
    ->createInstance('content');
  $data = $source_plugin
    ->getData($job_item);

  // Ensure that field 2 is not in the extracted data.
  $this
    ->assertFalse(isset($data['field2']));

  // Ensure some labels and structure for field 1.
  $this
    ->assertEquals('Field 1', $data['field1']['#label']);
  $this
    ->assertEquals('Delta #0', $data['field1'][0]['#label']);
  $this
    ->assertEquals('Name', $data['field1'][0]['entity']['name']['#label'], 'Name');
  $this
    ->assertEquals($data['field1'][0]['entity']['name'][0]['value']['#text'], 'Referenced entity #0');
  $this
    ->assertEquals($data['field1'][1]['entity']['name'][0]['value']['#text'], 'Referenced entity #1');
  $this
    ->assertEquals($data['field1'][2]['entity']['name'][0]['value']['#text'], 'Referenced entity #2');
  $this
    ->assertEquals($data['field1'][3]['entity']['name'][0]['value']['#text'], 'EN entity #5');
  $this
    ->assertEquals($data['field1'][4]['entity']['name'][0]['value']['#text'], 'Referenced entity #6');

  // Now request a translation.
  $job
    ->requestTranslation();

  // Mess with the source entity while the job is being translated. Remove
  // the second reference and switch positions.
  $entity_test
    ->set('field1', [
    $referenced_entities[2],
    $referenced_entities[0],
    $referenced_entities[5],
    $referenced_entities[6],
  ]);
  $entity_test
    ->save();
  $items = $job
    ->getItems();
  $item = reset($items);
  $item
    ->acceptTranslation();
  \Drupal::entityTypeManager()
    ->getStorage('entity_test_mul')
    ->resetCache();

  // Check that the translations were saved correctly, making sure that the
  // translations were attached to the correct referenced entities as far
  // as possible.
  $referenced_translation = EntityTestMul::load($referenced_entities[0]
    ->id())
    ->getTranslation('de');
  $this
    ->assertEquals('de(de-ch): Referenced entity #0', $referenced_translation
    ->get('name')->value);
  $referenced_translation = EntityTestMul::load($referenced_entities[2]
    ->id())
    ->getTranslation('de');
  $this
    ->assertEquals('de(de-ch): Referenced entity #2', $referenced_translation
    ->get('name')->value);
  $referenced_translation = EntityTestMul::load($referenced_entities[5]
    ->id())
    ->getTranslation('de');
  $this
    ->assertEquals('de(de-ch): EN entity #5', $referenced_translation
    ->get('name')->value);
  $referenced_translation = EntityTestMul::load($referenced_entities[6]
    ->id())
    ->getTranslation('de');
  $this
    ->assertEquals('de(de-ch): Referenced entity #6', $referenced_translation
    ->get('name')->value);
  $this
    ->assertFalse(EntityTestMul::load($referenced_entities[1]
    ->id())
    ->hasTranslation('de'));
  $this
    ->assertFalse(EntityTestMul::load($referenced_entities[3]
    ->id())
    ->hasTranslation('de'));
  $this
    ->assertFalse(EntityTestMul::load($referenced_entities[4]
    ->id())
    ->hasTranslation('de'));
}