ltsecurityinc安防产品在线商城代码

This commit is contained in:
2026-09-14 17:47:10 +08:00
parent 57d8444c15
commit 6a3c9c5bd8
14539 changed files with 1775310 additions and 0 deletions
@@ -0,0 +1,178 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\DownloadableProduct;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\ObjectManager\ObjectManager;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test cases for adding downloadable product to cart.
*/
class AddDownloadableProductToCartTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @var ObjectManager
*/
private $objectManager;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $this->objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
}
/**
* Add a downloadable product into shopping cart when "Links can be purchased separately" is enabled
*
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable_with_purchased_separately_links.php
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
*/
public function testAddDownloadableProductWithCustomLinks()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
$sku = 'downloadable-product-with-purchased-separately-links';
$qty = 1;
$links = $this->getProductsLinks($sku);
$linkId = key($links);
$query = <<<MUTATION
mutation {
addDownloadableProductsToCart(
input: {
cart_id: "{$maskedQuoteId}",
cart_items: [
{
data: {
quantity: {$qty},
sku: "{$sku}"
},
downloadable_product_links: [
{
link_id: {$linkId}
}
]
}
]
}
) {
cart {
items {
quantity
... on DownloadableCartItem {
links {
title
link_type
price
}
samples {
id
title
}
}
}
}
}
}
MUTATION;
$response = $this->graphQlMutation($query);
self::assertArrayHasKey('items', $response['addDownloadableProductsToCart']['cart']);
self::assertCount($qty, $response['addDownloadableProductsToCart']['cart']);
self::assertEquals(
$links[$linkId],
$response['addDownloadableProductsToCart']['cart']['items'][0]['links'][0]
);
}
/**
* Add a downloadable product into shopping cart when "Links can be purchased separately" is disabled
*
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable_without_purchased_separately_links.php
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
*/
public function testAddDownloadableProductWithoutCustomLinks()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
$sku = 'downloadable-product-without-purchased-separately-links';
$qty = 1;
$links = $this->getProductsLinks($sku);
$query = <<<MUTATION
mutation {
addDownloadableProductsToCart(
input: {
cart_id: "{$maskedQuoteId}",
cart_items: [
{
data: {
quantity: {$qty},
sku: "{$sku}"
}
}
]
}
) {
cart {
items {
quantity
... on DownloadableCartItem {
links {
title
link_type
price
}
}
}
}
}
}
MUTATION;
$response = $this->graphQlMutation($query);
self::assertArrayHasKey('items', $response['addDownloadableProductsToCart']['cart']);
self::assertCount($qty, $response['addDownloadableProductsToCart']['cart']);
self::assertEquals(
$links[key($links)],
$response['addDownloadableProductsToCart']['cart']['items'][0]['links'][0]
);
}
/**
* Function returns array of all product's links
*
* @param string $sku
* @return array
*/
private function getProductsLinks(string $sku) : array
{
$result = [];
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$product = $productRepository->get($sku, false, null, true);
foreach ($product->getDownloadableLinks() as $linkObject) {
$result[$linkObject->getLinkId()] = [
'title' => $linkObject->getTitle(),
'link_type' => null, //deprecated field
'price' => $linkObject->getPrice(),
];
}
return $result;
}
}
@@ -0,0 +1,233 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\DownloadableProduct;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Downloadable\Api\Data\LinkInterface;
use Magento\Downloadable\Api\Data\SampleInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for downloadable product.
*/
class DownloadableProductViewTest extends GraphQlAbstract
{
/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @magentoApiDataFixture Magento/Downloadable/_files/downloadable_product_with_files_and_sample_url.php
*/
public function testQueryAllFieldsDownloadableProductsWithDownloadableFileAndSample()
{
$productSku = 'downloadable-product';
$query = <<<QUERY
{
products(filter:{sku: {eq:"{$productSku}"}})
{
items{
id
name
sku
type_id
price{
regularPrice{
amount{
value
currency
}
adjustments{
code
description
}
}
}
... on DownloadableProduct {
links_title
links_purchased_separately
downloadable_product_links{
sample_url
sort_order
title
price
}
downloadable_product_samples{
title
sort_order
sort_order
sample_url
}
}
}
}
}
QUERY;
/** @var \Magento\Config\Model\ResourceModel\Config $config */
$config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class);
$config->saveConfig(
\Magento\Downloadable\Model\Link::XML_PATH_CONFIG_IS_SHAREABLE,
0
);
$response = $this->graphQlQuery($query);
/**
* @var ProductRepositoryInterface $productRepository
*/
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
$downloadableProduct = $productRepository->get($productSku, false, null, true);
$this->assertNull($downloadableProduct->getWeight());
$IsLinksPurchasedSeparately = $downloadableProduct->getLinksPurchasedSeparately();
$linksTitle = $downloadableProduct->getLinksTitle();
$this->assertEquals(
$IsLinksPurchasedSeparately,
$response['products']['items'][0]['links_purchased_separately']
);
$this->assertEquals($linksTitle, $response['products']['items'][0]['links_title']);
$this->assertDownloadableProductLinks($downloadableProduct, $response['products']['items'][0]);
$this->assertDownloadableProductSamples($downloadableProduct, $response['products']['items'][0]);
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testDownloadableProductQueryWithNoSample()
{
$productSku = 'downloadable-product';
$query = <<<QUERY
{
products(filter:{sku: {eq:"{$productSku}"}})
{
items{
id
name
sku
type_id
...on PhysicalProductInterface{
weight
}
price{
regularPrice{
amount{
value
currency
}
adjustments{
code
description
}
}
}
... on DownloadableProduct {
links_title
links_purchased_separately
downloadable_product_links{
sample_url
sort_order
title
price
}
downloadable_product_samples{
title
sort_order
sample_url
}
}
}
}
}
QUERY;
$response = $this->graphQlQuery($query);
/**
* @var ProductRepositoryInterface $productRepository
*/
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
$downloadableProduct = $productRepository->get($productSku, false, null, true);
/** @var \Magento\Config\Model\ResourceModel\Config $config */
$config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class);
$config->saveConfig(
\Magento\Downloadable\Model\Link::XML_PATH_CONFIG_IS_SHAREABLE,
0,
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
1
);
$IsLinksPurchasedSeparately = $downloadableProduct->getLinksPurchasedSeparately();
$linksTitle = $downloadableProduct->getLinksTitle();
$this->assertEquals(
$IsLinksPurchasedSeparately,
$response['products']['items'][0]['links_purchased_separately']
);
$this->assertEquals($linksTitle, $response['products']['items'][0]['links_title']);
$this->assertEmpty($response['products']['items'][0]['downloadable_product_samples']);
$this->assertNotEmpty(
$response['products']['items'][0]['downloadable_product_links'],
"Precondition failed: 'downloadable_product_links' must not be empty"
);
/** @var LinkInterface $downloadableProductLinks */
$downloadableProductLinks = $downloadableProduct->getExtensionAttributes()->getDownloadableProductLinks();
$downloadableProductLink = $downloadableProductLinks[0];
$this->assertResponseFields(
$response['products']['items'][0]['downloadable_product_links'][0],
[
'sort_order' => $downloadableProductLink->getSortOrder(),
'title' => $downloadableProductLink->getTitle(),
'price' => $downloadableProductLink->getPrice()
]
);
}
/**
* @param ProductInterface $product
* @param array $actualResponse
*/
private function assertDownloadableProductLinks($product, $actualResponse)
{
$this->assertNotEmpty(
$actualResponse['downloadable_product_links'],
"Precondition failed: 'downloadable_product_links' must not be empty"
);
/** @var LinkInterface $downloadableProductLinks */
$downloadableProductLinks = $product->getExtensionAttributes()->getDownloadableProductLinks();
$downloadableProductLink = $downloadableProductLinks[1];
$this->assertNotEmpty($actualResponse['downloadable_product_links'][1]['sample_url']);
$this->assertResponseFields(
$actualResponse['downloadable_product_links'][1],
[
'sort_order' => $downloadableProductLink->getSortOrder(),
'title' => $downloadableProductLink->getTitle(),
'price' => $downloadableProductLink->getPrice()
]
);
}
/**
* @param ProductInterface $product
* @param $actualResponse
*/
private function assertDownloadableProductSamples($product, $actualResponse)
{
$this->assertNotEmpty(
$actualResponse['downloadable_product_samples'],
"Precondition failed: 'downloadable_product_samples' must not be empty"
);
/** @var SampleInterface $downloadableProductSamples */
$downloadableProductSamples = $product->getExtensionAttributes()->getDownloadableProductSamples();
$downloadableProductSample = $downloadableProductSamples[0];
$this->assertNotEmpty($actualResponse['downloadable_product_samples'][0]['sample_url']);
$this->assertResponseFields(
$actualResponse['downloadable_product_samples'][0],
[
'title' => $downloadableProductSample->getTitle(),
'sort_order' => $downloadableProductSample->getSortOrder()
]
);
}
}
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\DownloadableProduct\Options\Uid;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test for downloadable product links uid
*/
class DownloadableLinksValueUidTest extends GraphQlAbstract
{
/**
* @magentoApiDataFixture Magento/Downloadable/_files/downloadable_product_with_files_and_sample_url.php
*/
public function testQueryUidForDownloadableLinks()
{
$productSku = 'downloadable-product';
$query = $this->getQuery($productSku);
$response = $this->graphQlQuery($query);
$responseProduct = $response['products']['items'][0];
self::assertNotEmpty($responseProduct['downloadable_product_links']);
foreach ($responseProduct['downloadable_product_links'] as $productLink) {
$uid = $this->getUidByLinkId((int) $productLink['id']);
self::assertEquals($uid, $productLink['uid']);
}
}
/**
* Get uid by link id
*
* @param int $linkId
*
* @return string
*/
private function getUidByLinkId(int $linkId): string
{
return base64_encode('downloadable/' . $linkId);
}
/**
* Get query
*
* @param string $sku
*
* @return string
*/
private function getQuery(string $sku): string
{
return <<<QUERY
query {
products(filter: { sku: { eq: "$sku" } }) {
items {
sku
... on DownloadableProduct {
downloadable_product_links {
id
uid
}
}
}
}
}
QUERY;
}
}
@@ -0,0 +1,202 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\DownloadableProduct;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\ObjectManager\ObjectManager;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\GraphQl\Quote\GetQuoteItemIdByReservedQuoteIdAndSku;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Item;
use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test cases for adding downloadable product to cart.
*/
class UpdateDownloadableCartItemsTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @var ObjectManager
*/
private $objectManager;
/**
* @var GetQuoteItemIdByReservedQuoteIdAndSku
*/
private $getQuoteItemIdByReservedQuoteIdAndSku;
/**
* @var QuoteFactory
*/
private $quoteFactory;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var QuoteResource
*/
private $quoteResource;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $this->objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->quoteFactory = $this->objectManager->get(QuoteFactory::class);
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$this->quoteResource = $this->objectManager->get(QuoteResource::class);
$this->getQuoteItemIdByReservedQuoteIdAndSku = $this->objectManager->get(
GetQuoteItemIdByReservedQuoteIdAndSku::class
);
}
/**
* Update a downloadable product into shopping cart when "Links can be purchased separately" is enabled
*
* @magentoApiDataFixture Magento/Checkout/_files/active_quote_with_downloadable_product.php
*/
public function testUpdateDownloadableCartItemQuantity()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
$sku = 'downloadable-product';
$qty = 1;
$finalQty = $qty + 1;
$links = $this->getProductsLinks($sku);
$linkId = key($links);
$query = <<<MUTATION
mutation {
addDownloadableProductsToCart(
input: {
cart_id: "{$maskedQuoteId}",
cart_items: [
{
data: {
quantity: {$qty},
sku: "{$sku}"
},
downloadable_product_links: [
{
link_id: {$linkId}
}
]
}
]
}
) {
cart {
items {
product {
sku
}
quantity
}
}
}
}
MUTATION;
$response = $this->graphQlMutation($query);
self::assertArrayHasKey('items', $response['addDownloadableProductsToCart']['cart']);
self::assertCount(1, $response['addDownloadableProductsToCart']['cart']['items']);
self::assertEquals($finalQty, $response['addDownloadableProductsToCart']['cart']['items'][0]['quantity']);
self::assertEquals($sku, $response['addDownloadableProductsToCart']['cart']['items'][0]['product']['sku']);
}
/**
* Update a downloadable product into shopping cart when "Links can be purchased separately" is enabled
*
* @magentoApiDataFixture Magento/Checkout/_files/active_quote_with_downloadable_product.php
*/
public function testRemoveCartItemIfQuantityIsZero()
{
$reservedOrderId = "test_order_1";
$sku = "downloadable-product";
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
/** @var Quote $quote */
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id');
$qty = 0;
$itemId = 0;
/** @var Item $item */
foreach ($quote->getAllItems() as $item) {
if ($item->getSku() == $sku) {
$itemId = $item->getId();
}
}
$query = <<<MUTATION
mutation {
updateCartItems(input: {
cart_id: "{$maskedQuoteId}"
cart_items:[
{
cart_item_id: {$itemId}
quantity: {$qty}
}
]
}) {
cart {
items {
id
quantity
}
}
}
}
MUTATION;
$response = $this->graphQlMutation($query);
self::assertArrayHasKey('updateCartItems', $response);
self::assertArrayHasKey('cart', $response['updateCartItems']);
$responseCart = $response['updateCartItems']['cart'];
self::assertCount(0, $responseCart['items']);
}
/**
* Function returns array of all product's links
*
* @param string $sku
* @return array
*/
private function getProductsLinks(string $sku) : array
{
$result = [];
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$product = $productRepository->get($sku, false, null, true);
foreach ($product->getDownloadableLinks() as $linkObject) {
$result[$linkObject->getLinkId()] = [
'title' => $linkObject->getTitle(),
'link_type' => null, //deprecated field
'price' => $linkObject->getPrice(),
];
}
return $result;
}
}