You are here

protected function EntityQueryTest::setUp in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityQueryTest.php \Drupal\system\Tests\Entity\EntityQueryTest::setUp()

Performs setup tasks before each individual test method is run.

Overrides EntityUnitTestBase::setUp

File

core/modules/system/src/Tests/Entity/EntityQueryTest.php, line 64
Contains \Drupal\system\Tests\Entity\EntityQueryTest.

Class

EntityQueryTest
Tests Entity Query functionality.

Namespace

Drupal\system\Tests\Entity

Code

protected function setUp() {
  parent::setUp();
  $this
    ->installEntitySchema('entity_test_mulrev');
  $this
    ->installConfig(array(
    'language',
  ));
  $figures = Unicode::strtolower($this
    ->randomMachineName());
  $greetings = Unicode::strtolower($this
    ->randomMachineName());
  foreach (array(
    $figures => 'shape',
    $greetings => 'text',
  ) as $field_name => $field_type) {
    $field_storage = entity_create('field_storage_config', array(
      'field_name' => $field_name,
      'entity_type' => 'entity_test_mulrev',
      'type' => $field_type,
      'cardinality' => 2,
    ));
    $field_storage
      ->save();
    $field_storages[] = $field_storage;
  }
  $bundles = array();
  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) {
      entity_create('field_config', array(
        'field_storage' => $field_storage,
        'bundle' => $bundle,
      ))
        ->save();
    }
    $bundles[] = $bundle;
  }

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

  // To make it easier to test sorting, the greetings get formats according
  // to their langcode.
  $units[] = array(
    $greetings,
    'tr',
    array(
      'value' => 'merhaba',
      'format' => 'format-tr',
    ),
  );
  $units[] = array(
    $greetings,
    'pl',
    array(
      '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 = entity_create('entity_test_mulrev', array(
      'type' => $bundles[$i & 1],
      'name' => $this
        ->randomMachineName(),
      'langcode' => 'en',
    ));

    // Make sure the name is set for every language that we might create.
    foreach (array(
      'tr',
      'pl',
    ) as $langcode) {
      $entity
        ->addTranslation($langcode)->name = $this
        ->randomMachineName();
    }
    foreach (array_reverse(str_split(decbin($i))) as $key => $bit) {
      if ($bit) {
        list($field_name, $langcode, $values) = $units[$key];
        $entity
          ->getTranslation($langcode)->{$field_name}[] = $values;
      }
    }
    $entity
      ->save();
  }
  $this->bundles = $bundles;
  $this->figures = $figures;
  $this->greetings = $greetings;
  $this->factory = \Drupal::service('entity.query');
}