You are here

protected function EntityFieldTest::doTestIterator in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php \Drupal\KernelTests\Core\Entity\EntityFieldTest::doTestIterator()

Executes the iterator tests for the given entity type.

Parameters

string $entity_type: The entity type to run the tests with.

1 call to EntityFieldTest::doTestIterator()
EntityFieldTest::testIterator in core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php
Tests iterating over properties.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php, line 521

Class

EntityFieldTest
Tests the Entity Field API.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function doTestIterator($entity_type) {
  $entity = $this
    ->createTestEntity($entity_type);
  foreach ($entity as $name => $field) {
    $this
      ->assertInstanceOf(FieldItemListInterface::class, $field);
    foreach ($field as $delta => $item) {
      $this
        ->assertInstanceOf(FieldItemInterface::class, $field[0]);
      foreach ($item as $value_name => $value_property) {
        $this
          ->assertInstanceOf(TypedDataInterface::class, $value_property);
        $value = $value_property
          ->getValue();
        $this
          ->assertTrue(!isset($value) || is_scalar($value) || $value instanceof EntityInterface, $entity_type . ": Value {$value_name} of item {$delta} of field {$name} is a primitive or an entity.");
      }
    }
  }
  $fields = $entity
    ->getFields();
  $this
    ->assertEquals(array_keys($entity
    ->getTypedData()
    ->getDataDefinition()
    ->getPropertyDefinitions()), array_keys($fields), new FormattableMarkup('%entity_type: All fields returned.', [
    '%entity_type' => $entity_type,
  ]));
  $this
    ->assertEquals(iterator_to_array($entity
    ->getIterator()), $fields, new FormattableMarkup('%entity_type: Entity iterator iterates over all fields.', [
    '%entity_type' => $entity_type,
  ]));
}