diff --git a/core/lib/Drupal/Core/Url.php b/core/lib/Drupal/Core/Url.php index 4747ed2..604d3d6 100644 --- a/core/lib/Drupal/Core/Url.php +++ b/core/lib/Drupal/Core/Url.php @@ -297,8 +297,13 @@ public function setOption($name, $value) { * * @return string * The external path. + * + * @throws \Exception */ public function getPath() { + if (!$this->isExternal()) { + throw new \Exception('Internal URLs do not have external paths.'); + } return $this->path; } diff --git a/core/tests/Drupal/Tests/Core/ExternalUrlTest.php b/core/tests/Drupal/Tests/Core/ExternalUrlTest.php index 6e7a163..f355b97 100644 --- a/core/tests/Drupal/Tests/Core/ExternalUrlTest.php +++ b/core/tests/Drupal/Tests/Core/ExternalUrlTest.php @@ -184,6 +184,17 @@ public function testGetInternalPath(Url $url) { } /** + * Tests the getPath() method. + * + * @depends testCreateFromPath + * + * @covers ::getPath() + */ + public function testGetPath(Url $url) { + $this->assertNotNull($url->getPath()); + } + + /** * Tests the getOptions() method. * * @depends testCreateFromPath diff --git a/core/tests/Drupal/Tests/Core/UrlTest.php b/core/tests/Drupal/Tests/Core/UrlTest.php index dd786dc..50ea4d3 100644 --- a/core/tests/Drupal/Tests/Core/UrlTest.php +++ b/core/tests/Drupal/Tests/Core/UrlTest.php @@ -202,6 +202,21 @@ public function testIsExternal($urls) { } /** + * Tests the getPath() method for internal URLs. + * + * @depends testCreateFromPath + * + * @expectedException \Exception + * + * @covers ::getPath() + */ + public function testGetPathForInternalUrl($urls) { + foreach ($urls as $url) { + $url->getPath(); + } + } + + /** * Tests the toString() method. * * @param \Drupal\Core\Url[] $urls