1 form.test FormCheckboxTestCase::testFormCheckbox()

File

core/modules/simpletest/tests/form.test, line 2245
Unit tests for the Backdrop Form API.

Class

FormCheckboxTestCase
Tests checkbox element.

Code

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) {
    // Only values that can be used for array indices are supported for
    // #return_value, with the exception of integer 0, which is not supported.
    // @see form_process_checkbox().
    foreach (array('0', '', 1, '1', 'foobar', '1foobar') as $return_value) {
      $form_array = backdrop_get_form('form_test_checkbox_type_juggling', $default_value, $return_value);
      $form = backdrop_render($form_array);
      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;
      $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),
      ));
      $this->assertIdentical($checked, $checked_in_html, $message);
    }
  }

  // Ensure that $form_state['values'] is populated correctly for a checkboxes
  // group that includes a 0-indexed array of options.
  $results = json_decode($this->backdropPost('form-test/checkboxes-zero', array(), 'Save'));
  $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');
  $edit = array('checkbox_off[0]' => '0');
  $results = json_decode($this->backdropPost('form-test/checkboxes-zero', $edit, 'Save'));
  $this->assertIdentical($results->checkbox_off, array('0', 0, 0), 'The first choice is on in checkbox_off but the rest is not');

  // Ensure that each checkbox is rendered correctly for a checkboxes group
  // that includes a 0-indexed array of options.
  $this->backdropPost('form-test/checkboxes-zero/0', array(), 'Save');
  $checkboxes = $this->xpath('//input[@type="checkbox"]');
  foreach ($checkboxes as $checkbox) {
    $checked = isset($checkbox['checked']);
    $name = (string) $checkbox['name'];
    $this->assertIdentical($checked, $name == 'checkbox_zero_default[0]' || $name == 'checkbox_string_zero_default[0]', format_string('Checkbox %name correctly checked', array('%name' => $name)));
  }
  $edit = array('checkbox_off[0]' => '0');
  $this->backdropPost('form-test/checkboxes-zero/0', $edit, 'Save');
  $checkboxes = $this->xpath('//input[@type="checkbox"]');
  foreach ($checkboxes as $checkbox) {
    $checked = isset($checkbox['checked']);
    $name = (string) $checkbox['name'];
    $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)));
  }

  // Make sure that a single checkbox gets the correct value when set via
  // $_GET parameters. The content overview page is a view with an exposed
  // form, where changing the sorting via tablesort sets values with $_GET.
  $admin_user = $this->backdropCreateUser(array(
    'access content overview',
    'bypass node access',
    'access administration pages',
  ));
  $this->backdropLogin($admin_user);
  $options = array(
    'query' => array(
      'status' => 'All',
      'type' => 'All',
      'title' => '',
      'order' => 'title',
      'sort' => 'asc',
    ),
  );
  $values = array(
    '0' => FALSE,
    'foobar' => FALSE,
    '1' => TRUE,
  );
  foreach ($values as $key => $checked) {
    $options['query']['timestamp'] = $key;
    $this->backdropGet('admin/content/node', $options);
    $checkbox = $this->xpath('//input[@id="edit-timestamp"]');
    $attributes = $checkbox[0]->attributes();

    $this->assertEqual(isset($attributes['checked']), $values[$key], format_string('Expected checked status is set via GET for value %key.', array(
      '%key' => $key,
    )));
  }
}