function DevelEntityToArrayTest::testWithReferences

Test method.

File

tests/src/Kernel/DevelEntityToArrayTest.php, line 83

Class

DevelEntityToArrayTest
Test Load with References.

Namespace

Drupal\Tests\devel\Kernel

Code

public function testWithReferences() : void {
    // Create the parent entity.
    $entity = $this->container
        ->get('entity_type.manager')
        ->getStorage($this->entityType)
        ->create([
        'type' => $this->bundle,
    ]);
    // Create three target entities and attach them to parent field.
    $target_entities = [];
    $reference_field = [];
    for ($i = 0; $i < 3; ++$i) {
        $target_entity = $this->container
            ->get('entity_type.manager')
            ->getStorage($this->referencedEntityType)
            ->create([
            'type' => $this->bundle,
            'name' => 'Related ' . $i,
        ]);
        $target_entity->save();
        $target_entities[] = $target_entity;
        $reference_field[]['target_id'] = $target_entity->id();
    }
    // Set the field value.
    $entity->{$this->fieldName}
        ->setValue($reference_field);
    $entity->save();
    
    /** @var \Drupal\devel\DevelDumperManagerInterface $dumper */
    $dumper = $this->container
        ->get('devel.dumper');
    $result = $dumper->export($entity, NULL, 'drupal_variable', TRUE);
    for ($i = 0; $i < 3; ++$i) {
        $this->assertStringContainsString('Related ' . $i, (string) $result, 'The referenced entities are present in the dumper output.');
    }
}