function EntityQueryRelationshipTest::setUp

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php \Drupal\KernelTests\Core\Entity\EntityQueryRelationshipTest::setUp()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php \Drupal\KernelTests\Core\Entity\EntityQueryRelationshipTest::setUp()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php \Drupal\KernelTests\Core\Entity\EntityQueryRelationshipTest::setUp()

Overrides EntityKernelTestBase::setUp

File

core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php, line 65

Class

EntityQueryRelationshipTest
Tests the Entity Query relationship API.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function setUp() : void {
    parent::setUp();
    $this->installEntitySchema('taxonomy_term');
    // We want an entity reference field. It needs a vocabulary, terms, a field
    // storage and a field. First, create the vocabulary.
    $vocabulary = Vocabulary::create([
        'vid' => $this->randomMachineName(),
        'name' => 'Tags',
    ]);
    $vocabulary->save();
    // Second, create the field.
    entity_test_create_bundle('test_bundle');
    $this->fieldName = $this->randomMachineName();
    $handler_settings = [
        'target_bundles' => [
            $vocabulary->id() => $vocabulary->id(),
        ],
        'auto_create' => TRUE,
    ];
    $this->createEntityReferenceField('entity_test', 'test_bundle', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings);
    // Create two terms and also two accounts.
    for ($i = 0; $i <= 1; $i++) {
        $term = Term::create([
            'name' => $this->randomMachineName(),
            '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 = EntityTest::create([
            'type' => 'test_bundle',
        ]);
        $entity->name->value = $this->randomMachineName();
        $index = $i ? 1 : 0;
        $entity->user_id->target_id = $this->accounts[$index]
            ->id();
        $entity->{$this->fieldName}->target_id = $this->terms[$index]
            ->id();
        $entity->save();
        $this->entities[] = $entity;
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.