@@ -18,6 +18,10 @@ class CreateHandlerTest extends \PHPUnit_Framework_TestCase
18
18
*/
19
19
protected $ createHandler ;
20
20
21
+ private $ fileName = '/m/a/magento_image.jpg ' ;
22
+
23
+ private $ fileLabel = 'Magento image ' ;
24
+
21
25
protected function setUp ()
22
26
{
23
27
$ this ->createHandler = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
@@ -28,31 +32,150 @@ protected function setUp()
28
32
/**
29
33
* @covers \Magento\Catalog\Model\Product\Gallery\CreateHandler::execute
30
34
*/
31
- public function testExecute ()
35
+ public function testExecuteWithImageDuplicate ()
32
36
{
33
- $ fileName = '/m/a/magento_image.jpg ' ;
34
- $ fileLabel = 'Magento image ' ;
35
37
/** @var $product \Magento\Catalog\Model\Product */
36
38
$ product = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
37
39
\Magento \Catalog \Model \Product::class
38
40
);
39
41
$ product ->load (1 );
40
42
$ product ->setData (
41
43
'media_gallery ' ,
42
- ['images ' => ['image ' => ['file ' => $ fileName , 'label ' => $ fileLabel ]]]
44
+ ['images ' => ['image ' => ['file ' => $ this -> fileName , 'label ' => $ this -> fileLabel ]]]
43
45
);
44
- $ product ->setData ('image ' , $ fileName );
46
+ $ product ->setData ('image ' , $ this -> fileName );
45
47
$ this ->createHandler ->execute ($ product );
46
48
$ this ->assertStringStartsWith ('/m/a/magento_image ' , $ product ->getData ('media_gallery/images/image/new_file ' ));
47
- $ this ->assertEquals ($ fileLabel , $ product ->getData ('image_label ' ));
49
+ $ this ->assertEquals ($ this -> fileLabel , $ product ->getData ('image_label ' ));
48
50
49
51
$ product ->setIsDuplicate (true );
50
52
$ product ->setData (
51
53
'media_gallery ' ,
52
- ['images ' => ['image ' => ['value_id ' => '100 ' , 'file ' => $ fileName , 'label ' => $ fileLabel ]]]
54
+ ['images ' => ['image ' => ['value_id ' => '100 ' , 'file ' => $ this -> fileName , 'label ' => $ this -> fileLabel ]]]
53
55
);
54
56
$ this ->createHandler ->execute ($ product );
55
57
$ this ->assertStringStartsWith ('/m/a/magento_image ' , $ product ->getData ('media_gallery/duplicate/100 ' ));
56
- $ this ->assertEquals ($ fileLabel , $ product ->getData ('image_label ' ));
58
+ $ this ->assertEquals ($ this ->fileLabel , $ product ->getData ('image_label ' ));
59
+ }
60
+
61
+ /**
62
+ * @dataProvider executeDataProvider
63
+ * @param $image
64
+ * @param $smallImage
65
+ * @param $swatchImage
66
+ * @param $thumbnail
67
+ */
68
+ public function testExecuteWithImageRoles ($ image , $ smallImage , $ swatchImage , $ thumbnail )
69
+ {
70
+ /** @var $product \Magento\Catalog\Model\Product */
71
+ $ product = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
72
+ \Magento \Catalog \Model \Product::class
73
+ );
74
+ $ product ->load (1 );
75
+ $ product ->setData (
76
+ 'media_gallery ' ,
77
+ ['images ' => ['image ' => ['file ' => $ this ->fileName , 'label ' => '' ]]]
78
+ );
79
+ $ product ->setData ('image ' , $ image );
80
+ $ product ->setData ('small_image ' , $ smallImage );
81
+ $ product ->setData ('swatch_image ' , $ swatchImage );
82
+ $ product ->setData ('thumbnail ' , $ thumbnail );
83
+ $ this ->createHandler ->execute ($ product );
84
+
85
+ $ resource = $ product ->getResource ();
86
+ $ id = $ product ->getId ();
87
+ $ storeId = $ product ->getStoreId ();
88
+
89
+ $ this ->assertStringStartsWith ('/m/a/magento_image ' , $ product ->getData ('media_gallery/images/image/new_file ' ));
90
+ $ this ->assertEquals (
91
+ $ image ,
92
+ $ resource ->getAttributeRawValue ($ id , $ resource ->getAttribute ('image ' ), $ storeId )
93
+ );
94
+ $ this ->assertEquals (
95
+ $ smallImage ,
96
+ $ resource ->getAttributeRawValue ($ id , $ resource ->getAttribute ('small_image ' ), $ storeId )
97
+ );
98
+ $ this ->assertEquals (
99
+ $ swatchImage ,
100
+ $ resource ->getAttributeRawValue ($ id , $ resource ->getAttribute ('swatch_image ' ), $ storeId )
101
+ );
102
+ $ this ->assertEquals (
103
+ $ thumbnail ,
104
+ $ resource ->getAttributeRawValue ($ id , $ resource ->getAttribute ('thumbnail ' ), $ storeId )
105
+ );
106
+ }
107
+
108
+ /**
109
+ * @dataProvider executeDataProvider
110
+ * @param $image
111
+ * @param $smallImage
112
+ * @param $swatchImage
113
+ * @param $thumbnail
114
+ */
115
+ public function testExecuteWithoutImages ($ image , $ smallImage , $ swatchImage , $ thumbnail )
116
+ {
117
+ /** @var $product \Magento\Catalog\Model\Product */
118
+ $ product = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
119
+ \Magento \Catalog \Model \Product::class
120
+ );
121
+ $ product ->load (1 );
122
+ $ product ->setData (
123
+ 'media_gallery ' ,
124
+ ['images ' => ['image ' => ['file ' => $ this ->fileName , 'label ' => '' ]]]
125
+ );
126
+ $ product ->setData ('image ' , $ image );
127
+ $ product ->setData ('small_image ' , $ smallImage );
128
+ $ product ->setData ('swatch_image ' , $ swatchImage );
129
+ $ product ->setData ('thumbnail ' , $ thumbnail );
130
+ $ this ->createHandler ->execute ($ product );
131
+
132
+ $ product ->unsetData ('image ' );
133
+ $ product ->unsetData ('small_image ' );
134
+ $ product ->unsetData ('swatch_image ' );
135
+ $ product ->unsetData ('thumbnail ' );
136
+ $ this ->createHandler ->execute ($ product );
137
+
138
+ $ resource = $ product ->getResource ();
139
+ $ id = $ product ->getId ();
140
+ $ storeId = $ product ->getStoreId ();
141
+
142
+ $ this ->assertStringStartsWith ('/m/a/magento_image ' , $ product ->getData ('media_gallery/images/image/new_file ' ));
143
+ $ this ->assertEquals (
144
+ $ image ,
145
+ $ resource ->getAttributeRawValue ($ id , $ resource ->getAttribute ('image ' ), $ storeId )
146
+ );
147
+ $ this ->assertEquals (
148
+ $ smallImage ,
149
+ $ resource ->getAttributeRawValue ($ id , $ resource ->getAttribute ('small_image ' ), $ storeId )
150
+ );
151
+ $ this ->assertEquals (
152
+ $ swatchImage ,
153
+ $ resource ->getAttributeRawValue ($ id , $ resource ->getAttribute ('swatch_image ' ), $ storeId )
154
+ );
155
+ $ this ->assertEquals (
156
+ $ thumbnail ,
157
+ $ resource ->getAttributeRawValue ($ id , $ resource ->getAttribute ('thumbnail ' ), $ storeId )
158
+ );
159
+ }
160
+
161
+ /**
162
+ * @return array
163
+ */
164
+ public function executeDataProvider ()
165
+ {
166
+ return [
167
+ [
168
+ 'image ' => $ this ->fileName ,
169
+ 'small_image ' => $ this ->fileName ,
170
+ 'swatch_image ' => $ this ->fileName ,
171
+ 'thumbnail ' => $ this ->fileName
172
+ ],
173
+ [
174
+ 'image ' => 'no_selection ' ,
175
+ 'small_image ' => 'no_selection ' ,
176
+ 'swatch_image ' => 'no_selection ' ,
177
+ 'thumbnail ' => 'no_selection '
178
+ ]
179
+ ];
57
180
}
58
181
}
0 commit comments