You are here

function FormTest::testSelect in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Form/FormTest.php \Drupal\system\Tests\Form\FormTest::testSelect()

Tests validation of #type 'select' elements.

File

core/modules/system/src/Tests/Form/FormTest.php, line 374
Contains \Drupal\system\Tests\Form\FormTest.

Class

FormTest
Tests various form element validation mechanisms.

Namespace

Drupal\system\Tests\Form

Code

function testSelect() {
  $form = \Drupal::formBuilder()
    ->getForm('Drupal\\form_test\\Form\\FormTestSelectForm');
  $this
    ->drupalGet('form-test/select');

  // Verify that the options are escaped as expected.
  $this
    ->assertEscaped('<strong>four</strong>');
  $this
    ->assertNoRaw('<strong>four</strong>');

  // Posting without any values should throw validation errors.
  $this
    ->drupalPostForm(NULL, array(), 'Submit');
  $no_errors = array(
    'select',
    'select_required',
    'select_optional',
    'empty_value',
    'empty_value_one',
    'no_default_optional',
    'no_default_empty_option_optional',
    'no_default_empty_value_optional',
    'multiple',
    'multiple_no_default',
  );
  foreach ($no_errors as $key) {
    $this
      ->assertNoText(t('@name field is required.', array(
      '@name' => $form[$key]['#title'],
    )));
  }
  $expected_errors = array(
    'no_default',
    'no_default_empty_option',
    'no_default_empty_value',
    'no_default_empty_value_one',
    'multiple_no_default_required',
  );
  foreach ($expected_errors as $key) {
    $this
      ->assertText(t('@name field is required.', array(
      '@name' => $form[$key]['#title'],
    )));
  }

  // Post values for required fields.
  $edit = array(
    'no_default' => 'three',
    'no_default_empty_option' => 'three',
    'no_default_empty_value' => 'three',
    'no_default_empty_value_one' => 'three',
    'multiple_no_default_required[]' => 'three',
  );
  $this
    ->drupalPostForm(NULL, $edit, 'Submit');
  $values = Json::decode($this
    ->getRawContent());

  // Verify expected values.
  $expected = array(
    'select' => 'one',
    'empty_value' => 'one',
    'empty_value_one' => 'one',
    'no_default' => 'three',
    'no_default_optional' => 'one',
    'no_default_optional_empty_value' => '',
    'no_default_empty_option' => 'three',
    'no_default_empty_option_optional' => '',
    'no_default_empty_value' => 'three',
    'no_default_empty_value_one' => 'three',
    'no_default_empty_value_optional' => 0,
    'multiple' => array(
      'two' => 'two',
    ),
    'multiple_no_default' => array(),
    'multiple_no_default_required' => array(
      'three' => 'three',
    ),
  );
  foreach ($expected as $key => $value) {
    $this
      ->assertIdentical($values[$key], $value, format_string('@name: @actual is equal to @expected.', array(
      '@name' => $key,
      '@actual' => var_export($values[$key], TRUE),
      '@expected' => var_export($value, TRUE),
    )));
  }
}