Skip to content

Commit a173699

Browse files
author
Serhii Balko
committed
Merge remote-tracking branch 'origin/MC-41854' into 2.4-develop-pr59
2 parents 5f05166 + cc509d0 commit a173699

File tree

2 files changed

+147
-2
lines changed
  • app/code/Magento/Cms

2 files changed

+147
-2
lines changed

app/code/Magento/Cms/Block/Adminhtml/Wysiwyg/Images/Tree.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ public function getTreeJson()
7676
'path' => substr($item->getFilename(), strlen($storageRoot)),
7777
'cls' => 'folder',
7878
];
79-
$nestedDirectories = $this->getMediaDirectory()->readRecursively($item->getFilename());
80-
$hasNestedDirectories = count($nestedDirectories) > 0;
79+
$hasNestedDirectories = $this->hasNestedDirectories($storageRoot, $item->getFilename());
8180

8281
// if no nested directories inside dir, add 'leaf' state so that jstree hides dropdown arrow next to dir
8382
if (!$hasNestedDirectories) {
@@ -89,6 +88,26 @@ public function getTreeJson()
8988
return $this->serializer->serialize($jsonArray);
9089
}
9190

91+
/**
92+
* Check if directory has nested directories
93+
*
94+
* @param string $storageRoot
95+
* @param string $fileName
96+
* @return bool
97+
*/
98+
private function hasNestedDirectories(string $storageRoot, string $fileName): bool
99+
{
100+
$pathList = $this->getMediaDirectory()->read($fileName);
101+
foreach ($pathList as $directoryPath) {
102+
$file = $this->_filesystem->getDirectoryReadByPath($storageRoot . $directoryPath);
103+
if ($file->isDirectory()) {
104+
return true;
105+
}
106+
}
107+
108+
return false;
109+
}
110+
92111
/**
93112
* Json source URL
94113
*
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Cms\Test\Unit\Block\Adminhtml\Wysiwyg\Images;
9+
10+
use Magento\Backend\Block\Template\Context;
11+
use Magento\Cms\Block\Adminhtml\Wysiwyg\Images\Tree;
12+
use Magento\Cms\Helper\Wysiwyg\Images;
13+
use Magento\Cms\Model\Wysiwyg\Images\Storage;
14+
use Magento\Framework\App\Filesystem\DirectoryList;
15+
use Magento\Framework\DataObject;
16+
use Magento\Framework\Filesystem;
17+
use Magento\Framework\Filesystem\Directory\Read;
18+
use Magento\Framework\Registry;
19+
use Magento\Framework\Serialize\Serializer\Json;
20+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
21+
use PHPUnit\Framework\MockObject\MockObject;
22+
use PHPUnit\Framework\TestCase;
23+
24+
/**
25+
* Get tree json test.
26+
*/
27+
class TreeTest extends TestCase
28+
{
29+
/**
30+
* @var Tree
31+
*/
32+
private $model;
33+
34+
/**
35+
* @var Registry|MockObject
36+
*/
37+
private $coreRegistryMock;
38+
39+
/**
40+
* @var Images|MockObject
41+
*/
42+
private $cmsWysiwygImagesMock;
43+
44+
/**
45+
* @var Storage|MockObject
46+
*/
47+
private $imagesStorageMock;
48+
49+
/**
50+
* @var Read|MockObject
51+
*/
52+
private $directoryMock;
53+
54+
/**
55+
* @var Filesystem|MockObject
56+
*/
57+
private $fileSystemMock;
58+
59+
/**
60+
* @inheritdoc
61+
*/
62+
protected function setUp(): void
63+
{
64+
$objectManager = new ObjectManager($this);
65+
$contextMock = $this->createMock(Context::class);
66+
$this->cmsWysiwygImagesMock = $this->createMock(Images::class);
67+
$this->coreRegistryMock = $this->createMock(Registry::class);
68+
$serializerMock = $this->createMock(Json::class);
69+
$this->imagesStorageMock = $this->createMock(Storage::class);
70+
71+
$this->directoryMock = $this->getMockBuilder(Read::class)
72+
->disableOriginalConstructor()
73+
->onlyMethods(['getRelativePath', 'isDirectory', 'getAbsolutePath', 'read'])
74+
->getMock();
75+
$this->fileSystemMock = $this->createMock(Filesystem::class);
76+
$this->fileSystemMock->method('getDirectoryRead')
77+
->with(DirectoryList::MEDIA)
78+
->willReturn($this->directoryMock);
79+
80+
$this->model = $objectManager->getObject(
81+
Tree::class,
82+
[
83+
'context' => $contextMock,
84+
'cmsWysiwygImages' => $this->cmsWysiwygImagesMock,
85+
'registry' => $this->coreRegistryMock,
86+
'serializer' => $serializerMock,
87+
'_filesystem' => $this->fileSystemMock
88+
]
89+
);
90+
}
91+
92+
/**
93+
* Test execute for get directories tree
94+
*
95+
* @return void
96+
*/
97+
public function testGetTreeJson(): void
98+
{
99+
$collection = [];
100+
$this->cmsWysiwygImagesMock->method('getStorageRoot')
101+
->willReturn('/storage/root/dir/');
102+
$this->cmsWysiwygImagesMock->method('getCurrentPath')
103+
->willReturn('/storage/root/dir/pub/media/');
104+
$fileNames = ['fileName'];
105+
foreach ($fileNames as $filename) {
106+
/** @var DataObject|MockObject $objectMock */
107+
$objectMock = $this->getMockBuilder(DataObject::class)
108+
->addMethods(['getFilename'])
109+
->disableOriginalConstructor()
110+
->getMock();
111+
$objectMock->method('getFilename')
112+
->willReturn('/storage/root/dir/' . $filename);
113+
$collection[] = $objectMock;
114+
}
115+
//items for collection
116+
$iterator = new \ArrayIterator($collection);
117+
$this->imagesStorageMock->method('getDirsCollection')
118+
->willReturn($iterator);
119+
$this->coreRegistryMock->method('registry')->willReturn($this->imagesStorageMock);
120+
$this->directoryMock->method('read')->willReturn($fileNames);
121+
$this->fileSystemMock->expects($this->once())
122+
->method('getDirectoryReadByPath')
123+
->willReturn($this->directoryMock);
124+
$this->model->getTreeJson();
125+
}
126+
}

0 commit comments

Comments
 (0)