You are here

public function ButtonsElementTest::testAddEmptyOption in Select (or other) 4.x

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

Make sure the empty option gets added when necessary.

File

tests/src/Unit/ButtonsElementTest.php, line 141

Class

ButtonsElementTest
Tests the form element implementation.

Namespace

Drupal\Tests\select_or_other\Unit

Code

public function testAddEmptyOption() {
  $element = [
    '#multiple' => TRUE,
    '#required' => TRUE,
    'select' => [
      '#options' => [],
      '#value' => [],
    ],
  ];
  $arguments = [
    &$element,
  ];
  $add_empty_option = new ReflectionMethod('Drupal\\select_or_other\\Element\\Buttons', 'addEmptyOption');
  $add_empty_option
    ->setAccessible(TRUE);
  $expected = $element;
  $add_empty_option
    ->invokeArgs(NULL, $arguments);
  $this
    ->assertArrayEquals($expected, $element);
  $expected['#multiple'] = $element['#multiple'] = FALSE;
  $add_empty_option
    ->invokeArgs(NULL, $arguments);
  $this
    ->assertArrayEquals($expected, $element);
  $expected['#required'] = $element['#required'] = FALSE;
  $add_empty_option
    ->invokeArgs(NULL, $arguments);
  $this
    ->assertArrayEquals($expected, $element);
  $expected['#default_value'] = $element['#default_value'] = [];
  $add_empty_option
    ->invokeArgs(NULL, $arguments);
  $this
    ->assertArrayEquals($expected, $element);
  $expected['#default_value'] = $element['#default_value'] = [
    'test',
  ];
  $expected['select']['#options'] = [
    '' => '- None -',
  ];
  $add_empty_option
    ->invokeArgs(NULL, $arguments);
  $this
    ->assertArrayEquals($expected, $element);
  $expected['#default_value'] = $element['#default_value'] = 'test';
  $expected['select']['#options'] = [
    '' => '- None -',
  ];
  $add_empty_option
    ->invokeArgs(NULL, $arguments);
  $this
    ->assertArrayEquals($expected, $element);
  $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);
}