View source
<?php
namespace Drupal\Tests\select_or_other\Unit {
use Drupal\Core\Form\FormState;
use Drupal\select_or_other\Element\ElementBase;
use Drupal\Tests\UnitTestCase;
use ReflectionMethod;
class ElementsTest extends UnitTestCase {
public function testAddOtherOption() {
$options = [];
$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);
}
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.');
}
}
public function testProcessSelectOrOther() {
$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);
}
public function testPrepareState() {
$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);
}
}
}
namespace {
if (!function_exists('t')) {
function t($string, array $args = []) {
return strtr($string, $args);
}
}
}