You are here

protected function EntityReferenceRevisionsCompositeTranslationTest::setUp in Entity Reference Revisions 8

Overrides EntityKernelTestBase::setUp

File

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

Class

EntityReferenceRevisionsCompositeTranslationTest
Tests the entity_reference_revisions composite relationship.

Namespace

Drupal\Tests\entity_reference_revisions\Kernel

Code

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();
}