Skip to content

Fix integration tests for Magento_Backend and Magento_Bundle #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public function execute(int $entityId, int $storeId): bool
['child_products' => $this->resourceConnection->getTableName('catalog_product_entity')],
'child_products.entity_id = bundle_selections.product_id',
[]
)->joinInner(
['child_stock_item' => $this->resourceConnection->getTableName('cataloginventory_stock_item')],
'child_stock_item.product_id = child_products.entity_id',
[]
)->group(
['bundle_options.parent_id', 'bundle_options.option_id']
)->where(
Expand Down Expand Up @@ -103,18 +107,36 @@ public function execute(int $entityId, int $storeId): bool
'1',
'0'
);

$hasMinRequiredQuantity = $connection->getCheckSql(
'required = 1 AND manage_stock = 1 AND selection_can_change_qty = 0',
'(qty >= bundle_selections.selection_qty OR backorders > 0) AND is_in_stock = 1',
'1'
);

$requiredInStock = $connection->getCheckSql(
'required = 1 AND manage_stock = 1 AND selection_can_change_qty = 1',
'(qty >= 0 OR backorders > 0) AND is_in_stock = 1',
'1'
);

$optionsSaleabilitySelect->columns([
'required' => 'bundle_options.required',
'is_salable' => $isOptionSalableExpr,
'is_required_and_unsalable' => $isRequiredOptionUnsalable,
'has_min_required_quantity' => $hasMinRequiredQuantity,
'required_in_stock' => $requiredInStock
]);

$select = $connection->select()->from(
$optionsSaleabilitySelect,
[new \Zend_Db_Expr('(MAX(is_salable) = 1 AND MAX(is_required_and_unsalable) = 0)')]
[new \Zend_Db_Expr(
'(MAX(is_salable) = 1 AND MAX(is_required_and_unsalable) = 0)' .
'AND MIN(required_in_stock) = 1 AND MIN(has_min_required_quantity) = 1'
)]
);
$isSalable = $connection->fetchOne($select);

$isSalable = $connection->fetchOne($select);
return (bool) $isSalable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

namespace Magento\Backend\Block\Page;

use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\ProductMetadata;
use Magento\TestFramework\Helper\Bootstrap;

/**
* Test \Magento\Backend\Block\Page\Footer
*
Expand All @@ -16,7 +20,7 @@ class FooterTest extends \PHPUnit\Framework\TestCase
/**
* Test Product Version Value
*/
const TEST_PRODUCT_VERSION = '222.333.444';
private const TEST_PRODUCT_VERSION = '222.333.444';

/**
* @var \Magento\Backend\Block\Page\Footer
Expand All @@ -26,13 +30,14 @@ class FooterTest extends \PHPUnit\Framework\TestCase
protected function setUp(): void
{
parent::setUp();
$productMetadataMock = $this->getMockBuilder(\Magento\Framework\App\ProductMetadata::class)
->onlyMethods(['getVersion'])
$productMetadataMock = $this->getMockBuilder(ProductMetadata::class)
->disableOriginalConstructor()
->getMock();

$productMetadataMock->expects($this->once())
->method('getVersion')
->method('getDistributionVersion')
->willReturn($this::TEST_PRODUCT_VERSION);

$this->block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Framework\View\LayoutInterface::class
)->createBlock(
Expand All @@ -44,6 +49,8 @@ protected function setUp(): void

public function testToHtml()
{
/** @var \Magento\Framework\App\CacheInterface $cacheManager */
Bootstrap::getObjectManager()->create(CacheInterface::class);
$footerContent = $this->block->toHtml();
$this->assertStringContainsString(
'ver. ' . $this::TEST_PRODUCT_VERSION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Magento\Bundle\Model\Product;

use Magento\Framework\Exception\NoSuchEntityException;

/**
* Test class for \Magento\Bundle\Model\Product\Type (bundle product type)
*
Expand Down Expand Up @@ -216,6 +218,7 @@ public function testIsSaleableOnBundleWithoutSaleableSelectionsOnRequiredOption(
/**
* Check bundle product is NOT saleable if
* there are not enough qty of selection on required option
* when user cannot define own quantities
*
* @magentoAppIsolation enabled
* @covers \Magento\Bundle\Model\Product\Type::isSalable
Expand All @@ -224,9 +227,7 @@ public function testIsSaleableOnBundleWithoutSaleableSelectionsOnRequiredOption(
public function testIsSaleableOnBundleWithNotEnoughQtyOfSelection()
{
$this->setQtyForSelections(['simple1', 'simple2', 'simple3'], 1);

$bundleProduct = $this->productRepository->get('bundle-product');

$this->assertFalse(
$bundleProduct->isSalable(),
'Bundle product supposed to be non saleable'
Expand Down Expand Up @@ -354,4 +355,22 @@ private function setQtyForSelections($productsSku, $qty)
$this->productRepository->save($product);
}
}

/**
* Check bundle product is not salable if required option where user can
* set own quantity is not in stock
*
* @return void
* @magentoAppIsolation enabled
* @throws NoSuchEntityException
*/
public function testIsSalableOnBundleWithRequiredOptionUserCanChangeQtyWithoutStock()
{
$product = $this->productRepository->get('bundle-product-checkbox-required-option');
$this->setQtyForSelections(['simple1'], 0);
$this->assertFalse(
$product->isSalable(),
'Bundle product with required option that has 0 stock should not be salable'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,49 @@

Resolver::getInstance()->requireDataFixture('Magento/Bundle/_files/multiple_products.php');

if (!function_exists('prepareBundleOptions')) {
function prepareBundleOptions(Magento\Catalog\Model\Product $product) {
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Catalog\Model\ProductRepository $productRepository */
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
if ($product->getBundleOptionsData()) {
$options = [];
foreach ($product->getBundleOptionsData() as $key => $optionData) {
if (!(bool)$optionData['delete']) {
$option = $objectManager->create(\Magento\Bundle\Api\Data\OptionInterfaceFactory::class)
->create(['data' => $optionData]);
$option->setSku($product->getSku());
$option->setOptionId(null);

$links = [];
$bundleLinks = $product->getBundleSelectionsData();
if (!empty($bundleLinks[$key])) {
foreach ($bundleLinks[$key] as $linkData) {
if (!(bool)$linkData['delete']) {
$link = $objectManager->create(\Magento\Bundle\Api\Data\LinkInterfaceFactory::class)
->create(['data' => $linkData]);
$linkProduct = $productRepository->getById($linkData['product_id']);
$link->setSku($linkProduct->getSku());
$link->setQty($linkData['selection_qty']);
if (isset($linkData['selection_can_change_qty'])) {
$link->setCanChangeQuantity((bool)$linkData['selection_can_change_qty']);
}
$links[] = $link;
}
}
$option->setProductLinks($links);
$options[] = $option;
}
}
}
$extension = $product->getExtensionAttributes();
$extension->setBundleProductOptions($options);
$product->setExtensionAttributes($extension);
}
$productRepository->save($product, true);
}
}

$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Catalog\Model\ProductRepository $productRepository */
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
Expand Down Expand Up @@ -177,36 +220,43 @@
]
);

if ($product->getBundleOptionsData()) {
$options = [];
foreach ($product->getBundleOptionsData() as $key => $optionData) {
if (!(bool)$optionData['delete']) {
$option = $objectManager->create(\Magento\Bundle\Api\Data\OptionInterfaceFactory::class)
->create(['data' => $optionData]);
$option->setSku($product->getSku());
$option->setOptionId(null);
$bundleProduct = $objectManager->create(\Magento\Catalog\Model\Product::class);;
$bundleProduct->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)
->setId(4)
->setAttributeSetId(4)
->setWebsiteIds([1])
->setName('Bundle Product Checkbox Required Option')
->setSku('bundle-product-checkbox-required-option')
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
->setPriceView(0)
->setSkuType(1)
->setWeightType(1)
->setPriceType(\Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC)
->setPrice(10.0)
->setShipmentType(Magento\Catalog\Model\Product\Type\AbstractType::SHIPMENT_TOGETHER)
->setBundleOptionsData([
[
'title' => 'Checkbox Options',
'default_title' => 'Checkbox Options',
'type' => 'checkbox',
'required' => 1,
'delete' => '',
],
])
->setBundleSelectionsData([
[
[
'product_id' => 10,
'selection_qty' => 1,
'selection_can_change_qty' => 1,
'delete' => '',
'option_id' => 6
],
]
]);

$links = [];
$bundleLinks = $product->getBundleSelectionsData();
if (!empty($bundleLinks[$key])) {
foreach ($bundleLinks[$key] as $linkData) {
if (!(bool)$linkData['delete']) {
$link = $objectManager->create(\Magento\Bundle\Api\Data\LinkInterfaceFactory::class)
->create(['data' => $linkData]);
$linkProduct = $productRepository->getById($linkData['product_id']);
$link->setSku($linkProduct->getSku());
$link->setQty($linkData['selection_qty']);
$links[] = $link;
}
}
$option->setProductLinks($links);
$options[] = $option;
}
}
}
$extension = $product->getExtensionAttributes();
$extension->setBundleProductOptions($options);
$product->setExtensionAttributes($extension);
}
prepareBundleOptions($product);
prepareBundleOptions($bundleProduct);

$productRepository->save($product, true);
Loading