DevelEntityToArrayTest.php
Namespace
Drupal\Tests\devel\Kernel
File
-
tests/src/Kernel/DevelEntityToArrayTest.php
View source
<?php
namespace Drupal\Tests\devel\Kernel;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\Tests\field\Traits\EntityReferenceFieldCreationTrait;
use PHPUnit\Framework\Attributes\Group;
class DevelEntityToArrayTest extends EntityKernelTestBase {
use EntityReferenceFieldCreationTrait;
protected $entityType = 'entity_test';
protected $referencedEntityType = 'entity_test_rev';
protected $bundle = 'entity_test';
protected $fieldName = 'field_test';
protected static $modules = [
'entity_test',
'devel',
];
protected function setUp() : void {
parent::setUp();
$this->installEntitySchema('entity_test_rev');
$user = $this->createUser(permissions: [
'access devel information',
], values: [
'name' => 'test',
]);
$current_user = $this->container
->get('current_user');
$current_user->setAccount($user);
$this->createEntityReferenceField($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->referencedEntityType, 'default', [
'target_bundles' => [
$this->bundle,
],
], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
}
public function testWithReferences() : void {
$entity = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'type' => $this->bundle,
]);
$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();
}
$entity->{$this->fieldName}
->setValue($reference_field);
$entity->save();
$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.');
}
}
}
Classes