You are here

class EntityReferenceRevisionsCompositeTranslationTest in Entity Reference Revisions 8

Tests the entity_reference_revisions composite relationship.

@group entity_reference_revisions

Hierarchy

Expanded class hierarchy of EntityReferenceRevisionsCompositeTranslationTest

File

tests/src/Kernel/EntityReferenceRevisionsCompositeTranslationTest.php, line 22

Namespace

Drupal\Tests\entity_reference_revisions\Kernel
View source
class EntityReferenceRevisionsCompositeTranslationTest extends EntityKernelTestBase {
  use ContentTypeCreationTrait;
  use NodeCreationTrait;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'node',
    'field',
    'entity_reference_revisions',
    'entity_composite_relationship_test',
    'language',
    'content_translation',
  ];

  /**
   * The current database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   *
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    ConfigurableLanguage::createFromLangcode('de')
      ->save();
    ConfigurableLanguage::createFromLangcode('fr')
      ->save();
    $this
      ->installEntitySchema('entity_test_composite');
    $this
      ->installSchema('node', [
      'node_access',
    ]);

    // Create article content type.
    NodeType::create([
      'type' => 'article',
      'name' => 'Article',
    ])
      ->save();

    // Create the reference to the composite entity test.
    $field_storage = FieldStorageConfig::create([
      'field_name' => 'composite_reference',
      'entity_type' => 'node',
      'type' => 'entity_reference_revisions',
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
      'settings' => [
        'target_type' => 'entity_test_composite',
      ],
    ]);
    $field_storage
      ->save();
    $field = FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => 'article',
      'translatable' => FALSE,
    ]);
    $field
      ->save();

    // Create an untranslatable field on the composite entity.
    $text_field_storage = FieldStorageConfig::create([
      'field_name' => 'field_untranslatable',
      'entity_type' => 'entity_test_composite',
      'type' => 'string',
    ]);
    $text_field_storage
      ->save();
    $text_field = FieldConfig::create([
      'field_storage' => $text_field_storage,
      'bundle' => 'entity_test_composite',
      'translatable' => FALSE,
    ]);
    $text_field
      ->save();

    // Add a nested composite field.
    $field_storage = FieldStorageConfig::create([
      'field_name' => 'composite_reference',
      'entity_type' => 'entity_test_composite',
      'type' => 'entity_reference_revisions',
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
      'settings' => [
        'target_type' => 'entity_test_composite',
      ],
    ]);
    $field_storage
      ->save();
    $field = FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => 'entity_test_composite',
      'translatable' => FALSE,
    ]);
    $field
      ->save();

    // Inject database connection and entity type manager for the tests.
    $this->database = \Drupal::database();
    $this->entityTypeManager = \Drupal::entityTypeManager();

    // @todo content_translation should not be needed for a storage test, but
    //   \Drupal\Core\Entity\ContentEntityBase::isTranslatable() only returns
    //   TRUE if the bundle is explicitly translatable.
    \Drupal::service('content_translation.manager')
      ->setEnabled('node', 'article', TRUE);
    \Drupal::service('content_translation.manager')
      ->setEnabled('entity_test_composite', 'entity_test_composite', TRUE);
    \Drupal::service('content_translation.manager')
      ->setBundleTranslationSettings('node', 'article', [
      'untranslatable_fields_hide' => TRUE,
    ]);
    \Drupal::service('entity_type.bundle.info')
      ->clearCachedBundles();
  }

  /**
   * Test the storage for handling pending revisions with translations.
   */
  public function testCompositePendingRevisionTranslation() {

    /** @var \Drupal\node\NodeStorageInterface $node_storage */
    $node_storage = \Drupal::entityTypeManager()
      ->getStorage('node');

    // Create a nested composite entity.
    $nested_composite = EntityTestCompositeRelationship::create([
      'langcode' => 'en',
      'name' => 'Initial Nested Source Composite',
    ]);
    $nested_composite
      ->save();

    // Create a composite entity.
    $composite = EntityTestCompositeRelationship::create([
      'langcode' => 'en',
      'name' => 'Initial Source Composite',
      'field_untranslatable' => 'Initial untranslatable field',
      'composite_reference' => $nested_composite,
    ]);
    $composite
      ->save();

    // Create a node with a reference to the test composite entity.
    $node = Node::create([
      'langcode' => 'en',
      'title' => 'Initial Source Node',
      'type' => 'article',
      'composite_reference' => $composite,
    ]);
    $node
      ->save();
    $initial_revision_id = $node
      ->getRevisionId();

    /** @var \Drupal\node\NodeInterface $node */
    $node = $node_storage
      ->load($node
      ->id());

    // Assert that there is only 1 revision when creating a node.
    $this
      ->assertRevisionCount(1, $node);

    // Assert there is no new composite revision after creating a host entity.
    $this
      ->assertRevisionCount(1, $composite);

    // Assert there is no new composite revision after creating a host entity.
    $this
      ->assertRevisionCount(1, $nested_composite);

    // Create a second nested composite entity.
    $second_nested_composite = EntityTestCompositeRelationship::create([
      'langcode' => 'en',
      'name' => 'Initial Nested Composite #2',
    ]);

    // Add a pending revision.
    $node = $node_storage
      ->createRevision($node, FALSE);
    $node
      ->get('composite_reference')->entity
      ->get('composite_reference')
      ->appendItem($second_nested_composite);
    $node
      ->save();
    $pending_en_revision_id = $node
      ->getRevisionId();
    $this
      ->assertRevisionCount(2, $node);
    $this
      ->assertRevisionCount(2, $composite);
    $this
      ->assertRevisionCount(2, $nested_composite);
    $this
      ->assertRevisionCount(1, $second_nested_composite);

    // Create a DE translation, start as a draft to replicate the behavior of
    // the UI.
    $node_de = $node
      ->addTranslation('de', [
      'title' => 'New Node #1 DE',
    ] + $node
      ->toArray());
    $node_de = $node_storage
      ->createRevision($node_de, FALSE);

    // Despite starting of the draft revision, creating draft of the translation
    // uses the paragraphs of the default revision.
    $this
      ->assertCount(1, $node_de
      ->get('composite_reference')->entity
      ->get('composite_reference'));
    $node_de
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->set('name', 'New Composite #1 DE');
    $node_de
      ->get('composite_reference')->entity
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->set('name', 'New Nested Composite #1 DE');
    $node_de
      ->isDefaultRevision(TRUE);
    $violations = $node_de
      ->validate();
    foreach ($violations as $violation) {
      $this
        ->fail($violation
        ->getPropertyPath() . ': ' . $violation
        ->getMessage());
    }
    $this
      ->assertEquals(0, count($violations));
    $node_de
      ->save();
    $this
      ->assertRevisionCount(3, $node);
    $this
      ->assertRevisionCount(3, $composite);
    $this
      ->assertRevisionCount(3, $nested_composite);
    $this
      ->assertRevisionCount(1, $second_nested_composite);

    // Update the translation as a pending revision for both the composite and
    // the node.
    $node_de
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->set('name', 'Pending Revision Composite #1 DE');
    $node_de
      ->get('composite_reference')->entity
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->set('name', 'Pending Nested Composite #1 DE');
    $node_de
      ->set('title', 'Pending Revision Node #1 DE');
    $node_de
      ->setNewRevision(TRUE);
    $node_de
      ->isDefaultRevision(FALSE);
    $violations = $node_de
      ->validate();
    foreach ($violations as $violation) {
      $this
        ->fail($violation
        ->getMessage());
    }
    $this
      ->assertEquals(0, count($violations));
    $node_de
      ->save();
    $this
      ->assertRevisionCount(4, $node);
    $this
      ->assertRevisionCount(4, $composite);
    $this
      ->assertRevisionCount(4, $nested_composite);
    $this
      ->assertRevisionCount(1, $second_nested_composite);

    /** @var \Drupal\node\NodeInterface $node_de */
    $node_de = $node_storage
      ->loadRevision($node_de
      ->getRevisionId());
    $this
      ->assertFalse($node_de
      ->isDefaultRevision());
    $this
      ->assertFalse((bool) $node_de
      ->isRevisionTranslationAffected());
    $this
      ->assertTrue((bool) $node_de
      ->getTranslation('de')
      ->isRevisionTranslationAffected());
    $this
      ->assertEquals('Pending Revision Node #1 DE', $node_de
      ->getTranslation('de')
      ->label());
    $this
      ->assertEquals('Initial Source Node', $node_de
      ->label());
    $this
      ->assertFalse($node_de
      ->get('composite_reference')->entity
      ->isDefaultRevision());
    $this
      ->assertEquals('Pending Revision Composite #1 DE', $node_de
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->label());
    $this
      ->assertEquals('Pending Nested Composite #1 DE', $node_de
      ->get('composite_reference')->entity
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->label());
    $this
      ->assertEquals('Initial untranslatable field', $node_de
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->get('field_untranslatable')->value);
    $this
      ->assertEquals('Initial Source Composite', $node_de
      ->get('composite_reference')->entity
      ->label());

    // Reload the default revision of the node, make sure that the composite
    // there is unchanged.
    $node = $node_storage
      ->load($node
      ->id());
    $this
      ->assertTrue($node
      ->hasTranslation('de'));
    $this
      ->assertEquals('Initial Source Node', $node
      ->label());
    $this
      ->assertTrue($node
      ->get('composite_reference')->entity
      ->hasTranslation('de'));
    $this
      ->assertEquals('Initial Source Composite', $node
      ->get('composite_reference')->entity
      ->label());

    // Create a FR translation, start as a draft to replicate the behavior of
    // the UI.
    $node_fr = $node
      ->addTranslation('fr', [
      'title' => 'Pending Revision Node #1 FR',
    ] + $node
      ->toArray());
    $node_fr = $node_storage
      ->createRevision($node_fr, FALSE);
    $node_fr
      ->get('composite_reference')->entity
      ->getTranslation('fr')
      ->set('name', 'Pending Revision Composite #1 FR');
    $node_fr
      ->get('composite_reference')->entity
      ->get('composite_reference')->entity
      ->getTranslation('fr')
      ->set('name', 'Pending Nested Composite #1 FR');
    $violations = $node_fr
      ->validate();
    $this
      ->assertEquals(0, count($violations));
    $node_fr
      ->save();

    // Now assert that all 3 revisions exist as expected. Two translation
    // pending revisions, each composite has the original revision as parent
    // without any existing translation.

    /** @var \Drupal\node\NodeInterface $node_fr */
    $node_fr = $node_storage
      ->loadRevision($node_fr
      ->getRevisionId());
    $this
      ->assertFalse($node_fr
      ->isDefaultRevision());
    $this
      ->assertTrue($node_fr
      ->hasTranslation('de'));
    $this
      ->assertFalse((bool) $node_fr
      ->isRevisionTranslationAffected());
    $this
      ->assertTrue((bool) $node_fr
      ->getTranslation('fr')
      ->isRevisionTranslationAffected());
    $this
      ->assertEquals('Pending Revision Node #1 FR', $node_fr
      ->getTranslation('fr')
      ->label());
    $this
      ->assertEquals('Initial Source Node', $node_fr
      ->label());
    $this
      ->assertFalse($node_fr
      ->get('composite_reference')->entity
      ->isDefaultRevision());
    $this
      ->assertTrue($node_fr
      ->get('composite_reference')->entity
      ->hasTranslation('de'));
    $this
      ->assertEquals('Pending Revision Composite #1 FR', $node_fr
      ->get('composite_reference')->entity
      ->getTranslation('fr')
      ->label());
    $this
      ->assertEquals('Pending Nested Composite #1 FR', $node_fr
      ->get('composite_reference')->entity
      ->get('composite_reference')->entity
      ->getTranslation('fr')
      ->label());
    $this
      ->assertEquals('Initial untranslatable field', $node_fr
      ->get('composite_reference')->entity
      ->getTranslation('fr')
      ->get('field_untranslatable')->value);
    $this
      ->assertEquals('Initial Source Composite', $node_fr
      ->get('composite_reference')->entity
      ->label());
    $node_de = $node_storage
      ->loadRevision($node_de
      ->getRevisionId());
    $this
      ->assertFalse($node_de
      ->isDefaultRevision());
    $this
      ->assertFalse($node_de
      ->hasTranslation('fr'));
    $this
      ->assertEquals('Pending Revision Node #1 DE', $node_de
      ->getTranslation('de')
      ->label());
    $this
      ->assertEquals('Initial Source Node', $node_de
      ->label());
    $this
      ->assertFalse($node_de
      ->get('composite_reference')->entity
      ->isDefaultRevision());
    $this
      ->assertFalse($node_de
      ->get('composite_reference')->entity
      ->hasTranslation('fr'));
    $this
      ->assertEquals('Pending Revision Composite #1 DE', $node_de
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->label());
    $this
      ->assertEquals('Pending Nested Composite #1 DE', $node_de
      ->get('composite_reference')->entity
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->label());
    $this
      ->assertEquals('Initial untranslatable field', $node_de
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->get('field_untranslatable')->value);
    $this
      ->assertEquals('Initial Source Composite', $node_de
      ->get('composite_reference')->entity
      ->label());

    // Reload the default revision of the node, make sure that the composite
    // there is unchanged.
    $node = $node_storage
      ->load($node
      ->id());
    $this
      ->assertTrue($node
      ->hasTranslation('de'));
    $this
      ->assertEquals('Initial Source Node', $node
      ->label());
    $this
      ->assertTrue($node
      ->get('composite_reference')->entity
      ->hasTranslation('de'));
    $this
      ->assertEquals('Initial Source Composite', $node
      ->get('composite_reference')->entity
      ->label());

    // Create another pending EN revision and make that the default.
    $node = $node_storage
      ->loadRevision($pending_en_revision_id);
    $new_revision = $node_storage
      ->createRevision($node);
    $new_revision
      ->get('composite_reference')->entity
      ->set('name', 'Updated Source Composite');
    $new_revision
      ->get('composite_reference')->entity
      ->set('field_untranslatable', 'Updated untranslatable field');
    $new_revision
      ->setTitle('Updated Source Node');
    $new_revision
      ->get('composite_reference')->entity
      ->get('composite_reference')[1]->entity
      ->set('name', 'Draft Nested Source Composite #2');
    $violations = $new_revision
      ->validate();
    $this
      ->assertEquals(0, count($violations));
    $new_revision
      ->save();

    // Assert the two english revisions.
    // Reload the default revision of the node, make sure that the composite
    // there is unchanged.
    $node = $node_storage
      ->load($node
      ->id());
    $this
      ->assertTrue($node
      ->isDefaultRevision());
    $this
      ->assertTrue($node
      ->hasTranslation('de'));
    $this
      ->assertFalse($node
      ->hasTranslation('fr'));
    $this
      ->assertTrue((bool) $node
      ->isRevisionTranslationAffected());
    $this
      ->assertEquals('Updated Source Node', $node
      ->label());
    $this
      ->assertTrue($node
      ->get('composite_reference')->entity
      ->isDefaultRevision());
    $this
      ->assertTrue($node
      ->get('composite_reference')->entity
      ->hasTranslation('de'));
    $this
      ->assertFalse($node
      ->get('composite_reference')->entity
      ->hasTranslation('fr'));
    $this
      ->assertEquals('Updated Source Composite', $node
      ->get('composite_reference')->entity
      ->label());
    $this
      ->assertEquals('Initial Nested Source Composite', $node
      ->get('composite_reference')->entity
      ->get('composite_reference')->entity
      ->label());
    $this
      ->assertEquals('Draft Nested Source Composite #2', $node
      ->get('composite_reference')->entity
      ->get('composite_reference')[1]->entity
      ->label());
    $this
      ->assertEquals('Updated untranslatable field', $node
      ->get('composite_reference')->entity
      ->get('field_untranslatable')->value);
    $node_initial = $node_storage
      ->loadRevision($initial_revision_id);
    $this
      ->assertFalse($node_initial
      ->isDefaultRevision());
    $this
      ->assertFalse($node_initial
      ->hasTranslation('de'));
    $this
      ->assertFalse($node_initial
      ->hasTranslation('fr'));
    $this
      ->assertEquals('Initial Source Node', $node_initial
      ->label());
    $this
      ->assertFalse($node_initial
      ->get('composite_reference')->entity
      ->isDefaultRevision());
    $this
      ->assertFalse($node_initial
      ->get('composite_reference')->entity
      ->hasTranslation('de'));
    $this
      ->assertEquals('Initial Source Composite', $node_initial
      ->get('composite_reference')->entity
      ->label());
    $this
      ->assertEquals('Initial Nested Source Composite', $node_initial
      ->get('composite_reference')->entity
      ->get('composite_reference')->entity
      ->label());
    $this
      ->assertEquals('Initial untranslatable field', $node_initial
      ->get('composite_reference')->entity
      ->get('field_untranslatable')->value);
    $this
      ->assertCount(1, $node_initial
      ->get('composite_reference')->entity
      ->get('composite_reference'));

    // The current node_fr pending revision still has the initial value before
    // "merging" it, but it will get the new value for the untranslatable field
    // in the new revision.
    $node_fr = $node_storage
      ->loadRevision($node_fr
      ->getRevisionId());
    $this
      ->assertEquals('Initial untranslatable field', $node_fr
      ->get('composite_reference')->entity
      ->get('field_untranslatable')->value);
    $this
      ->assertCount(1, $node_fr
      ->get('composite_reference')->entity
      ->get('composite_reference'));

    // Now publish the FR pending revision and also add a translation for
    // the second composite that it now has.
    $new_revision = $node_storage
      ->createRevision($node_fr
      ->getTranslation('fr'));
    $this
      ->assertCount(2, $new_revision
      ->get('composite_reference')->entity
      ->get('composite_reference'));
    $new_revision
      ->get('composite_reference')->entity
      ->get('composite_reference')[1]->entity
      ->getTranslation('fr')
      ->set('name', 'FR Nested Composite #2');
    $violations = $new_revision
      ->validate();
    $this
      ->assertEquals(0, count($violations));
    $new_revision
      ->save();
    $this
      ->assertRevisionCount(7, $node);
    $this
      ->assertRevisionCount(7, $composite);
    $this
      ->assertRevisionCount(7, $nested_composite);
    $this
      ->assertRevisionCount(3, $second_nested_composite);

    // The new default revision should now have the updated english source,
    // original german translation and the french pending revision.
    $node = $node_storage
      ->load($node
      ->id());
    $this
      ->assertTrue($node
      ->isDefaultRevision());
    $this
      ->assertTrue($node
      ->hasTranslation('de'));
    $this
      ->assertTrue($node
      ->hasTranslation('fr'));
    $this
      ->assertFalse((bool) $node
      ->isRevisionTranslationAffected());
    $this
      ->assertTrue((bool) $node
      ->getTranslation('fr')
      ->isRevisionTranslationAffected());
    $this
      ->assertEquals('Updated Source Node', $node
      ->label());
    $this
      ->assertTrue($node
      ->get('composite_reference')->entity
      ->isDefaultRevision());
    $this
      ->assertTrue($node
      ->get('composite_reference')->entity
      ->hasTranslation('de'));
    $this
      ->assertTrue($node
      ->get('composite_reference')->entity
      ->hasTranslation('fr'));
    $this
      ->assertEquals('Pending Revision Node #1 FR', $node
      ->getTranslation('fr')
      ->label());
    $this
      ->assertEquals('Pending Revision Composite #1 FR', $node
      ->get('composite_reference')->entity
      ->getTranslation('fr')
      ->label());
    $this
      ->assertEquals('Pending Nested Composite #1 FR', $node
      ->get('composite_reference')->entity
      ->get('composite_reference')->entity
      ->getTranslation('fr')
      ->label());
    $this
      ->assertEquals('New Node #1 DE', $node
      ->getTranslation('de')
      ->label());
    $this
      ->assertEquals('New Composite #1 DE', $node
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->label());
    $this
      ->assertEquals('New Nested Composite #1 DE', $node
      ->get('composite_reference')->entity
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->label());
    $this
      ->assertEquals('Updated Source Composite', $node
      ->get('composite_reference')->entity
      ->label());
    $this
      ->assertEquals('Updated untranslatable field', $node
      ->get('composite_reference')->entity
      ->get('field_untranslatable')->value);
    $this
      ->assertEquals('Draft Nested Source Composite #2', $node
      ->get('composite_reference')->entity
      ->get('composite_reference')[1]->entity
      ->label());
    $this
      ->assertEquals('FR Nested Composite #2', $node
      ->get('composite_reference')->entity
      ->get('composite_reference')[1]->entity
      ->getTranslation('fr')
      ->label());

    // Now publish the DE pending revision as well.
    $new_revision = $node_storage
      ->createRevision($node_de
      ->getTranslation('de'));
    $violations = $new_revision
      ->validate();
    $this
      ->assertCount(2, $new_revision
      ->get('composite_reference')->entity
      ->get('composite_reference'));
    $this
      ->assertEquals(0, count($violations));
    $new_revision
      ->save();
    $this
      ->assertRevisionCount(8, $node);
    $this
      ->assertRevisionCount(8, $composite);
    $this
      ->assertRevisionCount(8, $nested_composite);
    $this
      ->assertRevisionCount(4, $second_nested_composite);

    // The new default revision should now have the updated source and both
    // translations.
    $node = $node_storage
      ->load($node
      ->id());
    $this
      ->assertTrue($node
      ->isDefaultRevision());
    $this
      ->assertTrue($node
      ->hasTranslation('de'));
    $this
      ->assertTrue($node
      ->hasTranslation('fr'));
    $this
      ->assertFalse((bool) $node
      ->isRevisionTranslationAffected());
    $this
      ->assertFalse((bool) $node
      ->getTranslation('fr')
      ->isRevisionTranslationAffected());
    $this
      ->assertTrue((bool) $node
      ->getTranslation('de')
      ->isRevisionTranslationAffected());
    $this
      ->assertEquals('Updated Source Node', $node
      ->label());
    $this
      ->assertTrue($node
      ->get('composite_reference')->entity
      ->isDefaultRevision());
    $this
      ->assertTrue($node
      ->get('composite_reference')->entity
      ->hasTranslation('de'));
    $this
      ->assertTrue($node
      ->get('composite_reference')->entity
      ->hasTranslation('fr'));
    $this
      ->assertEquals('Pending Revision Node #1 FR', $node
      ->getTranslation('fr')
      ->label());
    $this
      ->assertEquals('Pending Revision Composite #1 FR', $node
      ->get('composite_reference')->entity
      ->getTranslation('fr')
      ->label());
    $this
      ->assertEquals('Pending Revision Node #1 DE', $node
      ->getTranslation('de')
      ->label());
    $this
      ->assertEquals('Pending Revision Composite #1 DE', $node
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->label());
    $this
      ->assertEquals('Pending Nested Composite #1 DE', $node
      ->get('composite_reference')->entity
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->label());
    $this
      ->assertEquals('Updated Source Composite', $node
      ->get('composite_reference')->entity
      ->label());
    $this
      ->assertEquals('Updated untranslatable field', $node
      ->get('composite_reference')->entity
      ->get('field_untranslatable')->value);
    $this
      ->assertEquals('Draft Nested Source Composite #2', $node
      ->get('composite_reference')->entity
      ->get('composite_reference')[1]->entity
      ->label());
    $this
      ->assertEquals('FR Nested Composite #2', $node
      ->get('composite_reference')->entity
      ->get('composite_reference')[1]->entity
      ->getTranslation('fr')
      ->label());

    // The second nested composite of DE inherited the default values for its
    // translation.
    $this
      ->assertEquals('Draft Nested Source Composite #2', $node
      ->get('composite_reference')->entity
      ->get('composite_reference')[1]->entity
      ->getTranslation('de')
      ->label());

    // Simulate creating a new pending revision like
    // \Drupal\content_moderation\EntityTypeInfo::entityPrepareForm().
    $new_revision = $node_storage
      ->createRevision($node);
    $revision_key = $new_revision
      ->getEntityType()
      ->getKey('revision');
    $new_revision
      ->set($revision_key, $new_revision
      ->getLoadedRevisionId());
    $new_revision
      ->save();
    $this
      ->assertEquals('Pending Nested Composite #1 DE', $new_revision
      ->get('composite_reference')->entity
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->label());
  }

  /**
   * Tests that composite translations affects the host entity's translations.
   */
  public function testCompositeTranslation() {

    /** @var \Drupal\node\NodeStorageInterface $node_storage */
    $node_storage = $this->entityTypeManager
      ->getStorage('node');

    // Create a composite entity.
    $composite = EntityTestCompositeRelationship::create([
      'langcode' => 'en',
      'name' => 'Initial Source Composite',
    ]);
    $composite
      ->save();

    // Create a node with a reference to the test composite entity.
    $node = Node::create([
      'langcode' => 'en',
      'title' => 'Initial Source Node',
      'type' => 'article',
      'composite_reference' => $composite,
    ]);
    $node
      ->save();

    /** @var \Drupal\node\NodeInterface $node */
    $node = $node_storage
      ->load($node
      ->id());

    // Assert that there is only 1 revision when creating a node.
    $this
      ->assertRevisionCount(1, $node);

    // Assert that there is only 1 affected revision when creating a node.
    $this
      ->assertAffectedRevisionCount(1, $node);

    // Assert there is no new composite revision after creating a host entity.
    $this
      ->assertRevisionCount(1, $composite);
    $node_de = $node
      ->addTranslation('de', [
      'title' => 'New Node #1 DE',
    ] + $node
      ->toArray());
    $node_de = $node_storage
      ->createRevision($node_de, FALSE);
    $node_de
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->set('name', 'New Composite #1 DE');
    $node_de
      ->isDefaultRevision(TRUE);
    $violations = $node_de
      ->validate();
    foreach ($violations as $violation) {
      $this
        ->fail($violation
        ->getPropertyPath() . ': ' . $violation
        ->getMessage());
    }
    $this
      ->assertEquals(0, count($violations));
    $node_de
      ->save();
    $this
      ->assertAffectedRevisionCount(1, $node_de);
    $this
      ->assertAffectedRevisionCount(1, $node);

    // Test that changing composite non default language (DE) reference results
    // in translation changes for this language but not for the default
    // language.
    $node_de
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->set('name', 'Change Composite #1 DE');
    $node_de
      ->setNewRevision();
    $node_de
      ->save();
    $this
      ->assertEquals('Change Composite #1 DE', $node_de
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->getName());

    // Make sure the node DE has one more affected translation revision.
    $this
      ->assertAffectedRevisionCount(2, $node_de);

    // Make sure the node EN has only one 1 affected translation revision.
    $this
      ->assertAffectedRevisionCount(1, $node);

    // Test that changing composite in default language (EN) results in
    // translation changes for this language but not for the DE language.
    $node = $node_storage
      ->load($node
      ->id());
    $node
      ->get('composite_reference')->entity
      ->set('name', 'Update Source #1');
    $node
      ->setNewRevision();
    $node
      ->save();
    $this
      ->assertEquals('Update Source #1', $node
      ->get('composite_reference')->entity
      ->getTranslation('en')
      ->getName());

    // The node EN now has 2 affected translation revision.
    $this
      ->assertAffectedRevisionCount(2, $node);

    // The node DE still has 2 affected translation revisions.
    $this
      ->assertAffectedRevisionCount(2, $node_de);
  }

  /**
   * Tests that nested composite translations affects the host translations.
   */
  public function testNestedCompositeTranslation() {

    /** @var \Drupal\node\NodeStorageInterface $node_storage */
    $node_storage = \Drupal::entityTypeManager()
      ->getStorage('node');

    // Create a nested composite entity.
    $nested_composite = EntityTestCompositeRelationship::create([
      'langcode' => 'en',
      'name' => 'Initial Nested Source Composite',
    ]);
    $nested_composite
      ->addTranslation('de', [
      'name' => 'Nested Source Composite DE',
    ] + $nested_composite
      ->toArray());
    $nested_composite
      ->save();

    // Create a composite entity.
    $composite = EntityTestCompositeRelationship::create([
      'langcode' => 'en',
      'name' => 'Initial Source Composite',
      'field_untranslatable' => 'Initial untranslatable field',
      'composite_reference' => $nested_composite,
    ]);
    $composite
      ->addTranslation('de', [
      'name' => 'Source Composite DE',
    ] + $composite
      ->toArray());
    $composite
      ->save();

    // Create a node with a reference to the test composite entity.
    $node = Node::create([
      'langcode' => 'en',
      'title' => 'Initial Source Node',
      'type' => 'article',
      'composite_reference' => $composite,
    ]);
    $node
      ->save();

    /** @var \Drupal\node\NodeInterface $node */
    $node = $node_storage
      ->load($node
      ->id());

    // Assert that there is only 1 revision when creating a node.
    $this
      ->assertRevisionCount(1, $node);

    // Assert that there is only 1 affected revision when creating a node.
    $this
      ->assertAffectedRevisionCount(1, $node);

    // Assert there is no new composite revision after creating a host entity.
    $this
      ->assertRevisionCount(1, $composite);

    // Assert there is no new nested composite revision after creating a host
    // entity.
    $this
      ->assertRevisionCount(1, $nested_composite);
    $node_de = $node
      ->addTranslation('de', [
      'title' => 'New Node #1 DE',
    ] + $node
      ->toArray());
    $node_de = $node_storage
      ->createRevision($node_de, FALSE);
    $node_de
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->set('name', 'New Nested Composite #1 DE');
    $node_de
      ->isDefaultRevision(TRUE);
    $node_de
      ->save();
    $this
      ->assertAffectedRevisionCount(1, $node_de);
    $this
      ->assertAffectedRevisionCount(1, $node);

    // Test that changing nested composite non default language (DE) reference
    // results in translation changes for this language but not for the default
    // language.
    $node_de
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->set('name', 'Change Nested Composite #1 DE');
    $node_de
      ->setNewRevision();
    $node_de
      ->save();
    $this
      ->assertEquals('Change Nested Composite #1 DE', $node_de
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->get('composite_reference')->entity
      ->getTranslation('de')
      ->getName());

    // Make sure the node DE has one more affected translation revision.
    $this
      ->assertAffectedRevisionCount(2, $node_de);

    // Make sure the node EN has only one 1 affected translation revision.
    $this
      ->assertAffectedRevisionCount(1, $node);

    // Test that changing nested composite in default language (EN) results in
    // translation changes for this language but not for the DE language.
    $node = $node_storage
      ->load($node
      ->id());
    $node
      ->get('composite_reference')->entity
      ->get('composite_reference')->entity
      ->set('name', 'Update Nested Source #1');
    $node
      ->setNewRevision();
    $node
      ->save();
    $this
      ->assertEquals('Update Nested Source #1', $node
      ->get('composite_reference')->entity
      ->getTranslation('en')
      ->get('composite_reference')->entity
      ->getTranslation('en')
      ->getName());

    // The node EN now has 2 affected translation revision.
    $this
      ->assertAffectedRevisionCount(2, $node);

    // The node DE still has 2 affected translation revisions.
    $this
      ->assertAffectedRevisionCount(2, $node_de);
  }

  /**
   * Asserts the affected revision count of a certain entity.
   *
   * @param int $expected
   *   The expected count.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   */
  protected function assertAffectedRevisionCount($expected, EntityInterface $entity) {
    $entity_type = $entity
      ->getEntityType();
    $affected_revisions_count = $this->entityTypeManager
      ->getStorage($entity_type
      ->id())
      ->getQuery()
      ->condition($entity_type
      ->getKey('id'), $entity
      ->id())
      ->condition($entity_type
      ->getKey('langcode'), $entity
      ->language()
      ->getId())
      ->condition($entity_type
      ->getKey('revision_translation_affected'), 1)
      ->allRevisions()
      ->count()
      ->execute();
    $this
      ->assertEquals($expected, $affected_revisions_count);
  }

  /**
   * Asserts the revision count of an entity.
   *
   * @param int $expected
   *   The expected amount of revisions.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   */
  protected function assertRevisionCount($expected, EntityInterface $entity) {
    $node_revisions_count = \Drupal::entityQuery($entity
      ->getEntityTypeId())
      ->condition($entity
      ->getEntityType()
      ->getKey('id'), $entity
      ->id())
      ->allRevisions()
      ->count()
      ->execute();
    $this
      ->assertEquals($expected, $node_revisions_count);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AssertContentTrait::$content protected property The current raw content.
AssertContentTrait::$drupalSettings protected property The drupalSettings value from the current raw $content.
AssertContentTrait::$elements protected property The XML structure parsed from the current raw $content. 1
AssertContentTrait::$plainTextContent protected property The plain-text content of raw $content (text nodes).
AssertContentTrait::assertEscaped protected function Passes if the raw text IS found escaped on the loaded page, fail otherwise.
AssertContentTrait::assertField protected function Asserts that a field exists with the given name or ID.
AssertContentTrait::assertFieldById protected function Asserts that a field exists with the given ID and value.
AssertContentTrait::assertFieldByName protected function Asserts that a field exists with the given name and value.
AssertContentTrait::assertFieldByXPath protected function Asserts that a field exists in the current page by the given XPath.
AssertContentTrait::assertFieldChecked protected function Asserts that a checkbox field in the current page is checked.
AssertContentTrait::assertFieldsByValue protected function Asserts that a field exists in the current page with a given Xpath result.
AssertContentTrait::assertLink protected function Passes if a link with the specified label is found.
AssertContentTrait::assertLinkByHref protected function Passes if a link containing a given href (part) is found.
AssertContentTrait::assertNoDuplicateIds protected function Asserts that each HTML ID is used for just a single element.
AssertContentTrait::assertNoEscaped protected function Passes if the raw text IS NOT found escaped on the loaded page, fail otherwise.
AssertContentTrait::assertNoField protected function Asserts that a field does not exist with the given name or ID.
AssertContentTrait::assertNoFieldById protected function Asserts that a field does not exist with the given ID and value.
AssertContentTrait::assertNoFieldByName protected function Asserts that a field does not exist with the given name and value.
AssertContentTrait::assertNoFieldByXPath protected function Asserts that a field does not exist or its value does not match, by XPath.
AssertContentTrait::assertNoFieldChecked protected function Asserts that a checkbox field in the current page is not checked.
AssertContentTrait::assertNoLink protected function Passes if a link with the specified label is not found.
AssertContentTrait::assertNoLinkByHref protected function Passes if a link containing a given href (part) is not found.
AssertContentTrait::assertNoLinkByHrefInMainRegion protected function Passes if a link containing a given href is not found in the main region.
AssertContentTrait::assertNoOption protected function Asserts that a select option in the current page does not exist.
AssertContentTrait::assertNoOptionSelected protected function Asserts that a select option in the current page is not checked.
AssertContentTrait::assertNoPattern protected function Triggers a pass if the perl regex pattern is not found in raw content.
AssertContentTrait::assertNoRaw protected function Passes if the raw text is NOT found on the loaded page, fail otherwise.
AssertContentTrait::assertNoText protected function Passes if the page (with HTML stripped) does not contains the text.
AssertContentTrait::assertNoTitle protected function Pass if the page title is not the given string.
AssertContentTrait::assertNoUniqueText protected function Passes if the text is found MORE THAN ONCE on the text version of the page.
AssertContentTrait::assertOption protected function Asserts that a select option in the current page exists.
AssertContentTrait::assertOptionByText protected function Asserts that a select option with the visible text exists.
AssertContentTrait::assertOptionSelected protected function Asserts that a select option in the current page is checked.
AssertContentTrait::assertOptionSelectedWithDrupalSelector protected function Asserts that a select option in the current page is checked.
AssertContentTrait::assertOptionWithDrupalSelector protected function Asserts that a select option in the current page exists.
AssertContentTrait::assertPattern protected function Triggers a pass if the Perl regex pattern is found in the raw content.
AssertContentTrait::assertRaw protected function Passes if the raw text IS found on the loaded page, fail otherwise.
AssertContentTrait::assertText protected function Passes if the page (with HTML stripped) contains the text.
AssertContentTrait::assertTextHelper protected function Helper for assertText and assertNoText.
AssertContentTrait::assertTextPattern protected function Asserts that a Perl regex pattern is found in the plain-text content.
AssertContentTrait::assertThemeOutput protected function Asserts themed output.
AssertContentTrait::assertTitle protected function Pass if the page title is the given string.
AssertContentTrait::assertUniqueText protected function Passes if the text is found ONLY ONCE on the text version of the page.
AssertContentTrait::assertUniqueTextHelper protected function Helper for assertUniqueText and assertNoUniqueText.
AssertContentTrait::buildXPathQuery protected function Builds an XPath query.
AssertContentTrait::constructFieldXpath protected function Helper: Constructs an XPath for the given set of attributes and value.
AssertContentTrait::cssSelect protected function Searches elements using a CSS selector in the raw content.
AssertContentTrait::getAllOptions protected function Get all option elements, including nested options, in a select.
AssertContentTrait::getDrupalSettings protected function Gets the value of drupalSettings for the currently-loaded page.
AssertContentTrait::getRawContent protected function Gets the current raw content.
AssertContentTrait::getSelectedItem protected function Get the selected value from a select field.
AssertContentTrait::getTextContent protected function Retrieves the plain-text content from the current raw content.
AssertContentTrait::getUrl protected function Get the current URL from the cURL handler. 1
AssertContentTrait::parse protected function Parse content returned from curlExec using DOM and SimpleXML.
AssertContentTrait::removeWhiteSpace protected function Removes all white-space between HTML tags from the raw content.
AssertContentTrait::setDrupalSettings protected function Sets the value of drupalSettings for the currently-loaded page.
AssertContentTrait::setRawContent protected function Sets the raw content (e.g. HTML).
AssertContentTrait::xpath protected function Performs an xpath search on the contents of the internal browser.
AssertHelperTrait::castSafeStrings protected static function Casts MarkupInterface objects into strings.
AssertLegacyTrait::assert protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertTrue() instead.
AssertLegacyTrait::assertEqual protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertEquals() instead.
AssertLegacyTrait::assertIdentical protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertSame() instead.
AssertLegacyTrait::assertIdenticalObject protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertEquals() instead.
AssertLegacyTrait::assertNotEqual protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertNotEquals() instead.
AssertLegacyTrait::assertNotIdentical protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertNotSame() instead.
AssertLegacyTrait::pass protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertTrue() instead.
AssertLegacyTrait::verbose protected function
ConfigTestTrait::configImporter protected function Returns a ConfigImporter object to import test configuration.
ConfigTestTrait::copyConfig protected function Copies configuration objects from source storage to target storage.
ContentTypeCreationTrait::createContentType protected function Creates a custom content type based on default settings. 1
DeprecatedServicePropertyTrait::__get public function Allows to access deprecated/removed properties.
EntityKernelTestBase::$deprecatedProperties protected property The list of deprecated services.
EntityKernelTestBase::$generatedIds protected property A list of generated identifiers.
EntityKernelTestBase::$state protected property The state service.
EntityKernelTestBase::createUser protected function Creates a user.
EntityKernelTestBase::generateRandomEntityId protected function Generates a random ID avoiding collisions.
EntityKernelTestBase::getHooksInfo protected function Returns the entity_test hook invocation info.
EntityKernelTestBase::installModule protected function Installs a module and refreshes services.
EntityKernelTestBase::refreshServices protected function Refresh services. 1
EntityKernelTestBase::reloadEntity protected function Reloads the given entity from the storage and returns it.
EntityKernelTestBase::uninstallModule protected function Uninstalls a module and refreshes services.
EntityReferenceRevisionsCompositeTranslationTest::$database protected property The current database connection.
EntityReferenceRevisionsCompositeTranslationTest::$entityTypeManager protected property The entity type manager. Overrides EntityKernelTestBase::$entityTypeManager
EntityReferenceRevisionsCompositeTranslationTest::$modules public static property Modules to enable. Overrides EntityKernelTestBase::$modules
EntityReferenceRevisionsCompositeTranslationTest::assertAffectedRevisionCount protected function Asserts the affected revision count of a certain entity.
EntityReferenceRevisionsCompositeTranslationTest::assertRevisionCount protected function Asserts the revision count of an entity.
EntityReferenceRevisionsCompositeTranslationTest::setUp protected function Overrides EntityKernelTestBase::setUp
EntityReferenceRevisionsCompositeTranslationTest::testCompositePendingRevisionTranslation public function Test the storage for handling pending revisions with translations.
EntityReferenceRevisionsCompositeTranslationTest::testCompositeTranslation public function Tests that composite translations affects the host entity's translations.
EntityReferenceRevisionsCompositeTranslationTest::testNestedCompositeTranslation public function Tests that nested composite translations affects the host translations.
KernelTestBase::$backupGlobals protected property Back up and restore any global variables that may be changed by tests.
KernelTestBase::$backupStaticAttributes protected property Back up and restore static class properties that may be changed by tests.
KernelTestBase::$backupStaticAttributesBlacklist protected property Contains a few static class properties for performance.
KernelTestBase::$classLoader protected property
KernelTestBase::$configImporter protected property @todo Move into Config test base class. 7
KernelTestBase::$configSchemaCheckerExclusions protected static property An array of config object names that are excluded from schema checking.
KernelTestBase::$container protected property
KernelTestBase::$databasePrefix protected property
KernelTestBase::$preserveGlobalState protected property Do not forward any global state from the parent process to the processes that run the actual tests.
KernelTestBase::$root protected property The app root.
KernelTestBase::$runTestInSeparateProcess protected property Kernel tests are run in separate processes because they allow autoloading of code from extensions. Running the test in a separate process isolates this behavior from other tests. Subclasses should not override this property.
KernelTestBase::$siteDirectory protected property
KernelTestBase::$strictConfigSchema protected property Set to TRUE to strict check all configuration saved. 6
KernelTestBase::$vfsRoot protected property The virtual filesystem root directory.
KernelTestBase::assertPostConditions protected function 1
KernelTestBase::bootEnvironment protected function Bootstraps a basic test environment.
KernelTestBase::bootKernel private function Bootstraps a kernel for a test.
KernelTestBase::config protected function Configuration accessor for tests. Returns non-overridden configuration.
KernelTestBase::disableModules protected function Disables modules for this test.
KernelTestBase::enableModules protected function Enables modules for this test.
KernelTestBase::getConfigSchemaExclusions protected function Gets the config schema exclusions for this test.
KernelTestBase::getDatabaseConnectionInfo protected function Returns the Database connection info to be used for this test. 1
KernelTestBase::getDatabasePrefix public function
KernelTestBase::getExtensionsForModules private function Returns Extension objects for $modules to enable.
KernelTestBase::getModulesToEnable private static function Returns the modules to enable for this test.
KernelTestBase::initFileCache protected function Initializes the FileCache component.
KernelTestBase::installConfig protected function Installs default configuration for a given list of modules.
KernelTestBase::installEntitySchema protected function Installs the storage schema for a specific entity type.
KernelTestBase::installSchema protected function Installs database tables from a module schema definition.
KernelTestBase::isTestInIsolation Deprecated protected function Returns whether the current test method is running in a separate process.
KernelTestBase::prepareTemplate protected function
KernelTestBase::register public function Registers test-specific services. Overrides ServiceProviderInterface::register 26
KernelTestBase::render protected function Renders a render array. 1
KernelTestBase::setInstallProfile protected function Sets the install profile and rebuilds the container to update it.
KernelTestBase::setSetting protected function Sets an in-memory Settings variable.
KernelTestBase::setUpBeforeClass public static function 1
KernelTestBase::setUpFilesystem protected function Sets up the filesystem, so things like the file directory. 2
KernelTestBase::stop protected function Stops test execution.
KernelTestBase::tearDown protected function 6
KernelTestBase::tearDownCloseDatabaseConnection public function @after
KernelTestBase::vfsDump protected function Dumps the current state of the virtual filesystem to STDOUT.
KernelTestBase::__sleep public function Prevents serializing any properties.
NodeCreationTrait::createNode protected function Creates a node based on default settings.
NodeCreationTrait::getNodeByTitle public function Get a node from the database based on its title.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
RandomGeneratorTrait::$randomGenerator protected property The random generator.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers. 1
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate public function Callback for random string validation.
StorageCopyTrait::replaceStorageContents protected static function Copy the configuration from one storage to another and remove stale items.
TestRequirementsTrait::checkModuleRequirements private function Checks missing module requirements.
TestRequirementsTrait::checkRequirements protected function Check module requirements for the Drupal use case. 1
TestRequirementsTrait::getDrupalRoot protected static function Returns the Drupal root directory.
UserCreationTrait::checkPermissions protected function Checks whether a given list of permission names is valid. Aliased as: drupalCheckPermissions
UserCreationTrait::createAdminRole protected function Creates an administrative role. Aliased as: drupalCreateAdminRole
UserCreationTrait::createRole protected function Creates a role with specified permissions. Aliased as: drupalCreateRole
UserCreationTrait::createUser protected function Create a user with a given set of permissions. Aliased as: drupalCreateUser
UserCreationTrait::grantPermissions protected function Grant permissions to a user role. Aliased as: drupalGrantPermissions
UserCreationTrait::setCurrentUser protected function Switch the current logged in user. Aliased as: drupalSetCurrentUser
UserCreationTrait::setUpCurrentUser protected function Creates a random user account and sets it as current user. Aliased as: drupalSetUpCurrentUser