diff --git a/core/modules/node/src/Tests/NodeViewTest.php b/core/modules/node/src/Tests/NodeViewTest.php index 426fec1..18f7133 100644 --- a/core/modules/node/src/Tests/NodeViewTest.php +++ b/core/modules/node/src/Tests/NodeViewTest.php @@ -7,6 +7,8 @@ namespace Drupal\node\Tests; +use Drupal\Core\Url; + /** * Tests the node/{node} page. * @@ -24,13 +26,13 @@ public function testHtmlHeadLinks() { $this->drupalGet($node->getSystemPath()); $result = $this->xpath('//link[@rel = "version-history"]'); - $this->assertEqual($result[0]['href'], _url("node/{$node->id()}/revisions")); + $this->assertEqual($result[0]['href'], Url::fromUri("base://node/{$node->id()}/revisions")->toString()); $result = $this->xpath('//link[@rel = "edit-form"]'); - $this->assertEqual($result[0]['href'], _url("node/{$node->id()}/edit")); + $this->assertEqual($result[0]['href'], Url::fromUri("base://node/{$node->id()}/edit")->toString()); $result = $this->xpath('//link[@rel = "canonical"]'); - $this->assertEqual($result[0]['href'], _url("node/{$node->id()}")); + $this->assertEqual($result[0]['href'], Url::fromUri("base://node/{$node->id()}")->toString()); } } diff --git a/core/modules/rest/src/Tests/AuthTest.php b/core/modules/rest/src/Tests/AuthTest.php index bf51c00..b461979 100644 --- a/core/modules/rest/src/Tests/AuthTest.php +++ b/core/modules/rest/src/Tests/AuthTest.php @@ -7,6 +7,7 @@ namespace Drupal\rest\Tests; +use Drupal\Core\Url; use Drupal\rest\Tests\RESTTestBase; /** @@ -87,7 +88,7 @@ protected function basicAuthGet($path, $username, $password) { $out = $this->curlExec( array( CURLOPT_HTTPGET => TRUE, - CURLOPT_URL => _url($path, array('absolute' => TRUE)), + CURLOPT_URL => Url::fromUri('base://' . $path, array('absolute' => TRUE))->toString(), CURLOPT_NOBODY => FALSE, CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => $username . ':' . $password, diff --git a/core/modules/rest/src/Tests/CsrfTest.php b/core/modules/rest/src/Tests/CsrfTest.php index 7ff6389..f087b65 100644 --- a/core/modules/rest/src/Tests/CsrfTest.php +++ b/core/modules/rest/src/Tests/CsrfTest.php @@ -5,6 +5,8 @@ namespace Drupal\rest\Tests; +use Drupal\Core\Url; + /** * Tests the CSRF protection. * @@ -107,7 +109,7 @@ protected function getCurlOptions() { CURLOPT_HTTPGET => FALSE, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $this->serialized, - CURLOPT_URL => _url('entity/' . $this->testEntityType, array('absolute' => TRUE)), + CURLOPT_URL => Url::fromUri('base://entity/' . $this->testEntityType, array('absolute' => TRUE))->toString(), CURLOPT_NOBODY => FALSE, CURLOPT_HTTPHEADER => array( "Content-Type: {$this->defaultMimeType}", diff --git a/core/modules/rest/src/Tests/NodeTest.php b/core/modules/rest/src/Tests/NodeTest.php index 71d4c33..48e6ab2 100644 --- a/core/modules/rest/src/Tests/NodeTest.php +++ b/core/modules/rest/src/Tests/NodeTest.php @@ -7,6 +7,7 @@ namespace Drupal\rest\Tests; +use Drupal\Core\Url; use Drupal\rest\Tests\RESTTestBase; /** @@ -69,7 +70,7 @@ public function testNodes() { $data = array( '_links' => array( 'type' => array( - 'href' => _url('rest/type/node/resttest', array('absolute' => TRUE)), + 'href' => Url::fromUri('base://rest/type/node/resttest', array('absolute' => TRUE))->toString(), ), ), 'title' => array( diff --git a/core/modules/rest/src/Tests/RESTTestBase.php b/core/modules/rest/src/Tests/RESTTestBase.php index 2fa0331..9f453d9 100644 --- a/core/modules/rest/src/Tests/RESTTestBase.php +++ b/core/modules/rest/src/Tests/RESTTestBase.php @@ -8,6 +8,7 @@ namespace Drupal\rest\Tests; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\Url; use Drupal\simpletest\WebTestBase; /** @@ -62,7 +63,7 @@ protected function setUp() { /** * Helper function to issue a HTTP request with simpletest's cURL. * - * @param string $url + * @param string $path * The relative URL, e.g. "entity/node/1" * @param string $method * HTTP method, one of GET, POST, PUT or DELETE. @@ -71,7 +72,7 @@ protected function setUp() { * @param string $mime_type * The MIME type of the transmitted content. */ - protected function httpRequest($url, $method, $body = NULL, $mime_type = NULL) { + protected function httpRequest($path, $method, $body = NULL, $mime_type = NULL) { if (!isset($mime_type)) { $mime_type = $this->defaultMimeType; } @@ -79,14 +80,19 @@ protected function httpRequest($url, $method, $body = NULL, $mime_type = NULL) { // GET the CSRF token first for writing requests. $token = $this->drupalGet('rest/session/token'); } + + $options = array('absolute' =>TRUE); + switch ($method) { case 'GET': // Set query if there are additional GET parameters. - $options = isset($body) ? array('absolute' => TRUE, 'query' => $body) : array('absolute' => TRUE); + if (isset($body)) { + $options['query'] = $body; + } $curl_options = array( CURLOPT_HTTPGET => TRUE, CURLOPT_CUSTOMREQUEST => 'GET', - CURLOPT_URL => _url($url, $options), + CURLOPT_URL => $this->container->get('url_generator')->generateFromPath($path, $options), CURLOPT_NOBODY => FALSE, CURLOPT_HTTPHEADER => array('Accept: ' . $mime_type), ); @@ -97,7 +103,7 @@ protected function httpRequest($url, $method, $body = NULL, $mime_type = NULL) { CURLOPT_HTTPGET => FALSE, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $body, - CURLOPT_URL => _url($url, array('absolute' => TRUE)), + CURLOPT_URL => $this->container->get('url_generator')->generateFromPath($path, $options), CURLOPT_NOBODY => FALSE, CURLOPT_HTTPHEADER => array( 'Content-Type: ' . $mime_type, @@ -111,7 +117,7 @@ protected function httpRequest($url, $method, $body = NULL, $mime_type = NULL) { CURLOPT_HTTPGET => FALSE, CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_POSTFIELDS => $body, - CURLOPT_URL => _url($url, array('absolute' => TRUE)), + CURLOPT_URL => $this->container->get('url_generator')->generateFromPath($path, $options), CURLOPT_NOBODY => FALSE, CURLOPT_HTTPHEADER => array( 'Content-Type: ' . $mime_type, @@ -125,7 +131,7 @@ protected function httpRequest($url, $method, $body = NULL, $mime_type = NULL) { CURLOPT_HTTPGET => FALSE, CURLOPT_CUSTOMREQUEST => 'PATCH', CURLOPT_POSTFIELDS => $body, - CURLOPT_URL => _url($url, array('absolute' => TRUE)), + CURLOPT_URL => $this->container->get('url_generator')->generateFromPath($path, $options), CURLOPT_NOBODY => FALSE, CURLOPT_HTTPHEADER => array( 'Content-Type: ' . $mime_type, @@ -138,7 +144,7 @@ protected function httpRequest($url, $method, $body = NULL, $mime_type = NULL) { $curl_options = array( CURLOPT_HTTPGET => FALSE, CURLOPT_CUSTOMREQUEST => 'DELETE', - CURLOPT_URL => _url($url, array('absolute' => TRUE)), + CURLOPT_URL => $this->container->get('url_generator')->generateFromPath($path, $options), CURLOPT_NOBODY => FALSE, CURLOPT_HTTPHEADER => array('X-CSRF-Token: ' . $token), ); @@ -149,7 +155,7 @@ protected function httpRequest($url, $method, $body = NULL, $mime_type = NULL) { $headers = $this->drupalGetHeaders(); $headers = implode("\n", $headers); - $this->verbose($method . ' request to: ' . $url . + $this->verbose($method . ' request to: ' . $path . '
Code: ' . curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE) . '
Response headers: ' . $headers . '
Response body: ' . $response); diff --git a/core/modules/serialization/src/Tests/EntityResolverTest.php b/core/modules/serialization/src/Tests/EntityResolverTest.php index 2bf4ed9..92242fd 100644 --- a/core/modules/serialization/src/Tests/EntityResolverTest.php +++ b/core/modules/serialization/src/Tests/EntityResolverTest.php @@ -6,6 +6,8 @@ namespace Drupal\serialization\Tests; +use Drupal\Core\Url; + /** * Tests that entities references can be resolved. * @@ -58,16 +60,16 @@ function testUuidEntityResolver() { $entity->set('field_test_entity_reference', array(array('target_id' => 1))); $entity->save(); - $field_uri = _url('rest/relation/entity_test_mulrev/entity_test_mulrev/field_test_entity_reference', array('absolute' => TRUE)); + $field_uri = Url::fromUri('base://rest/relation/entity_test_mulrev/entity_test_mulrev/field_test_entity_reference', array('absolute' => TRUE))->toString(); $data = array( '_links' => array( 'type' => array( - 'href' => _url('rest/type/entity_test_mulrev/entity_test_mulrev', array('absolute' => TRUE)), + 'href' => Url::fromUri('base://rest/type/entity_test_mulrev/entity_test_mulrev', array('absolute' => TRUE))->toString(), ), $field_uri => array( array( - 'href' => _url('entity/entity_test_mulrev/' . $entity->id()), + 'href' => Url::fromUri('base://entity/entity_test_mulrev/' . $entity->id())->toString(), ), ), ), @@ -75,7 +77,7 @@ function testUuidEntityResolver() { $field_uri => array( array( '_links' => array( - 'self' => _url('entity/entity_test_mulrev/' . $entity->id()), + 'self' => Url::fromUri('base://entity/entity_test_mulrev/' . $entity->id())->toString(), ), 'uuid' => array( array( diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php index d2b31ae..59f2edf 100644 --- a/core/modules/simpletest/src/WebTestBase.php +++ b/core/modules/simpletest/src/WebTestBase.php @@ -26,6 +26,7 @@ use Drupal\Core\Site\Settings; use Drupal\Core\StreamWrapper\PublicStream; use Drupal\Core\Datetime\DrupalDateTime; +use Drupal\Core\Url; use Drupal\block\Entity\Block; use Symfony\Component\HttpFoundation\Request; use Drupal\user\Entity\Role; @@ -1948,11 +1949,22 @@ protected function drupalProcessAjaxResponse($content, array $ajax_response, arr * * @see WebTestBase::getAjaxPageStatePostData() * @see WebTestBase::curlExec() - * @see _url() + * @see \Drupal\Core\Url::fromUri() */ protected function drupalPost($path, $accept, array $post, $options = array()) { + $options['absolute'] = TRUE; + + // The URL generator service is not necessarily available yet; e.g., in + // interactive installer tests. + if ($this->container->has('url_generator')) { + $url = $this->container->get('url_generator')->generateFromPath($path, $options); + } + else { + $url = $this->getAbsoluteUrl($path); + } + return $this->curlExec(array( - CURLOPT_URL => _url($path, $options + array('absolute' => TRUE)), + CURLOPT_URL => $url, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $this->serializePostValues($post), CURLOPT_HTTPHEADER => array( diff --git a/core/modules/system/src/Tests/Cache/PageCacheTagsTestBase.php b/core/modules/system/src/Tests/Cache/PageCacheTagsTestBase.php index 1da3a56..48f7e52 100644 --- a/core/modules/system/src/Tests/Cache/PageCacheTagsTestBase.php +++ b/core/modules/system/src/Tests/Cache/PageCacheTagsTestBase.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\Cache; +use Drupal\Core\Url; use Drupal\simpletest\WebTestBase; use Drupal\Component\Utility\String; @@ -53,7 +54,7 @@ protected function verifyPageCache($path, $hit_or_miss, $tags = FALSE) { $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), $hit_or_miss, $message); if ($hit_or_miss === 'HIT' && is_array($tags)) { - $cid_parts = array(_url($path, array('absolute' => TRUE)), 'html'); + $cid_parts = array(Url::fromUri('base://' . $path, array('absolute' => TRUE))->toString(), 'html'); $cid = implode(':', $cid_parts); $cache_entry = \Drupal::cache('render')->get($cid); sort($cache_entry->tags); diff --git a/core/modules/system/src/Tests/Common/UrlTest.php b/core/modules/system/src/Tests/Common/UrlTest.php index efc01c6..ad9ce96 100644 --- a/core/modules/system/src/Tests/Common/UrlTest.php +++ b/core/modules/system/src/Tests/Common/UrlTest.php @@ -36,12 +36,12 @@ function testLinkXSS() { $text = $this->randomMachineName(); $path = ""; $link = _l($text, $path); - $sanitized_path = check_url(_url($path)); + $sanitized_path = check_url(Url::fromUri('base://' . $path)->toString()); $this->assertTrue(strpos($link, $sanitized_path) !== FALSE, format_string('XSS attack @path was filtered by _l().', array('@path' => $path))); // Test _url(). - $link = _url($path); - $sanitized_path = check_url(_url($path)); + $link = Url::fromUri('base://' . $path)->toString(); + $sanitized_path = check_url(Url::fromUri('base://' . $path)->toString()); $this->assertTrue(strpos($link, $sanitized_path) !== FALSE, format_string('XSS attack @path was filtered by #theme', ['@path' => $path])); } @@ -257,30 +257,30 @@ function testExternalUrls() { // Verify external URL can contain a fragment. $url = $test_url . '#drupal'; - $result = _url($url); + $result = Url::fromUri($url)->toString(); $this->assertEqual($url, $result, 'External URL with fragment works without a fragment in $options.'); // Verify fragment can be overidden in an external URL. $url = $test_url . '#drupal'; $fragment = $this->randomMachineName(10); - $result = _url($url, array('fragment' => $fragment)); + $result = Url::fromUri($url, array('fragment' => $fragment))->toString(); $this->assertEqual($test_url . '#' . $fragment, $result, 'External URL fragment is overidden with a custom fragment in $options.'); // Verify external URL can contain a query string. $url = $test_url . '?drupal=awesome'; - $result = _url($url); + $result = Url::fromUri($url)->toString(); $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->randomMachineName(5) => $this->randomMachineName(5)); - $result = _url($url, array('query' => $query)); + $result = Url::fromUri($url, array('query' => $query))->toString(); $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 . '?drupal=awesome'; $query = array($this->randomMachineName(5) => $this->randomMachineName(5)); - $result = _url($url, array('query' => $query)); + $result = Url::fromUri($url, array('query' => $query))->toString(); $this->assertEqual($url . '&' . http_build_query($query, '', '&'), $result, 'External URL query string can be extended with a custom query string in $options.'); } } diff --git a/core/modules/system/src/Tests/Menu/LocalTasksTest.php b/core/modules/system/src/Tests/Menu/LocalTasksTest.php index fc61315..37b44d8 100644 --- a/core/modules/system/src/Tests/Menu/LocalTasksTest.php +++ b/core/modules/system/src/Tests/Menu/LocalTasksTest.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\Menu; +use Drupal\Core\Url; use Drupal\simpletest\WebTestBase; /** @@ -34,7 +35,7 @@ protected function assertLocalTasks(array $hrefs, $level = 0) { )); $this->assertTrue(count($elements), 'Local tasks found.'); foreach ($hrefs as $index => $element) { - $expected = _url($hrefs[$index]); + $expected = Url::fromUri('base://' . $hrefs[$index])->toString(); $method = ($elements[$index]['href'] == $expected ? 'pass' : 'fail'); $this->{$method}(format_string('Task @number href @value equals @expected.', array( '@number' => $index + 1, diff --git a/core/modules/system/src/Tests/Menu/MenuTestBase.php b/core/modules/system/src/Tests/Menu/MenuTestBase.php index 2478af3..4cb9fda 100644 --- a/core/modules/system/src/Tests/Menu/MenuTestBase.php +++ b/core/modules/system/src/Tests/Menu/MenuTestBase.php @@ -8,6 +8,7 @@ namespace Drupal\system\Tests\Menu; use Drupal\Component\Utility\String; +use Drupal\Core\Url; use Drupal\simpletest\WebTestBase; abstract class MenuTestBase extends WebTestBase { @@ -64,7 +65,12 @@ protected function assertBreadcrumbParts($trail) { // this test would go into an infinite loop, so we need to check that too. while ($trail && !empty($parts)) { foreach ($trail as $path => $title) { - $url = _url($path); + if ($path == '') { + $url = Url::fromRoute('')->toString(); + } + else { + $url = Url::fromUri('base://' . $path)->toString(); + } $part = array_shift($parts); $pass = ($pass && $part['href'] === $url && $part['text'] === String::checkPlain($title)); } @@ -101,7 +107,7 @@ protected function assertMenuActiveTrail($tree, $last_active) { $part_xpath .= 'li[contains(@class, :class)]/a[contains(@href, :href) and contains(text(), :title)]'; $part_args = array( ':class' => 'active-trail', - ':href' => _url($link_path), + ':href' => Url::fromUri('base://' . $link_path)->toString(), ':title' => $link_title, ); $xpath .= $this->buildXPathQuery($part_xpath, $part_args); @@ -121,7 +127,7 @@ protected function assertMenuActiveTrail($tree, $last_active) { $args = array( ':class-trail' => 'active-trail', ':class-active' => 'active', - ':href' => _url($active_link_path), + ':href' => Url::fromUri('base://' . $active_link_path)->toString(), ':title' => $active_link_title, ); $elements = $this->xpath($xpath, $args); diff --git a/core/modules/update/src/Tests/UpdateCoreTest.php b/core/modules/update/src/Tests/UpdateCoreTest.php index 6d32245..637949d 100644 --- a/core/modules/update/src/Tests/UpdateCoreTest.php +++ b/core/modules/update/src/Tests/UpdateCoreTest.php @@ -190,7 +190,7 @@ function testDatestampMismatch() { function testModulePageRunCron() { $this->setSystemInfo('8.0.0'); \Drupal::config('update.settings') - ->set('fetch.url', _url('update-test', array('absolute' => TRUE))) + ->set('fetch.url', Url::fromUri('base://update-test', array('absolute' => TRUE))->toString()) ->save(); \Drupal::config('update_test.settings') ->set('xml_map', array('drupal' => '0.0')) @@ -208,7 +208,7 @@ function testModulePageUpToDate() { $this->setSystemInfo('8.0.0'); // Instead of using refreshUpdateStatus(), set these manually. \Drupal::config('update.settings') - ->set('fetch.url', _url('update-test', array('absolute' => TRUE))) + ->set('fetch.url', Url::fromUri('base://update-test', array('absolute' => TRUE))->toString()) ->save(); \Drupal::config('update_test.settings') ->set('xml_map', array('drupal' => '0.0')) @@ -229,7 +229,7 @@ function testModulePageRegularUpdate() { $this->setSystemInfo('8.0.0'); // Instead of using refreshUpdateStatus(), set these manually. \Drupal::config('update.settings') - ->set('fetch.url', _url('update-test', array('absolute' => TRUE))) + ->set('fetch.url', Url::fromUri('base://update-test', array('absolute' => TRUE))->toString()) ->save(); \Drupal::config('update_test.settings') ->set('xml_map', array('drupal' => '0.1')) @@ -250,7 +250,7 @@ function testModulePageSecurityUpdate() { $this->setSystemInfo('8.0.0'); // Instead of using refreshUpdateStatus(), set these manually. \Drupal::config('update.settings') - ->set('fetch.url', _url('update-test', array('absolute' => TRUE))) + ->set('fetch.url', Url::fromUri('base://update-test', array('absolute' => TRUE))->toString()) ->save(); \Drupal::config('update_test.settings') ->set('xml_map', array('drupal' => '0.2-sec')) @@ -325,7 +325,7 @@ function testLanguageModuleUpdate() { $this->setSystemInfo('8.0.0'); // Instead of using refreshUpdateStatus(), set these manually. \Drupal::config('update.settings') - ->set('fetch.url', _url('update-test', array('absolute' => TRUE))) + ->set('fetch.url', Url::fromUri('base://update-test', array('absolute' => TRUE))->toString()) ->save(); \Drupal::config('update_test.settings') ->set('xml_map', array('drupal' => '0.1')) diff --git a/core/modules/update/src/Tests/UpdateTestBase.php b/core/modules/update/src/Tests/UpdateTestBase.php index 0025e21..b4439d2 100644 --- a/core/modules/update/src/Tests/UpdateTestBase.php +++ b/core/modules/update/src/Tests/UpdateTestBase.php @@ -44,7 +44,7 @@ protected function refreshUpdateStatus($xml_map, $url = 'update-test') { // Tell the Update Manager module to fetch from the URL provided by // update_test module. - \Drupal::config('update.settings')->set('fetch.url', _url($url, array('absolute' => TRUE)))->save(); + \Drupal::config('update.settings')->set('fetch.url', Url::fromUri('base://' . $url, array('absolute' => TRUE))->toString())->save(); // Save the map for update_test_mock_page() to use. \Drupal::config('update_test.settings')->set('xml_map', $xml_map)->save(); // Manually check the update status. diff --git a/core/modules/update/src/Tests/UpdateUploadTest.php b/core/modules/update/src/Tests/UpdateUploadTest.php index 78a9cbd..0f57c02 100644 --- a/core/modules/update/src/Tests/UpdateUploadTest.php +++ b/core/modules/update/src/Tests/UpdateUploadTest.php @@ -7,6 +7,8 @@ namespace Drupal\update\Tests; +use Drupal\Core\Url; + /** * Tests the Update Manager module's upload and extraction functionality. * @@ -80,7 +82,7 @@ function testUpdateManagerCoreSecurityUpdateMessages() { ->set('xml_map', array('drupal' => '0.2-sec')) ->save(); \Drupal::config('update.settings') - ->set('fetch.url', _url('update-test', array('absolute' => TRUE))) + ->set('fetch.url', Url::fromUri('base://update-test', array('absolute' => TRUE))->toString()) ->save(); // Initialize the update status. $this->drupalGet('admin/reports/updates');