From fa5d618347a575155777c8b56f0ace0b5f6b9e0a Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 25 Oct 2019 18:31:22 -0700 Subject: [PATCH] FormDataModel - Define behavior for more edgy HTML documents --- ext/afform/core/Civi/Afform/AHQ.php | 8 ++-- ext/afform/core/Civi/Afform/FormDataModel.php | 11 ++++- .../phpunit/Civi/Afform/FormDataModelTest.php | 46 +++++++++++++------ 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/ext/afform/core/Civi/Afform/AHQ.php b/ext/afform/core/Civi/Afform/AHQ.php index 7eb8877fa1..711ad668e3 100644 --- a/ext/afform/core/Civi/Afform/AHQ.php +++ b/ext/afform/core/Civi/Afform/AHQ.php @@ -13,12 +13,13 @@ class AHQ { /** * Returns all tags with a certain tag name, e.g. 'af-entity' * - * @param array $element + * @param array|string $element + * The ArrayHtml representation of a document/fragment. * @param string $tagName * @return array */ public static function getTags($element, $tagName) { - if ($element === []) { + if ($element === [] || is_string($element)) { return []; } $results = []; @@ -35,7 +36,8 @@ class AHQ { * Returns all the real properties of a collection, * filtering out any array keys that start with a hashtag * - * @param array $element + * @param array|string $element + * The ArrayHtml representation of a document/fragment. * @return array */ public static function getProps($element) { diff --git a/ext/afform/core/Civi/Afform/FormDataModel.php b/ext/afform/core/Civi/Afform/FormDataModel.php index c607dd9b36..15e0a12b3f 100644 --- a/ext/afform/core/Civi/Afform/FormDataModel.php +++ b/ext/afform/core/Civi/Afform/FormDataModel.php @@ -27,6 +27,9 @@ class FormDataModel { */ public static function create($layout) { $entities = array_column(AHQ::getTags($layout, 'af-entity'), NULL, 'name'); + foreach (array_keys($entities) as $entity) { + $entities[$entity]['fields'] = []; + } self::parseFields($layout, $entities); $self = new static(); @@ -43,8 +46,14 @@ class FormDataModel { * Ex: $entities['spouse']['type'] = 'Contact'; */ protected static function parseFields($layout, &$entities) { + if (!isset($layout['#children'])) { + return; + } foreach ($layout['#children'] as $child) { - if ($child['#tag'] == 'af-fieldset' && !empty($child['#children'])) { + if (is_string($child)) { + //nothing + } + elseif ($child['#tag'] == 'af-fieldset' && !empty($child['#children'])) { $entities[$child['model']]['fields'] = array_merge($entities[$child['model']]['fields'] ?? [], AHQ::getTags($child, 'af-field')); } elseif (!empty($child['#children'])) { diff --git a/ext/afform/core/tests/phpunit/Civi/Afform/FormDataModelTest.php b/ext/afform/core/tests/phpunit/Civi/Afform/FormDataModelTest.php index df5687028b..52e32fab2c 100644 --- a/ext/afform/core/tests/phpunit/Civi/Afform/FormDataModelTest.php +++ b/ext/afform/core/tests/phpunit/Civi/Afform/FormDataModelTest.php @@ -23,23 +23,23 @@ class FormDataModelTest extends \PHPUnit\Framework\TestCase implements HeadlessI public function getEntityExamples() { $cases = []; - //$cases[] = [ - // 'html' => 'Hello world', - // 'entities' => [], - //]; - // - //$cases[] = [ - // 'html' => '
', - // 'entities' => [], - //]; - // - //$cases[] = [ - // 'html' => '
Hello world
', - // 'entities' => [], - //]; + $cases[] = [ + 'html' => 'Hello world', + 'entities' => [], + ]; + + $cases[] = [ + 'html' => '
', + 'entities' => [], + ]; $cases[] = [ - 'html' => '', + 'html' => '
Hello world
', + 'entities' => [], + ]; + + $cases[] = [ + 'html' => '

', 'entities' => [ 'foobar' => [ 'type' => 'Foo', @@ -52,6 +52,22 @@ class FormDataModelTest extends \PHPUnit\Framework\TestCase implements HeadlessI ], ]; + $cases[] = [ + 'html' => '
', + 'entities' => [ + 'foobar' => [ + 'type' => 'Foo', + 'name' => 'foobar', + 'fields' => [], + ], + 'whiz_bang' => [ + 'type' => 'Whiz', + 'name' => 'whiz_bang', + 'fields' => [], + ], + ], + ]; + return $cases; } -- 2.25.1