Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.4-develop' into MCP-304
Browse files Browse the repository at this point in the history
  • Loading branch information
adifucan committed May 12, 2021
2 parents e4b139a + b5c918b commit d2731af
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 2 deletions.
23 changes: 21 additions & 2 deletions app/code/Magento/Cms/Block/Adminhtml/Wysiwyg/Images/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Cms\Test\Unit\Block\Adminhtml\Wysiwyg\Images;

use Magento\Backend\Block\Template\Context;
use Magento\Cms\Block\Adminhtml\Wysiwyg\Images\Tree;
use Magento\Cms\Helper\Wysiwyg\Images;
use Magento\Cms\Model\Wysiwyg\Images\Storage;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\DataObject;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\Read;
use Magento\Framework\Registry;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Get tree json test.
*/
class TreeTest extends TestCase
{
/**
* @var Tree
*/
private $model;

/**
* @var Registry|MockObject
*/
private $coreRegistryMock;

/**
* @var Images|MockObject
*/
private $cmsWysiwygImagesMock;

/**
* @var Storage|MockObject
*/
private $imagesStorageMock;

/**
* @var Read|MockObject
*/
private $directoryMock;

/**
* @var Filesystem|MockObject
*/
private $fileSystemMock;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = new ObjectManager($this);
$contextMock = $this->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();
}
}

0 comments on commit d2731af

Please sign in to comment.