Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RetrieveImageTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class RetrieveImageTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $contextMock;
19 
23  protected $rawFactoryMock;
24 
28  protected $configMock;
29 
33  protected $filesystemMock;
34 
38  protected $adapterMock;
39 
44 
48  protected $curlMock;
49 
53  protected $storageFileMock;
54 
58  protected $request;
59 
63  protected $abstractAdapter;
64 
69  protected $image;
70 
74  private $validatorMock;
75 
79  private $fileDriverMock;
80 
84  protected function setUp()
85  {
86  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
87  $this->contextMock = $this->createMock(\Magento\Backend\App\Action\Context::class);
88  $this->validatorMock = $this
89  ->createMock(\Magento\MediaStorage\Model\File\Validator\NotProtectedExtension::class);
90  $this->rawFactoryMock =
91  $this->createPartialMock(\Magento\Framework\Controller\Result\RawFactory::class, ['create']);
92  $response = new \Magento\Framework\DataObject();
93  $this->rawFactoryMock->expects($this->once())->method('create')->willReturn($response);
94  $this->configMock = $this->createMock(\Magento\Catalog\Model\Product\Media\Config::class);
95  $this->filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
96  $this->adapterMock =
97  $this->createMock(\Magento\Framework\Image::class);
98  $this->adapterFactoryMock =
99  $this->createPartialMock(\Magento\Framework\Image\AdapterFactory::class, ['create']);
100  $this->abstractAdapter = $this->createMock(\Magento\Framework\Image\Adapter\AbstractAdapter::class);
101  $this->adapterFactoryMock->expects($this->once())->method('create')->willReturn($this->abstractAdapter);
102  $this->curlMock = $this->createMock(\Magento\Framework\HTTP\Adapter\Curl::class);
103  $this->storageFileMock = $this->createMock(\Magento\MediaStorage\Model\ResourceModel\File\Storage\File::class);
104  $this->request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
105  $this->fileDriverMock = $this->createMock(\Magento\Framework\Filesystem\DriverInterface::class);
106  $this->contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
107  $managerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
108  ->disableOriginalConstructor()
109  ->setMethods(['get'])
110  ->getMockForAbstractClass();
111  $this->contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
112  $this->contextMock->expects($this->any())->method('getObjectManager')->willReturn($managerMock);
113 
114  $this->image = $objectManager->getObject(
115  \Magento\ProductVideo\Controller\Adminhtml\Product\Gallery\RetrieveImage::class,
116  [
117  'context' => $this->contextMock,
118  'resultRawFactory' => $this->rawFactoryMock,
119  'mediaConfig' => $this->configMock,
120  'fileSystem' => $this->filesystemMock,
121  'imageAdapterFactory' => $this->adapterFactoryMock,
122  'curl' => $this->curlMock,
123  'fileUtility' => $this->storageFileMock,
124  'protocolValidator' => new \Magento\Framework\Validator\AllowedProtocols(),
125  'extensionValidator' => $this->validatorMock,
126  'fileDriver' => $this->fileDriverMock,
127  ]
128  );
129  }
130 
134  public function testExecute()
135  {
136  $this->request->expects($this->any())->method('getParam')->willReturn(
137  'https://example.com/test.jpg'
138  );
139  $readInterface = $this->createMock(
140  \Magento\Framework\Filesystem\Directory\ReadInterface::class
141  );
142  $writeInterface = $this->createMock(
143  \Magento\Framework\Filesystem\Directory\WriteInterface::class
144  );
145  $this->filesystemMock->expects($this->any())->method('getDirectoryRead')->willReturn($readInterface);
146  $readInterface->expects($this->any())->method('getAbsolutePath')->willReturn('');
147  $this->abstractAdapter->expects($this->any())->method('validateUploadFile')->willReturn('true');
148  $this->validatorMock->expects($this->once())->method('isValid')->with('jpg')->willReturn('true');
149  $this->filesystemMock->expects($this->once())->method('getDirectoryWrite')->willReturn($writeInterface);
150  $this->curlMock->expects($this->once())->method('read')->willReturn('testimage');
151 
152  $this->image->execute();
153  }
154 
158  public function testExecuteInvalidFileImage()
159  {
160  $this->request->expects($this->any())->method('getParam')->willReturn(
161  'https://example.com/test.jpg'
162  );
163  $readInterface = $this->createMock(
164  \Magento\Framework\Filesystem\Directory\ReadInterface::class,
165  [],
166  [],
167  '',
168  false
169  );
170  $writeInterface = $this->createMock(
171  \Magento\Framework\Filesystem\Directory\WriteInterface::class,
172  [],
173  [],
174  '',
175  false
176  );
177  $this->filesystemMock->expects($this->any())->method('getDirectoryRead')->willReturn($readInterface);
178  $readInterface->expects($this->any())->method('getAbsolutePath')->willReturn('');
179  $this->abstractAdapter->expects($this->any())
180  ->method('validateUploadFile')
181  ->willThrowException(new \Exception('Invalid File.'));
182  $this->validatorMock->expects($this->once())->method('isValid')->with('jpg')->willReturn('true');
183  $this->curlMock->expects($this->once())->method('read')->willReturn('testimage');
184  $this->filesystemMock->expects($this->once())->method('getDirectoryWrite')->willReturn($writeInterface);
185  $writeInterface->expects($this->once())->method('isExist')->willReturn('true');
186  $writeInterface->expects($this->once())->method('delete')->willReturn('false');
187 
188  $this->image->execute();
189  }
190 
194  public function testExecuteInvalidFileType()
195  {
196  $this->request->expects($this->any())->method('getParam')->willReturn(
197  'https://example.com/test.php'
198  );
199  $readInterface = $this->createMock(
200  \Magento\Framework\Filesystem\Directory\ReadInterface::class,
201  [],
202  [],
203  '',
204  false
205  );
206  $writeInterface = $this->createMock(
207  \Magento\Framework\Filesystem\Directory\WriteInterface::class,
208  [],
209  [],
210  '',
211  false
212  );
213  $this->filesystemMock->expects($this->any())->method('getDirectoryRead')->willReturn($readInterface);
214  $readInterface->expects($this->any())->method('getAbsolutePath')->willReturn('');
215  $this->abstractAdapter->expects($this->never())->method('validateUploadFile');
216  $this->validatorMock->expects($this->once())->method('isValid')->with('php')->willReturn(false);
217  $this->filesystemMock->expects($this->once())->method('getDirectoryWrite')->willReturn($writeInterface);
218  $writeInterface->expects($this->never())->method('isExist');
219 
220  $this->image->execute();
221  }
222 }
$response
Definition: 404.php:11
$objectManager
Definition: bootstrap.php:17