CheckboxTest.php 4.09 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace Drupal\system\Tests\Form;

use Drupal\simpletest\WebTestBase;

/**
8 9 10 11
 * Tests form API checkbox handling of various combinations of #default_value
 * and #return_value.
 *
 * @group Form
12 13 14
 */
class CheckboxTest extends WebTestBase {

15 16 17 18 19 20 21
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('form_test');

22 23 24 25
  function testFormCheckbox() {
    // Ensure that the checked state is determined and rendered correctly for
    // tricky combinations of default and return values.
    foreach (array(FALSE, NULL, TRUE, 0, '0', '', 1, '1', 'foobar', '1foobar') as $default_value) {
26
      // Only values that can be used for array indices are supported for
27
      // #return_value, with the exception of integer 0, which is not supported.
28
      // @see \Drupal\Core\Render\Element\Checkbox::processCheckbox().
29
      foreach (array('0', '', 1, '1', 'foobar', '1foobar') as $return_value) {
30
        $form_array = \Drupal::formBuilder()->getForm('\Drupal\form_test\Form\FormTestCheckboxTypeJugglingForm', $default_value, $return_value);
31
        $form = \Drupal::service('renderer')->renderRoot($form_array);
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        if ($default_value === TRUE) {
          $checked = TRUE;
        }
        elseif ($return_value === '0') {
          $checked = ($default_value === '0');
        }
        elseif ($return_value === '') {
          $checked = ($default_value === '');
        }
        elseif ($return_value === 1 || $return_value === '1') {
          $checked = ($default_value === 1 || $default_value === '1');
        }
        elseif ($return_value === 'foobar') {
          $checked = ($default_value === 'foobar');
        }
        elseif ($return_value === '1foobar') {
          $checked = ($default_value === '1foobar');
        }
        $checked_in_html = strpos($form, 'checked') !== FALSE;
51
        $message = format_string('#default_value is %default_value #return_value is %return_value.', array('%default_value' => var_export($default_value, TRUE), '%return_value' => var_export($return_value, TRUE)));
52 53 54 55
        $this->assertIdentical($checked, $checked_in_html, $message);
      }
    }

56
    // Ensure that $form_state->getValues() is populated correctly for a checkboxes
57
    // group that includes a 0-indexed array of options.
58
    $results = json_decode($this->drupalPostForm('form-test/checkboxes-zero', array(), 'Save'));
59 60 61
    $this->assertIdentical($results->checkbox_off, array(0, 0, 0), 'All three in checkbox_off are zeroes: off.');
    $this->assertIdentical($results->checkbox_zero_default, array('0', 0, 0), 'The first choice is on in checkbox_zero_default');
    $this->assertIdentical($results->checkbox_string_zero_default, array('0', 0, 0), 'The first choice is on in checkbox_string_zero_default');
62
    $edit = array('checkbox_off[0]' => '0');
63
    $results = json_decode($this->drupalPostForm('form-test/checkboxes-zero', $edit, 'Save'));
64
    $this->assertIdentical($results->checkbox_off, array('0', 0, 0), 'The first choice is on in checkbox_off but the rest is not');
65 66 67

    // Ensure that each checkbox is rendered correctly for a checkboxes group
    // that includes a 0-indexed array of options.
68
    $this->drupalPostForm('form-test/checkboxes-zero/0', array(), 'Save');
69 70 71 72
    $checkboxes = $this->xpath('//input[@type="checkbox"]');
    foreach ($checkboxes as $checkbox) {
      $checked = isset($checkbox['checked']);
      $name = (string) $checkbox['name'];
73
      $this->assertIdentical($checked, $name == 'checkbox_zero_default[0]' || $name == 'checkbox_string_zero_default[0]', format_string('Checkbox %name correctly checked', array('%name' => $name)));
74 75
    }
    $edit = array('checkbox_off[0]' => '0');
76
    $this->drupalPostForm('form-test/checkboxes-zero/0', $edit, 'Save');
77 78 79 80
    $checkboxes = $this->xpath('//input[@type="checkbox"]');
    foreach ($checkboxes as $checkbox) {
      $checked = isset($checkbox['checked']);
      $name = (string) $checkbox['name'];
81
      $this->assertIdentical($checked, $name == 'checkbox_off[0]' || $name == 'checkbox_zero_default[0]' || $name == 'checkbox_string_zero_default[0]', format_string('Checkbox %name correctly checked', array('%name' => $name)));
82 83
    }
  }
84

85
}