ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Catalog\Helper;
|
||||
|
||||
/**
|
||||
* Class CategoryTest
|
||||
* @package Magento\Catalog\Helper
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
class CategoryTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Catalog\Helper\Category
|
||||
*/
|
||||
protected $_helper;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->_helper = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Catalog\Helper\Category::class
|
||||
);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if ($this->_helper) {
|
||||
$helperClass = get_class($this->_helper);
|
||||
/** @var $objectManager \Magento\TestFramework\ObjectManager */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$objectManager->get(\Magento\Framework\Registry::class)->unregister('_helper/' . $helperClass);
|
||||
}
|
||||
$this->_helper = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Catalog/_files/categories.php
|
||||
*/
|
||||
public function testGetStoreCategories()
|
||||
{
|
||||
$categories = $this->_helper->getStoreCategories();
|
||||
$this->assertInstanceOf(\Magento\Framework\Data\Tree\Node\Collection::class, $categories);
|
||||
$index = 0;
|
||||
$expectedPaths = [
|
||||
[3, '1/2/3'],
|
||||
[6, '1/2/6'],
|
||||
[7, '1/2/7'],
|
||||
[9, '1/2/9'],
|
||||
[10, '1/2/10'],
|
||||
[11, '1/2/11'],
|
||||
[12, '1/2/12'],
|
||||
];
|
||||
foreach ($categories as $category) {
|
||||
$this->assertInstanceOf(\Magento\Framework\Data\Tree\Node::class, $category);
|
||||
$this->assertEquals($expectedPaths[$index][0], $category->getId());
|
||||
$this->assertEquals($expectedPaths[$index][1], $category->getData('path'));
|
||||
$index++;
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetCategoryUrl()
|
||||
{
|
||||
$url = 'http://example.com/';
|
||||
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Catalog\Model\Category::class,
|
||||
['data' => ['url' => $url]]
|
||||
);
|
||||
$this->assertEquals($url, $this->_helper->getCategoryUrl($category));
|
||||
|
||||
$category = new \Magento\Framework\DataObject(['url' => $url]);
|
||||
$this->assertEquals($url, $this->_helper->getCategoryUrl($category));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Catalog/_files/categories.php
|
||||
*/
|
||||
public function testCanShow()
|
||||
{
|
||||
// by ID of a category that is not a root
|
||||
$this->assertTrue($this->_helper->canShow(7));
|
||||
}
|
||||
|
||||
public function testCanShowFalse()
|
||||
{
|
||||
/** @var $category \Magento\Catalog\Model\Category */
|
||||
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Catalog\Model\Category::class
|
||||
);
|
||||
$this->assertFalse($this->_helper->canShow($category));
|
||||
$category->setId(1);
|
||||
$this->assertFalse($this->_helper->canShow($category));
|
||||
$category->setIsActive(true);
|
||||
$this->assertFalse($this->_helper->canShow($category));
|
||||
}
|
||||
|
||||
public function testCanUseCanonicalTagDefault()
|
||||
{
|
||||
$this->assertEquals(0, $this->_helper->canUseCanonicalTag());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store catalog/seo/category_canonical_tag 1
|
||||
*/
|
||||
public function testCanUseCanonicalTag()
|
||||
{
|
||||
$this->assertEquals(1, $this->_helper->canUseCanonicalTag());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,511 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Catalog\Helper;
|
||||
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\Tax\Model\ClassModel;
|
||||
use Magento\Tax\Model\Config;
|
||||
use Magento\Tax\Model\TaxRuleFixtureFactory;
|
||||
|
||||
/**
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class DataTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* Tax helper
|
||||
*
|
||||
* @var \Magento\Catalog\Helper\Data
|
||||
*/
|
||||
private $helper;
|
||||
|
||||
/**
|
||||
* Object Manager
|
||||
*
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* Array of default tax classes ids
|
||||
*
|
||||
* Key is class name
|
||||
*
|
||||
* @var int[]
|
||||
*/
|
||||
private $taxClasses;
|
||||
|
||||
/**
|
||||
* Array of default tax rates ids.
|
||||
*
|
||||
* Key is rate percentage as string.
|
||||
*
|
||||
* @var int[]
|
||||
*/
|
||||
private $taxRates;
|
||||
|
||||
/**
|
||||
* Array of default tax rules ids.
|
||||
*
|
||||
* Key is rule code.
|
||||
*
|
||||
* @var int[]
|
||||
*/
|
||||
private $taxRules;
|
||||
|
||||
/**
|
||||
* Helps in creating required tax rules.
|
||||
*
|
||||
* @var TaxRuleFixtureFactory
|
||||
*/
|
||||
private $taxRuleFixtureFactory;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\MutableScopeConfig
|
||||
*/
|
||||
private $scopeConfig;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->helper = $this->objectManager->get(\Magento\Catalog\Helper\Data::class);
|
||||
$this->taxRuleFixtureFactory = new TaxRuleFixtureFactory();
|
||||
$this->scopeConfig = $this->objectManager->get(\Magento\Framework\App\MutableScopeConfig::class);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->tearDownDefaultRules();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Catalog/_files/categories.php
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testGetBreadcrumbPath()
|
||||
{
|
||||
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Catalog\Model\Category::class
|
||||
);
|
||||
$category->load(5);
|
||||
/** @var $objectManager \Magento\TestFramework\ObjectManager */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$objectManager->get(\Magento\Framework\Registry::class)->register('current_category', $category);
|
||||
|
||||
try {
|
||||
$path = $this->helper->getBreadcrumbPath();
|
||||
$this->assertIsArray($path);
|
||||
$this->assertEquals(['category3', 'category4', 'category5'], array_keys($path));
|
||||
$this->assertArrayHasKey('label', $path['category3']);
|
||||
$this->assertArrayHasKey('link', $path['category3']);
|
||||
$objectManager->get(\Magento\Framework\Registry::class)->unregister('current_category');
|
||||
} catch (\Exception $e) {
|
||||
$objectManager->get(\Magento\Framework\Registry::class)->unregister('current_category');
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetCategory()
|
||||
{
|
||||
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Catalog\Model\Category::class
|
||||
);
|
||||
/** @var $objectManager \Magento\TestFramework\ObjectManager */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$objectManager->get(\Magento\Framework\Registry::class)->register('current_category', $category);
|
||||
try {
|
||||
$this->assertSame($category, $this->helper->getCategory());
|
||||
$objectManager->get(\Magento\Framework\Registry::class)->unregister('current_category');
|
||||
} catch (\Exception $e) {
|
||||
$objectManager->get(\Magento\Framework\Registry::class)->unregister('current_category');
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetProduct()
|
||||
{
|
||||
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Catalog\Model\Product::class
|
||||
);
|
||||
/** @var $objectManager \Magento\TestFramework\ObjectManager */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$objectManager->get(\Magento\Framework\Registry::class)->register('current_product', $product);
|
||||
try {
|
||||
$this->assertSame($product, $this->helper->getProduct());
|
||||
$objectManager->get(\Magento\Framework\Registry::class)->unregister('current_product');
|
||||
} catch (\Exception $e) {
|
||||
$objectManager->get(\Magento\Framework\Registry::class)->unregister('current_product');
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function testSplitSku()
|
||||
{
|
||||
$sku = 'one-two-three';
|
||||
$this->assertEquals(['on', 'e-', 'tw', 'o-', 'th', 're', 'e'], $this->helper->splitSku($sku, 2));
|
||||
}
|
||||
|
||||
public function testGetAttributeHiddenFields()
|
||||
{
|
||||
$this->assertEquals([], $this->helper->getAttributeHiddenFields());
|
||||
/** @var $objectManager \Magento\TestFramework\ObjectManager */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$objectManager->get(\Magento\Framework\Registry::class)->register('attribute_type_hidden_fields', 'test');
|
||||
try {
|
||||
$this->assertEquals('test', $this->helper->getAttributeHiddenFields());
|
||||
$objectManager->get(\Magento\Framework\Registry::class)->unregister('attribute_type_hidden_fields');
|
||||
} catch (\Exception $e) {
|
||||
$objectManager->get(\Magento\Framework\Registry::class)->unregister('attribute_type_hidden_fields');
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetPriceScopeDefault()
|
||||
{
|
||||
// $this->assertEquals(\Magento\Store\Model\Store::PRICE_SCOPE_GLOBAL, $this->helper->getPriceScope());
|
||||
$this->assertNull($this->helper->getPriceScope());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store catalog/price/scope 1
|
||||
*/
|
||||
public function testGetPriceScope()
|
||||
{
|
||||
$this->assertEquals(\Magento\Store\Model\Store::PRICE_SCOPE_WEBSITE, $this->helper->getPriceScope());
|
||||
}
|
||||
|
||||
public function testIsPriceGlobalDefault()
|
||||
{
|
||||
$this->assertTrue($this->helper->isPriceGlobal());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store catalog/price/scope 1
|
||||
*/
|
||||
public function testIsPriceGlobal()
|
||||
{
|
||||
$this->assertFalse($this->helper->isPriceGlobal());
|
||||
}
|
||||
|
||||
public function testIsUsingStaticUrlsAllowedDefault()
|
||||
{
|
||||
$this->assertFalse($this->helper->isUsingStaticUrlsAllowed());
|
||||
}
|
||||
|
||||
/**
|
||||
* isUsingStaticUrlsAllowed()
|
||||
* setStoreId()
|
||||
* @magentoConfigFixture current_store cms/wysiwyg/use_static_urls_in_catalog 1
|
||||
*/
|
||||
public function testIsUsingStaticUrlsAllowed()
|
||||
{
|
||||
$this->assertTrue($this->helper->isUsingStaticUrlsAllowed());
|
||||
$this->helper->setStoreId(
|
||||
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Store\Model\StoreManagerInterface::class
|
||||
)->getStore()->getId()
|
||||
);
|
||||
$this->assertTrue($this->helper->isUsingStaticUrlsAllowed());
|
||||
}
|
||||
|
||||
public function testIsUrlDirectivesParsingAllowedDefault()
|
||||
{
|
||||
$this->assertTrue($this->helper->isUrlDirectivesParsingAllowed());
|
||||
}
|
||||
|
||||
/**
|
||||
* isUrlDirectivesParsingAllowed()
|
||||
* setStoreId()
|
||||
* @magentoConfigFixture current_store catalog/frontend/parse_url_directives 0
|
||||
*/
|
||||
public function testIsUrlDirectivesParsingAllowed()
|
||||
{
|
||||
$this->assertFalse($this->helper->isUrlDirectivesParsingAllowed());
|
||||
$this->helper->setStoreId(
|
||||
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Store\Model\StoreManagerInterface::class
|
||||
)->getStore()->getId()
|
||||
);
|
||||
$this->assertFalse($this->helper->isUrlDirectivesParsingAllowed());
|
||||
}
|
||||
|
||||
public function testGetPageTemplateProcessor()
|
||||
{
|
||||
$this->assertInstanceOf(\Magento\Framework\Filter\Template::class, $this->helper->getPageTemplateProcessor());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\DataObject $input
|
||||
* @param float $expectOutputPrice
|
||||
* @param string[] $configs
|
||||
* @param string $productClassName
|
||||
*
|
||||
* @magentoDataFixture Magento/Catalog/_files/products.php
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoDataFixture Magento/Customer/_files/customer_address.php
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppIsolation enabled
|
||||
* @dataProvider getTaxPriceDataProvider
|
||||
*/
|
||||
public function testGetTaxPrice(
|
||||
$input,
|
||||
$expectOutputPrice,
|
||||
$configs = [],
|
||||
$productClassName = 'DefaultProductClass'
|
||||
) {
|
||||
$this->setUpDefaultRules();
|
||||
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
|
||||
$productRepository = $this->objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
|
||||
/** @var \Magento\Catalog\Model\Product $product */
|
||||
$product = $productRepository->get('simple');
|
||||
$product->setTaxClassId($this->taxClasses[$productClassName]);
|
||||
$shippingAddress = $this->getCustomerAddress();
|
||||
$billingAddress = $shippingAddress;
|
||||
foreach ($configs as $config) {
|
||||
$this->scopeConfig->setValue($config['path'], $config['value'], ScopeInterface::SCOPE_STORE, 'default');
|
||||
}
|
||||
|
||||
$price = $this->helper->getTaxPrice(
|
||||
$product,
|
||||
$input->getPrice(),
|
||||
$input->getIncludingTax(),
|
||||
$shippingAddress,
|
||||
$billingAddress,
|
||||
$this->taxClasses['DefaultCustomerClass'],
|
||||
$input->getStore(),
|
||||
$input->getPriceIncludesTax(),
|
||||
$input->getRoundPrice()
|
||||
);
|
||||
if ($input->getNotEqual()) {
|
||||
$this->assertNotEquals($expectOutputPrice, $price);
|
||||
} else {
|
||||
$this->assertEquals($expectOutputPrice, $price);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function getTaxPriceDataProvider()
|
||||
{
|
||||
return [
|
||||
'price is 0' => [
|
||||
(new \Magento\Framework\DataObject())->setPrice(0),
|
||||
0,
|
||||
],
|
||||
'no price conversion, round' => [
|
||||
(new \Magento\Framework\DataObject())->setPrice(3.256)->setRoundPrice(true),
|
||||
'3.26',
|
||||
],
|
||||
'no price conversion, no round' => [
|
||||
(new \Magento\Framework\DataObject())->setPrice(3.256),
|
||||
'3.256',
|
||||
],
|
||||
'price conversion, display including tax, round' => [
|
||||
(new \Magento\Framework\DataObject())->setPrice(3.256)->setRoundPrice(true),
|
||||
'3.5',
|
||||
[
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX,
|
||||
'value' => '0',
|
||||
],
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_PRICE_DISPLAY_TYPE,
|
||||
'value' => Config::DISPLAY_TYPE_INCLUDING_TAX,
|
||||
],
|
||||
],
|
||||
],
|
||||
'price conversion, display including tax, no round' => [
|
||||
(new \Magento\Framework\DataObject())->setPrice(3.256)->setNotEqual(true),
|
||||
'3.5', // should be not equal to rounded value (eg, 3.5045009999999999)
|
||||
[
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX,
|
||||
'value' => '0',
|
||||
],
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_PRICE_DISPLAY_TYPE,
|
||||
'value' => Config::DISPLAY_TYPE_INCLUDING_TAX,
|
||||
],
|
||||
],
|
||||
],
|
||||
'price conversion, display including tax, high rate product tax class, cross boarder trade, round' => [
|
||||
(new \Magento\Framework\DataObject())->setPrice(3.256)->setRoundPrice(true),
|
||||
'3.98', // rounding issue: old code expects 3.97
|
||||
[
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX,
|
||||
'value' => '0',
|
||||
],
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_PRICE_DISPLAY_TYPE,
|
||||
'value' => Config::DISPLAY_TYPE_INCLUDING_TAX,
|
||||
],
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_CROSS_BORDER_TRADE_ENABLED,
|
||||
'value' => '1',
|
||||
],
|
||||
],
|
||||
'HigherProductClass',
|
||||
],
|
||||
'price include tax, display including tax, round' => [
|
||||
(new \Magento\Framework\DataObject())->setPrice(3.256)->setRoundPrice(true),
|
||||
'3.26',
|
||||
[
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX,
|
||||
'value' => '1',
|
||||
],
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_PRICE_DISPLAY_TYPE,
|
||||
'value' => Config::DISPLAY_TYPE_INCLUDING_TAX,
|
||||
],
|
||||
],
|
||||
],
|
||||
'price include tax, display excluding tax, round' => [
|
||||
(new \Magento\Framework\DataObject())->setPrice(3.256)->setRoundPrice(true),
|
||||
'3.03',
|
||||
[
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX,
|
||||
'value' => '1',
|
||||
],
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_PRICE_DISPLAY_TYPE,
|
||||
'value' => Config::DISPLAY_TYPE_EXCLUDING_TAX,
|
||||
],
|
||||
],
|
||||
],
|
||||
'price include tax, display excluding tax, request including tax, round' => [
|
||||
(new \Magento\Framework\DataObject())->setPrice(3.256)
|
||||
->setRoundPrice(true)
|
||||
->setIncludingTax(true),
|
||||
'3.26',
|
||||
[
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX,
|
||||
'value' => '1',
|
||||
],
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_PRICE_DISPLAY_TYPE,
|
||||
'value' => Config::DISPLAY_TYPE_EXCLUDING_TAX,
|
||||
],
|
||||
],
|
||||
],
|
||||
'price include tax, display excluding tax, high rate product tax class, round' => [
|
||||
(new \Magento\Framework\DataObject())->setPrice(3.256)->setRoundPrice(true),
|
||||
'2.97',
|
||||
[
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX,
|
||||
'value' => '1',
|
||||
],
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_PRICE_DISPLAY_TYPE,
|
||||
'value' => Config::DISPLAY_TYPE_EXCLUDING_TAX,
|
||||
],
|
||||
],
|
||||
'HigherProductClass',
|
||||
],
|
||||
'price include tax, display excluding tax, high rate product tax class, cross boarder trade, round' => [
|
||||
(new \Magento\Framework\DataObject())->setPrice(3.256)->setRoundPrice(true),
|
||||
'2.67',
|
||||
[
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX,
|
||||
'value' => '1',
|
||||
],
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_PRICE_DISPLAY_TYPE,
|
||||
'value' => Config::DISPLAY_TYPE_EXCLUDING_TAX,
|
||||
],
|
||||
[
|
||||
'path' => Config::CONFIG_XML_PATH_CROSS_BORDER_TRADE_ENABLED,
|
||||
'value' => '1',
|
||||
],
|
||||
],
|
||||
'HigherProductClass',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that sets up some default rules
|
||||
*/
|
||||
private function setUpDefaultRules()
|
||||
{
|
||||
$this->taxClasses = $this->taxRuleFixtureFactory->createTaxClasses([
|
||||
['name' => 'DefaultCustomerClass', 'type' => ClassModel::TAX_CLASS_TYPE_CUSTOMER],
|
||||
['name' => 'DefaultProductClass', 'type' => ClassModel::TAX_CLASS_TYPE_PRODUCT],
|
||||
['name' => 'HigherProductClass', 'type' => ClassModel::TAX_CLASS_TYPE_PRODUCT],
|
||||
]);
|
||||
|
||||
$this->taxRates = $this->taxRuleFixtureFactory->createTaxRates([
|
||||
['percentage' => 7.5, 'country' => 'US', 'region' => 42],
|
||||
['percentage' => 7.5, 'country' => 'US', 'region' => 12], // Default store rate
|
||||
]);
|
||||
|
||||
$higherRates = $this->taxRuleFixtureFactory->createTaxRates([
|
||||
['percentage' => 22, 'country' => 'US', 'region' => 42],
|
||||
['percentage' => 10, 'country' => 'US', 'region' => 12], // Default store rate
|
||||
]);
|
||||
|
||||
$this->taxRules = $this->taxRuleFixtureFactory->createTaxRules([
|
||||
[
|
||||
'code' => 'Default Rule',
|
||||
'customer_tax_class_ids' => [$this->taxClasses['DefaultCustomerClass'], 3],
|
||||
'product_tax_class_ids' => [$this->taxClasses['DefaultProductClass']],
|
||||
'tax_rate_ids' => array_values($this->taxRates),
|
||||
'sort_order' => 0,
|
||||
'priority' => 0,
|
||||
],
|
||||
[
|
||||
'code' => 'Higher Rate Rule',
|
||||
'customer_tax_class_ids' => [$this->taxClasses['DefaultCustomerClass'], 3],
|
||||
'product_tax_class_ids' => [$this->taxClasses['HigherProductClass']],
|
||||
'tax_rate_ids' => array_values($higherRates),
|
||||
'sort_order' => 0,
|
||||
'priority' => 0,
|
||||
],
|
||||
]);
|
||||
|
||||
// For cleanup
|
||||
$this->taxRates = array_merge($this->taxRates, $higherRates);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get fixture customer address
|
||||
*
|
||||
* @return \Magento\Customer\Model\Address
|
||||
*/
|
||||
private function getCustomerAddress()
|
||||
{
|
||||
$fixtureCustomerId = 1;
|
||||
$customerAddress = $this->objectManager->create(
|
||||
\Magento\Customer\Model\Address::class
|
||||
)->load($fixtureCustomerId);
|
||||
/** Set data which corresponds tax class fixture */
|
||||
$customerAddress->setCountryId('US')->setRegionId(42)->save();
|
||||
return $customerAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that tears down some default rules
|
||||
*/
|
||||
private function tearDownDefaultRules()
|
||||
{
|
||||
if ($this->taxRules) {
|
||||
$this->taxRuleFixtureFactory->deleteTaxRules(array_values($this->taxRules));
|
||||
}
|
||||
if ($this->taxRates) {
|
||||
$this->taxRuleFixtureFactory->deleteTaxRates(array_values($this->taxRates));
|
||||
}
|
||||
if ($this->taxClasses) {
|
||||
$this->taxRuleFixtureFactory->deleteTaxClasses(array_values($this->taxClasses));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Catalog\Helper;
|
||||
|
||||
use Magento\Catalog\Model\Category;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Framework\Phrase;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class OutputTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Output
|
||||
*/
|
||||
protected $_helper;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->_helper = Bootstrap::getObjectManager()->get(
|
||||
Output::class
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* addHandler()
|
||||
* getHandlers()
|
||||
*/
|
||||
public function testAddHandlerGetHandlers()
|
||||
{
|
||||
// invalid handler
|
||||
$this->_helper->addHandler('method', 'handler');
|
||||
$this->assertEquals([], $this->_helper->getHandlers('method'));
|
||||
|
||||
// add one handler
|
||||
$objectOne = new \StdClass();
|
||||
$this->_helper->addHandler('valid', $objectOne);
|
||||
$this->assertSame([$objectOne], $this->_helper->getHandlers('valid'));
|
||||
|
||||
// add another one
|
||||
$objectTwo = new \StdClass();
|
||||
$this->_helper->addHandler('valid', $objectTwo);
|
||||
$this->assertSame([$objectOne, $objectTwo], $this->_helper->getHandlers('valid'));
|
||||
}
|
||||
|
||||
public function testProcess()
|
||||
{
|
||||
$this->_helper->addHandler('sampleProcessor', $this);
|
||||
$this->assertStringStartsWith(__CLASS__, $this->_helper->process('sampleProcessor', uniqid(), []));
|
||||
}
|
||||
|
||||
public function testProductAttribute()
|
||||
{
|
||||
$this->_testAttribute(
|
||||
'productAttribute',
|
||||
Product::ENTITY,
|
||||
"<p>line1</p><br />\nline2"
|
||||
);
|
||||
}
|
||||
|
||||
public function testCategoryAttribute()
|
||||
{
|
||||
$this->_testAttribute(
|
||||
'categoryAttribute',
|
||||
Category::ENTITY,
|
||||
"<p>line1</p>\nline2"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if string has directives.
|
||||
*
|
||||
* @dataProvider isDirectiveDataProvider
|
||||
* @param string|Phrase $html
|
||||
* @param bool $expectedResult
|
||||
*/
|
||||
public function testIsDirectivesExists($html, bool $expectedResult): void
|
||||
{
|
||||
$this->assertEquals($expectedResult, $this->_helper->isDirectivesExists($html));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testIsDirectivesExists()
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function isDirectiveDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'attribute_html_without_directive' => ['Test string', false],
|
||||
'attribute_html_with_incorrect_directive' => ['{store url="customer/account/login"}', false],
|
||||
'attribute_html_with_correct_directive' => ['{{store url="customer/account/login"}}', true],
|
||||
'attribute_html_with_object_type' => [__('{{store url="%1"}}', 'customer/account/login'), true],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method for testProcess()
|
||||
*
|
||||
* @param Output $helper
|
||||
* @param string $string
|
||||
* @param mixed $params
|
||||
* @return string
|
||||
* @see testProcess()
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function sampleProcessor(Output $helper, $string, $params)
|
||||
{
|
||||
return __CLASS__ . $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test productAttribute() or categoryAttribute() method
|
||||
*
|
||||
* @param string $method
|
||||
* @param string $entityCode
|
||||
* @param string $expectedResult
|
||||
* @throws \Exception on assertion failure
|
||||
*/
|
||||
protected function _testAttribute($method, $entityCode, $expectedResult)
|
||||
{
|
||||
$attributeName = 'description';
|
||||
$attribute = Bootstrap::getObjectManager()->get(
|
||||
\Magento\Eav\Model\Config::class
|
||||
)->getAttribute(
|
||||
$entityCode,
|
||||
$attributeName
|
||||
);
|
||||
$isHtml = $attribute->getIsHtmlAllowedOnFront();
|
||||
$isWysiwyg = $attribute->getIsWysiwygEnabled();
|
||||
$attribute->setIsHtmlAllowedOnFront(0)->setIsWysiwygEnabled(0);
|
||||
|
||||
try {
|
||||
$this->assertEquals(
|
||||
$expectedResult,
|
||||
$this->_helper->{$method}(uniqid(), __("<p>line1</p>\nline2"), $attributeName)
|
||||
);
|
||||
|
||||
$attribute->setIsHtmlAllowedOnFront($isHtml)->setIsWysiwygEnabled($isWysiwyg);
|
||||
} catch (\Exception $e) {
|
||||
$attribute->setIsHtmlAllowedOnFront($isHtml)->setIsWysiwygEnabled($isWysiwyg);
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Catalog\Helper\Product;
|
||||
|
||||
class CompareTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Catalog\Helper\Product\Compare
|
||||
*/
|
||||
protected $_helper;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
protected $_objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->_helper = $this->_objectManager->get(\Magento\Catalog\Helper\Product\Compare::class);
|
||||
}
|
||||
|
||||
public function testGetListUrl()
|
||||
{
|
||||
/** @var $empty \Magento\Catalog\Helper\Product\Compare */
|
||||
$empty = $this->_objectManager->create(\Magento\Catalog\Helper\Product\Compare::class);
|
||||
$this->assertStringContainsString('/catalog/product_compare/index/', $empty->getListUrl());
|
||||
}
|
||||
|
||||
public function testGetAddUrl()
|
||||
{
|
||||
$this->_testGetProductUrl('getAddUrl', '/catalog/product_compare/add/');
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testGetAddToWishlistParams()
|
||||
{
|
||||
$product = $this->_objectManager->create(\Magento\Catalog\Model\Product::class);
|
||||
$product->setId(10);
|
||||
$json = $this->_helper->getAddToWishlistParams($product);
|
||||
$params = (array)json_decode($json);
|
||||
$data = (array)$params['data'];
|
||||
$this->assertEquals('10', $data['product']);
|
||||
$this->assertArrayHasKey('uenc', $data);
|
||||
$this->assertStringEndsWith(
|
||||
'wishlist/index/add/',
|
||||
$params['action']
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetAddToCartUrl()
|
||||
{
|
||||
$this->_testGetProductUrl('getAddToCartUrl', '/checkout/cart/add/');
|
||||
}
|
||||
|
||||
public function testGetRemoveUrl()
|
||||
{
|
||||
$url = $this->_helper->getRemoveUrl();
|
||||
$this->assertStringContainsString('/catalog/product_compare/remove/', $url);
|
||||
}
|
||||
|
||||
public function testGetClearListUrl()
|
||||
{
|
||||
$this->assertStringContainsString(
|
||||
'\/catalog\/product_compare\/clear\/',
|
||||
$this->_helper->getPostDataClearList()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see testGetListUrl() for coverage of customer case
|
||||
*/
|
||||
public function testGetItemCollection()
|
||||
{
|
||||
$this->assertInstanceOf(
|
||||
\Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection::class,
|
||||
$this->_helper->getItemCollection()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* calculate()
|
||||
* getItemCount()
|
||||
* hasItems()
|
||||
*
|
||||
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
|
||||
* @magentoDbIsolation disabled
|
||||
*/
|
||||
public function testCalculate()
|
||||
{
|
||||
/** @var \Magento\Catalog\Model\Session $session */
|
||||
$session = $this->_objectManager->get(\Magento\Catalog\Model\Session::class);
|
||||
try {
|
||||
$session->unsCatalogCompareItemsCount();
|
||||
$this->assertFalse($this->_helper->hasItems());
|
||||
$this->assertEquals(0, $session->getCatalogCompareItemsCount());
|
||||
|
||||
$this->_populateCompareList();
|
||||
$this->_helper->calculate();
|
||||
$this->assertEquals(2, $session->getCatalogCompareItemsCount());
|
||||
$this->assertTrue($this->_helper->hasItems());
|
||||
|
||||
$session->unsCatalogCompareItemsCount();
|
||||
} catch (\Exception $e) {
|
||||
$session->unsCatalogCompareItemsCount();
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function testSetGetAllowUsedFlat()
|
||||
{
|
||||
$this->assertTrue($this->_helper->getAllowUsedFlat());
|
||||
$this->_helper->setAllowUsedFlat(false);
|
||||
$this->assertFalse($this->_helper->getAllowUsedFlat());
|
||||
}
|
||||
|
||||
protected function _testGetProductUrl($method, $expectedFullAction)
|
||||
{
|
||||
$product = $this->_objectManager->create(\Magento\Catalog\Model\Product::class);
|
||||
$product->setId(10);
|
||||
$url = $this->_helper->{$method}($product);
|
||||
$this->assertStringContainsString($expectedFullAction, $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add products from fixture to compare list
|
||||
*/
|
||||
protected function _populateCompareList()
|
||||
{
|
||||
$productRepository = $this->_objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
|
||||
$productOne = $productRepository->get('simple1');
|
||||
$productTwo = $productRepository->get('simple2');
|
||||
/** @var $compareList \Magento\Catalog\Model\Product\Compare\ListCompare */
|
||||
$compareList = $this->_objectManager->create(\Magento\Catalog\Model\Product\Compare\ListCompare::class);
|
||||
$compareList->addProduct($productOne)->addProduct($productTwo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Catalog\Helper\Product;
|
||||
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Customer\Controller\RegistryConstants;
|
||||
use Magento\Framework\DataObject;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test Composite
|
||||
*/
|
||||
class CompositeTest extends TestCase
|
||||
{
|
||||
/** @var ObjectManagerInterface */
|
||||
private $objectManager;
|
||||
|
||||
/** @var Composite */
|
||||
private $helper;
|
||||
|
||||
/** @var Registry */
|
||||
private $registry;
|
||||
|
||||
/** @var ProductRepositoryInterface */
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->helper = $this->objectManager->get(Composite::class);
|
||||
$this->registry = $this->objectManager->get(Registry::class);
|
||||
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
$this->productRepository->cleanCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->registry->unregister('composite_configure_result_error_message');
|
||||
$this->registry->unregister(RegistryConstants::CURRENT_CUSTOMER_ID);
|
||||
$this->registry->unregister('current_product');
|
||||
$this->registry->unregister('product');
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @return void
|
||||
*/
|
||||
public function testRenderConfigureResult(): void
|
||||
{
|
||||
$product = $this->productRepository->get('simple');
|
||||
/** @var DataObject $buyRequest */
|
||||
$buyRequest = $this->objectManager->create(DataObject::class);
|
||||
$buyRequest->setData(['qty' => 1]);
|
||||
/** @var DataObject $configureResult */
|
||||
$configureResult = $this->objectManager->create(DataObject::class);
|
||||
$configureResult->setOk(true)
|
||||
->setProductId($product->getId())
|
||||
->setBuyRequest($buyRequest)
|
||||
->setCurrentCustomerId(1);
|
||||
|
||||
$resultLayout = $this->helper->renderConfigureResult($configureResult);
|
||||
|
||||
/** @var Product $preparedProduct */
|
||||
$preparedProduct = $this->registry->registry('product');
|
||||
$preparedCurrentProduct = $this->registry->registry('current_product');
|
||||
$this->assertTrue($preparedProduct && $preparedCurrentProduct);
|
||||
$this->assertEquals(1, $this->registry->registry(RegistryConstants::CURRENT_CUSTOMER_ID));
|
||||
$this->assertNotNull($preparedProduct->getPreconfiguredValues());
|
||||
$this->assertContains(
|
||||
'CATALOG_PRODUCT_COMPOSITE_CONFIGURE',
|
||||
$resultLayout->getLayout()->getUpdate()->getHandles()
|
||||
);
|
||||
$this->assertContains(
|
||||
'catalog_product_view_type_' . $product->getTypeId(),
|
||||
$resultLayout->getLayout()->getUpdate()->getHandles()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider renderConfigureResultExceptionProvider
|
||||
* @param array $data
|
||||
* @param string $expectedErrorMessage
|
||||
* @return void
|
||||
*/
|
||||
public function testRenderConfigureResultException(array $data, string $expectedErrorMessage): void
|
||||
{
|
||||
/** @var DataObject $configureResult */
|
||||
$configureResult = $this->objectManager->create(DataObject::class);
|
||||
$configureResult->setData($data);
|
||||
|
||||
$resultLayout = $this->helper->renderConfigureResult($configureResult);
|
||||
|
||||
$this->assertEquals(
|
||||
$expectedErrorMessage,
|
||||
$this->registry->registry('composite_configure_result_error_message')
|
||||
);
|
||||
$this->assertContains(
|
||||
'CATALOG_PRODUCT_COMPOSITE_CONFIGURE_ERROR',
|
||||
$resultLayout->getLayout()->getUpdate()->getHandles()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create render configure result exception provider
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function renderConfigureResultExceptionProvider(): array
|
||||
{
|
||||
return [
|
||||
'error_true' => [
|
||||
'data' => [
|
||||
'error' => true,
|
||||
'message' => 'Test Message'
|
||||
],
|
||||
'expected_error_message' => 'Test Message',
|
||||
],
|
||||
'without_product' => [
|
||||
'data' => [
|
||||
'ok' => true,
|
||||
],
|
||||
'expected_error_message' => 'The product that was requested doesn\'t exist.'
|
||||
. ' Verify the product and try again.',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Catalog\Helper\Product;
|
||||
|
||||
class FlatTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Catalog\Helper\Product\Flat\Indexer
|
||||
*/
|
||||
protected $_helper;
|
||||
|
||||
/**
|
||||
* @var \Magento\Catalog\Model\Indexer\Product\Flat\State
|
||||
*/
|
||||
protected $_state;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->_helper = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Catalog\Helper\Product\Flat\Indexer::class
|
||||
);
|
||||
$this->_state = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Catalog\Model\Indexer\Product\Flat\State::class
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store catalog/frontend/flat_catalog_product 1
|
||||
*/
|
||||
public function testIsEnabled()
|
||||
{
|
||||
$this->assertTrue($this->_state->isFlatEnabled());
|
||||
}
|
||||
|
||||
public function testIsAddFilterableAttributesDefault()
|
||||
{
|
||||
$this->assertEquals(0, $this->_helper->isAddFilterableAttributes());
|
||||
}
|
||||
|
||||
public function testIsAddFilterableAttributes()
|
||||
{
|
||||
$helper = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Catalog\Helper\Product\Flat\Indexer::class,
|
||||
['addFilterableAttrs' => 1]
|
||||
);
|
||||
$this->assertEquals(1, $helper->isAddFilterableAttributes());
|
||||
}
|
||||
|
||||
public function testIsAddChildDataDefault()
|
||||
{
|
||||
$this->assertEquals(0, $this->_helper->isAddChildData());
|
||||
}
|
||||
|
||||
public function testIsAddChildData()
|
||||
{
|
||||
$helper = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Catalog\Helper\Product\Flat\Indexer::class,
|
||||
['addChildData' => 1]
|
||||
);
|
||||
$this->assertEquals(1, $helper->isAddChildData());
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Catalog\Helper\Product\Stub;
|
||||
|
||||
class ProductControllerStub extends \Magento\Catalog\Controller\Product
|
||||
{
|
||||
public function execute()
|
||||
{
|
||||
// Empty method stub for test
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Catalog\Helper\Product;
|
||||
|
||||
use Magento\Customer\Model\Context;
|
||||
|
||||
/**
|
||||
* @magentoAppArea frontend
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class ViewTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Catalog\Helper\Product\View
|
||||
*/
|
||||
protected $_helper;
|
||||
|
||||
/**
|
||||
* @var \Magento\Catalog\Controller\Product
|
||||
*/
|
||||
protected $_controller;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\View\Result\Page
|
||||
*/
|
||||
protected $page;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
|
||||
$this->objectManager->get(\Magento\Framework\App\State::class)->setAreaCode('frontend');
|
||||
$this->objectManager->get(\Magento\Framework\App\Http\Context::class)
|
||||
->setValue(Context::CONTEXT_AUTH, false, false);
|
||||
$this->objectManager->get(\Magento\Framework\View\DesignInterface::class)
|
||||
->setDefaultDesignTheme();
|
||||
$this->_helper = $this->objectManager->get(\Magento\Catalog\Helper\Product\View::class);
|
||||
$request = $this->objectManager->get(\Magento\TestFramework\Request::class);
|
||||
$request->setRouteName('catalog')->setControllerName('product')->setActionName('view');
|
||||
$arguments = [
|
||||
'request' => $request,
|
||||
'response' => $this->objectManager->get(\Magento\TestFramework\Response::class),
|
||||
];
|
||||
$context = $this->objectManager->create(\Magento\Framework\App\Action\Context::class, $arguments);
|
||||
$this->_controller = $this->objectManager->create(
|
||||
\Magento\Catalog\Helper\Product\Stub\ProductControllerStub::class,
|
||||
['context' => $context]
|
||||
);
|
||||
$resultPageFactory = $this->objectManager->get(\Magento\Framework\View\Result\PageFactory::class);
|
||||
$this->page = $resultPageFactory->create();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup session, contaminated by product initialization methods
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->objectManager->get(\Magento\Catalog\Model\Session::class)->unsLastViewedProductId();
|
||||
$this->_controller = null;
|
||||
$this->_helper = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoAppArea frontend
|
||||
*/
|
||||
public function testInitProductLayout()
|
||||
{
|
||||
$uniqid = uniqid();
|
||||
/** @var $product \Magento\Catalog\Model\Product */
|
||||
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
|
||||
$product->setTypeId(\Magento\Catalog\Model\Product\Type::DEFAULT_TYPE)
|
||||
->setId(99)
|
||||
->setSku('test-sku')
|
||||
->setUrlKey($uniqid);
|
||||
/** @var $objectManager \Magento\TestFramework\ObjectManager */
|
||||
$objectManager = $this->objectManager;
|
||||
$objectManager->get(\Magento\Framework\Registry::class)->register('product', $product);
|
||||
|
||||
$this->_helper->initProductLayout($this->page, $product);
|
||||
|
||||
/** @var \Magento\Framework\View\Page\Config $pageConfig */
|
||||
$pageConfig = $this->objectManager->get(\Magento\Framework\View\Page\Config::class);
|
||||
$bodyClass = $pageConfig->getElementAttribute(
|
||||
\Magento\Framework\View\Page\Config::ELEMENT_TYPE_BODY,
|
||||
\Magento\Framework\View\Page\Config::BODY_ATTRIBUTE_CLASS
|
||||
);
|
||||
$this->assertStringContainsString("product-{$uniqid}", $bodyClass);
|
||||
$handles = $this->page->getLayout()->getUpdate()->getHandles();
|
||||
$this->assertContains('catalog_product_view_type_simple', $handles);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoAppArea frontend
|
||||
*/
|
||||
public function testPrepareAndRender()
|
||||
{
|
||||
// need for \Magento\Review\Block\Form::getProductInfo()
|
||||
$this->objectManager->get(\Magento\Framework\App\RequestInterface::class)->setParam('id', 10);
|
||||
|
||||
$this->_helper->prepareAndRender($this->page, 10, $this->_controller);
|
||||
/** @var \Magento\TestFramework\Response $response */
|
||||
$response = $this->objectManager->get(\Magento\TestFramework\Response::class);
|
||||
$this->page->renderResult($response);
|
||||
$this->assertNotEmpty($response->getBody());
|
||||
$this->assertEquals(
|
||||
10,
|
||||
$this->objectManager->get(
|
||||
\Magento\Catalog\Model\Session::class
|
||||
)->getLastViewedProductId()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testPrepareAndRenderWrongController()
|
||||
{
|
||||
$this->expectException(\Magento\Framework\Exception\NoSuchEntityException::class);
|
||||
|
||||
$objectManager = $this->objectManager;
|
||||
$controller = $objectManager->create(\Magento\Catalog\Helper\Product\Stub\ProductControllerStub::class);
|
||||
$this->_helper->prepareAndRender($this->page, 10, $controller);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testPrepareAndRenderWrongProduct()
|
||||
{
|
||||
$this->expectException(\Magento\Framework\Exception\NoSuchEntityException::class);
|
||||
|
||||
$this->_helper->prepareAndRender($this->page, 999, $this->_controller);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Catalog\Helper;
|
||||
|
||||
use Exception;
|
||||
use Magento\Catalog\Api\Data\CategoryInterfaceFactory;
|
||||
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Model\Category;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Catalog\Model\Product\Attribute\Source\Status;
|
||||
use Magento\Catalog\Model\Product\Visibility;
|
||||
use Magento\Catalog\Model\Session;
|
||||
use Magento\Catalog\Helper\Product as ProductHelper;
|
||||
use Magento\Framework\DataObject;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoAppArea frontend
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class ProductTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ProductHelper
|
||||
*/
|
||||
protected $helper;
|
||||
|
||||
/**
|
||||
* @var ProductRepositoryInterface
|
||||
*/
|
||||
protected $productRepository;
|
||||
|
||||
/**
|
||||
* @var ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var ProductInterfaceFactory
|
||||
*/
|
||||
private $productFactory;
|
||||
|
||||
/**
|
||||
* @var Registry
|
||||
*/
|
||||
private $registry;
|
||||
|
||||
/**
|
||||
* @inheridoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->helper = $this->objectManager->get(ProductHelper::class);
|
||||
/** @var ProductInterfaceFactory $productInterfaceFactory */
|
||||
$this->productFactory = $this->objectManager->get(ProductInterfaceFactory::class);
|
||||
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
$this->registry = $this->objectManager->get(Registry::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_simple.php
|
||||
*/
|
||||
public function testGetProductUrl()
|
||||
{
|
||||
$expectedUrl = 'http://localhost/index.php/simple-product.html';
|
||||
$product = $this->productRepository->get('simple');
|
||||
$this->assertEquals($expectedUrl, $this->helper->getProductUrl($product));
|
||||
|
||||
// product as ID
|
||||
$this->assertEquals($expectedUrl, $this->helper->getProductUrl($product->getId()));
|
||||
}
|
||||
|
||||
public function testGetPrice()
|
||||
{
|
||||
/** @var $product Product */
|
||||
$product = $this->productFactory->create();
|
||||
$product->setPrice(49.95);
|
||||
$this->assertEquals(49.95, $this->helper->getPrice($product));
|
||||
}
|
||||
|
||||
public function testGetFinalPrice()
|
||||
{
|
||||
/** @var $product Product */
|
||||
$product = $this->productFactory->create();
|
||||
$product->setPrice(49.95);
|
||||
$product->setFinalPrice(49.95);
|
||||
$this->assertEquals(49.95, $this->helper->getFinalPrice($product));
|
||||
}
|
||||
|
||||
public function testGetImageUrl()
|
||||
{
|
||||
/** @var $product Product */
|
||||
$product = $this->productFactory->create();
|
||||
$this->assertStringEndsWith('placeholder/image.jpg', $this->helper->getImageUrl($product));
|
||||
|
||||
$product->setImage('test_image.png');
|
||||
$this->assertStringEndsWith('/test_image.png', $this->helper->getImageUrl($product));
|
||||
}
|
||||
|
||||
public function testGetSmallImageUrl()
|
||||
{
|
||||
/** @var $product Product */
|
||||
$product = $this->productFactory->create();
|
||||
$this->assertStringEndsWith('placeholder/small_image.jpg', $this->helper->getSmallImageUrl($product));
|
||||
|
||||
$product->setSmallImage('test_image.png');
|
||||
$this->assertStringEndsWith('/test_image.png', $this->helper->getSmallImageUrl($product));
|
||||
}
|
||||
|
||||
public function testGetThumbnailUrl()
|
||||
{
|
||||
/** @var $product Product */
|
||||
$product = $this->productFactory->create();
|
||||
$this->assertStringEndsWith('placeholder/thumbnail.jpg', $this->helper->getThumbnailUrl($product));
|
||||
$product->setThumbnail('test_image.png');
|
||||
$this->assertStringEndsWith('/test_image.png', $this->helper->getThumbnailUrl($product));
|
||||
}
|
||||
|
||||
public function testGetEmailToFriendUrl()
|
||||
{
|
||||
$product = $this->productFactory->create();
|
||||
$product->setId(100);
|
||||
$category = $this->objectManager->create(CategoryInterfaceFactory::class)->create();
|
||||
$category->setId(10);
|
||||
$this->registry->register('current_category', $category);
|
||||
|
||||
try {
|
||||
$this->assertStringEndsWith(
|
||||
'sendfriend/product/send/id/100/cat_id/10/',
|
||||
$this->helper->getEmailToFriendUrl($product)
|
||||
);
|
||||
$this->registry->unregister('current_category');
|
||||
} catch (Exception $e) {
|
||||
$this->registry->unregister('current_category');
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetStatuses()
|
||||
{
|
||||
$this->assertEquals([], $this->helper->getStatuses());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Catalog/_files/products.php
|
||||
*/
|
||||
public function testCanShow()
|
||||
{
|
||||
// non-visible or disabled
|
||||
/** @var $product Product */
|
||||
$product = $this->productFactory->create();
|
||||
$this->assertFalse($this->helper->canShow($product));
|
||||
$existingProduct = $this->productRepository->get('simple');
|
||||
|
||||
// enabled and visible
|
||||
$product->setId($existingProduct->getId());
|
||||
$product->setStatus(Status::STATUS_ENABLED);
|
||||
$product->setVisibility(Visibility::VISIBILITY_BOTH);
|
||||
$this->assertTrue($this->helper->canShow($product));
|
||||
|
||||
$this->assertTrue($this->helper->canShow((int)$product->getId()));
|
||||
}
|
||||
|
||||
public function testCanUseCanonicalTagDefault()
|
||||
{
|
||||
$this->assertEquals('0', $this->helper->canUseCanonicalTag());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store catalog/seo/product_canonical_tag 1
|
||||
*/
|
||||
public function testCanUseCanonicalTag()
|
||||
{
|
||||
$this->assertEquals(1, $this->helper->canUseCanonicalTag());
|
||||
}
|
||||
|
||||
public function testGetAttributeInputTypes()
|
||||
{
|
||||
$types = $this->helper->getAttributeInputTypes();
|
||||
$this->assertArrayHasKey('multiselect', $types);
|
||||
$this->assertArrayHasKey('boolean', $types);
|
||||
foreach ($types as $type) {
|
||||
$this->assertIsArray($type);
|
||||
$this->assertNotEmpty($type);
|
||||
}
|
||||
|
||||
$this->assertNotEmpty($this->helper->getAttributeInputTypes('multiselect'));
|
||||
$this->assertNotEmpty($this->helper->getAttributeInputTypes('boolean'));
|
||||
}
|
||||
|
||||
public function testGetAttributeBackendModelByInputType()
|
||||
{
|
||||
$this->assertNotEmpty($this->helper->getAttributeBackendModelByInputType('multiselect'));
|
||||
$this->assertNull($this->helper->getAttributeBackendModelByInputType('boolean'));
|
||||
}
|
||||
|
||||
public function testGetAttributeSourceModelByInputType()
|
||||
{
|
||||
$this->assertNotEmpty($this->helper->getAttributeSourceModelByInputType('boolean'));
|
||||
$this->assertNull($this->helper->getAttributeSourceModelByInputType('multiselect'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoDataFixture Magento/Catalog/_files/categories.php
|
||||
*/
|
||||
public function testInitProduct()
|
||||
{
|
||||
$this->objectManager->get(Session::class)->setLastVisitedCategoryId(2);
|
||||
$product = $this->productRepository->get('simple');
|
||||
$this->helper->initProduct($product->getId(), 'view');
|
||||
|
||||
$this->assertInstanceOf(Product::class, $this->registry->registry('current_product'));
|
||||
$this->assertInstanceOf(Category::class, $this->registry->registry('current_category'));
|
||||
}
|
||||
|
||||
public function testPrepareProductOptions()
|
||||
{
|
||||
/** @var $product Product */
|
||||
$product = $this->productFactory->create();
|
||||
$buyRequest = new DataObject(['qty' => 100, 'options' => ['option' => 'value']]);
|
||||
$this->helper->prepareProductOptions($product, $buyRequest);
|
||||
$result = $product->getPreconfiguredValues();
|
||||
$this->assertInstanceOf(DataObject::class, $result);
|
||||
$this->assertEquals(100, $result->getQty());
|
||||
$this->assertEquals(['option' => 'value'], $result->getOptions());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user