1 common.test CommonURLWebTestCase::testUrl()

Test url() with/without query, with/without fragment, absolute on/off and assert all that works when clean URLs are on and off.

File

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

Class

CommonURLWebTestCase
Tests for URL generation functions.

Code

function testUrl() {
  global $base_url;

  foreach (array(FALSE, TRUE) as $absolute) {
    // Get the expected start of the path string.
    $base = $absolute ? $base_url . '/' : base_path();
    $absolute_string = $absolute ? 'absolute' : NULL;

    // Disable Clean URLs.
    config_set('system.core', 'clean_url', 0);
    backdrop_static_reset('url');

    $url = $base . '?q=node/123';
    $result = url('node/123', array('absolute' => $absolute));
    $this->assertEqual($url, $result, "$url == $result");

    $url = $base . '?q=node/123#foo';
    $result = url('node/123', array('fragment' => 'foo', 'absolute' => $absolute));
    $this->assertEqual($url, $result, "$url == $result");

    $url = $base . '?q=node/123&foo';
    $result = url('node/123', array('query' => array('foo' => NULL), 'absolute' => $absolute));
    $this->assertEqual($url, $result, "$url == $result");

    $url = $base . '?q=node/123&foo=bar&bar=baz';
    $result = url('node/123', array('query' => array('foo' => 'bar', 'bar' => 'baz'), 'absolute' => $absolute));
    $this->assertEqual($url, $result, "$url == $result");

    $url = $base . '?q=node/123&foo#bar';
    $result = url('node/123', array('query' => array('foo' => NULL), 'fragment' => 'bar', 'absolute' => $absolute));
    $this->assertEqual($url, $result, "$url == $result");

    $url = $base . '?q=node/123&foo#0';
    $result = url('node/123', array('query' => array('foo' => NULL), 'fragment' => '0', 'absolute' => $absolute));
    $this->assertEqual($url, $result, "$url == $result");

    $url = $base . '?q=node/123&foo';
    $result = url('node/123', array('query' => array('foo' => NULL), 'fragment' => '', 'absolute' => $absolute));
    $this->assertEqual($url, $result, "$url == $result");

    $url = $base;
    $result = url('<front>', array('absolute' => $absolute));
    $this->assertEqual($url, $result, "$url == $result");

    // Enable Clean URLs.
    config_set('system.core', 'clean_url', 1);
    backdrop_static_reset('url');

    $url = $base . 'node/123';
    $result = url('node/123', array('absolute' => $absolute));
    $this->assertEqual($url, $result, "$url == $result");

    $url = $base . 'node/123#foo';
    $result = url('node/123', array('fragment' => 'foo', 'absolute' => $absolute));
    $this->assertEqual($url, $result, "$url == $result");

    $url = $base . 'node/123?foo';
    $result = url('node/123', array('query' => array('foo' => NULL), 'absolute' => $absolute));
    $this->assertEqual($url, $result, "$url == $result");

    $url = $base . 'node/123?foo=bar&bar=baz';
    $result = url('node/123', array('query' => array('foo' => 'bar', 'bar' => 'baz'), 'absolute' => $absolute));
    $this->assertEqual($url, $result, "$url == $result");

    $url = $base . 'node/123?foo#bar';
    $result = url('node/123', array('query' => array('foo' => NULL), 'fragment' => 'bar', 'absolute' => $absolute));
    $this->assertEqual($url, $result, "$url == $result");

    $url = $base;
    $result = url('<front>', array('absolute' => $absolute));
    $this->assertEqual($url, $result, "$url == $result");
  }
}