Skip to content

Commit

Permalink
Added unit test for field
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Aug 29, 2024
1 parent c3583da commit c5fe603
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Plugin/Field/FieldWidget/TaxonomyLabelHierarchyWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}
*/
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace Drupal\Tests\stanford_fields\Kernel\Plugin\Field\FieldWidget;

use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\stanford_fields\Kernel\StanfordFieldKernelTestBase;
use Drupal\user\Entity\User;
/**
* Class TaxonomyLabelHierarchyWidgetTest.
*
* @group stanford_fields
* @coversDefaultClass \Drupal\stanford_fields\Plugin\Field\FieldWidget\TaxonomyLabelHierarchyWidget
*/
class TaxonomyLabelHierarchyWidgetTest extends StanfordFieldKernelTestBase {

/**
* {@inheritDoc}
*/
public function setup(): void {
parent::setUp();

Vocabulary::create(['vid' => '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']);
}

}
5 changes: 5 additions & 0 deletions tests/src/Kernel/StanfordFieldKernelTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class StanfordFieldKernelTestBase extends KernelTestBase {
'stanford_fields',
'field',
'link',
'taxonomy',
'text',
'cshs',
];

/**
Expand All @@ -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']);

Expand Down

0 comments on commit c5fe603

Please sign in to comment.