You are here

public function ReferenceWidgetTest::testFormElement in Select (or other) 4.x

Same name and namespace in other branches
  1. 8.3 Tests/src/Unit/ReferenceWidgetTest.php \Drupal\Tests\select_or_other\Unit\ReferenceWidgetTest::testFormElement()
  2. 8 tests/src/Unit/ReferenceWidgetTest.php \Drupal\Tests\select_or_other\Unit\ReferenceWidgetTest::testFormElement()

Test if formElement() adds the expected information.

File

tests/src/Unit/ReferenceWidgetTest.php, line 131

Class

ReferenceWidgetTest
Tests the form element implementation.

Namespace

Drupal\Tests\select_or_other\Unit

Code

public function testFormElement() {
  foreach ([
    'node',
    'taxonomy_term',
  ] as $target_type) {

    /** @var ReferenceWidget $mock */
    $mock = $this
      ->prepareFormElementMock($target_type);

    /** @var WidgetBase $parent */
    $parent = $this
      ->prepareFormElementMock($target_type, 'Drupal\\select_or_other\\Plugin\\Field\\FieldWidget\\WidgetBase');
    $entity = $this
      ->getMockForAbstractClass('Drupal\\Core\\Entity\\FieldableEntityInterface');
    $items = $this
      ->getMockForAbstractClass('Drupal\\Core\\Field\\FieldItemListInterface');
    $items
      ->method('getEntity')
      ->willReturn($entity);

    /** @var FieldItemListInterface $items */
    $delta = 1;
    $element = [];
    $form = [];
    $form_state = new FormState();
    $parent_result = $parent
      ->formElement($items, $delta, $element, $form, $form_state);
    $result = $mock
      ->formElement($items, $delta, $element, $form, $form_state);
    $added = array_diff_key($result, $parent_result);
    $expected = [
      '#target_type' => $target_type,
      '#selection_handler' => 'some_handler',
      '#selection_settings' => [],
      '#autocreate' => [
        'bundle' => 'autoCreateBundle',
        'uid' => 1,
      ],
      '#validate_reference' => TRUE,
      '#tags' => $target_type === 'taxonomy_term',
      '#merged_values' => TRUE,
      '#element_validate' => [
        [
          get_class($mock),
          'validateReferenceWidget',
        ],
      ],
    ];
    $this
      ->assertArrayEquals($expected, $added);
  }
}