From 32157c7c5d3c673343d3165a0808ac48779006cd Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Sat, 3 Feb 2018 18:47:04 +0000 Subject: [PATCH] Issue #2941457 by agentrickard, tim.plunkett, hbensalem: EntityAdapter::getIterator requires undocumented methods --- .../Entity/Plugin/DataType/EntityAdapter.php | 2 +- .../TypedData/EntityAdapterUnitTest.php | 28 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityAdapter.php b/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityAdapter.php index db194b548526..b0b94b8dbb93 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityAdapter.php +++ b/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityAdapter.php @@ -167,7 +167,7 @@ public function applyDefaultValue($notify = TRUE) { * {@inheritdoc} */ public function getIterator() { - return isset($this->entity) ? $this->entity->getIterator() : new \ArrayIterator([]); + return $this->entity instanceof \IteratorAggregate ? $this->entity->getIterator() : new \ArrayIterator([]); } } diff --git a/core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php index 8d0f25291b03..8e909d24bd4f 100644 --- a/core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php @@ -29,19 +29,33 @@ class EntityAdapterUnitTest extends UnitTestCase { protected $bundle; /** - * The entity used for testing. + * The content entity used for testing. * * @var \Drupal\Core\Entity\ContentEntityBase|\PHPUnit_Framework_MockObject_MockObject */ protected $entity; /** - * The entity adapter under test. + * The config entity used for testing. + * + * @var \Drupal\Core\Entity\ConfigtEntityBase|\PHPUnit_Framework_MockObject_MockObject + */ + protected $configEntity; + + /** + * The content entity adapter under test. * * @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter */ protected $entityAdapter; + /** + * The config entity adapter under test. + * + * @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter + */ + protected $configEntityAdapter; + /** * The entity type used for testing. * @@ -228,6 +242,10 @@ protected function setUp() { $this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', [$values, $this->entityTypeId, $this->bundle]); $this->entityAdapter = EntityAdapter::createFromEntity($this->entity); + + $this->configEntity = $this->getMockForAbstractClass('\Drupal\Core\Config\Entity\ConfigEntityBase', [$values, $this->entityTypeId, $this->bundle]); + + $this->configEntityAdapter = EntityAdapter::createFromEntity($this->configEntity); } /** @@ -428,6 +446,7 @@ public function testApplyDefaultValue() { * @covers ::getIterator */ public function testGetIterator() { + // Content entity test. $iterator = $this->entityAdapter->getIterator(); $fields = iterator_to_array($iterator); $this->assertArrayHasKey('id', $fields); @@ -436,6 +455,11 @@ public function testGetIterator() { $this->entityAdapter->setValue(NULL); $this->assertEquals(new \ArrayIterator([]), $this->entityAdapter->getIterator()); + + // Config entity test. + $iterator = $this->configEntityAdapter->getIterator(); + $this->configEntityAdapter->setValue(NULL); + $this->assertEquals(new \ArrayIterator([]), $this->entityAdapter->getIterator()); } } -- GitLab