You are here

public function SelectElementTest::testAddEmptyOption in Select (or other) 8

Same name and namespace in other branches
  1. 4.x tests/src/Unit/SelectElementTest.php \Drupal\Tests\select_or_other\Unit\SelectElementTest::testAddEmptyOption()

Make sure the empty option gets added when necessary.

File

tests/src/Unit/SelectElementTest.php, line 102

Class

SelectElementTest
Tests the form element implementation.

Namespace

Drupal\Tests\select_or_other\Unit

Code

public function testAddEmptyOption() {
  $element = [
    '#required' => TRUE,
    '#default_value' => 'not empty',
  ];
  $empty_option = [
    'select' => [
      '#empty_value' => '',
    ],
  ];
  $arguments = [
    &$element,
  ];
  $add_empty_option = new ReflectionMethod('Drupal\\select_or_other\\Element\\Select', 'addEmptyOption');
  $add_empty_option
    ->setAccessible(TRUE);
  $expected = $element;
  $add_empty_option
    ->invokeArgs(NULL, $arguments);
  $this
    ->assertArrayEquals($expected, $element, 'No empty option is added for required select widgets with a default value.');
  $element['#default_value'] = '';
  $expected = $element + $empty_option;
  $add_empty_option
    ->invokeArgs(NULL, $arguments);
  $this
    ->assertArrayEquals($expected, $element, 'Empty option is added for required select widgets without a default value.');
  $element['#default_value'] = '';
  $element['#required'] = FALSE;
  $expected = $element;
  $add_empty_option
    ->invokeArgs(NULL, $arguments);
  $this
    ->assertArrayEquals($expected, $element, 'No empty option is added for non-required select widgets without a default value.');
  $element['#default_value'] = 'not empty';
  $expected = $element + $empty_option;
  $add_empty_option
    ->invokeArgs(NULL, $arguments);
  $this
    ->assertArrayEquals($expected, $element, 'Empty option is added for non-required select widgets with a default value.');
  $expected['#no_empty_option'] = $element['#no_empty_option'] = FALSE;
  $add_empty_option
    ->invokeArgs(NULL, $arguments);
  $this
    ->assertArrayEquals($expected, $element);
  $element['#no_empty_option'] = TRUE;
  $expected = $element;
  $add_empty_option
    ->invokeArgs(NULL, $arguments);
  $this
    ->assertArrayEquals($expected, $element);
}