You are here

function URLFieldTestCase::testURLFieldValidation in URL field 7

Test number_decimal field.

File

./url.test, line 33
Tests for url.module.

Class

URLFieldTestCase
Tests for URL field types.

Code

function testURLFieldValidation() {

  // Create a field with settings to validate.
  $this->field = array(
    'field_name' => drupal_strtolower($this
      ->randomName()),
    'type' => 'url',
  );
  field_create_field($this->field);
  $this->instance = array(
    'field_name' => $this->field['field_name'],
    'entity_type' => 'test_entity',
    'bundle' => 'test_bundle',
    'settings' => array(
      'title_field' => FALSE,
      'title_fetch' => FALSE,
    ),
    'widget' => array(
      'type' => 'url_external',
    ),
    'display' => array(
      'default' => array(
        'type' => 'url_default',
      ),
    ),
  );
  field_create_instance($this->instance);

  // Display creation form.
  $this
    ->drupalGet('test-entity/add/test-bundle');
  $langcode = LANGUAGE_NONE;
  $this
    ->assertFieldByName("{$this->field['field_name']}[{$langcode}][0][value]", '', 'URL value field is displayed');
  $this
    ->assertNoFieldByName("{$this->field['field_name']}[{$langcode}][0][title]", '', 'URL title field is not displayed');

  // Submit a signed URL value.
  $value = 'http://www.google.com/';
  $edit = array(
    "{$this->field['field_name']}[{$langcode}][0][value]" => $value,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  preg_match('|test-entity/manage/(\\d+)/edit|', $this->url, $match);
  $id = $match[1];
  $this
    ->assertRaw(t('test_entity @id has been created.', array(
    '@id' => $id,
  )), 'Entity was created');
  $this
    ->assertRaw($value, 'Value is displayed.');

  // Try to create entries with invalid URLs.
  $wrong_entries = array(
    // Missing protcol
    'not-an-url',
    // Invalid protocol
    'invalid://not-a-valid-protocol',
    // Missing host name
    'http://',
  );
  foreach ($wrong_entries as $wrong_entry) {
    $this
      ->drupalGet('test-entity/add/test-bundle');
    $edit = array(
      "{$this->field['field_name']}[{$langcode}][0][value]" => $wrong_entry,
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $this
      ->assertText(t('The URL @url is not valid.', array(
      '@url' => $wrong_entry,
    )), 'Correctly failed to save invalid URL');
  }
}