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

Class

EntityQueryRelationshipTest
Tests Entity Query API relationship functionality.

Namespace

Drupal\system\Tests\Entity

Code

public function setUp() {
  parent::setUp();
  $this
    ->installSchema('taxonomy', array(
    'taxonomy_term_data',
    'taxonomy_term_hierarchy',
  ));

  // 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(
    'vid' => drupal_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
    ->id();
  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
        ->id(),
    ));
    $term
      ->save();
    $this->terms[] = $term;
    $this->accounts[] = $this
      ->createUser();
  }

  // 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->target_id = $this->accounts[$index]->uid;
    $entity->{$this->fieldName}->tid = $this->terms[$index]
      ->id();
    $entity
      ->save();
    $this->entities[] = $entity;
  }
  $this->factory = \Drupal::service('entity.query');
}