You are here

protected function EntityQueryTest::setUp in Drupal 9

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

Overrides EntityKernelTestBase::setUp

File

core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php, line 65

Class

EntityQueryTest
Tests Entity Query functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function setUp() : void {
  parent::setUp();
  $this
    ->installEntitySchema('entity_test_mulrev');
  $this
    ->installConfig([
    'language',
  ]);
  $figures = mb_strtolower($this
    ->randomMachineName());
  $greetings = mb_strtolower($this
    ->randomMachineName());
  foreach ([
    $figures => 'shape',
    $greetings => 'text',
  ] as $field_name => $field_type) {
    $field_storage = FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'entity_test_mulrev',
      'type' => $field_type,
      'cardinality' => 2,
    ]);
    $field_storage
      ->save();
    $field_storages[] = $field_storage;
  }
  $bundles = [];
  for ($i = 0; $i < 2; $i++) {

    // For the sake of tablesort, make sure the second bundle is higher than
    // the first one. Beware: MySQL is not case sensitive.
    do {
      $bundle = $this
        ->randomMachineName();
    } while ($bundles && strtolower($bundles[0]) >= strtolower($bundle));
    entity_test_create_bundle($bundle);
    foreach ($field_storages as $field_storage) {
      FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => $bundle,
      ])
        ->save();
    }
    $bundles[] = $bundle;
  }

  // Each unit is a list of field name, langcode and a column-value array.
  $units[] = [
    $figures,
    'en',
    [
      'color' => 'red',
      'shape' => 'triangle',
    ],
  ];
  $units[] = [
    $figures,
    'en',
    [
      'color' => 'blue',
      'shape' => 'circle',
    ],
  ];

  // To make it easier to test sorting, the greetings get formats according
  // to their langcode.
  $units[] = [
    $greetings,
    'tr',
    [
      'value' => 'merhaba',
      'format' => 'format-tr',
    ],
  ];
  $units[] = [
    $greetings,
    'pl',
    [
      'value' => 'siema',
      'format' => 'format-pl',
    ],
  ];

  // Make these languages available to the greetings field.
  ConfigurableLanguage::createFromLangcode('tr')
    ->save();
  ConfigurableLanguage::createFromLangcode('pl')
    ->save();

  // Calculate the cartesian product of the unit array by looking at the
  // bits of $i and add the unit at the bits that are 1. For example,
  // decimal 13 is binary 1101 so unit 3,2 and 0 will be added to the
  // entity.
  for ($i = 1; $i <= 15; $i++) {
    $entity = EntityTestMulRev::create([
      'type' => $bundles[$i & 1],
      'name' => $this
        ->randomMachineName(),
      'langcode' => 'en',
    ]);

    // Make sure the name is set for every language that we might create.
    foreach ([
      'tr',
      'pl',
    ] as $langcode) {
      $entity
        ->addTranslation($langcode)->name = $this
        ->randomMachineName();
    }
    foreach (array_reverse(str_split(decbin($i))) as $key => $bit) {
      if ($bit) {

        // @todo https://www.drupal.org/project/drupal/issues/3001920 Doing
        //   list($field_name, $langcode, $values) = $units[$key]; causes
        //   problems in PHP 7.3. Revert to better variable names once
        //   https://bugs.php.net/bug.php?id=76937 is fixed.
        $entity
          ->getTranslation($units[$key][1])->{$units[$key][0]}[] = $units[$key][2];
      }
    }
    $entity
      ->save();
  }
  $this->bundles = $bundles;
  $this->figures = $figures;
  $this->greetings = $greetings;
  $this->storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('entity_test_mulrev');
}