1616
1717namespace Magento \GraphQl \Quote ;
1818
19+ use Exception ;
20+ use Magento \Catalog \Api \ProductRepositoryInterface ;
21+ use Magento \Framework \Exception \LocalizedException ;
1922use Magento \TestFramework \Fixture \Config as ConfigFixture ;
2023use Magento \Catalog \Test \Fixture \Category as CategoryFixture ;
2124use Magento \Catalog \Test \Fixture \Product as ProductFixture ;
2427use Magento \Quote \Test \Fixture \QuoteIdMask as QuoteMaskFixture ;
2528use Magento \TestFramework \Fixture \DataFixture ;
2629use Magento \TestFramework \Fixture \DataFixtureStorageManager ;
30+ use Magento \TestFramework \Helper \Bootstrap ;
2731use Magento \TestFramework \TestCase \GraphQlAbstract ;
2832
2933/**
3034 * Test adding grouped products returns default thumbnail/product image thumbnail
3135 */
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+ ]
3294class AddGroupedProductToCartThumbnailTest extends GraphQlAbstract
3395{
3496 private const DEFAULT_THUMBNAIL_PATH = 'Magento_Catalog/images/product/placeholder/thumbnail.jpg ' ;
3597
98+ /**
99+ * @throws LocalizedException
100+ */
36101 #[
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 ' ),
72103 ]
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
74141 {
75142 $ 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 ));
78145
79146 $ this ->assertArrayHasKey ('addProductsToCart ' , $ response );
80147 $ this ->assertEquals (2 , count ($ response ['addProductsToCart ' ]['cart ' ]['itemsV2 ' ]['items ' ]));
148+
81149 $ this ->assertStringContainsString (
82- self :: DEFAULT_THUMBNAIL_PATH ,
150+ $ thumbnails [ ' product1 ' ] ,
83151 $ response ['addProductsToCart ' ]['cart ' ]['itemsV2 ' ]['items ' ][0 ]['product ' ]['thumbnail ' ]['url ' ]
84152 );
85153 $ this ->assertStringContainsString (
86- self :: DEFAULT_THUMBNAIL_PATH ,
154+ $ thumbnails [ ' product2 ' ] ,
87155 $ response ['addProductsToCart ' ]['cart ' ]['itemsV2 ' ]['items ' ][1 ]['product ' ]['thumbnail ' ]['url ' ]
88156 );
89157 }
90158
159+ /**
160+ * @throws LocalizedException
161+ */
91162 #[
92163 ConfigFixture('checkout/cart/grouped_product_image ' , 'itself ' ),
93164 DataFixture(CategoryFixture::class, ['name ' => 'Category ' ], 'category ' ),
@@ -97,21 +168,7 @@ public function testAddGroupedProductToCartWithoutImageShouldUseThumbnail()
97168 'name ' => 'Product 1 ' ,
98169 'sku ' => 'product-1 ' ,
99170 '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
115172 ],
116173 'product1 '
117174 ),
@@ -121,21 +178,7 @@ public function testAddGroupedProductToCartWithoutImageShouldUseThumbnail()
121178 'name ' => 'Product 2 ' ,
122179 'sku ' => 'product-2 ' ,
123180 '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
139182 ],
140183 'product2 '
141184 ),
@@ -154,25 +197,13 @@ public function testAddGroupedProductToCartWithoutImageShouldUseThumbnail()
154197 DataFixture(GuestCartFixture::class, as: 'cart ' ),
155198 DataFixture(QuoteMaskFixture::class, ['cart_id ' => '$cart.id$ ' ], 'quoteIdMask ' ),
156199 ]
157- public function testAddGroupedProductToCartWithImageShouldUseProductImageAsThumbnail ()
200+ public function testAddGroupedProductToCartWithoutImageShouldUseThumbnail ()
158201 {
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 );
176207 }
177208
178209 /**
0 commit comments