Skip to content

Commit

Permalink
Merge pull request magento#3484 from magento-tsg-csl3/2.2-develop-pr11
Browse files Browse the repository at this point in the history
[TSG-CSL3] For 2.2 (pr11)
  • Loading branch information
viktym authored Nov 27, 2018
2 parents ace297a + 2185923 commit f38e340
Show file tree
Hide file tree
Showing 10 changed files with 369 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
define([
'jquery',
'underscore',
'mage/utils/wrapper',
'Magento_Checkout/js/view/payment/default',
'Magento_Braintree/js/view/payment/adapter',
'Magento_Checkout/js/model/quote',
Expand All @@ -19,7 +18,6 @@ define([
], function (
$,
_,
wrapper,
Component,
Braintree,
quote,
Expand Down Expand Up @@ -105,6 +103,12 @@ define([
}
});

quote.shippingAddress.subscribe(function () {
if (self.isActive()) {
self.reInitPayPal();
}
});

// for each component initialization need update property
this.isReviewRequired(false);
this.initClientConfig();
Expand Down Expand Up @@ -222,9 +226,8 @@ define([

/**
* Re-init PayPal Auth Flow
* @param {Function} callback - Optional callback
*/
reInitPayPal: function (callback) {
reInitPayPal: function () {
if (Braintree.checkout) {
Braintree.checkout.teardown(function () {
Braintree.checkout = null;
Expand All @@ -235,17 +238,6 @@ define([
this.clientConfig.paypal.amount = this.grandTotalAmount;
this.clientConfig.paypal.shippingAddressOverride = this.getShippingAddress();

if (callback) {
this.clientConfig.onReady = wrapper.wrap(
this.clientConfig.onReady,
function (original, checkout) {
this.clientConfig.onReady = original;
original(checkout);
callback();
}.bind(this)
);
}

Braintree.setConfig(this.clientConfig);
Braintree.setup();
},
Expand Down Expand Up @@ -429,19 +421,17 @@ define([
* Triggers when customer click "Continue to PayPal" button
*/
payWithPayPal: function () {
this.reInitPayPal(function () {
if (!additionalValidators.validate()) {
return;
}
if (!additionalValidators.validate()) {
return;
}

try {
Braintree.checkout.paypal.initAuthFlow();
} catch (e) {
this.messageContainer.addErrorMessage({
message: $t('Payment ' + this.getTitle() + ' can\'t be initialized.')
});
}
}.bind(this));
try {
Braintree.checkout.paypal.initAuthFlow();
} catch (e) {
this.messageContainer.addErrorMessage({
message: $t('Payment ' + this.getTitle() + ' can\'t be initialized.')
});
}
},

/**
Expand Down
6 changes: 5 additions & 1 deletion app/code/Magento/Catalog/view/base/web/js/price-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ define([
pricesCode = [],
priceValue, origin, finalPrice;

this.cache.additionalPriceObject = this.cache.additionalPriceObject || {};
if (typeof newPrices !== 'undefined' && newPrices.hasOwnProperty('prices')) {
this.cache.additionalPriceObject = {};
} else {
this.cache.additionalPriceObject = this.cache.additionalPriceObject || {};
}

if (newPrices) {
$.extend(this.cache.additionalPriceObject, newPrices);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\Webapi\Rest\Request as RestRequest;
use Magento\Webapi\Controller\Rest\InputParamsResolver as InputParamsResolverController;

/**
* Plugin for InputParamsResolver
*
* Used to modify product data with save_rewrites_history flag
*/
class InputParamsResolver
{
/**
* @var RestRequest
*/
private $request;

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

/**
* Add 'save_rewrites_history' param to the product data
*
* @see \Magento\CatalogUrlRewrite\Plugin\Catalog\Controller\Adminhtml\Product\Initialization\Helper
* @param InputParamsResolverController $subject
* @param array $result
* @return array
*/
public function afterResolve(InputParamsResolverController $subject, array $result): array
{
$route = $subject->getRoute();
$serviceMethodName = $route->getServiceMethod();
$serviceClassName = $route->getServiceClass();
$requestBodyParams = $this->request->getBodyParams();

if ($this->isProductSaveCalled($serviceClassName, $serviceMethodName)
&& $this->isCustomAttributesExists($requestBodyParams)) {
foreach ($requestBodyParams['product']['custom_attributes'] as $attribute) {
if ($attribute['attribute_code'] === 'save_rewrites_history') {
foreach ($result as $resultItem) {
if ($resultItem instanceof Product) {
$resultItem->setData('save_rewrites_history', (bool)$attribute['value']);
break 2;
}
}
break;
}
}
}
return $result;
}

/**
* Check that product save method called
*
* @param string $serviceClassName
* @param string $serviceMethodName
* @return bool
*/
private function isProductSaveCalled(string $serviceClassName, string $serviceMethodName): bool
{
return $serviceClassName === ProductRepositoryInterface::class && $serviceMethodName === 'save';
}

/**
* Check is any custom options exists in product data
*
* @param array $requestBodyParams
* @return bool
*/
private function isCustomAttributesExists(array $requestBodyParams): bool
{
return !empty($requestBodyParams['product']['custom_attributes']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\CatalogUrlRewrite\Test\Unit\Plugin\Webapi\Controller\Rest;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Webapi\Controller\Rest\InputParamsResolver;
use Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest\InputParamsResolver as InputParamsResolverPlugin;
use Magento\Framework\Webapi\Rest\Request as RestRequest;
use Magento\Catalog\Model\Product;
use Magento\Webapi\Controller\Rest\Router\Route;
use Magento\Catalog\Api\ProductRepositoryInterface;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
* Unit test for InputParamsResolver plugin
*/
class InputParamsResolverTest extends \PHPUnit\Framework\TestCase
{
/**
* @var string
*/
private $saveRewritesHistory;

/**
* @var array
*/
private $requestBodyParams;

/**
* @var array
*/
private $result;

/**
* @var ObjectManager
*/
private $objectManager;

/**
* @var InputParamsResolver|MockObject
*/
private $subject;

/**
* @var RestRequest|MockObject
*/
private $request;

/**
* @var Product|MockObject
*/
private $product;

/**
* @var Route|MockObject
*/
private $route;

/**
* @var InputParamsResolverPlugin
*/
private $plugin;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->saveRewritesHistory = 'save_rewrites_history';
$this->requestBodyParams = [
'product' => [
'sku' => 'test',
'custom_attributes' => [
[
'attribute_code' => $this->saveRewritesHistory,
'value' => 1
]
]
]
];

$this->route = $this->createPartialMock(Route::class, ['getServiceMethod', 'getServiceClass']);
$this->request = $this->createPartialMock(RestRequest::class, ['getBodyParams']);
$this->request->method('getBodyParams')
->willReturn($this->requestBodyParams);
$this->subject = $this->createPartialMock(InputParamsResolver::class, ['getRoute']);
$this->subject->method('getRoute')
->willReturn($this->route);
$this->product = $this->createPartialMock(Product::class, ['setData']);

$this->result = [false, $this->product, 'test'];

$this->objectManager = new ObjectManager($this);
$this->plugin = $this->objectManager->getObject(
InputParamsResolverPlugin::class,
[
'request' => $this->request
]
);
}

public function testAfterResolve()
{
$this->route->method('getServiceClass')
->willReturn(ProductRepositoryInterface::class);
$this->route->method('getServiceMethod')
->willReturn('save');
$this->product->expects($this->once())
->method('setData')
->with($this->saveRewritesHistory, true);

$this->plugin->afterResolve($this->subject, $this->result);
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/CatalogUrlRewrite/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"magento/framework": "101.0.*",
"magento/module-ui": "101.0.*"
},
"suggest": {
"magento/module-webapi": "*"
},
"type": "magento2-module",
"version": "100.2.5",
"license": [
Expand Down
3 changes: 3 additions & 0 deletions app/code/Magento/CatalogUrlRewrite/etc/webapi_rest/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator" type="Magento\CatalogUrlRewrite\Model\WebapiProductUrlPathGenerator"/>
<type name="Magento\Webapi\Controller\Rest\InputParamsResolver">
<plugin name="product_save_rewrites_history_rest_plugin" type="Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest\InputParamsResolver" sortOrder="1" disabled="false" />
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ define([
var checkoutConfig = window.checkoutConfig,
validators = [],
observedElements = [],
postcodeElement = null,
postcodeElements = [],
postcodeElementName = 'postcode';

validators.push(defaultValidator);
Expand Down Expand Up @@ -101,7 +101,7 @@ define([

if (element.index === postcodeElementName) {
this.bindHandler(element, delay);
postcodeElement = element;
postcodeElements.push(element);
}
},

Expand Down Expand Up @@ -136,7 +136,13 @@ define([
if (!formPopUpState.isVisible()) {
clearTimeout(self.validateAddressTimeout);
self.validateAddressTimeout = setTimeout(function () {
self.postcodeValidation();
if (element.index === postcodeElementName) {
self.postcodeValidation(element);
} else {
$.each(postcodeElements, function (index, elem) {
self.postcodeValidation(elem);
});
}
self.validateFields();
}, delay);
}
Expand All @@ -148,8 +154,8 @@ define([
/**
* @return {*}
*/
postcodeValidation: function () {
var countryId = $('select[name="country_id"]').val(),
postcodeValidation: function (postcodeElement) {
var countryId = $('select[name="country_id"]:visible').val(),
validationResult,
warnMessage;

Expand Down Expand Up @@ -178,8 +184,8 @@ define([
*/
validateFields: function () {
var addressFlat = addressConverter.formDataProviderToFlatData(
this.collectObservedData(),
'shippingAddress'
this.collectObservedData(),
'shippingAddress'
),
address;

Expand Down
Loading

0 comments on commit f38e340

Please sign in to comment.