You are here

function ElementTest::testOptions in Zircon Profile 8

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

Tests expansion of #options for #type checkboxes and radios.

File

core/modules/system/src/Tests/Form/ElementTest.php, line 52
Contains \Drupal\system\Tests\Form\ElementTest.

Class

ElementTest
Tests building and processing of core form elements.

Namespace

Drupal\system\Tests\Form

Code

function testOptions() {
  $this
    ->drupalGet('form-test/checkboxes-radios');

  // Verify that all options appear in their defined order.
  foreach (array(
    'checkbox',
    'radio',
  ) as $type) {
    $elements = $this
      ->xpath('//input[@type=:type]', array(
      ':type' => $type,
    ));
    $expected_values = array(
      '0',
      'foo',
      '1',
      'bar',
      '>',
    );
    foreach ($elements as $element) {
      $expected = array_shift($expected_values);
      $this
        ->assertIdentical((string) $element['value'], $expected);
    }
  }

  // Verify that the choices are admin filtered as expected.
  $this
    ->assertRaw("<em>Special Char</em>alert('checkboxes');");
  $this
    ->assertRaw("<em>Special Char</em>alert('radios');");
  $this
    ->assertRaw('<em>Bar - checkboxes</em>');
  $this
    ->assertRaw('<em>Bar - radios</em>');

  // Enable customized option sub-elements.
  $this
    ->drupalGet('form-test/checkboxes-radios/customize');

  // Verify that all options appear in their defined order, taking a custom
  // #weight into account.
  foreach (array(
    'checkbox',
    'radio',
  ) as $type) {
    $elements = $this
      ->xpath('//input[@type=:type]', array(
      ':type' => $type,
    ));
    $expected_values = array(
      '0',
      'foo',
      'bar',
      '>',
      '1',
    );
    foreach ($elements as $element) {
      $expected = array_shift($expected_values);
      $this
        ->assertIdentical((string) $element['value'], $expected);
    }
  }

  // Verify that custom #description properties are output.
  foreach (array(
    'checkboxes',
    'radios',
  ) as $type) {
    $elements = $this
      ->xpath('//input[@id=:id]/following-sibling::div[@class=:class]', array(
      ':id' => 'edit-' . $type . '-foo',
      ':class' => 'description',
    ));
    $this
      ->assertTrue(count($elements), format_string('Custom %type option description found.', array(
      '%type' => $type,
    )));
  }
}