16
16
17
17
namespace Magento \GraphQl \Quote ;
18
18
19
+ use Exception ;
20
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
21
+ use Magento \Framework \Exception \LocalizedException ;
19
22
use Magento \TestFramework \Fixture \Config as ConfigFixture ;
20
23
use Magento \Catalog \Test \Fixture \Category as CategoryFixture ;
21
24
use Magento \Catalog \Test \Fixture \Product as ProductFixture ;
24
27
use Magento \Quote \Test \Fixture \QuoteIdMask as QuoteMaskFixture ;
25
28
use Magento \TestFramework \Fixture \DataFixture ;
26
29
use Magento \TestFramework \Fixture \DataFixtureStorageManager ;
30
+ use Magento \TestFramework \Helper \Bootstrap ;
27
31
use Magento \TestFramework \TestCase \GraphQlAbstract ;
28
32
29
33
/**
30
34
* Test adding grouped products returns default thumbnail/product image thumbnail
31
35
*/
36
+ #[
37
+ DataFixture(CategoryFixture::class, as: 'category ' ),
38
+ DataFixture(
39
+ ProductFixture::class,
40
+ [
41
+ 'category_ids ' => ['$category.id$ ' ],
42
+ 'media_gallery_entries ' => [
43
+ [
44
+ 'label ' => 'image ' ,
45
+ 'media_type ' => 'image ' ,
46
+ 'position ' => 1 ,
47
+ 'disabled ' => false ,
48
+ 'types ' => [
49
+ 'image ' ,
50
+ 'small_image ' ,
51
+ 'thumbnail '
52
+ ],
53
+ 'file ' => '/m/product1.jpg ' ,
54
+ ],
55
+ ],
56
+ ],
57
+ 'product1 '
58
+ ),
59
+ DataFixture(
60
+ ProductFixture::class,
61
+ [
62
+ 'category_ids ' => ['$category.id$ ' ],
63
+ 'media_gallery_entries ' => [
64
+ [
65
+ 'label ' => 'image ' ,
66
+ 'media_type ' => 'image ' ,
67
+ 'position ' => 1 ,
68
+ 'disabled ' => false ,
69
+ 'types ' => [
70
+ 'image ' ,
71
+ 'small_image ' ,
72
+ 'thumbnail '
73
+ ],
74
+ 'file ' => '/m/product2.jpg ' ,
75
+ ],
76
+ ],
77
+ ],
78
+ 'product2 '
79
+ ),
80
+ DataFixture(
81
+ GroupedProductFixture::class,
82
+ [
83
+ 'category_ids ' => ['$category.id$ ' ],
84
+ 'product_links ' => [
85
+ ['sku ' => '$product1.sku$ ' , 'qty ' => 1 ],
86
+ ['sku ' => '$product2.sku$ ' , 'qty ' => 1 ]
87
+ ]
88
+ ],
89
+ 'grouped-product '
90
+ ),
91
+ DataFixture(GuestCartFixture::class, as: 'cart ' ),
92
+ DataFixture(QuoteMaskFixture::class, ['cart_id ' => '$cart.id$ ' ], 'quoteIdMask ' ),
93
+ ]
32
94
class AddGroupedProductToCartThumbnailTest extends GraphQlAbstract
33
95
{
34
96
private const DEFAULT_THUMBNAIL_PATH = 'Magento_Catalog/images/product/placeholder/thumbnail.jpg ' ;
35
97
98
+ /**
99
+ * @throws LocalizedException
100
+ */
36
101
#[
37
- DataFixture(CategoryFixture::class, ['name ' => 'Category ' ], 'category ' ),
38
- DataFixture(
39
- ProductFixture::class,
40
- [
41
- 'name ' => 'Product 1 ' ,
42
- 'sku ' => 'product-1 ' ,
43
- 'category_ids ' => ['$category.id$ ' ],
44
- 'price ' => 10
45
- ],
46
- 'product1 '
47
- ),
48
- DataFixture(
49
- ProductFixture::class,
50
- [
51
- 'name ' => 'Product 2 ' ,
52
- 'sku ' => 'product-2 ' ,
53
- 'category_ids ' => ['$category.id$ ' ],
54
- 'price ' => 15
55
- ],
56
- 'product2 '
57
- ),
58
- DataFixture(
59
- GroupedProductFixture::class,
60
- [
61
- 'sku ' => 'grouped-product ' ,
62
- 'category_ids ' => ['$category.id$ ' ],
63
- 'product_links ' => [
64
- ['sku ' => '$product1.sku$ ' , 'qty ' => 1 ],
65
- ['sku ' => '$product2.sku$ ' , 'qty ' => 1 ]
66
- ]
67
- ],
68
- 'grouped-product '
69
- ),
70
- DataFixture(GuestCartFixture::class, as: 'cart ' ),
71
- DataFixture(QuoteMaskFixture::class, ['cart_id ' => '$cart.id$ ' ], 'quoteIdMask ' ),
102
+ ConfigFixture('checkout/cart/grouped_product_image ' , 'parent ' ),
72
103
]
73
- public function testAddGroupedProductToCartWithoutImageShouldUseThumbnail ()
104
+ public function testAddGroupedProductToCartWithImageShouldUseParentImageAsThumbnail ()
105
+ {
106
+ $ thumbnails = [
107
+ 'product1 ' => self ::DEFAULT_THUMBNAIL_PATH ,
108
+ 'product2 ' => self ::DEFAULT_THUMBNAIL_PATH
109
+ ];
110
+ $ this ->assertProductThumbnailUrl ($ thumbnails );
111
+ }
112
+
113
+ /**
114
+ * @throws LocalizedException
115
+ */
116
+ #[
117
+ ConfigFixture('checkout/cart/grouped_product_image ' , 'itself ' ),
118
+ ]
119
+ public function testAddGroupedProductToCartWithImageShouldUseProductImageAsThumbnail ()
120
+ {
121
+ $ productRepository = Bootstrap::getObjectManager ()->get (ProductRepositoryInterface::class);
122
+ $ product1Sku = DataFixtureStorageManager::getStorage ()->get ('product1 ' )->getSku ();
123
+ $ product2Sku = DataFixtureStorageManager::getStorage ()->get ('product2 ' )->getSku ();
124
+ $ thumbnails = [
125
+ 'product1 ' => $ productRepository ->get ($ product1Sku )->getThumbnail (),
126
+ 'product2 ' => $ productRepository ->get ($ product2Sku )->getThumbnail ()
127
+ ];
128
+
129
+ $ this ->assertProductThumbnailUrl ($ thumbnails );
130
+ }
131
+
132
+ /**
133
+ * Assert product thumbnail url
134
+ *
135
+ * @param array $thumbnails
136
+ * @return void
137
+ * @throws LocalizedException
138
+ * @throws Exception
139
+ */
140
+ private function assertProductThumbnailUrl (array $ thumbnails ): void
74
141
{
75
142
$ cartId = DataFixtureStorageManager::getStorage ()->get ('quoteIdMask ' )->getMaskedId ();
76
- $ groupedProductId = DataFixtureStorageManager::getStorage ()->get ('grouped-product ' )->getSku ();
77
- $ response = $ this ->graphQlMutation ($ this ->getMutation ($ cartId , $ groupedProductId ));
143
+ $ groupedProductSku = DataFixtureStorageManager::getStorage ()->get ('grouped-product ' )->getSku ();
144
+ $ response = $ this ->graphQlMutation ($ this ->getMutation ($ cartId , $ groupedProductSku ));
78
145
79
146
$ this ->assertArrayHasKey ('addProductsToCart ' , $ response );
80
147
$ this ->assertEquals (2 , count ($ response ['addProductsToCart ' ]['cart ' ]['itemsV2 ' ]['items ' ]));
148
+
81
149
$ this ->assertStringContainsString (
82
- self :: DEFAULT_THUMBNAIL_PATH ,
150
+ $ thumbnails [ ' product1 ' ] ,
83
151
$ response ['addProductsToCart ' ]['cart ' ]['itemsV2 ' ]['items ' ][0 ]['product ' ]['thumbnail ' ]['url ' ]
84
152
);
85
153
$ this ->assertStringContainsString (
86
- self :: DEFAULT_THUMBNAIL_PATH ,
154
+ $ thumbnails [ ' product2 ' ] ,
87
155
$ response ['addProductsToCart ' ]['cart ' ]['itemsV2 ' ]['items ' ][1 ]['product ' ]['thumbnail ' ]['url ' ]
88
156
);
89
157
}
90
158
159
+ /**
160
+ * @throws LocalizedException
161
+ */
91
162
#[
92
163
ConfigFixture('checkout/cart/grouped_product_image ' , 'itself ' ),
93
164
DataFixture(CategoryFixture::class, ['name ' => 'Category ' ], 'category ' ),
@@ -97,21 +168,7 @@ public function testAddGroupedProductToCartWithoutImageShouldUseThumbnail()
97
168
'name ' => 'Product 1 ' ,
98
169
'sku ' => 'product-1 ' ,
99
170
'category_ids ' => ['$category.id$ ' ],
100
- 'price ' => 10 ,
101
- 'media_gallery_entries ' => [
102
- [
103
- 'label ' => 'image ' ,
104
- 'media_type ' => 'image ' ,
105
- 'position ' => 1 ,
106
- 'disabled ' => false ,
107
- 'types ' => [
108
- 'image ' ,
109
- 'small_image ' ,
110
- 'thumbnail '
111
- ],
112
- 'file ' => '/m/product1.jpg ' ,
113
- ],
114
- ],
171
+ 'price ' => 10
115
172
],
116
173
'product1 '
117
174
),
@@ -121,21 +178,7 @@ public function testAddGroupedProductToCartWithoutImageShouldUseThumbnail()
121
178
'name ' => 'Product 2 ' ,
122
179
'sku ' => 'product-2 ' ,
123
180
'category_ids ' => ['$category.id$ ' ],
124
- 'price ' => 15 ,
125
- 'media_gallery_entries ' => [
126
- [
127
- 'label ' => 'image ' ,
128
- 'media_type ' => 'image ' ,
129
- 'position ' => 1 ,
130
- 'disabled ' => false ,
131
- 'types ' => [
132
- 'image ' ,
133
- 'small_image ' ,
134
- 'thumbnail '
135
- ],
136
- 'file ' => '/m/product2.jpg ' ,
137
- ],
138
- ],
181
+ 'price ' => 15
139
182
],
140
183
'product2 '
141
184
),
@@ -154,25 +197,13 @@ public function testAddGroupedProductToCartWithoutImageShouldUseThumbnail()
154
197
DataFixture(GuestCartFixture::class, as: 'cart ' ),
155
198
DataFixture(QuoteMaskFixture::class, ['cart_id ' => '$cart.id$ ' ], 'quoteIdMask ' ),
156
199
]
157
- public function testAddGroupedProductToCartWithImageShouldUseProductImageAsThumbnail ()
200
+ public function testAddGroupedProductToCartWithoutImageShouldUseThumbnail ()
158
201
{
159
- $ cartId = DataFixtureStorageManager::getStorage ()->get ('quoteIdMask ' )->getMaskedId ();
160
- $ groupedProductId = DataFixtureStorageManager::getStorage ()->get ('grouped-product ' )->getSku ();
161
- $ product1 = DataFixtureStorageManager::getStorage ()->get ('product1 ' );
162
- $ product2 = DataFixtureStorageManager::getStorage ()->get ('product2 ' );
163
-
164
- $ response = $ this ->graphQlMutation ($ this ->getMutation ($ cartId , $ groupedProductId ));
165
-
166
- $ this ->assertArrayHasKey ('addProductsToCart ' , $ response );
167
- $ this ->assertEquals (2 , count ($ response ['addProductsToCart ' ]['cart ' ]['itemsV2 ' ]['items ' ]));
168
- $ this ->assertStringContainsString (
169
- $ product1 ->getCustomAttribute ('thumbnail ' )->getValue (),
170
- $ response ['addProductsToCart ' ]['cart ' ]['itemsV2 ' ]['items ' ][0 ]['product ' ]['thumbnail ' ]['url ' ]
171
- );
172
- $ this ->assertStringContainsString (
173
- $ product2 ->getCustomAttribute ('thumbnail ' )->getValue (),
174
- $ response ['addProductsToCart ' ]['cart ' ]['itemsV2 ' ]['items ' ][1 ]['product ' ]['thumbnail ' ]['url ' ]
175
- );
202
+ $ thumbnails = [
203
+ 'product1 ' => self ::DEFAULT_THUMBNAIL_PATH ,
204
+ 'product2 ' => self ::DEFAULT_THUMBNAIL_PATH
205
+ ];
206
+ $ this ->assertProductThumbnailUrl ($ thumbnails );
176
207
}
177
208
178
209
/**
0 commit comments