You are here

protected function EntityFieldTest::doTestIterator in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityFieldTest.php \Drupal\system\Tests\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/modules/system/src/Tests/Entity/EntityFieldTest.php
Tests iterating over properties.

File

core/modules/system/src/Tests/Entity/EntityFieldTest.php, line 501
Contains \Drupal\system\Tests\Entity\EntityFieldTest.

Class

EntityFieldTest
Tests the Entity Field API.

Namespace

Drupal\system\Tests\Entity

Code

protected function doTestIterator($entity_type) {
  $entity = $this
    ->createTestEntity($entity_type);
  foreach ($entity as $name => $field) {
    $this
      ->assertTrue($field instanceof FieldItemListInterface, $entity_type . ": Field {$name} implements interface.");
    foreach ($field as $delta => $item) {
      $this
        ->assertTrue($field[0] instanceof FieldItemInterface, $entity_type . ": Item {$delta} of field {$name} implements interface.");
      foreach ($item as $value_name => $value_property) {
        $this
          ->assertTrue($value_property instanceof TypedDataInterface, $entity_type . ": Value {$value_name} of item {$delta} of field {$name} implements interface.");
        $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
    ->assertEqual(array_keys($fields), array_keys($entity
    ->getTypedData()
    ->getDataDefinition()
    ->getPropertyDefinitions()), format_string('%entity_type: All fields returned.', array(
    '%entity_type' => $entity_type,
  )));
  $this
    ->assertEqual($fields, iterator_to_array($entity
    ->getIterator()), format_string('%entity_type: Entity iterator iterates over all fields.', array(
    '%entity_type' => $entity_type,
  )));
}