You are here

public function LinkFieldCrudTest::testLinkTitlePreservesQueryParams in Link 7

Tests that link titles properly preserve the query params.

File

tests/LinkFieldCrudTest.test, line 518
Testing that users can not input bad URLs or labels.

Class

LinkFieldCrudTest
Testing that users can not input bad URLs or labels.

Code

public function testLinkTitlePreservesQueryParams() {

  // Create field.
  $field_name = $this
    ->createLinkField();

  // Create page form.
  $this
    ->drupalGet('node/add/page');
  $this
    ->assertField($field_name . '[und][0][title]', 'Title found');
  $this
    ->assertField($field_name . '[und][0][url]', 'URL found');
  $link_title_tests = array(
    array(
      'href' => 'http://example.com/' . $this
        ->randomName(),
    ),
    array(
      'href' => 'http://example.com/' . $this
        ->randomName() . '?property=value',
    ),
    array(
      'href' => 'http://example.com/' . $this
        ->randomName() . '?property=value&mango=thebest',
    ),
    array(
      'href' => 'http://example.com/' . $this
        ->randomName() . '#position',
    ),
    array(
      'href' => 'http://example.com/' . $this
        ->randomName() . '#lower?property=value2',
    ),
    array(
      'href' => 'http://example.com/' . $this
        ->randomName() . '?property=value2#lower',
    ),
  );
  foreach ($link_title_tests as &$input) {
    $this
      ->drupalGet('node/add/page');
    $this
      ->assertResponse(200);

    // Intentionally set empty title, to force using the url as the title.
    $edit = array(
      'title' => 'Some node title',
      $field_name . '[und][0][title]' => '',
      $field_name . '[und][0][url]' => $input['href'],
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));

    // Confirm the node saved correctly.
    $this
      ->assertResponse(200);
    $this
      ->assertText($edit['title']);
    $input['url'] = $this
      ->getUrl();
  }

  // Change to anonymous user.
  $this
    ->drupalLogout();

  // Loop over the nodes, confirm each URL is rendered correctly.
  foreach ($link_title_tests as $input) {
    $this
      ->drupalGet($input['url']);

    // This is precarious. It isn't possible to compare the link as-is because
    // it is already filtered via check_plain().
    $this
      ->assertRaw(l($input['href'], $input['href'], array(
      'html' => TRUE,
    )), "Test that the link title has been set to " . $input['href']);
  }
}