Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ValidatorFileTest.php
Go to the documentation of this file.
1 <?php
8 
10 
15 class ValidatorFileTest extends \PHPUnit\Framework\TestCase
16 {
20  protected $model;
21 
25  protected $objectManager;
26 
30  protected $httpFactoryMock;
31 
35  protected $maxFileSizeInMb;
36 
40  protected $maxFileSize;
41 
42  protected function setUp()
43  {
45  $this->httpFactoryMock = $this->createPartialMock(
46  \Magento\Framework\HTTP\Adapter\FileTransferFactory::class,
47  ['create']
48  );
50  $fileSize = $this->objectManager->create(\Magento\Framework\File\Size::class);
51  $this->maxFileSize = $fileSize->getMaxFileSize();
52  $this->maxFileSizeInMb = $fileSize->getMaxFileSizeInMb();
53  $random = $this->getMockBuilder(Random::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56  $random->expects($this->any())
57  ->method('getRandomString')
58  ->willReturn('RandomString');
59 
60  $this->model = $this->objectManager->create(
61  ValidatorFile::class,
62  [
63  'httpFactory' => $this->httpFactoryMock,
64  'random' => $random,
65  ]
66  );
67  }
68 
73  public function testRunValidationException()
74  {
75  $httpAdapterMock = $this->createPartialMock(\Zend_File_Transfer_Adapter_Http::class, ['isValid']);
76  $this->httpFactoryMock->expects($this->once())->method('create')->will($this->returnValue($httpAdapterMock));
77 
78  $this->model->validate(
79  $this->objectManager->create(\Magento\Framework\DataObject::class),
80  $this->getProductOption(['is_require' => false])
81  );
82  }
83 
88  public function testLargeSizeFile()
89  {
90  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
91  $exceptionMessage = 'The file was too big and couldn\'t be uploaded. Use a file smaller than %s MBs and try ' .
92  'to upload again.';
93  $this->expectExceptionMessage(
94  sprintf($exceptionMessage, $this->maxFileSizeInMb)
95  );
96  $this->prepareEnv();
97  $_SERVER['CONTENT_LENGTH'] = $this->maxFileSize + 1;
98  $httpAdapterMock = $this->createPartialMock(\Zend_File_Transfer_Adapter_Http::class, ['getFileInfo']);
99  $exception = function () {
100  throw new \Exception();
101  };
102  $httpAdapterMock->expects($this->once())->method('getFileInfo')->will($this->returnCallback($exception));
103  $this->httpFactoryMock->expects($this->once())->method('create')->will($this->returnValue($httpAdapterMock));
104 
105  $property = new \ReflectionProperty($httpAdapterMock, '_files');
106  $property->setAccessible(true);
107  $property->setValue($httpAdapterMock, ['options_1_file' => $_FILES['options_1_file']]);
108  $this->model->validate(
109  $this->objectManager->create(\Magento\Framework\DataObject::class),
110  $this->getProductOption(['is_require' => false])
111  );
112  }
113 
118  public function testOptionRequiredException()
119  {
120  $this->prepareEnv();
121  $httpAdapterMock = $this->createPartialMock(\Zend_File_Transfer_Adapter_Http::class, ['getFileInfo']);
122  $exception = function () {
123  throw new \Exception();
124  };
125  $httpAdapterMock->expects($this->once())->method('getFileInfo')->will($this->returnCallback($exception));
126  $this->httpFactoryMock->expects($this->once())->method('create')->will($this->returnValue($httpAdapterMock));
127 
128  $property = new \ReflectionProperty($httpAdapterMock, '_files');
129  $property->setAccessible(true);
130  $property->setValue($httpAdapterMock, ['options_1_file' => $_FILES['options_1_file']]);
131  $this->model->validate(
132  $this->objectManager->create(\Magento\Framework\DataObject::class),
133  $this->getProductOption(['is_require' => false])
134  );
135  }
136 
141  public function testException()
142  {
143  $this->prepareEnv();
144  $httpAdapterMock = $this->createPartialMock(\Zend_File_Transfer_Adapter_Http::class, ['isUploaded']);
145  $httpAdapterMock->expects($this->once())->method('isUploaded')->will($this->returnValue(false));
146  $this->httpFactoryMock->expects($this->once())->method('create')->will($this->returnValue($httpAdapterMock));
147 
148  $property = new \ReflectionProperty($httpAdapterMock, '_files');
149  $property->setAccessible(true);
150  $property->setValue($httpAdapterMock, ['options_1_file' => $_FILES['options_1_file']]);
151  $this->model->validate(
152  $this->objectManager->create(\Magento\Framework\DataObject::class),
153  $this->getProductOption()
154  );
155 
156  $this->expectExceptionMessage(
157  "The product's required option(s) weren't entered. Make sure the options are entered and try again."
158  );
159  }
160 
164  public function testInvalidateFile()
165  {
166  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
167  $this->expectExceptionMessage(
168  "The file 'test.jpg' for 'MediaOption' has an invalid extension.\n"
169  . "The file 'test.jpg' for 'MediaOption' has an invalid extension.\n"
170  . "The maximum allowed image size for 'MediaOption' is 2000x2000 px.\n"
171  . sprintf(
172  "The file 'test.jpg' you uploaded is larger than the %s megabytes allowed by our server.",
173  $this->maxFileSizeInMb
174  )
175  );
176  $this->prepareEnv();
177  $httpAdapterMock = $this->createPartialMock(
178  \Zend_File_Transfer_Adapter_Http::class,
179  ['isValid', 'getErrors', 'getFileInfo', 'isUploaded']
180  );
181  $httpAdapterMock->expects($this->once())
182  ->method('getFileInfo')
183  ->willReturn([
184  'options_1_file' => [
185  'name' => 'test.jpg'
186  ]
187  ]);
188  $httpAdapterMock->expects($this->once())
189  ->method('isValid')
190  ->willReturn(false);
191  $httpAdapterMock->expects($this->exactly(2))
192  ->method('getErrors')
193  ->willReturn(
194  [
199  ]
200  );
201  $this->httpFactoryMock->expects($this->once())
202  ->method('create')
203  ->willReturn($httpAdapterMock);
204  $httpAdapterMock->expects($this->once())
205  ->method('isUploaded')
206  ->willReturn(true);
207  $this->model->validate(
208  $this->objectManager->create(\Magento\Framework\DataObject::class),
209  $this->getProductOption()
210  );
211  }
212 
216  public function testValidate()
217  {
218  $this->prepareGoodEnv();
219  $httpAdapterMock = $this->createPartialMock(\Zend_File_Transfer_Adapter_Http::class, ['isValid']);
220  $httpAdapterMock->expects($this->once())->method('isValid')->will($this->returnValue(true));
221  $this->httpFactoryMock->expects($this->once())->method('create')->will($this->returnValue($httpAdapterMock));
222 
223  $property = new \ReflectionProperty($httpAdapterMock, '_files');
224  $property->setAccessible(true);
225  $property->setValue($httpAdapterMock, ['options_1_file' => $_FILES['options_1_file']]);
226  $result = $this->model->validate(
227  $this->objectManager->create(\Magento\Framework\DataObject::class),
228  $this->getProductOption()
229  );
230  unset($result['fullpath'], $result['secret_key']);
231  $this->assertEquals($this->expectedValidate(), $result);
232  }
233 
234  public function testEmptyFile()
235  {
236  $this->prepareEnvForEmptyFile();
237 
238  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
239  $this->expectExceptionMessage('The file is empty. Select another file and try again.');
240 
241  $httpAdapterMock = $this->createPartialMock(\Zend_File_Transfer_Adapter_Http::class, ['isValid']);
242  $httpAdapterMock->expects($this->once())->method('isValid')->will($this->returnValue(true));
243  $this->httpFactoryMock->expects($this->once())->method('create')->will($this->returnValue($httpAdapterMock));
244 
245  $property = new \ReflectionProperty($httpAdapterMock, '_files');
246  $property->setAccessible(true);
247  $property->setValue($httpAdapterMock, ['options_1_file' => $_FILES['options_1_file']]);
248  $this->model->validate(
249  $this->objectManager->create(\Magento\Framework\DataObject::class),
250  $this->getProductOption()
251  );
252  }
253 
258  protected function getProductOption(array $options = [])
259  {
260  $data = [
261  'option_id' => '1',
262  'product_id' => '4',
263  'type' => 'file',
264  'is_require' => '1',
265  'sku' => null,
266  'max_characters' => null,
267  'file_extension' => null,
268  'image_size_x' => '2000',
269  'image_size_y' => '2000',
270  'sort_order' => '0',
271  'default_title' => 'MediaOption',
272  'store_title' => null,
273  'title' => 'MediaOption',
274  'default_price' => '5.0000',
275  'default_price_type' => 'fixed',
276  'store_price' => null,
277  'store_price_type' => null,
278  'price' => '5.0000',
279  'price_type' => 'fixed',
280  ];
281  $option = $this->objectManager->create(
282  \Magento\Catalog\Model\Product\Option::class,
283  [
284  'data' => array_merge($data, $options)
285  ]
286  );
287 
288  return $option;
289  }
290 
294  protected function prepareEnv()
295  {
296  $file = 'magento_small_image.jpg';
297 
299  $filesystem = $this->objectManager->get(\Magento\Framework\Filesystem::class);
300  $tmpDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
301  $filePath = $tmpDirectory->getAbsolutePath($file);
302 
303  $_FILES['options_1_file'] = [
304  'name' => 'test.jpg',
305  'type' => 'image/jpeg',
306  'tmp_name' => $filePath,
307  'error' => 0,
308  'size' => 12500,
309  ];
310  }
311 
315  protected function prepareGoodEnv()
316  {
317  $file = 'magento_small_image.jpg';
318 
320  $filesystem = $this->objectManager->get(\Magento\Framework\Filesystem::class);
321  $tmpDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
322  $filePath = $tmpDirectory->getAbsolutePath($file);
323 
324  $_FILES['options_1_file'] = [
325  'name' => 'test.jpg',
326  'type' => 'image/jpeg',
327  'tmp_name' => $filePath,
328  'error' => 0,
329  'size' => '3046',
330  ];
331  }
332 
338  protected function prepareEnvForEmptyFile()
339  {
340  $file = 'magento_empty.jpg';
341 
343  $filesystem = $this->objectManager->get(\Magento\Framework\Filesystem::class);
344  $tmpDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
345  $filePath = $tmpDirectory->getAbsolutePath($file);
346 
347  $_FILES['options_1_file'] = [
348  'name' => 'test.jpg',
349  'type' => 'image/jpeg',
350  'tmp_name' => $filePath,
351  ];
352  }
353 
357  protected function expectedValidate()
358  {
359  return [
360  'type' => 'image/jpeg',
361  'title' => 'test.jpg',
362  'quote_path' => 'custom_options/quote/t/e/RandomString',
363  'order_path' => 'custom_options/order/t/e/RandomString',
364  'size' => '3046',
365  'width' => 136,
366  'height' => 131,
367  ];
368  }
369 }
$tmpDirectory
$filesystem