You are here

protected function EntityLanguageTestBase::setUp in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/EntityLanguageTestBase.php \Drupal\KernelTests\Core\Entity\EntityLanguageTestBase::setUp()
  2. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityLanguageTestBase.php \Drupal\KernelTests\Core\Entity\EntityLanguageTestBase::setUp()

File

core/tests/Drupal/KernelTests/Core/Entity/EntityLanguageTestBase.php, line 44

Class

EntityLanguageTestBase
Base class for language-aware entity tests.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function setUp() : void {
  parent::setUp();
  $this->languageManager = $this->container
    ->get('language_manager');
  foreach (entity_test_entity_types() as $entity_type_id) {

    // The entity_test schema is installed by the parent.
    if ($entity_type_id != 'entity_test') {
      $this
        ->installEntitySchema($entity_type_id);
    }
  }
  $this
    ->installConfig([
    'language',
  ]);

  // Create the test field.
  $this->container
    ->get('module_handler')
    ->loadInclude('entity_test', 'install');
  entity_test_install();

  // Enable translations for the test entity type.
  $this->state
    ->set('entity_test.translation', TRUE);

  // Create a translatable test field.
  $this->fieldName = mb_strtolower($this
    ->randomMachineName() . '_field_name');

  // Create an untranslatable test field.
  $this->untranslatableFieldName = mb_strtolower($this
    ->randomMachineName() . '_field_name');

  // Create field fields in all entity variations.
  foreach (entity_test_entity_types() as $entity_type) {
    FieldStorageConfig::create([
      'field_name' => $this->fieldName,
      'entity_type' => $entity_type,
      'type' => 'text',
      'cardinality' => 4,
    ])
      ->save();
    FieldConfig::create([
      'field_name' => $this->fieldName,
      'entity_type' => $entity_type,
      'bundle' => $entity_type,
      'translatable' => TRUE,
    ])
      ->save();
    FieldStorageConfig::create([
      'field_name' => $this->untranslatableFieldName,
      'entity_type' => $entity_type,
      'type' => 'text',
      'cardinality' => 4,
    ])
      ->save();
    FieldConfig::create([
      'field_name' => $this->untranslatableFieldName,
      'entity_type' => $entity_type,
      'bundle' => $entity_type,
      'translatable' => FALSE,
    ])
      ->save();
  }

  // Create the default languages.
  $this
    ->installConfig([
    'language',
  ]);

  // Create test languages.
  $this->langcodes = [];
  for ($i = 0; $i < 3; ++$i) {
    $language = ConfigurableLanguage::create([
      'id' => 'l' . $i,
      'label' => $this
        ->randomString(),
      'weight' => $i,
    ]);
    $this->langcodes[$i] = $language
      ->getId();
    $language
      ->save();
  }
}