You are here

protected function RangeFieldWidgetValidationTestCase::testRangeIntegerFormValidation in Range 7

Tests field widget validation for range integer fields.

File

tests/range.field_widget_validation.test, line 94
Contains range form validation tests.

Class

RangeFieldWidgetValidationTestCase
Tests form validation for range fields.

Code

protected function testRangeIntegerFormValidation() {
  $this
    ->createTestRangeField('range_integer');
  $this
    ->drupalGet("node/add/{$this->contentType}");
  $field_name = $this
    ->getTestFieldName('range_integer');
  $form_element_key = $field_name . '[' . LANGUAGE_NONE . '][0]';

  // Test field widget validation error messages.
  // Only numbers are allowed.
  $incorrect_entries = array(
    array(
      'from' => 'A',
      'to' => 'Z',
    ),
    array(
      'from' => '1.0',
      'to' => '2.00',
    ),
    array(
      'from' => '-1.0',
      'to' => '2-',
    ),
    array(
      'from' => '0A',
      'to' => '9Z',
    ),
    array(
      'from' => '-1-',
      'to' => '2-2',
    ),
    array(
      'from' => '-',
      'to' => '-',
    ),
  );
  foreach ($incorrect_entries as $entries) {
    $edit = array(
      "{$form_element_key}[from]" => $entries['from'],
      "{$form_element_key}[to]" => $entries['to'],
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $error_message = t('Only numbers are allowed in %field.', array(
      '%field' => static::INSTANCE_SETTINGS_FROM_LABEL,
    ));
    $this
      ->assertRaw($error_message, format_string('Correct widget validation error message found on the page: !error_message', array(
      '!error_message' => $error_message,
    )));
    $error_message = t('Only numbers are allowed in %field.', array(
      '%field' => static::INSTANCE_SETTINGS_TO_LABEL,
    ));
    $this
      ->assertRaw($error_message, format_string('Correct widget validation error message found on the page: !error_message', array(
      '!error_message' => $error_message,
    )));
  }
  $correct_entries = array(
    array(
      'from' => '-200',
      'to' => '-100',
    ),
    array(
      'from' => '-3',
      'to' => '99',
    ),
    array(
      'from' => '0',
      'to' => '9',
    ),
    array(
      'from' => '1',
      'to' => '5',
    ),
    array(
      'from' => '10000',
      'to' => '20000',
    ),
  );
  foreach ($correct_entries as $entries) {
    $edit = array(
      "{$form_element_key}[from]" => $entries['from'],
      "{$form_element_key}[to]" => $entries['to'],
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $error_message = t('Only numbers are allowed in %field.', array(
      '%field' => static::INSTANCE_SETTINGS_FROM_LABEL,
    ));
    $this
      ->assertNoRaw($error_message, format_string('Correct widget validation error message found on the page: !error_message', array(
      '!error_message' => $error_message,
    )));
    $error_message = t('Only numbers are allowed in %field.', array(
      '%field' => static::INSTANCE_SETTINGS_TO_LABEL,
    ));
    $this
      ->assertNoRaw($error_message, format_string('Correct widget validation error message found on the page: !error_message', array(
      '!error_message' => $error_message,
    )));
  }
}