You are here

function LinkFieldTest::testURLValidation in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/link/src/Tests/LinkFieldTest.php \Drupal\link\Tests\LinkFieldTest::testURLValidation()

Tests link field URL validation.

File

core/modules/link/src/Tests/LinkFieldTest.php, line 57
Contains \Drupal\link\Tests\LinkFieldTest.

Class

LinkFieldTest
Tests link field widgets and formatters.

Namespace

Drupal\link\Tests

Code

function testURLValidation() {
  $field_name = Unicode::strtolower($this
    ->randomMachineName());

  // Create a field with settings to validate.
  $this->fieldStorage = entity_create('field_storage_config', array(
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'link',
  ));
  $this->fieldStorage
    ->save();
  $this->field = entity_create('field_config', array(
    'field_storage' => $this->fieldStorage,
    'bundle' => 'entity_test',
    'settings' => array(
      'title' => DRUPAL_DISABLED,
      'link_type' => LinkItemInterface::LINK_GENERIC,
    ),
  ));
  $this->field
    ->save();
  entity_get_form_display('entity_test', 'entity_test', 'default')
    ->setComponent($field_name, array(
    'type' => 'link_default',
    'settings' => array(
      'placeholder_url' => 'http://example.com',
    ),
  ))
    ->save();
  entity_get_display('entity_test', 'entity_test', 'full')
    ->setComponent($field_name, array(
    'type' => 'link',
  ))
    ->save();

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName("{$field_name}[0][uri]", '', 'Link URL field is displayed');
  $this
    ->assertRaw('placeholder="http://example.com"');

  // Create a path alias.
  \Drupal::service('path.alias_storage')
    ->save('/admin', '/a/path/alias');

  // Create a node to test the link widget.
  $node = $this
    ->drupalCreateNode();

  // Define some valid URLs (keys are the entered values, values are the
  // strings displayed to the user).
  $valid_external_entries = array(
    'http://www.example.com/' => 'http://www.example.com/',
  );
  $valid_internal_entries = array(
    '/entity_test/add' => '/entity_test/add',
    '/a/path/alias' => '/a/path/alias',
    // Front page, with query string and fragment.
    '/' => '<front>',
    '/?example=llama' => '<front>?example=llama',
    '/#example' => '<front>#example',
    // @todo '<front>' is valid input for BC reasons, may be removed by
    //   https://www.drupal.org/node/2421941
    '<front>' => '&lt;front&gt;',
    '<front>#example' => '&lt;front&gt;#example',
    '<front>?example=llama' => '&lt;front&gt;?example=llama',
    // Query string and fragment.
    '?example=llama' => '?example=llama',
    '#example' => '#example',
    // Entity reference autocomplete value.
    $node
      ->label() . ' (1)' => $node
      ->label() . ' (1)',
    // Entity URI displayed as ER autocomplete value when displayed in a form.
    'entity:node/1' => $node
      ->label() . ' (1)',
    // URI for an entity that exists, but is not accessible by the user.
    'entity:user/1' => '- Restricted access - (1)',
    // URI for an entity that doesn't exist, but with a valid ID.
    'entity:user/999999' => 'entity:user/999999',
  );

  // Define some invalid URLs.
  $validation_error_1 = "The path '@link_path' is invalid.";
  $validation_error_2 = 'Manually entered paths should start with /, ? or #.';
  $validation_error_3 = "The path '@link_path' is inaccessible.";
  $invalid_external_entries = array(
    // Invalid protocol
    'invalid://not-a-valid-protocol' => $validation_error_1,
    // Missing host name
    'http://' => $validation_error_1,
  );
  $invalid_internal_entries = array(
    'no-leading-slash' => $validation_error_2,
    'entity:non_existing_entity_type/yar' => $validation_error_1,
    // URI for an entity that doesn't exist, with an invalid ID.
    'entity:user/invalid-parameter' => $validation_error_1,
  );

  // Test external and internal URLs for 'link_type' = LinkItemInterface::LINK_GENERIC.
  $this
    ->assertValidEntries($field_name, $valid_external_entries + $valid_internal_entries);
  $this
    ->assertInvalidEntries($field_name, $invalid_external_entries + $invalid_internal_entries);

  // Test external URLs for 'link_type' = LinkItemInterface::LINK_EXTERNAL.
  $this->field
    ->setSetting('link_type', LinkItemInterface::LINK_EXTERNAL);
  $this->field
    ->save();
  $this
    ->assertValidEntries($field_name, $valid_external_entries);
  $this
    ->assertInvalidEntries($field_name, $valid_internal_entries + $invalid_external_entries);

  // Test external URLs for 'link_type' = LinkItemInterface::LINK_INTERNAL.
  $this->field
    ->setSetting('link_type', LinkItemInterface::LINK_INTERNAL);
  $this->field
    ->save();
  $this
    ->assertValidEntries($field_name, $valid_internal_entries);
  $this
    ->assertInvalidEntries($field_name, $valid_external_entries + $invalid_internal_entries);

  // Ensure that users with 'link to any page', don't apply access checking.
  $this
    ->drupalLogin($this
    ->drupalCreateUser([
    'view test entity',
    'administer entity_test content',
  ]));
  $this
    ->assertValidEntries($field_name, [
    '/entity_test/add' => '/entity_test/add',
  ]);
  $this
    ->assertInValidEntries($field_name, [
    '/admin' => $validation_error_3,
  ]);
}