diff --git a/app/code/Magento/MediaGallery/Model/Directory/Command/CreateByPaths.php b/app/code/Magento/MediaGallery/Model/Directory/Command/CreateByPaths.php index bfb6b677ce19c..4d87c1aa95285 100644 --- a/app/code/Magento/MediaGallery/Model/Directory/Command/CreateByPaths.php +++ b/app/code/Magento/MediaGallery/Model/Directory/Command/CreateByPaths.php @@ -63,7 +63,7 @@ public function execute(array $paths): void //phpcs:ignore Magento2.Functions.DiscouragedFunction $name = basename($path); //phpcs:ignore Magento2.Functions.DiscouragedFunction - $folder = str_replace($name, '', $path); + $folder = substr($path, 0, strrpos($path, $name)); $this->storage->createDirectory( $name, diff --git a/dev/tests/integration/testsuite/Magento/MediaGallery/Model/Directory/Command/CreateByPathsTest.php b/dev/tests/integration/testsuite/Magento/MediaGallery/Model/Directory/Command/CreateByPathsTest.php index 3511cdec532fa..f1a9272c2b13c 100644 --- a/dev/tests/integration/testsuite/Magento/MediaGallery/Model/Directory/Command/CreateByPathsTest.php +++ b/dev/tests/integration/testsuite/Magento/MediaGallery/Model/Directory/Command/CreateByPathsTest.php @@ -94,10 +94,24 @@ public function notAllowedPathsProvider(): array ]; } + /** + * Test create child directory with the same name as parent + */ + public function testCreateChildDirectoryTheSameNameAsParentDirectory(): void + { + $dir = self::TEST_DIRECTORY_NAME; + $childPath = $dir . '/' . $dir; + + $this->createByPaths->execute([$dir]); + $this->assertFileExists($this->mediaDirectoryPath . $dir); + $this->createByPaths->execute([$childPath]); + $this->assertFileExists($this->mediaDirectoryPath . $childPath); + } + /** * @throws \Magento\Framework\Exception\FileSystemException */ - public static function tearDownAfterClass() + protected function tearDown() { $filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() ->get(\Magento\Framework\Filesystem::class);