This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,126 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\GroupedProduct;
use Magento\Framework\Exception\AuthenticationException;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Wishlist\Model\Item;
use Magento\Wishlist\Model\WishlistFactory;
class AddGroupedProductToWishlistTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var WishlistFactory
*/
private $wishlistFactory;
/**
* Set Up
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
$this->wishlistFactory = $objectManager->get(WishlistFactory::class);
}
/**
* @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped.php
* @magentoConfigFixture default_store wishlist/general/active 1
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testAllFieldsGroupedProduct()
{
$productSku = 'grouped-product';
$customerId = 1;
$qty = 1;
$mutation = $this->getMutation($productSku, $qty);
$response = $this->graphQlMutation($mutation, [], '', $this->getHeaderMap());
$wishlist = $this->wishlistFactory->create()->loadByCustomerId($customerId, true);
/** @var Item $item */
$item = $wishlist->getItemCollection()->getFirstItem();
$this->assertArrayHasKey('addProductsToWishlist', $response);
$this->assertArrayHasKey('wishlist', $response['addProductsToWishlist']);
$response = $response['addProductsToWishlist']['wishlist'];
$this->assertEquals($wishlist->getItemsCount(), $response['items_count']);
$this->assertEquals($wishlist->getSharingCode(), $response['sharing_code']);
$this->assertEquals($wishlist->getUpdatedAt(), $response['updated_at']);
$this->assertEquals((int) $item->getQty(), $response['items_v2']['items'][0]['quantity']);
$this->assertEquals($item->getAddedAt(), $response['items_v2']['items'][0]['added_at']);
$this->assertEquals($productSku, $response['items_v2']['items'][0]['product']['sku']);
}
private function getMutation(
string $sku,
int $qty,
int $wishlistId = 0
): string {
return <<<MUTATION
mutation {
addProductsToWishlist(
wishlistId: {$wishlistId},
wishlistItems: [
{
sku: "{$sku}"
quantity: {$qty}
}
]
) {
user_errors {
code
message
}
wishlist {
id
sharing_code
items_count
updated_at
items_v2 {
items{
id
description
quantity
added_at
product {
sku
}
}
}
}
}
}
MUTATION;
}
/**
* Authentication header map
*
* @param string $username
* @param string $password
*
* @return array
*
* @throws AuthenticationException
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
return ['Authorization' => 'Bearer ' . $customerToken];
}
}
@@ -0,0 +1,144 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\GroupedProduct;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Class to test GraphQl response with grouped products
*/
class GroupedProductViewTest extends GraphQlAbstract
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
}
/**
* @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped.php
*/
public function testAllFieldsGroupedProduct()
{
$productSku = 'grouped-product';
$query = <<<QUERY
{
products(filter: {sku: {eq: "{$productSku}"}}) {
items {
id
name
sku
type_id
... on GroupedProduct {
items{
qty
position
product{
sku
name
type_id
url_key
}
}
product_links{
linked_product_sku
position
link_type
}
}
}
}
}
QUERY;
$response = $this->graphQlQuery($query);
$groupedProduct = $this->productRepository->get($productSku, false, null, true);
$this->assertNotEmpty(
$response['products']['items'][0]['items'],
"Precondition failed: 'Grouped product items' must not be empty"
);
$this->assertGroupedProductItems($groupedProduct, $response['products']['items'][0]['items']);
$this->assertNotEmpty(
$response['products']['items'][0]['product_links'],
"Precondition failed: 'Linked product items' must not be empty"
);
$this->assertProductLinks($groupedProduct, $response['products']['items'][0]['product_links']);
}
/**
* @param ProductInterface $product
* @param array $items
*/
private function assertGroupedProductItems(ProductInterface $product, array $items): void
{
$this->assertCount(2, $items);
$groupedProductLinks = $product->getProductLinks();
foreach ($items as $itemIndex => $bundleItem) {
$this->assertNotEmpty($bundleItem);
$associatedProductSku = $groupedProductLinks[$itemIndex]->getLinkedProductSku();
$associatedProduct = $this->productRepository->get($associatedProductSku);
$this->assertEquals(
$groupedProductLinks[$itemIndex]->getExtensionAttributes()->getQty(),
$bundleItem['qty']
);
$this->assertEquals(
$groupedProductLinks[$itemIndex]->getPosition(),
$bundleItem['position']
);
$this->assertResponseFields(
$bundleItem['product'],
[
'sku' => $associatedProductSku,
'type_id' => $groupedProductLinks[$itemIndex]->getLinkedProductType(),
'url_key'=> $associatedProduct->getUrlKey(),
'name' => $associatedProduct->getName()
]
);
}
}
/**
* @param ProductInterface $product
* @param array $links
* @return void
*/
private function assertProductLinks(ProductInterface $product, array $links): void
{
$this->assertCount(2, $links);
$productLinks = $product->getProductLinks();
foreach ($links as $itemIndex => $linkedItem) {
$this->assertNotEmpty($linkedItem);
$this->assertEquals(
$productLinks[$itemIndex]->getPosition(),
$linkedItem['position']
);
$this->assertEquals(
$productLinks[$itemIndex]->getLinkedProductSku(),
$linkedItem['linked_product_sku']
);
$this->assertEquals(
$productLinks[$itemIndex]->getLinkType(),
$linkedItem['link_type']
);
}
}
}
@@ -0,0 +1,147 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\GroupedProduct;
use Magento\TestFramework\TestCase\GraphQlAbstract;
class ProductViewTest extends GraphQlAbstract
{
/**
* @magentoApiDataFixture Magento/Store/_files/second_website_with_two_stores.php
* @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped_in_multiple_websites.php
*/
public function testGroupedProductAssignedToOneWebsite()
{
$headerMapFirstStore['Store'] = 'default';
$headerMapSecondStore['Store'] = 'fixture_second_store';
$productSku = 'grouped-product';
$query = $this->getQuery($productSku);
$responseForFirstWebsite = $this->graphQlQuery($query, [], '', $headerMapFirstStore);
$responseForSecondWebsite = $this->graphQlQuery($query, [], '', $headerMapSecondStore);
self::assertEmpty($responseForFirstWebsite['products']['items']);
$groupedProductLinks = [
[
'qty' => 1,
'position' => 1,
'product' => [
'sku' => 'simple',
'name' => 'Simple Product',
'type_id' => 'simple',
'url_key' => 'simple-product'
]
],
[
'qty' => 2,
'position' => 2,
'product' => [
'sku' => 'virtual-product',
'name' => 'Virtual Product',
'type_id' => 'virtual',
'url_key' => 'virtual-product'
]
]
];
$this->assertGroupedProductItems($groupedProductLinks, $responseForSecondWebsite['products']['items'][0]);
}
/**
* @magentoApiDataFixture Magento/Store/_files/second_website_with_two_stores.php
* @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped_items_in_multiple_websites.php
*/
public function testGroupedProductItemsAssignedToDifferentWebsites()
{
$headerMapFirstStore['Store'] = 'default';
$headerMapSecondStore['Store'] = 'fixture_second_store';
$productSku = 'grouped-product';
$query = $this->getQuery($productSku);
$responseForFirstWebsite = $this->graphQlQuery($query, [], '', $headerMapFirstStore);
$responseForSecondWebsite = $this->graphQlQuery($query, [], '', $headerMapSecondStore);
$firstWebsiteGroupedProductLinks = [
[
'qty' => 1,
'position' => 1,
'product' => [
'sku' => 'simple',
'name' => 'Simple Product',
'type_id' => 'simple',
'url_key' => 'simple-product'
]
]
];
$secondWebsiteGroupedProductLinks = [
[
'qty' => 2,
'position' => 2,
'product' => [
'sku' => 'virtual-product',
'name' => 'Virtual Product',
'type_id' => 'virtual',
'url_key' => 'virtual-product'
]
]
];
$this->assertGroupedProductItems(
$firstWebsiteGroupedProductLinks,
$responseForFirstWebsite['products']['items'][0]
);
$this->assertGroupedProductItems(
$secondWebsiteGroupedProductLinks,
$responseForSecondWebsite['products']['items'][0]
);
}
/**
* @param string $sku
* @return string
*/
private function getQuery(string $sku): string
{
return <<<QUERY
{
products(filter: {sku: {eq: "{$sku}"}}) {
items {
id
name
sku
type_id
... on GroupedProduct {
items{
qty
position
product{
sku
name
type_id
url_key
}
}
}
}
}
}
QUERY;
}
/**
* @param array $groupedProductLinks
* @param $actualResponse
*/
private function assertGroupedProductItems(array $groupedProductLinks, $actualResponse)
{
self::assertNotEmpty(
$actualResponse['items'],
"Precondition failed: 'grouped product items' must not be empty"
);
self::assertCount(count($groupedProductLinks), $actualResponse['items']);
foreach ($actualResponse['items'] as $itemIndex => $bundleItems) {
self::assertEquals($groupedProductLinks[$itemIndex], $bundleItems);
}
}
}