1 common.test CommonURLWebTestCase::testExternalUrls()

Test external URL handling.

File

core/modules/simpletest/tests/common.test, line 204
Tests for common.inc functionality.

Class

CommonURLWebTestCase
Tests for URL generation functions.

Code

function testExternalUrls() {
  $test_url = 'https://backdropcms.org/';

  // Verify external URL can contain a fragment.
  $url = $test_url . '#backdrop';
  $result = url($url);
  $this->assertEqual($url, $result, 'External URL with fragment works without a fragment in $options.');

  // Verify fragment can be overridden in an external URL.
  $url = $test_url . '#backdrop';
  $fragment = $this->randomName(10);
  $result = url($url, array('fragment' => $fragment));
  $this->assertEqual($test_url . '#' . $fragment, $result, 'External URL fragment is overridden with a custom fragment in $options.');

  // Verify external URL can contain a query string.
  $url = $test_url . '?backdrop=awesome';
  $result = url($url);
  $this->assertEqual($url, $result, 'External URL with query string works without a query string in $options.');

  // Verify external URL can be extended with a query string.
  $url = $test_url;
  $query = array($this->randomName(5) => $this->randomName(5));
  $result = url($url, array('query' => $query));
  $this->assertEqual($url . '?' . http_build_query($query, '', '&'), $result, 'External URL can be extended with a query string in $options.');

  // Verify query string can be extended in an external URL.
  $url = $test_url . '?backdrop=awesome';
  $query = array($this->randomName(5) => $this->randomName(5));
  $result = url($url, array('query' => $query));
  $this->assertEqual($url . '&' . http_build_query($query, '', '&'), $result, 'External URL query string can be extended with a custom query string in $options.');

  // Verify that an internal URL does not result in an external URL without
  // protocol part.
  $url = '/backdropcms.org';
  $result = url($url);
  $this->assertTrue(strpos($result, '//') === FALSE, 'Internal URL does not turn into an external URL.');

  // Verify that an external URL without protocol part is recognized as such.
  $url = '//backdropcms.org';
  $result = url($url);
  $this->assertEqual($url, $result, 'External URL without protocol is not altered.');
}