protected function EntityQueryRelationshipTest::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/EntityQueryRelationshipTest.php, line 72
Definition of Drupal\Core\Entity\Tests\EntityQueryRelationshipTest.

Class

EntityQueryRelationshipTest
Tests Entity Query API relationship functionality.

Namespace

Drupal\system\Tests\Entity

Code

protected function setUp() {
  parent::setUp();

  // We want a taxonomy term reference field. It needs a vocabulary, terms,
  // a field and an instance. First, create the vocabulary.
  $vocabulary = entity_create('taxonomy_vocabulary', array(
    'machine_name' => strtolower($this
      ->randomName()),
  ));
  $vocabulary
    ->save();

  // Second, create the field.
  $this->fieldName = strtolower($this
    ->randomName());
  $field = array(
    'field_name' => $this->fieldName,
    'type' => 'taxonomy_term_reference',
  );
  $field['settings']['allowed_values']['vocabulary'] = $vocabulary->machine_name;
  field_create_field($field);

  // Third, create the instance.
  $instance = array(
    'entity_type' => 'entity_test',
    'field_name' => $this->fieldName,
    'bundle' => 'entity_test',
  );
  field_create_instance($instance);

  // Create two terms and also two accounts.
  for ($i = 0; $i <= 1; $i++) {
    $term = entity_create('taxonomy_term', array(
      'name' => $this
        ->randomName(),
      'vid' => $vocabulary->vid,
    ));
    $term
      ->save();
    $this->terms[] = $term;
    $this->accounts[] = $this
      ->drupalCreateUser();
  }

  // Create three entity_test entities, the 0th entity will point to the
  // 0th account and 0th term, the 1st and 2nd entity will point to the
  // 1st account and 1st term.
  for ($i = 0; $i <= 2; $i++) {
    $entity = entity_create('entity_test', array());
    $entity->name->value = $this
      ->randomName();
    $index = $i ? 1 : 0;
    $entity->user_id->value = $this->accounts[$index]->uid;
    $entity->{$this->fieldName}->tid = $this->terms[$index]->tid;
    $entity
      ->save();
    $this->entities[] = $entity;
  }
  $this->factory = drupal_container()
    ->get('entity.query');
}