You are here

class ElementsTest in Select (or other) 8

Same name and namespace in other branches
  1. 8.3 Tests/src/Unit/ElementsTest.php \Drupal\Tests\select_or_other\Unit\ElementsTest
  2. 4.x tests/src/Unit/ElementsTest.php \Drupal\Tests\select_or_other\Unit\ElementsTest

Tests the form element implementation.

@group select_or_other

@covers \Drupal\select_or_other\Element\ElementBase

Hierarchy

Expanded class hierarchy of ElementsTest

File

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

Namespace

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

  /**
   * Tests the addition of the other option to an options array.
   */
  public function testAddOtherOption() {
    $options = [];

    // Make the protected method accessible and invoke it.
    $method = new ReflectionMethod('Drupal\\select_or_other\\Element\\ElementBase', 'addOtherOption');
    $method
      ->setAccessible(TRUE);
    $options = $method
      ->invoke(NULL, $options);
    $this
      ->assertArrayEquals([
      'select_or_other' => "Other",
    ], $options);
  }

  /**
   * Tests the value callback.
   */
  public function testValueCallback() {
    $form_state = new FormState();
    $element = [
      '#multiple' => FALSE,
    ];
    $input = [
      'select' => 'Selected text',
      'other' => 'Other text',
    ];
    $expected = [
      $input['select'],
    ];
    $values = ElementBase::valueCallback($element, $input, $form_state);
    $this
      ->assertArrayEquals($expected, $values, 'Returned single value select.');
    $input['select'] = 'select_or_other';
    $expected = [
      $input['other'],
    ];
    $values = ElementBase::valueCallback($element, $input, $form_state);
    $this
      ->assertArrayEquals($expected, $values, 'Returned single value other.');
    $element['#multiple'] = TRUE;
    $input['select'] = [
      'Selected text',
    ];
    $expected = [
      'select' => $input['select'],
      'other' => [],
    ];
    $values = ElementBase::valueCallback($element, $input, $form_state);
    $this
      ->assertArrayEquals($expected, $values, 'Returned select array and empty other array.');
    $input['select'][] = 'select_or_other';
    $expected['other'] = [
      $input['other'],
    ];
    $values = ElementBase::valueCallback($element, $input, $form_state);
    $this
      ->assertArrayEquals($expected, $values, 'Returned select array and other array.');
    $input['select'] = [
      'select_or_other',
    ];
    $expected = [
      'select' => [],
      'other' => [
        $input['other'],
      ],
    ];
    $values = ElementBase::valueCallback($element, $input, $form_state);
    $this
      ->assertArrayEquals($expected, $values, 'Returned empty select and other array.');
    $input['select'][] = 'Selected';
    $element['#merged_values'] = TRUE;
    $expected = [
      'Selected',
      $input['other'],
    ];
    $values = ElementBase::valueCallback($element, $input, $form_state);
    $this
      ->assertArrayEquals($expected, $values, 'Returned merged array.');
    $input['select'] = [
      'Selected',
    ];
    $input['other'] = '';
    $expected = [
      'Selected',
    ];
    $values = ElementBase::valueCallback($element, $input, $form_state);
    $this
      ->assertArrayEquals($expected, $values, 'Returned merged array.');
    foreach ([
      TRUE,
      FALSE,
    ] as $multiple) {
      $element['#multiple'] = $multiple;
      $input = [
        'other' => 'Other value',
      ];
      $expected = [];
      $values = ElementBase::valueCallback($element, $input, $form_state);
      $this
        ->assertArrayEquals($expected, $values, 'Submitting only the other value results in an empty array.');
    }
  }

  /**
   * 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 = [];
    $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",
      ],
    ];
    $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",
        ],
      ],
    ];
    $resulting_element = ElementBase::processSelectOrOther($element, $form_state, $form);
    $this
      ->assertArrayEquals($expected_element, $resulting_element);
    $this
      ->assertArrayEquals($resulting_element, $element);
  }

  /**
   * Tests the processing of form API #state .
   */
  public function testPrepareState() {

    // Test ElementBase.
    // Make the protected method accessible and invoke it.
    $method = new ReflectionMethod('Drupal\\select_or_other\\Element\\ElementBase', 'prepareState');
    $method
      ->setAccessible(TRUE);
    $result = $method
      ->invoke(null, 'state', 'name', 'key', 'value');
    $expected = [
      'state' => [
        ':input[name="name"]' => [
          'key' => 'value',
        ],
      ],
    ];
    $this
      ->assertArrayEquals($expected, $result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ElementsTest::testAddOtherOption public function Tests the addition of the other option to an options array.
ElementsTest::testPrepareState public function Tests the processing of form API #state .
ElementsTest::testProcessSelectOrOther public function Tests the processing of a select or other element.
ElementsTest::testValueCallback public function Tests the value callback.
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