function EntityQueryTest::setUp

Sets up Drupal unit test environment.

Overrides EntityUnitTestBase::setUp

See also

DrupalUnitTestBase::$modules

DrupalUnitTestBase

File

drupal/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryTest.php, line 56
Definition of Drupal\system\Tests\Entity\EntityQueryTest.

Class

EntityQueryTest
Tests the basic Entity API.

Namespace

Drupal\system\Tests\Entity

Code

function setUp() {
  parent::setUp();
  $this
    ->installSchema('entity_test', array(
    'entity_test_mulrev',
    'entity_test_mulrev_property_data',
    'entity_test_mulrev_property_revision',
  ));
  $this
    ->installSchema('language', array(
    'language',
  ));
  $this
    ->installSchema('system', array(
    'variable',
  ));
  $figures = drupal_strtolower($this
    ->randomName());
  $greetings = drupal_strtolower($this
    ->randomName());
  foreach (array(
    $figures => 'shape',
    $greetings => 'text',
  ) as $field_name => $field_type) {
    $field = array(
      'field_name' => $field_name,
      'type' => $field_type,
      'cardinality' => 2,
      'translatable' => TRUE,
    );
    $fields[] = field_create_field($field);
  }
  $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
        ->randomName();
    } while ($bundles && strtolower($bundles[0]) >= strtolower($bundle));
    entity_test_create_bundle($bundle);
    foreach ($fields as $field) {
      $instance = array(
        'field_name' => $field['field_name'],
        'entity_type' => 'entity_test_mulrev',
        'bundle' => $bundle,
      );
      field_create_instance($instance);
    }
    $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.
  $langcode = new Language(array(
    'langcode' => 'en',
    'name' => $this
      ->randomString(),
  ));
  language_save($langcode);
  $langcode = new Language(array(
    'langcode' => 'tr',
    'name' => $this
      ->randomString(),
  ));
  language_save($langcode);
  $langcode = new Language(array(
    'langcode' => 'pl',
    'name' => $this
      ->randomString(),
  ));
  language_save($langcode);
  $field_langcodes =& drupal_static('field_available_languages');
  $field_langcodes['entity_test_mulrev'][$greetings] = array(
    'tr',
    'pl',
  );

  // 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
        ->randomName(),
      'langcode' => 'en',
    ));

    // Make sure the name is set for every language that we might create.
    foreach (array(
      'tr',
      'pl',
    ) as $langcode) {
      $entity
        ->getTranslation($langcode)->name = $this
        ->randomName();
    }
    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->figures = $figures;
  $this->greetings = $greetings;
  $this->factory = \Drupal::service('entity.query');
}