diff --git a/app/code/Magento/Cms/Block/Adminhtml/Wysiwyg/Images/Tree.php b/app/code/Magento/Cms/Block/Adminhtml/Wysiwyg/Images/Tree.php
index c033e09ca8db0..028a1a4327621 100644
--- a/app/code/Magento/Cms/Block/Adminhtml/Wysiwyg/Images/Tree.php
+++ b/app/code/Magento/Cms/Block/Adminhtml/Wysiwyg/Images/Tree.php
@@ -76,8 +76,7 @@ public function getTreeJson()
'path' => substr($item->getFilename(), strlen($storageRoot)),
'cls' => 'folder',
];
- $nestedDirectories = $this->getMediaDirectory()->readRecursively($item->getFilename());
- $hasNestedDirectories = count($nestedDirectories) > 0;
+ $hasNestedDirectories = $this->hasNestedDirectories($storageRoot, $item->getFilename());
// if no nested directories inside dir, add 'leaf' state so that jstree hides dropdown arrow next to dir
if (!$hasNestedDirectories) {
@@ -89,6 +88,26 @@ public function getTreeJson()
return $this->serializer->serialize($jsonArray);
}
+ /**
+ * Check if directory has nested directories
+ *
+ * @param string $storageRoot
+ * @param string $fileName
+ * @return bool
+ */
+ private function hasNestedDirectories(string $storageRoot, string $fileName): bool
+ {
+ $pathList = $this->getMediaDirectory()->read($fileName);
+ foreach ($pathList as $directoryPath) {
+ $file = $this->_filesystem->getDirectoryReadByPath($storageRoot . $directoryPath);
+ if ($file->isDirectory()) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
/**
* Json source URL
*
diff --git a/app/code/Magento/Cms/Test/Unit/Block/Adminhtml/Wysiwyg/Images/TreeTest.php b/app/code/Magento/Cms/Test/Unit/Block/Adminhtml/Wysiwyg/Images/TreeTest.php
new file mode 100644
index 0000000000000..5fde8d946835a
--- /dev/null
+++ b/app/code/Magento/Cms/Test/Unit/Block/Adminhtml/Wysiwyg/Images/TreeTest.php
@@ -0,0 +1,126 @@
+createMock(Context::class);
+ $this->cmsWysiwygImagesMock = $this->createMock(Images::class);
+ $this->coreRegistryMock = $this->createMock(Registry::class);
+ $serializerMock = $this->createMock(Json::class);
+ $this->imagesStorageMock = $this->createMock(Storage::class);
+
+ $this->directoryMock = $this->getMockBuilder(Read::class)
+ ->disableOriginalConstructor()
+ ->onlyMethods(['getRelativePath', 'isDirectory', 'getAbsolutePath', 'read'])
+ ->getMock();
+ $this->fileSystemMock = $this->createMock(Filesystem::class);
+ $this->fileSystemMock->method('getDirectoryRead')
+ ->with(DirectoryList::MEDIA)
+ ->willReturn($this->directoryMock);
+
+ $this->model = $objectManager->getObject(
+ Tree::class,
+ [
+ 'context' => $contextMock,
+ 'cmsWysiwygImages' => $this->cmsWysiwygImagesMock,
+ 'registry' => $this->coreRegistryMock,
+ 'serializer' => $serializerMock,
+ '_filesystem' => $this->fileSystemMock
+ ]
+ );
+ }
+
+ /**
+ * Test execute for get directories tree
+ *
+ * @return void
+ */
+ public function testGetTreeJson(): void
+ {
+ $collection = [];
+ $this->cmsWysiwygImagesMock->method('getStorageRoot')
+ ->willReturn('/storage/root/dir/');
+ $this->cmsWysiwygImagesMock->method('getCurrentPath')
+ ->willReturn('/storage/root/dir/pub/media/');
+ $fileNames = ['fileName'];
+ foreach ($fileNames as $filename) {
+ /** @var DataObject|MockObject $objectMock */
+ $objectMock = $this->getMockBuilder(DataObject::class)
+ ->addMethods(['getFilename'])
+ ->disableOriginalConstructor()
+ ->getMock();
+ $objectMock->method('getFilename')
+ ->willReturn('/storage/root/dir/' . $filename);
+ $collection[] = $objectMock;
+ }
+ //items for collection
+ $iterator = new \ArrayIterator($collection);
+ $this->imagesStorageMock->method('getDirsCollection')
+ ->willReturn($iterator);
+ $this->coreRegistryMock->method('registry')->willReturn($this->imagesStorageMock);
+ $this->directoryMock->method('read')->willReturn($fileNames);
+ $this->fileSystemMock->expects($this->once())
+ ->method('getDirectoryReadByPath')
+ ->willReturn($this->directoryMock);
+ $this->model->getTreeJson();
+ }
+}