You are here

class ButtonsElementTest 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

Tests the form element implementation.

@group select_or_other

@covers \Drupal\select_or_other\Element\Buttons

Hierarchy

Expanded class hierarchy of ButtonsElementTest

File

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

Namespace

Drupal\Tests\select_or_other\Unit
View source
class ButtonsElementTest extends UnitTestCase {

  /**
   * Tests the processing of a select or other element.
   */
  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);
  }

  /**
   * Make sure radio buttons always have a correct default value.
   */
  public function testEnsureCorrectDefaultValue() {
    $element = [
      'select' => [
        '#type' => 'radios',
      ],
    ];
    $arguments = [
      &$element,
    ];
    $ensure_correct_default_value = new ReflectionMethod('Drupal\\select_or_other\\Element\\Buttons', 'ensureCorrectDefaultValue');
    $ensure_correct_default_value
      ->setAccessible(TRUE);
    $expected = $element;
    $ensure_correct_default_value
      ->invokeArgs(NULL, $arguments);
    $this
      ->assertArrayEquals($expected, $element);
    $element['select']['#default_value'] = 'non_array_default';
    $expected = $element;
    $ensure_correct_default_value
      ->invokeArgs(NULL, $arguments);
    $this
      ->assertArrayEquals($expected, $element);
    $element['select']['#default_value'] = [
      'array_default',
    ];
    $expected['select']['#default_value'] = 'array_default';
    $ensure_correct_default_value
      ->invokeArgs(NULL, $arguments);
    $this
      ->assertArrayEquals($expected, $element);
    $expected['select']['#type'] = $element['select']['#type'] = 'checkboxes';
    $expected['select']['#default_value'] = $element['select']['#default_value'] = [
      'array_default',
    ];
    $ensure_correct_default_value
      ->invokeArgs(NULL, $arguments);
    $this
      ->assertArrayEquals($expected, $element);
  }

  /**
   * Make sure the empty option gets added when necessary.
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ButtonsElementTest::testAddEmptyOption public function Make sure the empty option gets added when necessary.
ButtonsElementTest::testEnsureCorrectDefaultValue public function Make sure radio buttons always have a correct default value.
ButtonsElementTest::testProcessSelectOrOther public function Tests the processing of a select or other element.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340