function EntityQueryTest::setUp

Sets up a Drupal site for running functional and integration tests.

Generates a random database prefix and installs Drupal with the specified installation profile in Drupal\simpletest\WebTestBase::$profile into the prefixed database. Afterwards, installs any additional modules specified by the test.

After installation all caches are flushed and several configuration values are reset to the values of the parent site executing the test, since the default values may be incompatible with the environment in which tests are being executed.

Parameters

...: List of modules to enable for the duration of the test. This can be either a single array or a variable number of string arguments.

Overrides WebTestBase::setUp

See also

Drupal\simpletest\WebTestBase::prepareDatabasePrefix()

Drupal\simpletest\WebTestBase::changeDatabasePrefix()

Drupal\simpletest\WebTestBase::prepareEnvironment()

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();
  $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,
    );
    $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));
    field_test_create_bundle($bundle);
    foreach ($fields as $field) {
      $instance = array(
        'field_name' => $field['field_name'],
        'entity_type' => 'test_entity',
        '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,
    LANGUAGE_NOT_SPECIFIED,
    array(
      'color' => 'red',
      'shape' => 'triangle',
    ),
  );
  $units[] = array(
    $figures,
    LANGUAGE_NOT_SPECIFIED,
    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.
  $field_langcodes =& drupal_static('field_available_languages');
  $field_langcodes['test_entity'][$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('test_entity', array(
      'ftid' => $i,
      'ftvid' => $i,
      'fttype' => $bundles[$i & 1],
    ));
    $entity
      ->enforceIsNew();
    $entity
      ->setNewRevision();
    foreach (array_reverse(str_split(decbin($i))) as $key => $bit) {
      if ($bit) {
        $unit = $units[$key];
        $entity->{$unit[0]}[$unit[1]][] = $unit[2];
      }
    }
    $entity
      ->save();
  }
  $this->figures = $figures;
  $this->greetings = $greetings;
  $this->factory = drupal_container()
    ->get('entity.query');
}