Skip to content

Commit

Permalink
Merge pull request #5333 from magento-tsg/2.4-develop-com-pr7
Browse files Browse the repository at this point in the history
[TSG-Commerce] Tests for 2.4 (pr7)
  • Loading branch information
zakdma authored Feb 14, 2020
2 parents ff70b28 + 569b34a commit e280215
Show file tree
Hide file tree
Showing 50 changed files with 3,709 additions and 182 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\TestFramework\Bundle\Model;

use Magento\Bundle\Api\Data\LinkInterfaceFactory;
use Magento\Bundle\Api\Data\OptionInterfaceFactory;
use Magento\Bundle\Model\Product\Price;
use Magento\Catalog\Api\Data\ProductExtensionFactory;
use Magento\Catalog\Api\Data\ProductInterface;

/**
* Prepare bundle product links
*/
class PrepareBundleLinks
{
/** @var LinkInterfaceFactory */
private $linkFactory;

/** @var OptionInterfaceFactory */
private $optionLinkFactory;

/** @var ProductExtensionFactory */
private $extensionAttributesFactory;

/**
* @param LinkInterfaceFactory $linkFactory
* @param OptionInterfaceFactory $optionLinkFactory
* @param ProductExtensionFactory $extensionAttributesFactory
*/
public function __construct(
LinkInterfaceFactory $linkFactory,
OptionInterfaceFactory $optionLinkFactory,
ProductExtensionFactory $extensionAttributesFactory
) {
$this->linkFactory = $linkFactory;
$this->optionLinkFactory = $optionLinkFactory;
$this->extensionAttributesFactory = $extensionAttributesFactory;
}

/**
* Prepare bundle product links
*
* @param ProductInterface $product
* @param array $bundleOptionsData
* @param array $bundleSelectionsData
* @return ProductInterface
*/
public function execute(
ProductInterface $product,
array $bundleOptionsData,
array $bundleSelectionsData
): ProductInterface {
$product->setBundleOptionsData($bundleOptionsData)
->setBundleSelectionsData($bundleSelectionsData);
$options = [];
foreach ($product->getBundleOptionsData() as $key => $optionData) {
$option = $this->optionLinkFactory->create(['data' => $optionData]);
$option->setSku($product->getSku());
$option->setOptionId(null);
$links = [];
$bundleLinks = $product->getBundleSelectionsData();
foreach ($bundleLinks[$key] as $linkData) {
$link = $this->linkFactory->create(['data' => $linkData]);
$link->setQty($linkData['selection_qty']);
$priceType = $price = null;
if ($product->getPriceType() === Price::PRICE_TYPE_FIXED) {
$priceType = $linkData['selection_price_type'] ?? null;
$price = $linkData['selection_price_value'] ?? null;
}
$link->setPriceType($priceType);
$link->setPrice($price);
$links[] = $link;
}
$option->setProductLinks($links);
$options[] = $option;
}
/** @var ProductExtensionFactory $extensionAttributesFactory */
$extensionAttributes = $product->getExtensionAttributes() ?? $this->extensionAttributesFactory->create();
$extensionAttributes->setBundleProductOptions($options);
$product->setExtensionAttributes($extensionAttributes);

return $product;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\TestFramework\Directory\Model;

use Magento\Directory\Model\RegionFactory;

/**
* Return region ID by region default name and country code.
*/
class GetRegionIdByName
{
/**
* @var RegionFactory
*/
private $regionFactory;

/**
* @var array
*/
private $regionIdsCache;

/**
* @param RegionFactory $regionFactory
*/
public function __construct(
RegionFactory $regionFactory
) {
$this->regionFactory = $regionFactory;
}

/**
* Get region ID from cache property if region id exist or load it.
*
* @param string $regionName
* @param string $countryId
* @return int|null
*/
public function execute(string $regionName, string $countryId): ?int
{
$cacheKey = "{$regionName}_{$countryId}";

if (!isset($this->regionIdsCache[$cacheKey])) {
$region = $this->regionFactory->create()->loadByName($regionName, $countryId);
$this->regionIdsCache[$cacheKey] = $region->getRegionId() ? (int)$region->getRegionId() : null;
}

return $this->regionIdsCache[$cacheKey];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Bundle\Block\Catalog\Product\View\Type;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\View\Result\PageFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;

/**
* Class consist of basic logic for bundle options view
*/
abstract class AbstractBundleOptionsViewTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;

/** @var ProductRepositoryInterface */
private $productRepository;

/** @var SerializerInterface */
private $serializer;

/** @var Registry */
private $registry;

/** @var PageFactory */
private $pageFactory;

/** @var ProductResource */
private $productResource;

/** @var string */
private $selectLabelXpath = "//fieldset[contains(@class, 'fieldset-bundle-options')]"
. "//label/span[normalize-space(text()) = '%s']";

/** @var string */
private $backToProductDetailButtonXpath = "//button[contains(@class, 'back customization')]";

/** @var string */
private $titleXpath = "//fieldset[contains(@class, 'bundle-options')]//span[contains(text(), 'Customize %s')]";

/** @var string */
private $singleOptionXpath = "//input[contains(@class, 'bundle-option') and contains(@type, 'hidden')]";

/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();

$this->objectManager = Bootstrap::getObjectManager();
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$this->productRepository->cleanCache();
$this->serializer = $this->objectManager->get(SerializerInterface::class);
$this->registry = $this->objectManager->get(Registry::class);
$this->pageFactory = $this->objectManager->get(PageFactory::class);
$this->productResource = $this->objectManager->get(ProductResource::class);
}

/**
* @inheritdoc
*/
protected function tearDown()
{
$this->registry->unregister('product');
$this->registry->unregister('current_product');

parent::tearDown();
}

/**
* Process bundle options view with few selections
*
* @param string $sku
* @param string $optionsSelectLabel
* @param array $expectedSelectionsNames
* @param bool $requiredOption
* @return void
*/
protected function processMultiSelectionsView(
string $sku,
string $optionsSelectLabel,
array $expectedSelectionsNames,
bool $requiredOption = false
): void {
$product = $this->productRepository->get($sku);
$result = $this->renderProductOptionsBlock($product);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath($this->backToProductDetailButtonXpath, $result),
"'Back to product details' button doesn't exist on the page"
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($this->selectLabelXpath, $optionsSelectLabel), $result),
'Options select label does not exist on the page'
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($this->titleXpath, $product->getName()), $result),
sprintf('Customize %s label does not exist on the page', $product->getName())
);
$selectPath = $requiredOption ? $this->getRequiredSelectXpath() : $this->getNotRequiredSelectXpath();
foreach ($expectedSelectionsNames as $selection) {
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($selectPath, $selection), $result),
sprintf('Option for product named %s does not exist on the page', $selection)
);
}
}

/**
* Process bundle options view with single selection
*
* @param string $sku
* @param string $optionsSelectLabel
* @return void
*/
protected function processSingleSelectionView(string $sku, string $optionsSelectLabel): void
{
$product = $this->productRepository->get($sku);
$result = $this->renderProductOptionsBlock($product);
$this->assertEquals(1, Xpath::getElementsCountForXpath($this->backToProductDetailButtonXpath, $result));
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($this->selectLabelXpath, $optionsSelectLabel), $result),
'Options select label does not exist on the page'
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath($this->singleOptionXpath, $result),
'Bundle product options select with single option does not display correctly'
);
}

/**
* Register product
*
* @param ProductInterface $product
* @return void
*/
private function registerProduct(ProductInterface $product): void
{
$this->registry->unregister('product');
$this->registry->unregister('current_product');
$this->registry->register('product', $product);
$this->registry->register('current_product', $product);
}

/**
* Render bundle product options block
*
* @param ProductInterface $product
* @return string
*/
private function renderProductOptionsBlock(ProductInterface $product): string
{
$this->registerProduct($product);
$page = $this->pageFactory->create();
$page->addHandle(['default', 'catalog_product_view', 'catalog_product_view_type_bundle']);
$page->getLayout()->generateXml();
$block = $page->getLayout()->getBlock('product.info.bundle.options');

return $block->toHtml();
}

/**
* Get required select Xpath
*
* @return string
*/
abstract protected function getRequiredSelectXpath(): string;

/**
* Get not required select Xpath
*
* @return string
*/
abstract protected function getNotRequiredSelectXpath(): string;
}
Loading

0 comments on commit e280215

Please sign in to comment.