diff --git a/core/modules/path_alias/src/AliasManager.php b/core/modules/path_alias/src/AliasManager.php index 56670f1e89627fd90cf7842bd7e090ad4456f1bc..8651a767c96a105147f4dd31b36d11e9ac984ba0 100644 --- a/core/modules/path_alias/src/AliasManager.php +++ b/core/modules/path_alias/src/AliasManager.php @@ -194,7 +194,7 @@ public function getPathByAlias($alias, $langcode = NULL) { * {@inheritdoc} */ public function getAliasByPath($path, $langcode = NULL) { - if ($path[0] !== '/') { + if (!str_starts_with($path, '/')) { throw new \InvalidArgumentException(sprintf('Source path %s has to start with a slash.', $path)); } // If no language is explicitly specified we default to the current URL diff --git a/core/modules/path_alias/tests/src/Unit/AliasManagerTest.php b/core/modules/path_alias/tests/src/Unit/AliasManagerTest.php index 8a852787f56989001c6579ca56d84ab3c6f17a4d..fd9234cf551af78cbc6e5f00056e893cf2057d05 100644 --- a/core/modules/path_alias/tests/src/Unit/AliasManagerTest.php +++ b/core/modules/path_alias/tests/src/Unit/AliasManagerTest.php @@ -210,6 +210,16 @@ public function testGetAliasByPathNoMatch() { $this->aliasManager->writeCache(); } + /** + * Tests the getAliasByPath method exception. + * + * @covers ::getAliasByPath + */ + public function testGetAliasByPathException(): void { + $this->expectException(\InvalidArgumentException::class); + $this->aliasManager->getAliasByPath('no-leading-slash-here'); + } + /** * Tests the getAliasByPath method for a path that has a matching alias. *