|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Ui\Test\Unit\Component\Form\Element\DataType\Media; |
| 10 | + |
| 11 | +use Magento\Ui\Component\Form\Element\DataType\Media\Image; |
| 12 | +use Magento\Store\Model\StoreManagerInterface; |
| 13 | +use Magento\Store\Api\Data\StoreInterface; |
| 14 | +use Magento\Framework\File\Size; |
| 15 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 16 | + |
| 17 | +class ImageTest extends \Magento\Ui\Test\Unit\Component\Form\Element\DataType\MediaTest |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject |
| 21 | + */ |
| 22 | + private $store; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 26 | + */ |
| 27 | + private $storeManager; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var Size|\PHPUnit_Framework_MockObject_MockObject |
| 31 | + */ |
| 32 | + private $fileSize; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var ObjectManager |
| 36 | + */ |
| 37 | + private $objectManager; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var Image |
| 41 | + */ |
| 42 | + private $image; |
| 43 | + |
| 44 | + public function setUp() |
| 45 | + { |
| 46 | + parent::setUp(); |
| 47 | + |
| 48 | + $this->processor = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\Processor::class) |
| 49 | + ->disableOriginalConstructor() |
| 50 | + ->getMock(); |
| 51 | + |
| 52 | + $this->context->expects($this->atLeastOnce())->method('getProcessor')->willReturn($this->processor); |
| 53 | + |
| 54 | + $this->storeManager = $this->getMockForAbstractClass(StoreManagerInterface::class); |
| 55 | + |
| 56 | + $this->store = $this->getMockBuilder(StoreInterface::class) |
| 57 | + ->disableOriginalConstructor() |
| 58 | + ->getMock(); |
| 59 | + |
| 60 | + $this->store->expects($this->any())->method('getId')->willReturn(0); |
| 61 | + |
| 62 | + $this->storeManager->expects($this->any())->method('getStore')->willReturn($this->store); |
| 63 | + |
| 64 | + $this->fileSize = $this->getMockBuilder(Size::class)->getMock(); |
| 65 | + |
| 66 | + $this->objectManager = new ObjectManager($this); |
| 67 | + |
| 68 | + $this->image = $this->objectManager->getObject(Image::class, [ |
| 69 | + 'context' => $this->context, |
| 70 | + 'storeManager' => $this->storeManager, |
| 71 | + 'fileSize' => $this->fileSize |
| 72 | + ]); |
| 73 | + |
| 74 | + $this->image->setData([ |
| 75 | + 'config' => [ |
| 76 | + 'initialMediaGalleryOpenSubpath' => 'open/sesame', |
| 77 | + ], |
| 78 | + ]); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @dataProvider prepareDataProvider |
| 83 | + */ |
| 84 | + public function testPrepare() |
| 85 | + { |
| 86 | + $this->assertExpectedPreparedConfiguration(...func_get_args()); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Data provider for testPrepare |
| 91 | + * @return array |
| 92 | + */ |
| 93 | + public function prepareDataProvider(): array |
| 94 | + { |
| 95 | + return [ |
| 96 | + [['maxFileSize' => 10], 10, ['maxFileSize' => 10]], |
| 97 | + [['maxFileSize' => null], 10, ['maxFileSize' => 10]], |
| 98 | + [['maxFileSize' => 10], 5, ['maxFileSize' => 5]], |
| 99 | + [['maxFileSize' => 10], 20, ['maxFileSize' => 10]], |
| 100 | + [['maxFileSize' => 0], 10, ['maxFileSize' => 10]], |
| 101 | + ]; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @param array $initialConfig |
| 106 | + * @param int $maxFileSizeSupported |
| 107 | + * @param array $expectedPreparedConfig |
| 108 | + */ |
| 109 | + private function assertExpectedPreparedConfiguration( |
| 110 | + array $initialConfig, |
| 111 | + int $maxFileSizeSupported, |
| 112 | + array $expectedPreparedConfig |
| 113 | + ) { |
| 114 | + $this->image->setData(array_merge_recursive(['config' => $initialConfig], $this->image->getData())); |
| 115 | + |
| 116 | + $this->fileSize->expects($this->any())->method('getMaxFileSize')->willReturn($maxFileSizeSupported); |
| 117 | + |
| 118 | + $this->image->prepare(); |
| 119 | + |
| 120 | + $actualRelevantPreparedConfig = array_intersect_key($this->image->getConfiguration(), $initialConfig); |
| 121 | + |
| 122 | + $this->assertEquals( |
| 123 | + $expectedPreparedConfig, |
| 124 | + $actualRelevantPreparedConfig |
| 125 | + ); |
| 126 | + } |
| 127 | +} |
0 commit comments