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,59 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GroupedProduct\Model\Inventory;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\CatalogInventory\Model\Stock\StockItemRepository;
use PHPUnit\Framework\TestCase;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Framework\ObjectManagerInterface;
/**
* Test stock status parent product
*/
class ParentItemProcessorTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
protected $objectManager;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
}
/**
* Test stock status parent product if children are out of stock
*
* @magentoDataFixture Magento/GroupedProduct/_files/product_grouped_with_simple_out_of_stock.php
*
* @return void
*/
public function testOutOfStockParentProduct(): void
{
$productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
/** @var Product $product */
$product = $productRepository->get('simple_100000001');
$product->setStockData(['qty' => 0, 'is_in_stock' => 0]);
$productRepository->save($product);
/** @var StockItemRepository $stockItemRepository */
$stockItemRepository = $this->objectManager->create(StockItemRepository::class);
/** @var StockRegistryInterface $stockRegistry */
$stockRegistry = $this->objectManager->create(StockRegistryInterface::class);
$stockItem = $stockRegistry->getStockItemBySku('grouped');
$stockItem = $stockItemRepository->get($stockItem->getItemId());
$this->assertEquals(false, $stockItem->getIsInStock());
}
}
@@ -0,0 +1,309 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\GroupedProduct\Model\Product\Type;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
use Magento\Catalog\Test\Fixture\Virtual as VirtualProductFixture;
use Magento\CatalogInventory\Model\Configuration;
use Magento\Framework\App\Config\ReinitableConfigInterface;
use Magento\Framework\App\Config\Value;
use Magento\GroupedProduct\Test\Fixture\Product as GroupedProductFixture;
use Magento\TestFramework\Fixture\AppArea;
use Magento\TestFramework\Fixture\DataFixture;
class GroupedTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ReinitableConfigInterface
*/
private $reinitableConfig;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $objectManager;
/**
* @var \Magento\Catalog\Model\Product\Type
*/
protected $_productType;
protected function setUp(): void
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->_productType = $this->objectManager->get(\Magento\Catalog\Model\Product\Type::class);
$this->reinitableConfig = $this->objectManager->get(ReinitableConfigInterface::class);
}
protected function tearDown(): void
{
$this->dropConfigValue(Configuration::XML_PATH_SHOW_OUT_OF_STOCK);
}
public function testFactory()
{
$product = new \Magento\Framework\DataObject();
$product->setTypeId(Grouped::TYPE_CODE);
$type = $this->_productType->factory($product);
$this->assertInstanceOf(Grouped::class, $type);
}
#[
AppArea('frontend'),
DataFixture(ProductFixture::class, ['sku' => 'simple', 'name' => 'Simple Product', 'price' => 10], 'p1'),
DataFixture(
VirtualProductFixture::class,
['sku' => 'virtual-product', 'name' => 'Virtual Product', 'price' => 10],
'p2'
),
DataFixture(
GroupedProductFixture::class,
['sku' => 'gr1', 'product_links' => ['$p1$', ['sku' => '$p2.sku$', 'qty' => 2]]],
'gr1'
),
]
public function testGetAssociatedProducts()
{
$productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
/** @var Product $product */
$product = $productRepository->get('gr1');
$type = $product->getTypeInstance();
$this->assertInstanceOf(Grouped::class, $type);
$associatedProducts = $type->getAssociatedProducts($product);
$this->assertCount(2, $associatedProducts);
$this->assertProductInfo($associatedProducts[0]);
$this->assertProductInfo($associatedProducts[1]);
}
/**
* @param Product $product
*/
private function assertProductInfo($product)
{
$data = [
'simple' => [
'sku' => 'simple',
'name' => 'Simple Product',
'price' => '10.000000',
'qty' => '1',
'position' => '1'
],
'virtual-product' => [
'sku' => 'virtual-product',
'name' => 'Virtual Product',
'price' => '10.000000',
'qty' => '2',
'position' => '2'
]
];
$productId = $product->getSku();
$this->assertEquals($data[$productId]['sku'], $product->getSku());
$this->assertEquals($data[$productId]['name'], $product->getName());
$this->assertEquals($data[$productId]['price'], $product->getPrice());
$this->assertEquals($data[$productId]['qty'], $product->getQty());
$this->assertEquals($data[$productId]['position'], $product->getPosition());
}
/**
* @magentoDataFixture Magento/GroupedProduct/_files/product_grouped.php
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
public function testPrepareProduct()
{
$buyRequest = $this->objectManager->create(
\Magento\Framework\DataObject::class,
['data' => ['value' => ['qty' => 2]]]
);
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$product = $productRepository->get('grouped-product');
/** @var Grouped $type */
$type = $this->objectManager->get(Grouped::class);
$processModes = [
Grouped::PROCESS_MODE_FULL,
Grouped::PROCESS_MODE_LITE
];
$expectedData = [
Grouped::PROCESS_MODE_FULL => [
1 => '{"super_product_config":{"product_type":"grouped","product_id":"'
. $product->getId() . '"}}',
21 => '{"super_product_config":{"product_type":"grouped","product_id":"'
. $product->getId() . '"}}',
],
Grouped::PROCESS_MODE_LITE => [
$product->getId() => '{"value":{"qty":2}}',
]
];
foreach ($processModes as $processMode) {
$products = $type->processConfiguration($buyRequest, $product, $processMode);
foreach ($products as $item) {
$productId = $item->getId();
$this->assertEquals(
$expectedData[$processMode][$productId],
$item->getCustomOptions()['info_buyRequest']->getValue(),
"Wrong info_buyRequest data for product with id: $productId"
);
}
}
}
/**
* Test adding grouped product to cart when one of subproducts is out of stock.
*
* @magentoDataFixture Magento/GroupedProduct/_files/product_grouped_with_out_of_stock.php
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
* @dataProvider outOfStockSubProductDataProvider
* @param bool $outOfStockShown
* @param array $data
* @param array $expected
*/
public function testOutOfStockSubProduct(bool $outOfStockShown, array $data, array $expected)
{
$this->changeConfigValue(Configuration::XML_PATH_SHOW_OUT_OF_STOCK, $outOfStockShown);
$buyRequest = new \Magento\Framework\DataObject($data);
$productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
/** @var Product $product */
$product = $productRepository->get('grouped-product');
/** @var Grouped $groupedProduct */
$groupedProduct = $this->objectManager->get(Grouped::class);
$actual = $groupedProduct->prepareForCartAdvanced($buyRequest, $product, Grouped::PROCESS_MODE_FULL);
self::assertEquals(
count($expected),
count($actual)
);
/** @var Product $product */
foreach ($actual as $product) {
$sku = $product->getSku();
self::assertEquals(
$expected[$sku],
$product->getCartQty(),
"Failed asserting that Product Cart Quantity matches expected"
);
}
}
/**
* Data provider for testOutOfStockSubProduct.
*
* @return array
*/
public function outOfStockSubProductDataProvider()
{
return [
'Out of stock product are shown #1' => [
true,
[
'product' => 3,
'qty' => 1,
'super_group' => [
1 => 4,
21 => 5,
],
],
[
'virtual-product' => 5,
'simple' => 4
],
],
'Out of stock product are shown #2' => [
true,
[
'product' => 3,
'qty' => 1,
'super_group' => [
1 => 0,
],
],
[
'virtual-product' => 2.5, // This is a default quantity.
],
],
'Out of stock product are hidden #1' => [
false,
[
'product' => 3,
'qty' => 1,
'super_group' => [
1 => 4,
21 => 5,
],
],
[
'virtual-product' => 5,
'simple' => 4,
],
],
'Out of stock product are hidden #2' => [
false,
[
'product' => 3,
'qty' => 1,
'super_group' => [
1 => 0,
],
],
[
'virtual-product' => 2.5, // This is a default quantity.
],
],
];
}
/**
* Write config value to database.
*
* @param string $path
* @param string $value
* @param string $scope
* @param int $scopeId
*/
private function changeConfigValue(string $path, string $value, string $scope = 'default', int $scopeId = 0)
{
$configValue = $this->objectManager->create(Value::class);
$configValue->setPath($path)
->setValue($value)
->setScope($scope)
->setScopeId($scopeId)
->save();
$this->reinitConfig();
}
/**
* Delete config value from database.
*
* @param string $path
*/
private function dropConfigValue(string $path)
{
$configValue = $this->objectManager->create(Value::class);
try {
$configValue->load($path, 'path');
$configValue->delete();
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
// do nothing
}
$this->reinitConfig();
}
/**
* Reinit config.
*/
private function reinitConfig()
{
$this->reinitableConfig->reinit();
}
}
@@ -0,0 +1,86 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GroupedProduct\Model\ProductLink;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\ProductLink\Data\ListCriteria;
use Magento\Catalog\Model\ProductLink\ProductLinkQuery;
use Magento\Catalog\Model\ProductRepository;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Test grouped links.
*/
class GroupedProductLinkQueryTest extends TestCase
{
/**
* @var ProductLinkQuery
*/
private $query;
/**
* @var ProductRepository
*/
private $productRepo;
/**
* @var SearchCriteriaBuilder
*/
private $criteriaBuilder;
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();
$objectManager = Bootstrap::getObjectManager();
$this->query = $objectManager->get(ProductLinkQuery::class);
$this->productRepo = $objectManager->get(ProductRepository::class);
$this->criteriaBuilder = $objectManager->get(SearchCriteriaBuilder::class);
}
/**
* Test getting links for a list of products.
*
* @magentoDataFixture Magento/GroupedProduct/_files/product_grouped.php
* @return void
* @throws \Throwable
*/
public function testSearch(): void
{
$sku = 'grouped-product';
//Generating criteria
/** @var ListCriteria[] $criteriaList */
$criteriaList = [
new ListCriteria($sku, ['associated']),
new ListCriteria($sku, ['related'])
];
//Finding the list
$result = $this->query->search($criteriaList);
//Validating results
//1st criteria
$this->assertEmpty($result[0]->getError());
$this->assertNotEmpty($result[0]->getResult());
$this->assertCount(2, $result[0]->getResult());
foreach ($result[0]->getResult() as $link) {
$this->assertEquals($sku, $link->getSku());
$this->assertEquals('associated', $link->getLinkType());
$this->assertContains($link->getLinkedProductSku(), ['virtual-product', 'simple']);
$this->assertNotEmpty($link->getExtensionAttributes()->getQty());
}
//2nd criteria
$this->assertEmpty($result[1]->getError());
$this->assertEmpty($result[1]->getResult());
}
}
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\GroupedProduct\Model\ResourceModel\Product\Indexer\Price;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory;
use Magento\Catalog\Model\Indexer\Product\Price\Processor;
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
/**
* Test class for Magento\GroupedProduct\Model\ResourceModel\Product\Indexer\Price\Grouped
*/
class GroupedTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var CollectionFactory
*/
private $productCollectionFactory;
/**
* @var Processor
*/
private $indexerProcessor;
/**
* @var ProductTierPriceInterfaceFactory
*/
private $tierPriceFactory;
protected function setUp(): void
{
$this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
$this->productCollectionFactory = Bootstrap::getObjectManager()->get(CollectionFactory::class);
$this->indexerProcessor = Bootstrap::getObjectManager()->get(Processor::class);
$this->tierPriceFactory = Bootstrap::getObjectManager()->get(ProductTierPriceInterfaceFactory::class);
}
/**
* @magentoDataFixture Magento/GroupedProduct/_files/product_grouped.php
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
public function testReindex()
{
$simpleProductPrice = 15;
$virtualProductPrice = 5;
/** @var \Magento\Catalog\Model\Product $simpleProduct */
$simpleProduct = $this->productRepository->get('simple', true);
/** @var \Magento\Catalog\Model\Product $virtualProduct */
$virtualProduct = $this->productRepository->get('virtual-product', true);
$simpleProduct->setData('price', $simpleProductPrice);
$virtualProduct->setData('price', $virtualProductPrice);
$this->productRepository->save($simpleProduct);
$this->productRepository->save($virtualProduct);
$this->indexerProcessor->reindexAll();
/** @var ProductCollection $collection */
$collection = $this->productCollectionFactory->create();
$collection->addPriceData()->addFieldToFilter(ProductInterface::SKU, 'grouped-product');
/** @var \Magento\Catalog\Model\Product $item */
$item = $collection->getFirstItem();
$this->assertEquals($virtualProductPrice, $item->getData('min_price'));
$this->assertEquals($simpleProductPrice, $item->getData('max_price'));
}
}
@@ -0,0 +1,66 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\GroupedProduct\Model\ResourceModel\Product\Indexer\Stock;
class GroupedTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\CatalogInventory\Model\Indexer\Stock\Processor
*/
protected $processor;
protected function setUp(): void
{
$this->processor = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\CatalogInventory\Model\Indexer\Stock\Processor::class
);
}
/**
* @magentoDbIsolation disabled
* @magentoAppIsolation enabled
* @magentoDataFixture Magento/GroupedProduct/_files/product_grouped.php
*/
public function testReindexAll()
{
$this->processor->reindexAll();
/** @var \Magento\Catalog\Model\CategoryFactory $categoryFactory */
$categoryFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Catalog\Model\CategoryFactory::class
);
$category = $categoryFactory->create()->load(2);
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$productCollection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Catalog\Model\ResourceModel\Product\Collection::class
);
$productCollection->addAttributeToSelect('name');
$productCollection->addUrlRewrite($category->getId());
$productCollection->joinField(
'qty',
'cataloginventory_stock_status',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left'
);
$this->assertCount(3, $productCollection);
$expectedResult = [
'Simple Product' => 22,
'Virtual Product' => 10,
'Grouped Product' => 0
];
/** @var $product \Magento\Catalog\Model\Product */
foreach ($productCollection as $product) {
$this->assertEquals($expectedResult[$product->getName()], $product->getQty());
}
}
}
@@ -0,0 +1,38 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\GroupedProduct\Model\ResourceModel\Product\Type\Grouped;
class AssociatedProductsCollectionTest extends \PHPUnit\Framework\TestCase
{
/**
* @magentoDataFixture Magento/GroupedProduct/_files/product_grouped.php
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
public function testGetColumnValues()
{
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
/** @var $product \Magento\Catalog\Model\Product */
$product = $productRepository->get('grouped-product');
/** @var $objectManager \Magento\TestFramework\ObjectManager */
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$objectManager->get(\Magento\Framework\Registry::class)->register('current_product', $product);
$collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\GroupedProduct\Model\ResourceModel\Product\Type\Grouped\AssociatedProductsCollection::class
);
$resultData = $collection->getColumnValues('sku');
$this->assertNotEmpty($resultData);
$expected = ['virtual-product', 'simple'];
sort($expected);
sort($resultData);
$this->assertEquals($expected, $resultData);
}
}