Skip to content

Commit

Permalink
Merge pull request magento#5276 from magento-tsg/2.4-develop-com-pr5
Browse files Browse the repository at this point in the history
[TSG-Commerce] Tests for 2.4 (pr5)
  • Loading branch information
zakdma authored Feb 1, 2020
2 parents e6e6725 + 3f5b2f4 commit 90a479f
Show file tree
Hide file tree
Showing 23 changed files with 1,604 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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\ResourceModel\Currency;

/**
* Remove currency rates by currency code
*/
class RemoveCurrencyRateByCode
{
/** @var Currency */
private $currencyResource;

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

/**
* Remove currency rates
*
* @param string $currencyCode
* @return void
*/
public function execute(string $currencyCode): void
{
$connection = $this->currencyResource->getConnection();
$rateTable = $this->currencyResource->getTable('directory_currency_rate');
$connection->delete($rateTable, $connection->quoteInto('currency_to = ? OR currency_from = ?', $currencyCode));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Block\Product\View;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\PageFactory;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Class consist of general logic for currency tests
*/
abstract class AbstractCurrencyTest extends TestCase
{
protected const TIER_PRICE_BLOCK_NAME = 'product.price.tier';
protected const FINAL_PRICE_BLOCK_NAME = 'product.price.final';

/** @var ObjectManagerInterface */
protected $objectManager;

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

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

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

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

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

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

parent::tearDown();
}

/**
* Process price view on product page
*
* @param string|ProductInterface $product
* @param string $blockName
* @return string
*/
protected function processPriceView($product, string $blockName = self::FINAL_PRICE_BLOCK_NAME): string
{
$product = is_string($product) ? $this->productRepository->get($product) : $product;
$this->registerProduct($product);

return trim(
preg_replace('/(?:\s|&nbsp;)+/', ' ', strip_tags($this->getProductPriceBlockHtml($blockName)))
);
}

/**
* Get product price block content
*
* @param string $blockName
* @return string
*/
private function getProductPriceBlockHtml(string $blockName): string
{
$page = $this->pageFactory->create();
$page->addHandle([
'default',
'catalog_product_view',
'catalog_product_view_type_configurable',
]);
$page->getLayout()->generateXml();
$block = $page->getLayout()->getBlock($blockName);
$this->assertNotFalse($block);

return $block->toHtml();
}

/**
* Register the product
*
* @param ProductInterface $product
* @return void
*/
private function registerProduct(ProductInterface $product): void
{
$this->registry->unregister('product');
$this->registry->register('product', $product);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Block\Product\View;

use Magento\Store\Model\StoreManagerInterface;

/**
* Checks currency displaying and converting on the catalog pages on multi store mode
*
* @magentoAppArea frontend
* @magentoDbIsolation disabled
*/
class MultiStoreCurrencyTest extends AbstractCurrencyTest
{
/** @var StoreManagerInterface */
private $storeManager;

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

$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
}

/**
* @magentoConfigFixture default/currency/options/base USD
* @magentoConfigFixture current_store currency/options/default CNY
* @magentoConfigFixture current_store currency/options/allow CNY,USD
* @magentoConfigFixture fixturestore_store currency/options/default UAH
* @magentoConfigFixture fixturestore_store currency/options/allow UAH,USD
*
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
* @magentoDataFixture Magento/Directory/_files/usd_cny_rate.php
* @magentoDataFixture Magento/Directory/_files/usd_uah_rate.php
*
* @return void
*/
public function testMultiStoreRenderPrice(): void
{
$this->assertProductStorePrice('simple2', 'CN¥70.00');
$this->reloadProductPriceInfo();
$this->assertProductStorePrice('simple2', '₴240.00', 'fixturestore');
}

/**
* @magentoConfigFixture default/currency/options/base USD
* @magentoConfigFixture current_store currency/options/default CNY
* @magentoConfigFixture current_store currency/options/allow CNY,USD
* @magentoConfigFixture fixturestore_store currency/options/default UAH
* @magentoConfigFixture fixturestore_store currency/options/allow UAH,USD
*
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
* @magentoDataFixture Magento/Catalog/_files/product_special_price.php
* @magentoDataFixture Magento/Directory/_files/usd_cny_rate.php
* @magentoDataFixture Magento/Directory/_files/usd_uah_rate.php
*
* @return void
*/
public function testMultiStoreRenderSpecialPrice(): void
{
$this->assertProductStorePrice('simple', 'Special Price CN¥41.93 Regular Price CN¥70.00');
$this->reloadProductPriceInfo();
$this->assertProductStorePrice('simple', 'Special Price ₴143.76 Regular Price ₴240.00', 'fixturestore');
}

/**
* @magentoConfigFixture default/currency/options/base USD
* @magentoConfigFixture current_store currency/options/default CNY
* @magentoConfigFixture current_store currency/options/allow CNY,USD
* @magentoConfigFixture fixturestore_store currency/options/default UAH
* @magentoConfigFixture fixturestore_store currency/options/allow UAH,USD
*
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_fixed_tier_price.php
* @magentoDataFixture Magento/Directory/_files/usd_cny_rate.php
* @magentoDataFixture Magento/Directory/_files/usd_uah_rate.php
*
* @return void
*/
public function testMultiStoreRenderTierPrice(): void
{
$this->assertProductStorePrice(
'simple-product-tax-none',
'Buy 2 for CN¥280.00 each and save 80%',
'default',
self::TIER_PRICE_BLOCK_NAME
);
$this->reloadProductPriceInfo();
$this->assertProductStorePrice(
'simple-product-tax-none',
'Buy 2 for ₴960.00 each and save 80%',
'fixturestore',
self::TIER_PRICE_BLOCK_NAME
);
}

/**
* Check price per stores
*
* @param string $productSku
* @param string $expectedData
* @param string $storeCode
* @param string $priceBlockName
* @return void
*/
private function assertProductStorePrice(
string $productSku,
string $expectedData,
string $storeCode = 'default',
string $priceBlockName = self::FINAL_PRICE_BLOCK_NAME
): void {
$currentStore = $this->storeManager->getStore();
try {
if ($currentStore->getCode() !== $storeCode) {
$this->storeManager->setCurrentStore($storeCode);
}

$actualData = $this->processPriceView($productSku, $priceBlockName);
$this->assertEquals($expectedData, $actualData);
} finally {
if ($currentStore->getCode() !== $storeCode) {
$this->storeManager->setCurrentStore($currentStore);
}
}
}

/**
* Reload product price info
*
* @return void
*/
private function reloadProductPriceInfo(): void
{
$product = $this->registry->registry('product');
$this->assertNotNull($product);
$product->reloadPriceInfo();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Block\Product\View;

/**
* Checks currency displaying and converting on the catalog pages
*
* @magentoAppArea frontend
*/
class SingleStoreCurrencyTest extends AbstractCurrencyTest
{
/**
* @magentoConfigFixture current_store currency/options/base USD
* @magentoConfigFixture current_store currency/options/default CNY
* @magentoConfigFixture current_store currency/options/allow CNY,USD
*
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
* @magentoDataFixture Magento/Directory/_files/usd_cny_rate.php
*
* @return void
*/
public function testRenderPrice(): void
{
$priceHtml = $this->processPriceView('simple2');
$this->assertEquals('CN¥70.00', $priceHtml);
}

/**
* @magentoConfigFixture current_store currency/options/base USD
* @magentoConfigFixture current_store currency/options/default CNY
* @magentoConfigFixture current_store currency/options/allow EUR,CNY
*
* @magentoDataFixture Magento/Catalog/_files/product_special_price.php
* @magentoDataFixture Magento/Directory/_files/usd_cny_rate.php
*
* @return void
*/
public function testRenderSpecialPrice(): void
{
$priceHtml = $this->processPriceView('simple');
$this->assertEquals('Special Price CN¥41.93 Regular Price CN¥70.00', $priceHtml);
}

/**
* @magentoConfigFixture current_store currency/options/base USD
* @magentoConfigFixture current_store currency/options/default CNY
* @magentoConfigFixture current_store currency/options/allow CNY,USD
*
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_fixed_tier_price.php
* @magentoDataFixture Magento/Directory/_files/usd_cny_rate.php
*
* @return void
*/
public function testRenderTierPrice(): void
{
$priceHtml = $this->processPriceView('simple-product-tax-none', self::TIER_PRICE_BLOCK_NAME);
$this->assertEquals('Buy 2 for CN¥280.00 each and save 80%', $priceHtml);
}
}
Loading

0 comments on commit 90a479f

Please sign in to comment.