From abca8e74cde3c213ba49c208a513eb900dee9250 Mon Sep 17 00:00:00 2001
From: phenaproxima <phenaproxima@205645.no-reply.drupal.org>
Date: Thu, 22 Sep 2022 12:09:12 +0000
Subject: [PATCH] Issue #3303185 by phenaproxima, omkar.podey: Add unit test
 coverage of PathLocator::getWebRoot()

---
 package_manager/src/PathLocator.php           |  2 +-
 .../tests/src/Unit/PathLocatorTest.php        | 85 +++++++++++++++++++
 2 files changed, 86 insertions(+), 1 deletion(-)

diff --git a/package_manager/src/PathLocator.php b/package_manager/src/PathLocator.php
index ab5262f2ed..10fc43f331 100644
--- a/package_manager/src/PathLocator.php
+++ b/package_manager/src/PathLocator.php
@@ -110,7 +110,7 @@ class PathLocator {
    *   project root and Drupal root are the same.
    */
   public function getWebRoot(): string {
-    $web_root = str_replace($this->getProjectRoot(), '', $this->appRoot);
+    $web_root = str_replace(trim($this->getProjectRoot(), DIRECTORY_SEPARATOR), '', trim($this->appRoot, DIRECTORY_SEPARATOR));
     return trim($web_root, DIRECTORY_SEPARATOR);
   }
 
diff --git a/package_manager/tests/src/Unit/PathLocatorTest.php b/package_manager/tests/src/Unit/PathLocatorTest.php
index 285247ee17..1bcc0b8b1c 100644
--- a/package_manager/tests/src/Unit/PathLocatorTest.php
+++ b/package_manager/tests/src/Unit/PathLocatorTest.php
@@ -34,6 +34,91 @@ class PathLocatorTest extends UnitTestCase {
     $this->assertSame('/path/to/temp/.package_managermy_site_id', $path_locator->getStagingRoot());
   }
 
+  /**
+   * Data provider for ::testWebRoot().
+   *
+   * @return string[][]
+   *   Sets of arguments to pass to the test method.
+   */
+  public function providerWebRoot(): array {
+    // In certain sites (like those created by drupal/recommended-project), the
+    // web root is a subdirectory of the project, and exists next to the
+    // vendor directory.
+    return [
+      'recommended project' => [
+        '/path/to/project/www',
+        '/path/to/project',
+        'www',
+      ],
+      'recommended project with trailing slash on app root' => [
+        '/path/to/project/www/',
+        '/path/to/project',
+        'www',
+      ],
+      'recommended project with trailing slash on project root' => [
+        '/path/to/project/www',
+        '/path/to/project/',
+        'www',
+      ],
+      'recommended project with trailing slashes' => [
+        '/path/to/project/www/',
+        '/path/to/project/',
+        'www',
+      ],
+      // In legacy projects (i.e., created by drupal/legacy-project), the
+      // web root is the project root.
+      'legacy project' => [
+        '/path/to/drupal',
+        '/path/to/drupal',
+        '',
+      ],
+      'legacy project with trailing slash on app root' => [
+        '/path/to/drupal/',
+        '/path/to/drupal',
+        '',
+      ],
+      'legacy project with trailing slash on project root' => [
+        '/path/to/drupal',
+        '/path/to/drupal/',
+        '',
+      ],
+      'legacy project with trailing slashes' => [
+        '/path/to/drupal/',
+        '/path/to/drupal/',
+        '',
+      ],
+    ];
+  }
+
+  /**
+   * Tests that the web root is computed correctly.
+   *
+   * @param string $app_root
+   *   The absolute path of the Drupal root.
+   * @param string $project_root
+   *   The absolute path of the project root.
+   * @param string $expected_web_root
+   *   The value expected from getWebRoot().
+   *
+   * @covers ::getWebRoot
+   *
+   * @dataProvider providerWebRoot
+   */
+  public function testWebRoot(string $app_root, string $project_root, string $expected_web_root): void {
+    $path_locator = $this->getMockBuilder(PathLocator::class)
+      // Mock all methods except getWebRoot().
+      ->setMethodsExcept(['getWebRoot'])
+      ->setConstructorArgs([
+        $app_root,
+        $this->getConfigFactoryStub(),
+        $this->prophesize(FileSystemInterface::class)->reveal(),
+      ])
+      ->getMock();
+
+    $path_locator->method('getProjectRoot')->willReturn($project_root);
+    $this->assertSame($expected_web_root, $path_locator->getWebRoot());
+  }
+
   /**
    * Tests that deprecations are raised for missing constructor arguments.
    *
-- 
GitLab