Skip to content

Commit

Permalink
Merge pull request magento#3325 from magento-tsg-csl3/2.2-develop-pr6
Browse files Browse the repository at this point in the history
[TSG-CSL3] Backporting 2.2 (pr6)
  • Loading branch information
viktym authored Oct 22, 2018
2 parents 8fd8aca + d418636 commit e9c1946
Show file tree
Hide file tree
Showing 20 changed files with 515 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function build(array $buildSubject)

$payment = $paymentDO->getPayment();
$data = $payment->getAdditionalInformation();
// the payment token could be stored only if a customer checks the Vault flow on storefront
// see https://developers.braintreepayments.com/guides/paypal/vault/javascript/v2#invoking-the-vault-flow
if (!empty($data[VaultConfigProvider::IS_ACTIVE_CODE])) {
$result[self::$optionsKey] = [
self::$storeInVaultOnSuccess => true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\Braintree\Gateway\Request;

use Magento\Braintree\Gateway\SubjectReader;
use Magento\Payment\Gateway\Command\CommandException;
use Magento\Payment\Gateway\Request\BuilderInterface;
use Magento\Payment\Helper\Formatter;

Expand Down Expand Up @@ -41,6 +42,9 @@ public function build(array $buildSubject)
$payment = $paymentDO->getPayment();
$extensionAttributes = $payment->getExtensionAttributes();
$paymentToken = $extensionAttributes->getVaultPaymentToken();
if ($paymentToken === null) {
throw new CommandException(__('The Payment Token is not available to perform the request.'));
}
return [
'amount' => $this->formatPrice($this->subjectReader->readAmount($buildSubject)),
'paymentMethodToken' => $paymentToken->getGatewayToken()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Braintree\Test\Unit\Gateway\Request;

use Magento\Braintree\Gateway\SubjectReader;
use Magento\Braintree\Gateway\Request\VaultCaptureDataBuilder;
use Magento\Braintree\Gateway\SubjectReader;
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
use Magento\Sales\Api\Data\OrderPaymentExtension;
use Magento\Sales\Model\Order\Payment;
use Magento\Vault\Model\PaymentToken;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
* Tests VaultCaptureDataBuilder.
*/
class VaultCaptureDataBuilderTest extends \PHPUnit\Framework\TestCase
{
/**
Expand All @@ -30,7 +34,15 @@ class VaultCaptureDataBuilderTest extends \PHPUnit\Framework\TestCase
*/
private $payment;

public function setUp()
/**
* @var SubjectReader|MockObject
*/
private $subjectReader;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->paymentDO = $this->createMock(PaymentDataObjectInterface::class);
$this->payment = $this->getMockBuilder(Payment::class)
Expand All @@ -39,31 +51,44 @@ public function setUp()
$this->paymentDO->method('getPayment')
->willReturn($this->payment);

$this->builder = new VaultCaptureDataBuilder(new SubjectReader());
$this->subjectReader = $this->getMockBuilder(SubjectReader::class)
->disableOriginalConstructor()
->getMock();

$this->builder = new VaultCaptureDataBuilder($this->subjectReader);
}

/**
* \Magento\Braintree\Gateway\Request\VaultCaptureDataBuilder::build
* Checks the result after builder execution.
*/
public function testBuild()
{
$amount = 30.00;
$token = '5tfm4c';
$buildSubject = [
'payment' => $this->paymentDO,
'amount' => $amount
'amount' => $amount,
];

$expected = [
'amount' => $amount,
'paymentMethodToken' => $token
'paymentMethodToken' => $token,
];

$this->subjectReader->method('readPayment')
->with($buildSubject)
->willReturn($this->paymentDO);
$this->subjectReader->method('readAmount')
->with($buildSubject)
->willReturn($amount);

/** @var OrderPaymentExtension|MockObject $paymentExtension */
$paymentExtension = $this->getMockBuilder(OrderPaymentExtension::class)
->setMethods(['getVaultPaymentToken'])
->disableOriginalConstructor()
->getMockForAbstractClass();

/** @var PaymentToken|MockObject $paymentToken */
$paymentToken = $this->getMockBuilder(PaymentToken::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -79,4 +104,39 @@ public function testBuild()
$result = $this->builder->build($buildSubject);
self::assertEquals($expected, $result);
}

/**
* Checks a builder execution if Payment Token doesn't exist.
*
* @expectedException \Magento\Payment\Gateway\Command\CommandException
* @expectedExceptionMessage The Payment Token is not available to perform the request.
*/
public function testBuildWithoutPaymentToken(): void
{
$amount = 30.00;
$buildSubject = [
'payment' => $this->paymentDO,
'amount' => $amount,
];

$this->subjectReader->method('readPayment')
->with($buildSubject)
->willReturn($this->paymentDO);
$this->subjectReader->method('readAmount')
->with($buildSubject)
->willReturn($amount);

/** @var OrderPaymentExtension|MockObject $paymentExtension */
$paymentExtension = $this->getMockBuilder(OrderPaymentExtension::class)
->setMethods(['getVaultPaymentToken'])
->disableOriginalConstructor()
->getMockForAbstractClass();

$this->payment->method('getExtensionAttributes')
->willReturn($paymentExtension);
$paymentExtension->method('getVaultPaymentToken')
->willReturn(null);

$this->builder->build($buildSubject);
}
}
1 change: 1 addition & 0 deletions app/code/Magento/Braintree/i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,4 @@ Currency,Currency
"Too many concurrent attempts to refund this transaction. Try again later.","Too many concurrent attempts to refund this transaction. Try again later."
"Too many concurrent attempts to void this transaction. Try again later.","Too many concurrent attempts to void this transaction. Try again later."
"Braintree Settlement","Braintree Settlement"
"The Payment Token is not available to perform the request.","The Payment Token is not available to perform the request."
51 changes: 37 additions & 14 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1574,8 +1574,14 @@ protected function _saveProducts()
$rowScope = $this->getRowScope($rowData);

$urlKey = $this->getUrlKey($rowData);
if (!empty($urlKey)) {
if (!empty($rowData[self::URL_KEY])) {
// If url_key column and its value were in the CSV file
$rowData[self::URL_KEY] = $urlKey;
} else if ($this->isNeedToChangeUrlKey($rowData)) {
// If url_key column was empty or even not declared in the CSV file but by the rules it is need to
// be setteed. In case when url_key is generating from name column we have to ensure that the bunch
// of products will pass for the event with url_key column.
$bunch[$rowNum][self::URL_KEY] = $rowData[self::URL_KEY] = $urlKey;
}

$rowSku = $rowData[self::COL_SKU];
Expand Down Expand Up @@ -2475,17 +2481,17 @@ public function validateRow(array $rowData, $rowNum)
}

/**
* Check if need to validate url key.
*
* @param array $rowData
* @return bool
*/
private function isNeedToValidateUrlKey($rowData)
{
$urlKey = $this->getUrlKey($rowData);

return (!empty($urlKey))
return (!empty($rowData[self::URL_KEY]) || !empty($rowData[self::COL_NAME]))
&& (empty($rowData[self::COL_VISIBILITY])
|| $rowData[self::COL_VISIBILITY]
!== (string)Visibility::getOptionArray()[Visibility::VISIBILITY_NOT_VISIBLE]);
|| $rowData[self::COL_VISIBILITY]
!== (string)Visibility::getOptionArray()[Visibility::VISIBILITY_NOT_VISIBLE]);
}

/**
Expand Down Expand Up @@ -2785,23 +2791,20 @@ protected function getProductUrlSuffix($storeId = null)
}

/**
* Retrieve url key from provided row data.
*
* @param array $rowData
* @return string
*
* @since 100.0.3
*/
protected function getUrlKey($rowData)
{
if (!empty($rowData[self::URL_KEY])) {
return $this->productUrl->formatUrlKey($rowData[self::URL_KEY]);
}

/**
* If the product exists, assume it already has a URL Key and even
* if a name is provided in the import data, it should not be used
* to overwrite that existing URL Key the product already has.
*/
$isSkuExist = $this->isSkuExist($rowData[self::COL_SKU]);
if (!$isSkuExist && !empty($rowData[self::COL_NAME])) {

if (!empty($rowData[self::COL_NAME])) {
return $this->productUrl->formatUrlKey($rowData[self::COL_NAME]);
}

Expand All @@ -2820,6 +2823,26 @@ protected function getResource()
return $this->_resource;
}

/**
* Whether a url key is needed to be change.
*
* @param array $rowData
* @return bool
*/
private function isNeedToChangeUrlKey(array $rowData): bool
{
$urlKey = $this->getUrlKey($rowData);
$productExists = $this->isSkuExist($rowData[self::COL_SKU]);
$markedToEraseUrlKey = isset($rowData[self::URL_KEY]);
// The product isn't new and the url key index wasn't marked for change.
if (!$urlKey && $productExists && !$markedToEraseUrlKey) {
// Seems there is no need to change the url key
return false;
}

return true;
}

/**
* Get product entity link field
*
Expand Down
17 changes: 15 additions & 2 deletions app/code/Magento/Sales/Model/AdminOrder/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Magento\Quote\Model\Quote\Item;
use Magento\Sales\Api\Data\OrderAddressInterface;
use Magento\Sales\Model\Order;
use Magento\Store\Model\StoreManagerInterface;

/**
* Order create model
Expand Down Expand Up @@ -243,6 +244,11 @@ class Create extends \Magento\Framework\DataObject implements \Magento\Checkout\
*/
private $dataObjectConverter;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param \Magento\Framework\ObjectManagerInterface $objectManager
* @param \Magento\Framework\Event\ManagerInterface $eventManager
Expand Down Expand Up @@ -274,6 +280,7 @@ class Create extends \Magento\Framework\DataObject implements \Magento\Checkout\
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @param ExtensibleDataObjectConverter|null $dataObjectConverter
* @param StoreManagerInterface $storeManager
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand Down Expand Up @@ -306,7 +313,8 @@ public function __construct(
\Magento\Quote\Model\QuoteFactory $quoteFactory,
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null,
ExtensibleDataObjectConverter $dataObjectConverter = null
ExtensibleDataObjectConverter $dataObjectConverter = null,
StoreManagerInterface $storeManager = null
) {
$this->_objectManager = $objectManager;
$this->_eventManager = $eventManager;
Expand Down Expand Up @@ -340,6 +348,7 @@ public function __construct(
parent::__construct($data);
$this->dataObjectConverter = $dataObjectConverter ?: ObjectManager::getInstance()
->get(ExtensibleDataObjectConverter::class);
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
}

/**
Expand Down Expand Up @@ -417,7 +426,8 @@ public function setRecollect($flag)

/**
* Recollect totals for customer cart.
* Set recollect totals flag for quote
*
* Set recollect totals flag for quote.
*
* @return $this
*/
Expand Down Expand Up @@ -1327,6 +1337,7 @@ protected function _createCustomerForm(\Magento\Customer\Api\Data\CustomerInterf

/**
* Set and validate Quote address
*
* All errors added to _errors
*
* @param \Magento\Quote\Model\Quote\Address $address
Expand Down Expand Up @@ -1530,6 +1541,8 @@ public function resetShippingMethod()
*/
public function collectShippingRates()
{
$store = $this->getQuote()->getStore();
$this->storeManager->setCurrentStore($store);
$this->getQuote()->getShippingAddress()->setCollectShippingRates(true);
$this->collectRates();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,19 @@ public function __construct(
) {
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
}

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

$tableDescription = $this->getConnection()->describeTable($this->getMainTable());
foreach ($tableDescription as $columnInfo) {
$this->addFilterToMap($columnInfo['COLUMN_NAME'], 'main_table.' . $columnInfo['COLUMN_NAME']);
}

return $this;
}
}
Loading

0 comments on commit e9c1946

Please sign in to comment.