Skip to content

Commit

Permalink
Merge pull request #4250 from magento-tsg-csl3/2.3-develop-pr22
Browse files Browse the repository at this point in the history
[TSG-CSL3] For 2.3 (pr22)
  • Loading branch information
zakdma authored May 27, 2019
2 parents 9f8bfc8 + b6bb82c commit 15100ac
Show file tree
Hide file tree
Showing 15 changed files with 727 additions and 137 deletions.
90 changes: 70 additions & 20 deletions app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1578,26 +1578,9 @@ public function addAttributeToFilter($attribute, $condition = null, $joinType =
$this->_allIdsCache = null;

if (is_string($attribute) && $attribute == 'is_saleable') {
$columns = $this->getSelect()->getPart(\Magento\Framework\DB\Select::COLUMNS);
foreach ($columns as $columnEntry) {
list($correlationName, $column, $alias) = $columnEntry;
if ($alias == 'is_saleable') {
if ($column instanceof \Zend_Db_Expr) {
$field = $column;
} else {
$connection = $this->getSelect()->getConnection();
if (empty($correlationName)) {
$field = $connection->quoteColumnAs($column, $alias, true);
} else {
$field = $connection->quoteColumnAs([$correlationName, $column], $alias, true);
}
}
$this->getSelect()->where("{$field} = ?", $condition);
break;
}
}

return $this;
$this->addIsSaleableAttributeToFilter($condition);
} elseif (is_string($attribute) && $attribute == 'tier_price') {
$this->addTierPriceAttributeToFilter($attribute, $condition);
} else {
return parent::addAttributeToFilter($attribute, $condition, $joinType);
}
Expand Down Expand Up @@ -2481,4 +2464,71 @@ public function getPricesCount()

return $this->_pricesCount;
}

/**
* Add is_saleable attribute to filter
*
* @param array|null $condition
* @return $this
*/
private function addIsSaleableAttributeToFilter(?array $condition): self
{
$columns = $this->getSelect()->getPart(Select::COLUMNS);
foreach ($columns as $columnEntry) {
list($correlationName, $column, $alias) = $columnEntry;
if ($alias == 'is_saleable') {
if ($column instanceof \Zend_Db_Expr) {
$field = $column;
} else {
$connection = $this->getSelect()->getConnection();
if (empty($correlationName)) {
$field = $connection->quoteColumnAs($column, $alias, true);
} else {
$field = $connection->quoteColumnAs([$correlationName, $column], $alias, true);
}
}
$this->getSelect()->where("{$field} = ?", $condition);
break;
}
}

return $this;
}

/**
* Add tier price attribute to filter
*
* @param string $attribute
* @param array|null $condition
* @return $this
*/
private function addTierPriceAttributeToFilter(string $attribute, ?array $condition): self
{
$attrCode = $attribute;
$connection = $this->getConnection();
$attrTable = $this->_getAttributeTableAlias($attrCode);
$entity = $this->getEntity();
$fKey = 'e.' . $this->getEntityPkName($entity);
$pKey = $attrTable . '.' . $this->getEntityPkName($entity);
$attribute = $entity->getAttribute($attrCode);
$attrFieldName = $attrTable . '.value';
$fKey = $connection->quoteColumnAs($fKey, null);
$pKey = $connection->quoteColumnAs($pKey, null);

$condArr = ["{$pKey} = {$fKey}"];
$this->getSelect()->join(
[$attrTable => $this->getTable('catalog_product_entity_tier_price')],
'(' . implode(') AND (', $condArr) . ')',
[$attrCode => $attrFieldName]
);
$this->removeAttributeToSelect($attrCode);
$this->_filterAttributes[$attrCode] = $attribute->getId();
$this->_joinFields[$attrCode] = ['table' => '', 'field' => $attrFieldName];
$field = $this->_getAttributeTableAlias($attrCode) . '.value';
$conditionSql = $this->_getConditionSql($field, $condition);
$this->getSelect()->where($conditionSql, null, Select::TYPE_CONDITION);
$this->_totalRecords = null;

return $this;
}
}
5 changes: 3 additions & 2 deletions app/code/Magento/Catalog/etc/acl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
<resource id="Magento_Backend::admin">
<resource id="Magento_Catalog::catalog" title="Catalog" translate="title" sortOrder="30">
<resource id="Magento_Catalog::catalog_inventory" title="Inventory" translate="title" sortOrder="10">
<resource id="Magento_Catalog::products" title="Products" translate="title" sortOrder="10" />
<resource id="Magento_Catalog::products" title="Products" translate="title" sortOrder="10">
<resource id="Magento_Catalog::update_attributes" title="Update Attributes" translate="title" />
</resource>
<resource id="Magento_Catalog::categories" title="Categories" translate="title" sortOrder="20" />
</resource>
</resource>
Expand All @@ -23,7 +25,6 @@
</resource>
<resource id="Magento_Backend::stores_attributes">
<resource id="Magento_Catalog::attributes_attributes" title="Product" translate="title" sortOrder="30" />
<resource id="Magento_Catalog::update_attributes" title="Update Attributes" translate="title" sortOrder="35" />
<resource id="Magento_Catalog::sets" title="Attribute Set" translate="title" sortOrder="40"/>
</resource>
</resource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ public function savePaymentInformation(
$quoteRepository = $this->getCartRepository();
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $quoteRepository->getActive($cartId);
$customerId = $quote->getBillingAddress()
->getCustomerId();
if (!$billingAddress->getCustomerId() && $customerId) {
//It's necessary to verify the price rules with the customer data
$billingAddress->setCustomerId($customerId);
}
$quote->removeAddress($quote->getBillingAddress()->getId());
$quote->setBillingAddress($billingAddress);
$quote->setDataChanges(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,31 @@ public function testSavePaymentInformationAndPlaceOrderWithLocolizedException()
$this->model->savePaymentInformationAndPlaceOrder($cartId, $paymentMock, $billingAddressMock);
}

/**
* Test for save payment and place order with new billing address
*
* @return void
*/
public function testSavePaymentInformationAndPlaceOrderWithNewBillingAddress(): void
{
$cartId = 100;
$quoteBillingAddressId = 1;
$customerId = 1;
$quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
$quoteBillingAddress = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
$billingAddressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
$paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);

$quoteBillingAddress->method('getCustomerId')->willReturn($customerId);
$quoteMock->method('getBillingAddress')->willReturn($quoteBillingAddress);
$quoteBillingAddress->method('getId')->willReturn($quoteBillingAddressId);
$this->cartRepositoryMock->method('getActive')->with($cartId)->willReturn($quoteMock);

$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
$billingAddressMock->expects($this->once())->method('setCustomerId')->with($customerId);
$this->assertTrue($this->model->savePaymentInformation($cartId, $paymentMock, $billingAddressMock));
}

/**
* @param int $cartId
* @param \PHPUnit_Framework_MockObject_MockObject $billingAddressMock
Expand All @@ -179,9 +204,10 @@ private function getMockForAssignBillingAddress($cartId, $billingAddressMock)
['setLimitCarrier', 'getShippingMethod', 'getShippingRateByCode']
);
$this->cartRepositoryMock->expects($this->any())->method('getActive')->with($cartId)->willReturn($quoteMock);
$quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($quoteBillingAddress);
$quoteMock->method('getBillingAddress')->willReturn($quoteBillingAddress);
$quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($quoteShippingAddress);
$quoteBillingAddress->expects($this->once())->method('getId')->willReturn($billingAddressId);
$quoteBillingAddress->expects($this->once())->method('getId')->willReturn($billingAddressId);
$quoteMock->expects($this->once())->method('removeAddress')->with($billingAddressId);
$quoteMock->expects($this->once())->method('setBillingAddress')->with($billingAddressMock);
$quoteMock->expects($this->once())->method('setDataChanges')->willReturnSelf();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
<test name="AdminExactMatchSearchInCustomerGridTest">
<annotations>
<features value="Customer"/>
<stories value="Customer Search"/>
<title value="Admin customer grid exact match searching"/>
<description value="Admin customer grid exact match searching with quotes in keyword"/>
<severity value="MAJOR"/>
<testCaseId value="MC-16335"/>
<useCaseId value="MAGETWO-99605"/>
<group value="customer"/>
</annotations>
<before>
<createData entity="Simple_US_Customer" stepKey="createFirstCustomer"/>
<createData entity="Simple_US_Customer" stepKey="createSecondCustomer">
<field key="firstname">&quot;Jane Doe&quot;</field>
</createData>
<actionGroup ref="LoginAsAdmin" stepKey="login"/>
</before>
<after>
<deleteData createDataKey="createFirstCustomer" stepKey="deleteFirstCustomer"/>
<deleteData createDataKey="createSecondCustomer" stepKey="deleteSecondCustomer"/>
<amOnPage url="{{AdminCustomerPage.url}}" stepKey="openCustomersGridPage"/>
<actionGroup ref="AdminResetFilterInCustomerAddressGrid" stepKey="clearCustomerGridFilter"/>
<actionGroup ref="logout" stepKey="logout"/>
</after>
<!--Step 1: Go to Customers > All Customers-->
<amOnPage url="{{AdminCustomerPage.url}}" stepKey="openCustomersGridPage"/>
<!--Step 2: On Customers grid page search customer by keyword with quotes-->
<actionGroup ref="searchAdminDataGridByKeyword" stepKey="searchCustomer">
<argument name="keyword" value="$$createSecondCustomer.firstname$$"/>
</actionGroup>
<!--Step 3: Check if customer is placed in a first row and clear grid filter-->
<actionGroup ref="AdminAssertCustomerInCustomersGrid" stepKey="checkCustomerInGrid">
<argument name="text" value="$$createSecondCustomer.fullname$$"/>
<argument name="row" value="1"/>
</actionGroup>
</test>
</tests>
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,6 @@ public function __construct(
*/
protected $_foregroundCountries = [];

/**
* Add top destinition countries to head of option array
*
* @param string $emptyLabel
* @param array $options
* @return array
*/
private function addForegroundCountriesToOptionArray($emptyLabel, $options)
{
if ($emptyLabel !== false && count($this->_foregroundCountries) !== 0 &&
count($options) === count($this->_foregroundCountries)
) {
$options[] = ['value' => '', 'label' => $emptyLabel];
return $options;
}
return $options;
}

/**
* Define main table
*
Expand Down Expand Up @@ -269,24 +251,20 @@ public function addCountryIdFilter($countryId)
public function toOptionArray($emptyLabel = ' ')
{
$options = $this->_toOptionArray('country_id', 'name', ['title' => 'iso2_code']);
$sort = [];
foreach ($options as $data) {
$name = (string)$this->_localeLists->getCountryTranslation($data['value']);
if (!empty($name)) {
$sort[$name] = $data['value'];
}
}
$sort = $this->getSort($options);

$this->_arrayUtils->ksortMultibyte($sort, $this->_localeResolver->getLocale());
foreach (array_reverse($this->_foregroundCountries) as $foregroundCountry) {
$name = array_search($foregroundCountry, $sort);
unset($sort[$name]);
$sort = [$name => $foregroundCountry] + $sort;
if ($name) {
unset($sort[$name]);
$sort = [$name => $foregroundCountry] + $sort;
}
}
$isRegionVisible = (bool)$this->helperData->isShowNonRequiredState();

$options = [];
foreach ($sort as $label => $value) {
$options = $this->addForegroundCountriesToOptionArray($emptyLabel, $options);
$option = ['value' => $value, 'label' => $label];
if ($this->helperData->isRegionRequired($value)) {
$option['is_region_required'] = true;
Expand Down Expand Up @@ -366,4 +344,23 @@ public function getCountriesWithRequiredStates()
}
return $countries;
}

/**
* Get sort
*
* @param array $options
* @return array
*/
private function getSort(array $options): array
{
$sort = [];
foreach ($options as $data) {
$name = (string)$this->_localeLists->getCountryTranslation($data['value']);
if (!empty($name)) {
$sort[$name] = $data['value'];
}
}

return $sort;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ public function testToOptionArray($optionsArray, $emptyLabel, $foregroundCountri

$this->_model->setForegroundCountries($foregroundCountries);
$result = $this->_model->toOptionArray($emptyLabel);
$this->assertCount(count($optionsArray) + (int)(!empty($emptyLabel) && !empty($foregroundCountries)) +
(int)(!empty($emptyLabel)), $result);
$this->assertCount(count($optionsArray) + (int)(!empty($emptyLabel)), $result);
foreach ($expectedResults as $index => $expectedResult) {
$this->assertEquals($expectedResult, $result[$index]['label']);
}
Expand All @@ -121,8 +120,10 @@ public function toOptionArrayDataProvider()
[$optionsArray, false, [], ['AD', 'US', 'ES', 'BZ']],
[$optionsArray, false, 'US', ['US', 'AD', 'ES', 'BZ']],
[$optionsArray, false, ['US', 'BZ'], ['US', 'BZ', 'AD', 'ES']],
[$optionsArray, ' ', 'US', [' ', 'US', ' ', 'AD', 'ES', 'BZ']],
[$optionsArray, ' ', [], [' ', 'AD', 'US', 'ES', 'BZ']]
[$optionsArray, ' ', 'US', [' ', 'US', 'AD', 'ES', 'BZ']],
[$optionsArray, ' ', [], [' ', 'AD', 'US', 'ES', 'BZ']],
[$optionsArray, ' ', 'UA', [' ', 'AD', 'US', 'ES', 'BZ']],
[$optionsArray, ' ', ['AF', 'UA'], [' ', 'AD', 'US', 'ES', 'BZ']],
];
}
}
19 changes: 16 additions & 3 deletions app/code/Magento/Sales/Controller/Adminhtml/Order/AddressSave.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Directory\Model\RegionFactory;
use Magento\Sales\Api\OrderAddressRepositoryInterface;
use Magento\Sales\Api\OrderManagementInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\Data\OrderAddressInterface;
Expand All @@ -25,11 +26,14 @@
use Magento\Framework\Controller\Result\RawFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Action\HttpPostActionInterface;

/**
* Sales address save
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AddressSave extends Order
class AddressSave extends Order implements HttpPostActionInterface
{
/**
* Authorization level of a basic admin session
Expand All @@ -43,6 +47,11 @@ class AddressSave extends Order
*/
private $regionFactory;

/**
* @var OrderAddressRepositoryInterface
*/
private $orderAddressRepository;

/**
* @param Context $context
* @param Registry $coreRegistry
Expand All @@ -56,6 +65,7 @@ class AddressSave extends Order
* @param OrderRepositoryInterface $orderRepository
* @param LoggerInterface $logger
* @param RegionFactory|null $regionFactory
* @param OrderAddressRepositoryInterface|null $orderAddressRepository
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -71,9 +81,12 @@ public function __construct(
OrderManagementInterface $orderManagement,
OrderRepositoryInterface $orderRepository,
LoggerInterface $logger,
RegionFactory $regionFactory = null
RegionFactory $regionFactory = null,
OrderAddressRepositoryInterface $orderAddressRepository = null
) {
$this->regionFactory = $regionFactory ?: ObjectManager::getInstance()->get(RegionFactory::class);
$this->orderAddressRepository = $orderAddressRepository ?: ObjectManager::getInstance()
->get(OrderAddressRepositoryInterface::class);
parent::__construct(
$context,
$coreRegistry,
Expand Down Expand Up @@ -107,7 +120,7 @@ public function execute()
if ($data && $address->getId()) {
$address->addData($data);
try {
$address->save();
$this->orderAddressRepository->save($address);
$this->_eventManager->dispatch(
'admin_sales_order_address_update',
[
Expand Down
Loading

0 comments on commit 15100ac

Please sign in to comment.