Skip to content

Commit

Permalink
refactored to decrease execution time
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnaAPak committed Jul 23, 2021
1 parent b3aa9ff commit deceabb
Show file tree
Hide file tree
Showing 80 changed files with 2,332 additions and 690 deletions.
27 changes: 23 additions & 4 deletions app/code/Magento/Backend/Block/Store/Switcher.php
Expand Up @@ -7,6 +7,8 @@

namespace Magento\Backend\Block\Store;

use Magento\Framework\Exception\LocalizedException;

/**
* Store switcher block
*
Expand Down Expand Up @@ -471,9 +473,17 @@ public function getCurrentSelectionName()
*/
public function getCurrentWebsiteName()
{
if ($this->getWebsiteId() !== null) {
$websiteId = $this->getWebsiteId();
if ($websiteId !== null) {
if ($this->hasData('get_data_from_request')) {
$requestedWebsite = $this->getRequest()->getParams('website');
if (!empty($requestedWebsite)
&& array_key_exists('website', $requestedWebsite)) {
$websiteId = $requestedWebsite['website'];
}
}
$website = $this->_websiteFactory->create();
$website->load($this->getWebsiteId());
$website->load($websiteId);
if ($website->getId()) {
return $website->getName();
}
Expand Down Expand Up @@ -504,12 +514,21 @@ public function getCurrentStoreGroupName()
* Get current store view name
*
* @return string
* @throws LocalizedException
*/
public function getCurrentStoreName()
{
if ($this->getStoreId() !== null) {
$storeId = $this->getStoreId();
if ($storeId !== null) {
if ($this->hasData('get_data_from_request')) {
$requestedStore = $this->getRequest()->getParams('store');
if (!empty($requestedStore)
&& array_key_exists('store', $requestedStore)) {
$storeId = $requestedStore['store'];
}
}
$store = $this->_storeFactory->create();
$store->load($this->getStoreId());
$store->load($storeId);
if ($store->getId()) {
return $store->getName();
}
Expand Down
10 changes: 10 additions & 0 deletions app/code/Magento/Backend/Test/Mftf/Page/AdminOrderViewPage.xml
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<pages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/PageObject.xsd">
<page name="AdminOrderViewPage" url="sales/order/view/order_id/{{entity_id}}" area="admin" module="Backend"/>
</pages>
157 changes: 155 additions & 2 deletions app/code/Magento/Backend/Test/Unit/Block/Store/SwitcherTest.php
Expand Up @@ -8,10 +8,15 @@
namespace Magento\Backend\Test\Unit\Block\Store;

use Magento\Backend\Block\Store\Switcher;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Model\StoreFactory;
use Magento\Store\Model\Store;
use Magento\Store\Model\WebsiteFactory;
use Magento\Store\Model\Website;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\Website;
use PHPUnit\Framework\TestCase;

class SwitcherTest extends TestCase
Expand All @@ -23,20 +28,81 @@ class SwitcherTest extends TestCase

private $storeManagerMock;

/**
* @var RequestInterface|MockObject
*/
private $requestMock;

/**
* @var WebsiteFactory|MockObject
*/
private $websiteFactoryMock;

/**
* @var StoreFactory|MockObject
*/
private $storeFactoryMock;

/**
* @var Website|MockObject
*/
private $websiteMock;

/**
* @var Store|MockObject
*/
private $storeMock;

protected function setUp(): void
{
$this->storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class);
$objectHelper = new ObjectManager($this);
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
->getMockForAbstractClass();
$this->websiteFactoryMock = $this->getMockBuilder(WebsiteFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->storeFactoryMock = $this->getMockBuilder(StoreFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->websiteMock = $this->getMockBuilder(Website::class)
->disableOriginalConstructor()
->setMethods(['load', 'getId', 'getName'])
->getMock();
$this->storeMock = $this->getMockBuilder(Store::class)
->disableOriginalConstructor()
->setMethods(['load', 'getId', 'getName'])
->getMock();
$this->websiteFactoryMock->expects($this->any())
->method('create')
->willReturn($this->websiteMock);
$this->storeFactoryMock->expects($this->any())
->method('create')
->willReturn($this->storeMock);
$this->websiteMock->expects($this->any())
->method('load')
->willReturnSelf();
$this->storeMock->expects($this->any())
->method('load')
->willReturnSelf();
$context = $objectHelper->getObject(
Context::class,
[
'storeManager' => $this->storeManagerMock,
'request' => $this->requestMock
]
);

$this->switcherBlock = $objectHelper->getObject(
Switcher::class,
['context' => $context]
[
'context' => $context,
'data' => ['get_data_from_request' => 1],
'websiteFactory' => $this->websiteFactoryMock,
'storeFactory' => $this->storeFactoryMock
]
);
}

Expand All @@ -58,4 +124,91 @@ public function testGetWebsitesIfSetWebsiteIds()
$expected = [1 => $websiteMock];
$this->assertEquals($expected, $this->switcherBlock->getWebsites());
}

/**
* Test case for after current store name plugin
*
* @param array $requestedStore
* @param string $expectedResult
* @return void
* @dataProvider getStoreNameDataProvider
* @throws LocalizedException
*/
public function testAfterGetCurrentStoreName(array $requestedStore, string $expectedResult): void
{
$this->requestMock->expects($this->any())
->method('getParams')
->willReturn($requestedStore);
$this->storeMock->expects($this->any())
->method('getId')
->willReturn($requestedStore);
$this->storeMock->expects($this->any())
->method('getName')
->willReturn($expectedResult);
$this->assertSame($expectedResult, $this->switcherBlock->getCurrentStoreName());
}

/**
* Data provider for getStoreName plugin
*
* @return array
*/
public function getStoreNameDataProvider(): array
{
return [
'test storeName with valid requested store' =>
[
['store' => 'test store'],
'base store'
],
'test storeName with invalid requested store' =>
[
['store' => 'test store'],
'test store'
]
];
}

/**
* Test case for get current website name
*
* @param array $requestedWebsite
* @param string $expectedResult
* @return void
* @dataProvider getWebsiteNameDataProvider
*/
public function testGetCurrentWebsiteName(array $requestedWebsite, string $expectedResult): void
{
$this->requestMock->expects($this->any())
->method('getParams')
->willReturn($requestedWebsite);
$this->websiteMock->expects($this->any())
->method('getId')
->willReturn($requestedWebsite);
$this->websiteMock->expects($this->any())
->method('getName')
->willReturn($expectedResult);
$this->assertSame($expectedResult, $this->switcherBlock->getCurrentWebsiteName());
}

/**
* Data provider for getWebsiteName plugin
*
* @return array
*/
public function getWebsiteNameDataProvider(): array
{
return [
'test websiteName with valid requested website' =>
[
['website' => 'test website'],
'base website'
],
'test websiteName with invalid requested website' =>
[
['website' => 'test website'],
'test website'
]
];
}
}
30 changes: 25 additions & 5 deletions app/code/Magento/Catalog/Model/Product/Copier.php
Expand Up @@ -6,12 +6,13 @@
namespace Magento\Catalog\Model\Product;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Attribute\ScopeOverriddenValue;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Option\Repository as OptionRepository;
use Magento\Catalog\Model\ProductFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Catalog\Model\ResourceModel\DuplicatedProductAttributesCopier;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Store\Model\Store;
use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException;
Expand Down Expand Up @@ -50,25 +51,41 @@ class Copier
*/
private $scopeOverriddenValue;

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

/**
* @var DuplicatedProductAttributesCopier
*/
private $attributeCopier;

/**
* @param CopyConstructorInterface $copyConstructor
* @param ProductFactory $productFactory
* @param ScopeOverriddenValue $scopeOverriddenValue
* @param OptionRepository|null $optionRepository
* @param MetadataPool|null $metadataPool
* @param ProductRepositoryInterface $productRepository
* @param DuplicatedProductAttributesCopier $attributeCopier
*/
public function __construct(
CopyConstructorInterface $copyConstructor,
ProductFactory $productFactory,
ScopeOverriddenValue $scopeOverriddenValue,
OptionRepository $optionRepository,
MetadataPool $metadataPool
MetadataPool $metadataPool,
ProductRepositoryInterface $productRepository,
DuplicatedProductAttributesCopier $attributeCopier
) {
$this->productFactory = $productFactory;
$this->copyConstructor = $copyConstructor;
$this->scopeOverriddenValue = $scopeOverriddenValue;
$this->optionRepository = $optionRepository;
$this->metadataPool = $metadataPool;
$this->productRepository = $productRepository;
$this->attributeCopier = $attributeCopier;
}

/**
Expand All @@ -79,11 +96,13 @@ public function __construct(
*/
public function copy(Product $product): Product
{
$product->getWebsiteIds();
$product->getCategoryIds();

$metadata = $this->metadataPool->getMetadata(ProductInterface::class);

/* Regardless in what scope the product was provided,
for duplicating we want to clone product in Global scope first */
if ((int)$product->getStoreId() !== Store::DEFAULT_STORE_ID) {
$product = $this->productRepository->getById($product->getId(), true, Store::DEFAULT_STORE_ID);
}
/** @var Product $duplicate */
$duplicate = $this->productFactory->create();
$productData = $product->getData();
Expand All @@ -102,6 +121,7 @@ public function copy(Product $product): Product
$duplicate->setStoreId(Store::DEFAULT_STORE_ID);
$this->copyConstructor->build($product, $duplicate);
$this->setDefaultUrl($product, $duplicate);
$this->attributeCopier->copyProductAttributes($product, $duplicate);
$this->setStoresUrl($product, $duplicate);
$this->optionRepository->duplicate($product, $duplicate);

Expand Down

0 comments on commit deceabb

Please sign in to comment.