You are here

protected function LinkCheckerLinkAccessTest::createEntity in Link checker 8

Helper function for entity creation.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entityTypeDefinition: The entity type.

string $bundleId: Bundle ID.

Return value

\Drupal\Core\Entity\FieldableEntityInterface The entity.

1 call to LinkCheckerLinkAccessTest::createEntity()
LinkCheckerLinkAccessTest::testLinkAccess in tests/src/Kernel/LinkCheckerLinkAccessTest.php
Runs basic tests for link access.

File

tests/src/Kernel/LinkCheckerLinkAccessTest.php, line 278

Class

LinkCheckerLinkAccessTest
Tests basic linkchecker link access functionality.

Namespace

Drupal\Tests\linkchecker\Kernel

Code

protected function createEntity(EntityTypeInterface $entityTypeDefinition, $bundleId) {
  $entityData = [];
  $entityData[$entityTypeDefinition
    ->getKey('bundle')] = $bundleId;
  if ($entityTypeDefinition
    ->hasKey('published')) {
    $entityData[$entityTypeDefinition
      ->getKey('published')] = 1;
  }

  /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
  $entity = $this->entityTypeManager
    ->getStorage($entityTypeDefinition
    ->id())
    ->create($entityData);
  foreach ($entity
    ->getFieldDefinitions() as $fieldDefinition) {

    // Skip read-only fields.
    if ($fieldDefinition
      ->isReadOnly()) {
      continue;
    }

    // Skip non-required fields.
    if (!$fieldDefinition
      ->isRequired()) {
      continue;
    }

    // Skip non-empty fields.
    if (!$entity
      ->get($fieldDefinition
      ->getName())
      ->isEmpty()) {
      continue;
    }
    $field = $entity
      ->get($fieldDefinition
      ->getName());
    switch ($fieldDefinition
      ->getType()) {
      case 'integer':
        $field
          ->setValue([
          'value' => 1,
        ]);
        break;
      default:
        $field
          ->setValue([
          'value' => $this
            ->randomString(),
        ]);
        break;
    }
  }
  $entity
    ->save();
  return $entity;
}