|
| 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 | + * @param array $fileNames |
| 96 | + * @dataProvider dirsCollectionDataProvider |
| 97 | + * @return void |
| 98 | + */ |
| 99 | + public function testGetTreeJson($fileNames): void |
| 100 | + { |
| 101 | + $collection = []; |
| 102 | + $this->cmsWysiwygImagesMock->method('getStorageRoot') |
| 103 | + ->willReturn('/storage/root/dir/'); |
| 104 | + $this->cmsWysiwygImagesMock->method('getCurrentPath') |
| 105 | + ->willReturn('/storage/root/dir/pub/media/'); |
| 106 | + foreach ($fileNames as $filename) { |
| 107 | + /** @var DataObject|MockObject $objectMock */ |
| 108 | + $objectMock = $this->getMockBuilder(DataObject::class) |
| 109 | + ->addMethods(['getFilename']) |
| 110 | + ->disableOriginalConstructor() |
| 111 | + ->getMock(); |
| 112 | + $objectMock->method('getFilename') |
| 113 | + ->willReturn('/storage/root/dir/' . $filename); |
| 114 | + $collection[] = $objectMock; |
| 115 | + } |
| 116 | + //items for collection |
| 117 | + $iterator = new \ArrayIterator($collection); |
| 118 | + $this->imagesStorageMock->method('getDirsCollection') |
| 119 | + ->willReturn($iterator); |
| 120 | + $this->coreRegistryMock->method('registry')->willReturn($this->imagesStorageMock); |
| 121 | + $this->directoryMock->method('read')->willReturn($fileNames); |
| 122 | + $this->fileSystemMock->expects($this->once()) |
| 123 | + ->method('getDirectoryReadByPath') |
| 124 | + ->willReturn($this->directoryMock); |
| 125 | + $this->model->getTreeJson(); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Data provider for testGetTreeJson() |
| 130 | + * |
| 131 | + * @return array |
| 132 | + */ |
| 133 | + public function dirsCollectionDataProvider(): array |
| 134 | + { |
| 135 | + return [ |
| 136 | + [ |
| 137 | + 'filenames' => [ |
| 138 | + '/dress', |
| 139 | + ] |
| 140 | + ] |
| 141 | + ]; |
| 142 | + } |
| 143 | +} |
0 commit comments