42 protected function setUp()
45 $this->httpFactoryMock = $this->createPartialMock(
46 \
Magento\Framework\HTTP\Adapter\FileTransferFactory::class,
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()
56 $random->expects($this->any())
57 ->method(
'getRandomString')
58 ->willReturn(
'RandomString');
60 $this->model = $this->objectManager->create(
63 'httpFactory' => $this->httpFactoryMock,
75 $httpAdapterMock = $this->createPartialMock(\Zend_File_Transfer_Adapter_Http::class, [
'isValid']);
76 $this->httpFactoryMock->expects($this->once())->method(
'create')->will($this->returnValue($httpAdapterMock));
78 $this->model->validate(
79 $this->objectManager->create(\
Magento\Framework\DataObject::class),
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 ' .
93 $this->expectExceptionMessage(
94 sprintf($exceptionMessage, $this->maxFileSizeInMb)
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();
102 $httpAdapterMock->expects($this->once())->method(
'getFileInfo')->will($this->returnCallback($exception));
103 $this->httpFactoryMock->expects($this->once())->method(
'create')->will($this->returnValue($httpAdapterMock));
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),
121 $httpAdapterMock = $this->createPartialMock(\Zend_File_Transfer_Adapter_Http::class, [
'getFileInfo']);
122 $exception =
function () {
123 throw new \Exception();
125 $httpAdapterMock->expects($this->once())->method(
'getFileInfo')->will($this->returnCallback($exception));
126 $this->httpFactoryMock->expects($this->once())->method(
'create')->will($this->returnValue($httpAdapterMock));
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),
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));
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),
156 $this->expectExceptionMessage(
157 "The product's required option(s) weren't entered. Make sure the options are entered and try again." 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" 172 "The file 'test.jpg' you uploaded is larger than the %s megabytes allowed by our server.",
173 $this->maxFileSizeInMb
177 $httpAdapterMock = $this->createPartialMock(
178 \Zend_File_Transfer_Adapter_Http::class,
179 [
'isValid',
'getErrors',
'getFileInfo',
'isUploaded']
181 $httpAdapterMock->expects($this->once())
182 ->method(
'getFileInfo')
184 'options_1_file' => [
188 $httpAdapterMock->expects($this->once())
191 $httpAdapterMock->expects($this->exactly(2))
192 ->method(
'getErrors')
201 $this->httpFactoryMock->expects($this->once())
203 ->willReturn($httpAdapterMock);
204 $httpAdapterMock->expects($this->once())
205 ->method(
'isUploaded')
207 $this->model->validate(
208 $this->objectManager->create(\
Magento\Framework\DataObject::class),
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));
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),
236 $this->prepareEnvForEmptyFile();
238 $this->expectException(\
Magento\Framework\
Exception\LocalizedException::class);
239 $this->expectExceptionMessage(
'The file is empty. Select another file and try again.');
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));
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),
266 'max_characters' =>
null,
267 'file_extension' =>
null,
268 'image_size_x' =>
'2000',
269 'image_size_y' =>
'2000',
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,
279 'price_type' =>
'fixed',
281 $option = $this->objectManager->create(
294 protected function prepareEnv()
296 $file =
'magento_small_image.jpg';
303 $_FILES[
'options_1_file'] = [
304 'name' =>
'test.jpg',
305 'type' =>
'image/jpeg',
306 'tmp_name' => $filePath,
315 protected function prepareGoodEnv()
317 $file =
'magento_small_image.jpg';
324 $_FILES[
'options_1_file'] = [
325 'name' =>
'test.jpg',
326 'type' =>
'image/jpeg',
327 'tmp_name' => $filePath,
338 protected function prepareEnvForEmptyFile()
340 $file =
'magento_empty.jpg';
347 $_FILES[
'options_1_file'] = [
348 'name' =>
'test.jpg',
349 'type' =>
'image/jpeg',
350 'tmp_name' => $filePath,
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',
getProductOption(array $options=[])
testRunValidationException()
testOptionRequiredException()
static getObjectManager()