You are here

public function WebformElementOtherTest::testProcessingOtherElements in Webform 6.x

Same name and namespace in other branches
  1. 8.5 tests/src/Functional/Element/WebformElementOtherTest.php \Drupal\Tests\webform\Functional\Element\WebformElementOtherTest::testProcessingOtherElements()

Tests value processing for other elements.

File

tests/src/Functional/Element/WebformElementOtherTest.php, line 104

Class

WebformElementOtherTest
Tests for webform element other.

Namespace

Drupal\Tests\webform\Functional\Element

Code

public function testProcessingOtherElements() {
  $webform = Webform::load('test_element_other');

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

  // Basic input processing.

  /**************************************************************************/
  $this
    ->postSubmission($webform);
  $this
    ->assertRaw("select_other_basic: Four\nselect_other_advanced: Four\nselect_other_multiple:\n  - One\n  - Two\n  - Four\nselect_other_zero: '0'\ncheckboxes_other_basic:\n  - One\n  - Two\n  - Four\ncheckboxes_other_advanced:\n  - One\n  - Two\n  - Four\nradios_other_basic: Four\nradios_other_advanced: Four\nwrapper_other_fieldset: ''\nwrapper_other_form_element: ''\nwrapper_other_container: ''");

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

  // select_other

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

  // Check select other is required when selected.
  $edit = [
    'select_other_basic[select]' => '_other_',
    'select_other_basic[other]' => '',
  ];
  $this
    ->drupalPostForm('/webform/test_element_other', $edit, '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('/webform/test_element_other', $edit, '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('/webform/test_element_other', $edit, 'Submit');
  $this
    ->assertNoRaw('Select other advanced field is required.');
  $this
    ->assertRaw('This is a custom required error message.');

  // Check select other custom required error.
  $edit = [
    'select_other_advanced[select]' => '_other_',
    'select_other_advanced[other]' => '',
  ];
  $this
    ->drupalPostForm('/webform/test_element_other', $edit, 'Submit');
  $this
    ->assertNoRaw('Select other advanced field is required.');
  $this
    ->assertRaw('This is a custom required error message.');

  // Check select other processing w/ other min/max character validation.
  $edit = [
    'select_other_advanced[select]' => '_other_',
    'select_other_advanced[other]' => 'X',
  ];
  $this
    ->drupalPostForm('/webform/test_element_other', $edit, 'Submit');
  $this
    ->assertRaw('Other must be longer than <em class="placeholder">4</em> characters but is currently <em class="placeholder">1</em> characters long.');

  // Check select other processing w/ other.
  $edit = [
    'select_other_advanced[select]' => '_other_',
    'select_other_advanced[other]' => 'Five',
  ];
  $this
    ->drupalPostForm('/webform/test_element_other', $edit, '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('/webform/test_element_other', $edit, '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 = $webform
    ->getElementsDecoded();
  $elements['select_other']['select_other_advanced']['#default_value'] = NULL;
  $webform
    ->setElements($elements)
    ->save();
  $this
    ->drupalPostForm('/webform/test_element_other', [], 'Submit');
  $this
    ->assertRaw('This is a custom required error message.');

  // Check select other validation is skipped when #access is set to FALSE.
  $elements['select_other']['select_other_advanced']['#access'] = FALSE;
  $webform
    ->setElements($elements)
    ->save();
  $this
    ->drupalPostForm('/webform/test_element_other', [], 'Submit');
  $this
    ->assertNoRaw('This is a custom required error message.');

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

  // radios_other

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

  // Check radios other required when checked.
  $edit = [
    'radios_other_basic[radios]' => '_other_',
    'radios_other_basic[other]' => '',
  ];
  $this
    ->drupalPostForm('/webform/test_element_other', $edit, '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('/webform/test_element_other', $edit, '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('/webform/test_element_other', $edit, '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('/webform/test_element_other', $edit, '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('/webform/test_element_other', $edit, '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('/webform/test_element_other', $edit, '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('/webform/test_element_other', $edit, '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('/webform/test_element_other', $edit, '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('/webform/test_element_other', $edit, '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('/webform/test_element_other', $edit, 'Submit');
  $this
    ->assertRaw('checkboxes_other_advanced:
  - One');
  $this
    ->assertNoRaw('checkboxes_other_advanced:
  - Five');
}