From c5fe60394eaf1c3cc531f7a513766e6263ce71bd Mon Sep 17 00:00:00 2001 From: Mike Decker Date: Thu, 29 Aug 2024 15:20:45 -0700 Subject: [PATCH] Added unit test for field --- .../TaxonomyLabelHierarchyWidget.php | 10 ++ .../TaxonomyLabelHierarchyWidgetTest.php | 122 ++++++++++++++++++ .../Kernel/StanfordFieldKernelTestBase.php | 5 + 3 files changed, 137 insertions(+) create mode 100644 tests/src/Kernel/Plugin/Field/FieldWidget/TaxonomyLabelHierarchyWidgetTest.php diff --git a/src/Plugin/Field/FieldWidget/TaxonomyLabelHierarchyWidget.php b/src/Plugin/Field/FieldWidget/TaxonomyLabelHierarchyWidget.php index de3eabe..6e19c96 100644 --- a/src/Plugin/Field/FieldWidget/TaxonomyLabelHierarchyWidget.php +++ b/src/Plugin/Field/FieldWidget/TaxonomyLabelHierarchyWidget.php @@ -6,6 +6,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Field\Attribute\FieldWidget; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase; use Drupal\Core\Form\FormStateInterface; @@ -25,6 +26,14 @@ )] final class TaxonomyLabelHierarchyWidget extends OptionsWidgetBase { + /** + * {@inheritDoc} + */ + public static function isApplicable(FieldDefinitionInterface $field_definition) { + // Make sure the CSHS module is available. + return \Drupal::moduleHandler()->moduleExists('cshs'); + } + /** * {@inheritdoc} */ @@ -87,6 +96,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#value' => $this->t('Add More'), '#name' => $key, '#submit' => [[self::class, 'addOne']], + '#limit_validation_errors' => [], '#ajax' => [ 'callback' => [self::class, 'addMoreCallback'], 'wrapper' => $key, diff --git a/tests/src/Kernel/Plugin/Field/FieldWidget/TaxonomyLabelHierarchyWidgetTest.php b/tests/src/Kernel/Plugin/Field/FieldWidget/TaxonomyLabelHierarchyWidgetTest.php new file mode 100644 index 0000000..3465ca3 --- /dev/null +++ b/tests/src/Kernel/Plugin/Field/FieldWidget/TaxonomyLabelHierarchyWidgetTest.php @@ -0,0 +1,122 @@ + 'tag_terms', 'label' => 'terms'])->save(); + + $field_storage = FieldStorageConfig::create([ + 'field_name' => 'field_terms', + 'entity_type' => 'node', + 'type' => 'entity_reference', + 'cardinality' => -1, + 'settings' => ['target_type' => 'taxonomy_term'], + ]); + $field_storage->save(); + + $field = FieldConfig::create([ + 'field_name' => 'field_terms', + 'entity_type' => 'node', + 'bundle' => 'page', + 'settings' => [ + 'handler' => 'default:taxonomy_term', + 'handler_settings' => [ + 'target_bundles' => ['tag_terms' => 'tag_terms'], + ], + ], + ]); + $field->save(); + + /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $entity_form_display */ + $entity_form_display = EntityFormDisplay::create([ + 'targetEntityType' => 'node', + 'bundle' => 'page', + 'mode' => 'default', + 'status' => TRUE, + ]); + $entity_form_display->setComponent('field_terms', [ + 'type' => 'taxonomy_label_hierarchy', + ])->removeComponent('created')->save(); + } + + /** + * Test the entity form is displayed correctly. + */ + public function testWidgetForm() { + $parent_term = Term::create([ + 'vid' => 'tag_terms', + 'name' => 'Parent', + 'parent' => [0], + ]); + $parent_term->save(); + + $second_parent_term = Term::create([ + 'vid' => 'tag_terms', + 'name' => 'Second Parent', + 'parent' => [0], + ]); + $second_parent_term->save(); + + $foo = Term::create([ + 'vid' => 'tag_terms', + 'name' => 'Foo', + 'parent' => [$parent_term->id()], + ]); + $foo->save(); + + $bar = Term::create([ + 'vid' => 'tag_terms', + 'name' => 'Bar', + 'parent' => [$parent_term->id()], + ]); + $bar->save(); + + $baz = Term::create([ + 'vid' => 'tag_terms', + 'name' => 'Baz', + 'parent' => [$second_parent_term->id()], + ]); + $baz->save(); + + $author = User::create(['name' => 'foo']); + $author->save(); + $node = Node::create(['type' => 'page', 'title' => 'foobar', 'uid' => $author->id()]); + $node->save(); + + /** @var \Drupal\Core\Entity\EntityFormBuilderInterface $form_builder */ + $form_builder = $this->container->get('entity.form_builder'); + $form = $form_builder->getForm($node); + + $widget_value = $form['field_terms']['widget']; + + $this->assertArrayHasKey($foo->id(), $widget_value['parent'][0]['target_id']['#options']); + $this->assertArrayHasKey($bar->id(), $widget_value['parent'][0]['target_id']['#options']); + $this->assertArrayNotHasKey($baz->id(), $widget_value['parent'][0]['target_id']['#options']); + + $this->assertArrayNotHasKey($foo->id(), $widget_value['second_parent'][0]['target_id']['#options']); + $this->assertArrayNotHasKey($bar->id(), $widget_value['second_parent'][0]['target_id']['#options']); + $this->assertArrayHasKey($baz->id(), $widget_value['second_parent'][0]['target_id']['#options']); + } + +} diff --git a/tests/src/Kernel/StanfordFieldKernelTestBase.php b/tests/src/Kernel/StanfordFieldKernelTestBase.php index 82f3210..a8d6aa4 100644 --- a/tests/src/Kernel/StanfordFieldKernelTestBase.php +++ b/tests/src/Kernel/StanfordFieldKernelTestBase.php @@ -22,6 +22,9 @@ class StanfordFieldKernelTestBase extends KernelTestBase { 'stanford_fields', 'field', 'link', + 'taxonomy', + 'text', + 'cshs', ]; /** @@ -33,6 +36,8 @@ public function setup(): void { $this->installEntitySchema('node'); $this->installEntitySchema('field_config'); $this->installEntitySchema('date_format'); + $this->installEntitySchema('taxonomy_term'); + $this->installEntitySchema('path_alias'); $this->installConfig(['system', 'field', 'link']); $this->installSchema('node', ['node_access']);