diff --git a/core/lib/Drupal/Core/Url.php b/core/lib/Drupal/Core/Url.php index 9f78645..8c7b0df 100644 --- a/core/lib/Drupal/Core/Url.php +++ b/core/lib/Drupal/Core/Url.php @@ -452,6 +452,13 @@ public function toString() { } /** + * Implements __toString magic method for conversion into a string. + */ + public function __toString() { + return $this->toString(); + } + + /** * Returns all the information about the route. * * @return array diff --git a/core/tests/Drupal/Tests/Core/UrlTest.php b/core/tests/Drupal/Tests/Core/UrlTest.php index 404702e..119eee6 100644 --- a/core/tests/Drupal/Tests/Core/UrlTest.php +++ b/core/tests/Drupal/Tests/Core/UrlTest.php @@ -235,6 +235,24 @@ public function testToString($urls) { } /** + * Tests the __toString() method. + * + * @param \Drupal\Core\Url[] $urls + * An array of Url objects. + * + * @depends testUrlFromRequest + * + * @covers ::__toString + */ + public function testMagicToString($urls) { + foreach ($urls as $index => $url) { + $url->setUrlGenerator(\Drupal::urlGenerator()); + $path = array_pop($this->map[$index]); + $this->assertSame($path, (string) $url); + } + } + + /** * Tests the toArray() method. * * @param \Drupal\Core\Url[] $urls