You are here

public function ButtonsElementTest::testProcessSelectOrOther in Select (or other) 8

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

Tests the processing of a select or other element.

File

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

Class

ButtonsElementTest
Tests the form element implementation.

Namespace

Drupal\Tests\select_or_other\Unit

Code

public function testProcessSelectOrOther() {

  // Test ElementBase.
  // Make the protected method accessible and invoke it.
  $method = new ReflectionMethod('Drupal\\select_or_other\\Element\\ElementBase', 'addOtherOption');
  $method
    ->setAccessible(TRUE);
  $form_state = new FormState();
  $form = [];
  $original_element = $element = [
    '#name' => 'select_or_other',
    '#no_empty_option' => FALSE,
    '#default_value' => 'default',
    '#required' => TRUE,
    '#multiple' => FALSE,
    '#options' => [
      'first_option' => 'First option',
      'second_option' => "Second option",
    ],
  ];
  $base_expected_element = $expected_element = $element + [
    'select' => [
      '#default_value' => $element['#default_value'],
      '#required' => $element['#required'],
      '#multiple' => $element['#multiple'],
      '#options' => $method
        ->invoke(NULL, $element['#options']),
      '#attributes' => [
        'aria-label' => isset($element['#title']) ? $element['#title'] : $element['#name'],
      ],
      '#weight' => 10,
    ],
    'other' => [
      '#type' => 'textfield',
      '#attributes' => [
        'aria-label' => isset($element['#title']) ? $element['#title'] . ' Other' : $element['#name'] . ' Other',
      ],
      '#weight' => 20,
      '#attributes' => [
        'placeholder' => "Other: please specify here",
      ],
    ],
  ];

  // Test single cardinality Buttons.
  $element = $original_element;
  $expected_element = array_merge_recursive($base_expected_element, [
    'select' => [
      '#type' => 'checkboxes',
      '#value' => [],
    ],
    'other' => [
      '#states' => [
        'visible' => [
          ':input[name="' . $element['#name'] . '[select][select_or_other]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ],
  ]);
  $element['#multiple'] = $expected_element['#multiple'] = $expected_element['select']['#multiple'] = TRUE;
  $resulting_element = Buttons::processSelectOrOther($element, $form_state, $form);
  $this
    ->assertArrayEquals($expected_element, $resulting_element);
  $this
    ->assertArrayEquals($resulting_element, $element);

  // Test multiple cardinality Buttons.
  $element = $original_element;
  $expected_element = array_merge_recursive($base_expected_element, [
    'select' => [
      '#type' => 'radios',
      '#value' => [],
    ],
    'other' => [
      '#states' => [
        'visible' => [
          ':input[name="' . $element['#name'] . '[select]"]' => [
            'value' => 'select_or_other',
          ],
        ],
      ],
    ],
  ]);
  $resulting_element = Buttons::processSelectOrOther($element, $form_state, $form);
  $this
    ->assertArrayEquals($expected_element, $resulting_element);
  $this
    ->assertArrayEquals($resulting_element, $element);
}