You are here

function EntityReferenceRevisionsCompositeTest::testCompositeRelationshipWithTranslationTranslatableField in Entity Reference Revisions 8

Tests composite relationship with translations and a translatable field.

File

tests/src/Kernel/EntityReferenceRevisionsCompositeTest.php, line 282

Class

EntityReferenceRevisionsCompositeTest
Tests the entity_reference_revisions composite relationship.

Namespace

Drupal\Tests\entity_reference_revisions\Kernel

Code

function testCompositeRelationshipWithTranslationTranslatableField() {
  $field_config = FieldConfig::loadByName('node', 'article', 'composite_reference');
  $field_config
    ->setTranslatable(TRUE);
  $field_config
    ->save();
  ConfigurableLanguage::createFromLangcode('de')
    ->save();

  // Create the test composite entity with a translation.
  $composite = EntityTestCompositeRelationship::create(array(
    'uuid' => $this
      ->randomMachineName(),
    'name' => $this
      ->randomMachineName(),
  ));
  $composite
    ->addTranslation('de', $composite
    ->toArray());
  $composite
    ->save();

  // Create a node with a reference to the test composite entity.
  $node = Node::create(array(
    'title' => $this
      ->randomMachineName(),
    'type' => 'article',
    'composite_reference' => $composite,
  ));
  $node
    ->addTranslation('de', $node
    ->toArray());
  $node
    ->save();

  // Verify the value of parent type and id after create a node.
  $composite = EntityTestCompositeRelationship::load($composite
    ->id());
  $this
    ->assertEquals($node
    ->getEntityTypeId(), $composite->parent_type->value);
  $this
    ->assertEquals($node
    ->id(), $composite->parent_id->value);
  $this
    ->assertEquals('composite_reference', $composite->parent_field_name->value);

  // Test that the composite entity is not deleted when the German parent
  // translation is removed.
  $node
    ->removeTranslation('de');
  $node
    ->save();
  $this->cron
    ->run();
  $composite = EntityTestCompositeRelationship::load($composite
    ->id());
  $this
    ->assertNotNull($composite);

  // Test that the composite entity is deleted when its parent is deleted.
  $node
    ->delete();
  $this->cron
    ->run();
  $composite = EntityTestCompositeRelationship::load($composite
    ->id());
  $this
    ->assertNull($composite);
}