You are here

public function YamlFormElementOtherTest::testProcessingOtherElements in YAML Form 8

Tests value processing for other elements.

File

src/Tests/YamlFormElementOtherTest.php, line 107

Class

YamlFormElementOtherTest
Tests for form element other.

Namespace

Drupal\yamlform\Tests

Code

public function testProcessingOtherElements() {
  $yamlform = YamlForm::load('test_element_other');

  /**************************************************************************/

  // select_other

  /**************************************************************************/

  // Check select other is required when selected.
  $edit = [
    'select_other_basic[select]' => '_other_',
    'select_other_basic[other]' => '',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertRaw('Select other basic field is required.');

  // Check select other is not required when not selected.
  $edit = [
    'select_other_basic[select]' => '',
    'select_other_basic[other]' => '',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertNoRaw('Select other basic field is required.');

  // Check select other required validation.
  $edit = [
    'select_other_advanced[select]' => '',
    'select_other_advanced[other]' => '',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertRaw('Select other advanced field is required.');

  // Check select other processing w/ other.
  $edit = [
    'select_other_advanced[select]' => '_other_',
    'select_other_advanced[other]' => 'Five',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertRaw('select_other_advanced: Five');

  // Check select other processing w/o other.
  $edit = [
    'select_other_advanced[select]' => 'One',
    // This value is ignored, because 'select_other_advanced[select]' is not set to '_other_'.
    'select_other_advanced[other]' => 'Five',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertRaw('select_other_advanced: One');
  $this
    ->assertNoRaw('select_other_advanced: Five');

  // Check select other validation is required when default value is NULL.
  $elements = $yamlform
    ->getElementsDecoded();
  $elements['select_other']['select_other_advanced']['#default_value'] = NULL;
  $yamlform
    ->setElements($elements)
    ->save();
  $this
    ->drupalPostForm('yamlform/test_element_other', [], t('Submit'));
  $this
    ->assertRaw('Select other advanced field is required.');

  // Check select other validation is skipped when #access is set to FALSE.
  $elements['select_other']['select_other_advanced']['#access'] = FALSE;
  $yamlform
    ->setElements($elements)
    ->save();
  $this
    ->drupalPostForm('yamlform/test_element_other', [], t('Submit'));
  $this
    ->assertNoRaw('Select other advanced field is required.');

  /**************************************************************************/

  // radios_other

  /**************************************************************************/

  // Check radios other required when checked.
  $edit = [
    'radios_other_basic[radios]' => '_other_',
    'radios_other_basic[other]' => '',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertRaw('Radios other basic field is required.');

  // Check radios other not required when not checked.
  $edit = [
    'radios_other_basic[radios]' => 'One',
    'radios_other_basic[other]' => '',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertNoRaw('Radios other basic field is required.');

  // Check radios other required validation.
  $edit = [
    'radios_other_advanced[radios]' => '_other_',
    'radios_other_advanced[other]' => '',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertRaw('Radios other advanced field is required.');

  // Check radios other processing w/ other.
  $edit = [
    'radios_other_advanced[radios]' => '_other_',
    'radios_other_advanced[other]' => 'Five',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertRaw('radios_other_advanced: Five');

  // Check radios other processing w/o other.
  $edit = [
    'radios_other_advanced[radios]' => 'One',
    // This value is ignored, because 'radios_other_advanced[radios]' is not set to '_other_'.
    'radios_other_advanced[other]' => 'Five',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertRaw('radios_other_advanced: One');
  $this
    ->assertNoRaw('radios_other_advanced: Five');

  /**************************************************************************/

  // checkboxes_other

  /**************************************************************************/

  // Check checkboxes other required when checked.
  $edit = [
    'checkboxes_other_basic[checkboxes][_other_]' => TRUE,
    'checkboxes_other_basic[other]' => '',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertRaw('Checkboxes other basic field is required.');

  // Check checkboxes other not required when not checked.
  $edit = [
    'checkboxes_other_basic[checkboxes][_other_]' => FALSE,
    'checkboxes_other_basic[other]' => '',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertNoRaw('Checkboxes other basic field is required.');

  // Check checkboxes other required validation.
  $edit = [
    'checkboxes_other_advanced[checkboxes][One]' => FALSE,
    'checkboxes_other_advanced[checkboxes][Two]' => FALSE,
    'checkboxes_other_advanced[checkboxes][Three]' => FALSE,
    'checkboxes_other_advanced[checkboxes][_other_]' => TRUE,
    'checkboxes_other_advanced[other]' => '',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertRaw('Checkboxes other advanced field is required.');

  // Check checkboxes other processing w/ other.
  $edit = [
    'checkboxes_other_advanced[checkboxes][One]' => FALSE,
    'checkboxes_other_advanced[checkboxes][Two]' => FALSE,
    'checkboxes_other_advanced[checkboxes][Three]' => FALSE,
    'checkboxes_other_advanced[checkboxes][_other_]' => TRUE,
    'checkboxes_other_advanced[other]' => 'Five',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertRaw('checkboxes_other_advanced:
  - Five');

  // Check checkboxes other processing w/o other.
  $edit = [
    'checkboxes_other_advanced[checkboxes][One]' => TRUE,
    'checkboxes_other_advanced[checkboxes][Two]' => FALSE,
    'checkboxes_other_advanced[checkboxes][Three]' => FALSE,
    'checkboxes_other_advanced[checkboxes][_other_]' => FALSE,
    // This value is ignored, because 'radios_other_advanced[radios]' is not set to '_other_'.
    'checkboxes_other_advanced[other]' => 'Five',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertRaw('checkboxes_other_advanced:
  - One');
  $this
    ->assertNoRaw('checkboxes_other_advanced:
  - Five');

  /**************************************************************************/

  // buttons_other

  /**************************************************************************/

  // Check buttons other required when checked.
  $edit = [
    'buttons_other_basic[buttons]' => '_other_',
    'buttons_other_basic[other]' => '',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertRaw('Buttons other basic field is required.');

  // Check buttons other not required when not checked.
  $edit = [
    'buttons_other_basic[buttons]' => 'One',
    'buttons_other_basic[other]' => '',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertNoRaw('Buttons other basic field is required.');

  // Check buttons other required validation.
  $edit = [
    'buttons_other_advanced[buttons]' => '_other_',
    'buttons_other_advanced[other]' => '',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertRaw('Buttons other advanced field is required.');

  // Check buttons other processing w/ other.
  $edit = [
    'buttons_other_advanced[buttons]' => '_other_',
    'buttons_other_advanced[other]' => 'Five',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertRaw('buttons_other_advanced: Five');

  // Check buttons other processing w/o other.
  $edit = [
    'buttons_other_advanced[buttons]' => 'One',
    // This value is ignored, because 'buttons_other_advanced[buttons]' is not set to '_other_'.
    'buttons_other_advanced[other]' => 'Five',
  ];
  $this
    ->drupalPostForm('yamlform/test_element_other', $edit, t('Submit'));
  $this
    ->assertRaw('buttons_other_advanced: One');
  $this
    ->assertNoRaw('buttons_other_advanced: Five');
}