From 29fcd9583fb585f90d66c81cae9bc0627c727ead Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Fri, 29 Mar 2024 14:21:02 +0000
Subject: [PATCH] Issue #2514196 by pameeela, Aki Tendo, Lendude, lauriii:
 AliasManager->getAliasByPath() handles $path in confusing manner

---
 core/modules/path_alias/src/AliasManager.php           |  2 +-
 .../path_alias/tests/src/Unit/AliasManagerTest.php     | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/core/modules/path_alias/src/AliasManager.php b/core/modules/path_alias/src/AliasManager.php
index 56670f1e8962..8651a767c96a 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 8a852787f569..fd9234cf551a 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.
    *
-- 
GitLab