You are here

public function LinkPathPrefixesTest::testLanguagePrefixedPaths in Link 7

Creates a link field, fills it, then uses a loaded node to test paths.

File

tests/LinkPathPrefixesTest.test, line 79
Tests that exercise the relative / absolute output of the link module.

Class

LinkPathPrefixesTest
Testing that absolute / relative outputs match the expected format.

Code

public function testLanguagePrefixedPaths() {
  $this
    ->setUpLanguage();

  // Create fields.
  // Field for absolute urls.
  $field_name_absolute = $this
    ->createLinkField('page');

  // Field for relative urls.
  $settings = array(
    'instance[settings][absolute_url]' => FALSE,
  );
  $field_name_relative = $this
    ->createLinkField('page', $settings);

  // Check the node edit form.
  $this
    ->drupalGet('node/add/page');
  $this
    ->assertField($field_name_absolute . '[und][0][title]', 'Title absolute found');
  $this
    ->assertField($field_name_absolute . '[und][0][url]', 'URL absolute found');
  $this
    ->assertField($field_name_relative . '[und][0][title]', 'Title relative found');
  $this
    ->assertField($field_name_relative . '[und][0][url]', 'URL relative found');

  // Create test content.
  $url_tests = array(
    1 => array(
      'href' => 'http://dummy.com/' . $this
        ->randomName(),
      'label' => $this
        ->randomName(),
    ),
    2 => array(
      'href' => 'node/1',
      'label' => $this
        ->randomName(),
    ),
    3 => array(
      'href' => 'node/1?property=value',
      'label' => $this
        ->randomName(),
      'query' => array(
        'property' => 'value',
      ),
    ),
    4 => array(
      'href' => 'node/1#position',
      'label' => $this
        ->randomName(),
      'fragment' => 'position',
    ),
    5 => array(
      'href' => 'node/1?property=value2#lower',
      'label' => $this
        ->randomName(),
      'fragment' => 'lower',
      'query' => array(
        'property' => 'value2',
      ),
    ),
  );
  foreach ($url_tests as $index => &$input) {
    $this
      ->drupalGet('node/add/page');
    $edit = array(
      'title' => $input['label'],
      $field_name_absolute . '[und][0][title]' => $input['label'],
      $field_name_absolute . '[und][0][url]' => $input['href'],
      $field_name_relative . '[und][0][title]' => $input['label'],
      $field_name_relative . '[und][0][url]' => $input['href'],
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $url = $this
      ->getUrl();
    $input['url'] = $url;
  }

  // Change to anonymous user.
  $this
    ->drupalLogout();
  foreach (array_slice($url_tests, 1, NULL, TRUE) as $index => $input2) {
    $node = node_load($index);
    $this
      ->assertNotEqual(NULL, $node, "Do we have a node?");
    $this
      ->assertEqual($node->nid, $index, "Test that we have a node.");
    $this
      ->drupalGet('node/' . $index);
    $relative_expected = url('node/1', array(
      'absolute' => FALSE,
    ) + $input2);
    $absolute_expected = url('node/1', array(
      'absolute' => TRUE,
    ) + $input2);
    $absolute_result = $this
      ->xpath('//*[contains(@class, "field-name-' . drupal_clean_css_identifier($field_name_absolute) . '")]/div/div/a/@href');
    $absolute_result = (string) reset($absolute_result);
    $this
      ->assertEqual($absolute_result, $absolute_expected, "Absolute url output ('" . $absolute_result . "') looks as expected ('" . $absolute_expected . "')");
    $relative_result = $this
      ->xpath('//*[contains(@class, "field-name-' . drupal_clean_css_identifier($field_name_relative) . '")]/div/div/a/@href');
    $relative_result = (string) reset($relative_result);
    $this
      ->assertEqual($relative_result, $relative_expected, "Relative url output ('" . $relative_result . "') looks as expected ('" . $relative_expected . "')");
  }

  // Check if this works with the alias too.
  // Add a path alias for node 1.
  $path = array(
    'source' => 'node/1',
    'alias' => $url_tests[1]['label'],
  );
  path_save($path);

  // Another iteration over the same nodes - this time they should use the
  // path alias.
  foreach (array_slice($url_tests, 1, NULL, TRUE) as $index => $input2) {
    $node = node_load($index);
    $this
      ->assertNotEqual(NULL, $node, "Do we have a node?");
    $this
      ->assertEqual($node->nid, $index, "Test that we have a node.");
    $this
      ->drupalGet('node/' . $index);
    $relative_expected = url('node/1', array(
      'absolute' => FALSE,
    ) + $input2);
    $absolute_expected = url('node/1', array(
      'absolute' => TRUE,
    ) + $input2);
    $absolute_result = $this
      ->xpath('//*[contains(@class, "field-name-' . drupal_clean_css_identifier($field_name_absolute) . '")]/div/div/a/@href');
    $absolute_result = (string) reset($absolute_result);
    $this
      ->assertEqual($absolute_result, $absolute_expected, "Absolute alias-url output ('" . $absolute_result . "') looks as expected ('" . $absolute_expected . "')");
    $relative_result = $this
      ->xpath('//*[contains(@class, "field-name-' . drupal_clean_css_identifier($field_name_relative) . '")]/div/div/a/@href');
    $relative_result = (string) reset($relative_result);
    $this
      ->assertEqual($relative_result, $relative_expected, "Relative alias-url output ('" . $relative_result . "') looks as expected ('" . $relative_expected . "')");
  }
}