You are here

public function LinkUnitTestCase::testLinkParseUrl in Link 7

Test _link_parse_url().

File

tests/LinkUnitTestCase.test, line 35
Unit tests for Link module's internal APIs.

Class

LinkUnitTestCase
Unit tests for Link module's internal APIs.

Code

public function testLinkParseUrl() {

  // Each of the keys is the URL to check, it will then be converted into a
  // matching array with three possible elements - 'url', 'query' and
  // 'fragment'.
  $urls = array(
    'https://www.drupal.org' => array(
      'url' => 'https://www.drupal.org',
    ),
    'https://www.drupal.org/?page=42' => array(
      'url' => 'https://www.drupal.org/',
      'query' => array(
        'page' => 42,
      ),
    ),
    'https://www.drupal.org/?page=42&mango=thebest' => array(
      'url' => 'https://www.drupal.org/',
      'query' => array(
        'page' => 42,
        'mango' => 'thebest',
      ),
    ),
    'https://www.drupal.org/#footer' => array(
      'url' => 'https://www.drupal.org/',
      'fragment' => 'footer',
    ),
    'https://www.drupal.org/?page=42#footer' => array(
      'url' => 'https://www.drupal.org/',
      'query' => array(
        'page' => 42,
      ),
      'fragment' => 'footer',
    ),
  );
  foreach ($urls as $url => $expected_parts) {
    $actual_parts = _link_parse_url($url);

    // First off, compare the URL segment.
    $this
      ->assertEqual($expected_parts['url'], $actual_parts['url']);

    // Secondly, compare the query string, if it was expected.
    if (isset($expected_parts['query'])) {
      $this
        ->assertTrue(isset($actual_parts['query']));
      $this
        ->assertTrue(is_array($actual_parts['query']));
      $this
        ->assertEqual(count($expected_parts['query']), count($actual_parts['query']));
      foreach ($expected_parts['query'] as $key => $val) {
        $this
          ->assertTrue(isset($actual_parts['query'][$key]));
        $this
          ->assertEqual($val, $actual_parts['query'][$key]);
      }
    }
    else {
      $this
        ->assertFalse(isset($actual_parts['query']));
    }

    // Lastly, compare the query fragment, if it was expected.
    if (isset($expected_parts['fragment'])) {
      $this
        ->assertEqual($expected_parts['fragment'], $actual_parts['fragment']);
    }
    else {
      $this
        ->assertFalse(isset($actual_parts['fragment']));
    }
  }
}