class EntityReferenceRevisionsCompositeTranslationTest in Entity Reference Revisions 8
Tests the entity_reference_revisions composite relationship.
@group entity_reference_revisions
Hierarchy
- class \Drupal\KernelTests\KernelTestBase extends \PHPUnit\Framework\TestCase implements ServiceProviderInterface uses AssertContentTrait, AssertLegacyTrait, AssertHelperTrait, ConfigTestTrait, PhpunitCompatibilityTrait, RandomGeneratorTrait, TestRequirementsTrait
- class \Drupal\KernelTests\Core\Entity\EntityKernelTestBase uses DeprecatedServicePropertyTrait, UserCreationTrait
- class \Drupal\Tests\entity_reference_revisions\Kernel\EntityReferenceRevisionsCompositeTranslationTest uses ContentTypeCreationTrait, NodeCreationTrait
- class \Drupal\KernelTests\Core\Entity\EntityKernelTestBase uses DeprecatedServicePropertyTrait, UserCreationTrait
Expanded class hierarchy of EntityReferenceRevisionsCompositeTranslationTest
File
- tests/
src/ Kernel/ EntityReferenceRevisionsCompositeTranslationTest.php, line 22
Namespace
Drupal\Tests\entity_reference_revisions\KernelView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AssertContentTrait:: |
protected | property | The current raw content. | |
AssertContentTrait:: |
protected | property | The drupalSettings value from the current raw $content. | |
AssertContentTrait:: |
protected | property | The XML structure parsed from the current raw $content. | 1 |
AssertContentTrait:: |
protected | property | The plain-text content of raw $content (text nodes). | |
AssertContentTrait:: |
protected | function | Passes if the raw text IS found escaped on the loaded page, fail otherwise. | |
AssertContentTrait:: |
protected | function | Asserts that a field exists with the given name or ID. | |
AssertContentTrait:: |
protected | function | Asserts that a field exists with the given ID and value. | |
AssertContentTrait:: |
protected | function | Asserts that a field exists with the given name and value. | |
AssertContentTrait:: |
protected | function | Asserts that a field exists in the current page by the given XPath. | |
AssertContentTrait:: |
protected | function | Asserts that a checkbox field in the current page is checked. | |
AssertContentTrait:: |
protected | function | Asserts that a field exists in the current page with a given Xpath result. | |
AssertContentTrait:: |
protected | function | Passes if a link with the specified label is found. | |
AssertContentTrait:: |
protected | function | Passes if a link containing a given href (part) is found. | |
AssertContentTrait:: |
protected | function | Asserts that each HTML ID is used for just a single element. | |
AssertContentTrait:: |
protected | function | Passes if the raw text IS NOT found escaped on the loaded page, fail otherwise. | |
AssertContentTrait:: |
protected | function | Asserts that a field does not exist with the given name or ID. | |
AssertContentTrait:: |
protected | function | Asserts that a field does not exist with the given ID and value. | |
AssertContentTrait:: |
protected | function | Asserts that a field does not exist with the given name and value. | |
AssertContentTrait:: |
protected | function | Asserts that a field does not exist or its value does not match, by XPath. | |
AssertContentTrait:: |
protected | function | Asserts that a checkbox field in the current page is not checked. | |
AssertContentTrait:: |
protected | function | Passes if a link with the specified label is not found. | |
AssertContentTrait:: |
protected | function | Passes if a link containing a given href (part) is not found. | |
AssertContentTrait:: |
protected | function | Passes if a link containing a given href is not found in the main region. | |
AssertContentTrait:: |
protected | function | Asserts that a select option in the current page does not exist. | |
AssertContentTrait:: |
protected | function | Asserts that a select option in the current page is not checked. | |
AssertContentTrait:: |
protected | function | Triggers a pass if the perl regex pattern is not found in raw content. | |
AssertContentTrait:: |
protected | function | Passes if the raw text is NOT found on the loaded page, fail otherwise. | |
AssertContentTrait:: |
protected | function | Passes if the page (with HTML stripped) does not contains the text. | |
AssertContentTrait:: |
protected | function | Pass if the page title is not the given string. | |
AssertContentTrait:: |
protected | function | Passes if the text is found MORE THAN ONCE on the text version of the page. | |
AssertContentTrait:: |
protected | function | Asserts that a select option in the current page exists. | |
AssertContentTrait:: |
protected | function | Asserts that a select option with the visible text exists. | |
AssertContentTrait:: |
protected | function | Asserts that a select option in the current page is checked. | |
AssertContentTrait:: |
protected | function | Asserts that a select option in the current page is checked. | |
AssertContentTrait:: |
protected | function | Asserts that a select option in the current page exists. | |
AssertContentTrait:: |
protected | function | Triggers a pass if the Perl regex pattern is found in the raw content. | |
AssertContentTrait:: |
protected | function | Passes if the raw text IS found on the loaded page, fail otherwise. | |
AssertContentTrait:: |
protected | function | Passes if the page (with HTML stripped) contains the text. | |
AssertContentTrait:: |
protected | function | Helper for assertText and assertNoText. | |
AssertContentTrait:: |
protected | function | Asserts that a Perl regex pattern is found in the plain-text content. | |
AssertContentTrait:: |
protected | function | Asserts themed output. | |
AssertContentTrait:: |
protected | function | Pass if the page title is the given string. | |
AssertContentTrait:: |
protected | function | Passes if the text is found ONLY ONCE on the text version of the page. | |
AssertContentTrait:: |
protected | function | Helper for assertUniqueText and assertNoUniqueText. | |
AssertContentTrait:: |
protected | function | Builds an XPath query. | |
AssertContentTrait:: |
protected | function | Helper: Constructs an XPath for the given set of attributes and value. | |
AssertContentTrait:: |
protected | function | Searches elements using a CSS selector in the raw content. | |
AssertContentTrait:: |
protected | function | Get all option elements, including nested options, in a select. | |
AssertContentTrait:: |
protected | function | Gets the value of drupalSettings for the currently-loaded page. | |
AssertContentTrait:: |
protected | function | Gets the current raw content. | |
AssertContentTrait:: |
protected | function | Get the selected value from a select field. | |
AssertContentTrait:: |
protected | function | Retrieves the plain-text content from the current raw content. | |
AssertContentTrait:: |
protected | function | Get the current URL from the cURL handler. | 1 |
AssertContentTrait:: |
protected | function | Parse content returned from curlExec using DOM and SimpleXML. | |
AssertContentTrait:: |
protected | function | Removes all white-space between HTML tags from the raw content. | |
AssertContentTrait:: |
protected | function | Sets the value of drupalSettings for the currently-loaded page. | |
AssertContentTrait:: |
protected | function | Sets the raw content (e.g. HTML). | |
AssertContentTrait:: |
protected | function | Performs an xpath search on the contents of the internal browser. | |
AssertHelperTrait:: |
protected static | function | Casts MarkupInterface objects into strings. | |
AssertLegacyTrait:: |
protected | function | Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertTrue() instead. | |
AssertLegacyTrait:: |
protected | function | Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertEquals() instead. | |
AssertLegacyTrait:: |
protected | function | Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertSame() instead. | |
AssertLegacyTrait:: |
protected | function | Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertEquals() instead. | |
AssertLegacyTrait:: |
protected | function | Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertNotEquals() instead. | |
AssertLegacyTrait:: |
protected | function | Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertNotSame() instead. | |
AssertLegacyTrait:: |
protected | function | Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertTrue() instead. | |
AssertLegacyTrait:: |
protected | function | ||
ConfigTestTrait:: |
protected | function | Returns a ConfigImporter object to import test configuration. | |
ConfigTestTrait:: |
protected | function | Copies configuration objects from source storage to target storage. | |
ContentTypeCreationTrait:: |
protected | function | Creates a custom content type based on default settings. | 1 |
DeprecatedServicePropertyTrait:: |
public | function | Allows to access deprecated/removed properties. | |
EntityKernelTestBase:: |
protected | property | The list of deprecated services. | |
EntityKernelTestBase:: |
protected | property | A list of generated identifiers. | |
EntityKernelTestBase:: |
protected | property | The state service. | |
EntityKernelTestBase:: |
protected | function | Creates a user. | |
EntityKernelTestBase:: |
protected | function | Generates a random ID avoiding collisions. | |
EntityKernelTestBase:: |
protected | function | Returns the entity_test hook invocation info. | |
EntityKernelTestBase:: |
protected | function | Installs a module and refreshes services. | |
EntityKernelTestBase:: |
protected | function | Refresh services. | 1 |
EntityKernelTestBase:: |
protected | function | Reloads the given entity from the storage and returns it. | |
EntityKernelTestBase:: |
protected | function | Uninstalls a module and refreshes services. | |
EntityReferenceRevisionsCompositeTranslationTest:: |
protected | property | The current database connection. | |
EntityReferenceRevisionsCompositeTranslationTest:: |
protected | property |
The entity type manager. Overrides EntityKernelTestBase:: |
|
EntityReferenceRevisionsCompositeTranslationTest:: |
public static | property |
Modules to enable. Overrides EntityKernelTestBase:: |
|
EntityReferenceRevisionsCompositeTranslationTest:: |
protected | function | Asserts the affected revision count of a certain entity. | |
EntityReferenceRevisionsCompositeTranslationTest:: |
protected | function | Asserts the revision count of an entity. | |
EntityReferenceRevisionsCompositeTranslationTest:: |
protected | function |
Overrides EntityKernelTestBase:: |
|
EntityReferenceRevisionsCompositeTranslationTest:: |
public | function | Test the storage for handling pending revisions with translations. | |
EntityReferenceRevisionsCompositeTranslationTest:: |
public | function | Tests that composite translations affects the host entity's translations. | |
EntityReferenceRevisionsCompositeTranslationTest:: |
public | function | Tests that nested composite translations affects the host translations. | |
KernelTestBase:: |
protected | property | Back up and restore any global variables that may be changed by tests. | |
KernelTestBase:: |
protected | property | Back up and restore static class properties that may be changed by tests. | |
KernelTestBase:: |
protected | property | Contains a few static class properties for performance. | |
KernelTestBase:: |
protected | property | ||
KernelTestBase:: |
protected | property | @todo Move into Config test base class. | 7 |
KernelTestBase:: |
protected static | property | An array of config object names that are excluded from schema checking. | |
KernelTestBase:: |
protected | property | ||
KernelTestBase:: |
protected | property | ||
KernelTestBase:: |
protected | property | Do not forward any global state from the parent process to the processes that run the actual tests. | |
KernelTestBase:: |
protected | property | The app root. | |
KernelTestBase:: |
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:: |
protected | property | ||
KernelTestBase:: |
protected | property | Set to TRUE to strict check all configuration saved. | 6 |
KernelTestBase:: |
protected | property | The virtual filesystem root directory. | |
KernelTestBase:: |
protected | function | 1 | |
KernelTestBase:: |
protected | function | Bootstraps a basic test environment. | |
KernelTestBase:: |
private | function | Bootstraps a kernel for a test. | |
KernelTestBase:: |
protected | function | Configuration accessor for tests. Returns non-overridden configuration. | |
KernelTestBase:: |
protected | function | Disables modules for this test. | |
KernelTestBase:: |
protected | function | Enables modules for this test. | |
KernelTestBase:: |
protected | function | Gets the config schema exclusions for this test. | |
KernelTestBase:: |
protected | function | Returns the Database connection info to be used for this test. | 1 |
KernelTestBase:: |
public | function | ||
KernelTestBase:: |
private | function | Returns Extension objects for $modules to enable. | |
KernelTestBase:: |
private static | function | Returns the modules to enable for this test. | |
KernelTestBase:: |
protected | function | Initializes the FileCache component. | |
KernelTestBase:: |
protected | function | Installs default configuration for a given list of modules. | |
KernelTestBase:: |
protected | function | Installs the storage schema for a specific entity type. | |
KernelTestBase:: |
protected | function | Installs database tables from a module schema definition. | |
KernelTestBase:: |
protected | function | Returns whether the current test method is running in a separate process. | |
KernelTestBase:: |
protected | function | ||
KernelTestBase:: |
public | function |
Registers test-specific services. Overrides ServiceProviderInterface:: |
26 |
KernelTestBase:: |
protected | function | Renders a render array. | 1 |
KernelTestBase:: |
protected | function | Sets the install profile and rebuilds the container to update it. | |
KernelTestBase:: |
protected | function | Sets an in-memory Settings variable. | |
KernelTestBase:: |
public static | function | 1 | |
KernelTestBase:: |
protected | function | Sets up the filesystem, so things like the file directory. | 2 |
KernelTestBase:: |
protected | function | Stops test execution. | |
KernelTestBase:: |
protected | function | 6 | |
KernelTestBase:: |
public | function | @after | |
KernelTestBase:: |
protected | function | Dumps the current state of the virtual filesystem to STDOUT. | |
KernelTestBase:: |
public | function | Prevents serializing any properties. | |
NodeCreationTrait:: |
protected | function | Creates a node based on default settings. | |
NodeCreationTrait:: |
public | function | Get a node from the database based on its title. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
RandomGeneratorTrait:: |
protected | property | The random generator. | |
RandomGeneratorTrait:: |
protected | function | Gets the random generator for the utility methods. | |
RandomGeneratorTrait:: |
protected | function | Generates a unique random string containing letters and numbers. | 1 |
RandomGeneratorTrait:: |
public | function | Generates a random PHP object. | |
RandomGeneratorTrait:: |
public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
RandomGeneratorTrait:: |
public | function | Callback for random string validation. | |
StorageCopyTrait:: |
protected static | function | Copy the configuration from one storage to another and remove stale items. | |
TestRequirementsTrait:: |
private | function | Checks missing module requirements. | |
TestRequirementsTrait:: |
protected | function | Check module requirements for the Drupal use case. | 1 |
TestRequirementsTrait:: |
protected static | function | Returns the Drupal root directory. | |
UserCreationTrait:: |
protected | function | Checks whether a given list of permission names is valid. Aliased as: drupalCheckPermissions | |
UserCreationTrait:: |
protected | function | Creates an administrative role. Aliased as: drupalCreateAdminRole | |
UserCreationTrait:: |
protected | function | Creates a role with specified permissions. Aliased as: drupalCreateRole | |
UserCreationTrait:: |
protected | function | Create a user with a given set of permissions. Aliased as: drupalCreateUser | |
UserCreationTrait:: |
protected | function | Grant permissions to a user role. Aliased as: drupalGrantPermissions | |
UserCreationTrait:: |
protected | function | Switch the current logged in user. Aliased as: drupalSetCurrentUser | |
UserCreationTrait:: |
protected | function | Creates a random user account and sets it as current user. Aliased as: drupalSetUpCurrentUser |