You are here

public function WebformSubmissionTestCase::testWebformSubmissionComponentLength in Webform 7.4

Test length validation.

File

tests/WebformSubmissionTestCase.test, line 214

Class

WebformSubmissionTestCase
Webform module submission tests.

Code

public function testWebformSubmissionComponentLength() {
  $this
    ->drupalLogin($this->webform_users['admin']);
  $this
    ->webformReset();

  // Create the Webform test node.
  $node = $this
    ->webformForm();
  $node = node_load($node->nid);

  // Get the cid of the textfield component.
  foreach ($node->webform['components'] as &$component) {
    if ($component['form_key'] === 'textfield') {
      $textfield_cid = $component['cid'];
      break;
    }
  }

  // Set length validation rules.
  $node->webform['components'][$textfield_cid]['extra']['maxlength'] = 5;
  $node->webform['components'][$textfield_cid]['extra']['minlength'] = 4;
  node_save($node);

  // Text value that is too long.
  $this
    ->drupalPost('node/' . $node->nid, array(
    'submitted[textfield]' => '123456',
  ), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
  $this
    ->assertRaw(t('!name cannot be longer than %max characters but is currently %length characters long.', array(
    '!name' => $node->webform['components'][$textfield_cid]['name'],
    '%max' => 5,
    '%length' => 6,
  )));

  // Text value that is too short.
  $this
    ->drupalPost('node/' . $node->nid, array(
    'submitted[textfield]' => '123',
  ), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
  $this
    ->assertRaw(t('!name cannot be shorter than %min characters but is currently %length characters long.', array(
    '!name' => $node->webform['components'][$textfield_cid]['name'],
    '%min' => 4,
    '%length' => 3,
  )));

  // Test value that meets validation rules has no error message.
  $this
    ->drupalPost('node/' . $node->nid, array(
    'submitted[textfield]' => '12345',
  ), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
  $this
    ->assertNoPattern('/ cannot be (longer|shorter) than /');
}