@@ -73,20 +73,10 @@ public function setUp(): void
73
73
*/
74
74
public function testProcessCustomerAddressValue ()
75
75
{
76
- $ this ->mediaDirectory ->delete ('customer_address ' );
77
- $ this ->mediaDirectory ->create ($ this ->mediaDirectory ->getRelativePath ('customer_address/tmp/ ' ));
78
- $ tmpFilePath = $ this ->mediaDirectory ->getAbsolutePath ('customer_address/tmp/ ' . $ this ->fileName );
79
- $ this ->copyFile ($ this ->imageFixtureDir . DIRECTORY_SEPARATOR . $ this ->fileName , $ tmpFilePath );
76
+ $ entityTypeCode = 'customer_address ' ;
77
+ $ tmpFilePath = $ this ->prepareImageForTest ($ entityTypeCode );
80
78
81
- $ imageFile = [
82
- 'name ' => $ this ->fileName ,
83
- 'type ' => 'image/jpeg ' ,
84
- 'tmp_name ' => $ this ->fileName ,
85
- 'file ' => $ this ->fileName ,
86
- 'error ' => 0 ,
87
- 'size ' => 12500 ,
88
- 'previewType ' => 'image ' ,
89
- ];
79
+ $ imageFile = $ this ->getImageValues ();
90
80
91
81
$ params = [
92
82
'entityTypeCode ' => 'customer_address ' ,
@@ -119,23 +109,12 @@ public function testProcessCustomerAddressValue()
119
109
*/
120
110
public function testProcessCustomerValue ()
121
111
{
122
- $ this ->mediaDirectory ->delete ('customer ' );
123
- $ this ->mediaDirectory ->create ($ this ->mediaDirectory ->getRelativePath ('customer/tmp/ ' ));
124
- $ tmpFilePath = $ this ->mediaDirectory ->getAbsolutePath ('customer/tmp/ ' . $ this ->fileName );
125
- $ this ->copyFile ($ this ->imageFixtureDir . DIRECTORY_SEPARATOR . $ this ->fileName , $ tmpFilePath );
126
-
127
- $ imageFile = [
128
- 'name ' => $ this ->fileName ,
129
- 'type ' => 'image/jpeg ' ,
130
- 'tmp_name ' => $ this ->fileName ,
131
- 'file ' => $ this ->fileName ,
132
- 'error ' => 0 ,
133
- 'size ' => 12500 ,
134
- 'previewType ' => 'image ' ,
135
- ];
112
+ $ entityTypeCode = 'customer ' ;
113
+ $ tmpFilePath = $ this ->prepareImageForTest ($ entityTypeCode );
114
+ $ imageFile = $ this ->getImageValues ();
136
115
137
116
$ params = [
138
- 'entityTypeCode ' => ' customer ' ,
117
+ 'entityTypeCode ' => $ entityTypeCode ,
139
118
'formCode ' => 'customer_edit ' ,
140
119
'isAjax ' => false ,
141
120
'value ' => $ imageFile
@@ -167,23 +146,14 @@ public function testProcessCustomerInvalidValue()
167
146
\Magento \Framework \Exception \ValidatorException::class
168
147
);
169
148
170
- $ this ->mediaDirectory ->delete ('customer ' );
171
- $ this ->mediaDirectory ->create ($ this ->mediaDirectory ->getRelativePath ('customer/tmp/ ' ));
172
- $ tmpFilePath = $ this ->mediaDirectory ->getAbsolutePath ('customer/tmp/ ' . $ this ->fileName );
173
- $ this ->copyFile ($ this ->imageFixtureDir . DIRECTORY_SEPARATOR . $ this ->fileName , $ tmpFilePath );
149
+ $ entityTypeCode = 'customer ' ;
150
+ $ this ->prepareImageForTest ($ entityTypeCode );
174
151
175
- $ imageFile = [
176
- 'name ' => $ this ->fileName ,
177
- 'type ' => 'image/jpeg ' ,
178
- 'tmp_name ' => $ this ->fileName ,
179
- 'file ' => $ this ->invalidFileName ,
180
- 'error ' => 0 ,
181
- 'size ' => 12500 ,
182
- 'previewType ' => 'image ' ,
183
- ];
152
+ $ imageFile = $ this ->getImageValues ();
153
+ $ imageFile ['file ' ] = $ this ->invalidFileName ;
184
154
185
155
$ params = [
186
- 'entityTypeCode ' => ' customer ' ,
156
+ 'entityTypeCode ' => $ entityTypeCode ,
187
157
'formCode ' => 'customer_edit ' ,
188
158
'isAjax ' => false ,
189
159
'value ' => $ imageFile
@@ -199,6 +169,36 @@ public function testProcessCustomerInvalidValue()
199
169
$ processCustomerAddressValueMethod ->invoke ($ image , $ imageFile );
200
170
}
201
171
172
+ /**
173
+ * Test for _validateByRules method with not existed file
174
+ *
175
+ * @magentoAppIsolation enabled
176
+ *
177
+ * @throws FileSystemException
178
+ * @throws \ReflectionException
179
+ */
180
+ public function testValidateByRulesWithNotValidFile ()
181
+ {
182
+ $ entityTypeCode = 'customer ' ;
183
+ $ this ->mediaDirectory ->delete ($ entityTypeCode );
184
+ $ imageFile = $ this ->getImageValues ();
185
+ $ params = [
186
+ 'entityTypeCode ' => $ entityTypeCode ,
187
+ 'formCode ' => 'customer_edit ' ,
188
+ 'isAjax ' => false ,
189
+ 'value ' => $ imageFile
190
+ ];
191
+
192
+ $ image = $ this ->objectManager ->create (\Magento \Customer \Model \Metadata \Form \Image::class, $ params );
193
+ $ processValidateMethod = new \ReflectionMethod (
194
+ \Magento \Customer \Model \Metadata \Form \Image::class,
195
+ '_validateByRules '
196
+ );
197
+ $ processValidateMethod ->setAccessible (true );
198
+ $ validationResult = $ processValidateMethod ->invoke ($ image , $ imageFile );
199
+ $ this ->assertEquals ('" ' . $ this ->fileName .'" is not a valid file. ' , $ validationResult [0 ]->__toString ());
200
+ }
201
+
202
202
/**
203
203
* @inheritdoc
204
204
* @throws FileSystemException
@@ -226,4 +226,38 @@ private function copyFile(string $source, string $destination)
226
226
$ driver ->createDirectory (dirname ($ destination ));
227
227
$ driver ->filePutContents ($ destination , file_get_contents ($ source ));
228
228
}
229
+
230
+ /**
231
+ * Returns image values
232
+ *
233
+ * @return array
234
+ */
235
+ private function getImageValues (): array
236
+ {
237
+ return [
238
+ 'name ' => $ this ->fileName ,
239
+ 'type ' => 'image/jpeg ' ,
240
+ 'tmp_name ' => $ this ->fileName ,
241
+ 'file ' => $ this ->fileName ,
242
+ 'error ' => 0 ,
243
+ 'size ' => 12500 ,
244
+ 'previewType ' => 'image ' ,
245
+ ];
246
+ }
247
+
248
+ /**
249
+ * Copies image from fixture to necessary for test dir
250
+ *
251
+ * @param string $entityTypeCode
252
+ * @return string
253
+ * @throws FileSystemException
254
+ */
255
+ private function prepareImageForTest (string $ entityTypeCode ): string
256
+ {
257
+ $ this ->mediaDirectory ->delete ($ entityTypeCode );
258
+ $ this ->mediaDirectory ->create ($ this ->mediaDirectory ->getRelativePath ($ entityTypeCode . '/tmp/ ' ));
259
+ $ tmpFilePath = $ this ->mediaDirectory ->getAbsolutePath ($ entityTypeCode . '/tmp/ ' . $ this ->fileName );
260
+ $ this ->copyFile ($ this ->imageFixtureDir . DIRECTORY_SEPARATOR . $ this ->fileName , $ tmpFilePath );
261
+ return $ tmpFilePath ;
262
+ }
229
263
}
0 commit comments