Skip to content

Commit 1be4916

Browse files
committed
Merge remote-tracking branch 'mpi/MAGETWO-53312' into MPI-PR-3005
2 parents 7360b89 + 8bd4e74 commit 1be4916

File tree

10 files changed

+196
-34
lines changed

10 files changed

+196
-34
lines changed

app/code/Magento/CheckoutAgreements/Model/Checkout/Plugin/Validation.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function beforeSavePaymentInformationAndPlaceOrder(
5858
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
5959
) {
6060
if ($this->isAgreementEnabled()) {
61-
$this->validateAgreements($paymentMethod->getExtensionAttributes()->getAgreementIds());
61+
$this->validateAgreements($paymentMethod);
6262
}
6363
}
6464

@@ -77,17 +77,21 @@ public function beforeSavePaymentInformation(
7777
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
7878
) {
7979
if ($this->isAgreementEnabled()) {
80-
$this->validateAgreements($paymentMethod->getExtensionAttributes()->getAgreementIds());
80+
$this->validateAgreements($paymentMethod);
8181
}
8282
}
8383

8484
/**
85-
* @param int[] $agreements
85+
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
8686
* @throws \Magento\Framework\Exception\CouldNotSaveException
8787
* @return void
8888
*/
89-
protected function validateAgreements($agreements)
89+
protected function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod)
9090
{
91+
$agreements = $paymentMethod->getExtensionAttributes() === null
92+
? []
93+
: $paymentMethod->getExtensionAttributes()->getAgreementIds();
94+
9195
if (!$this->agreementsValidator->isValid($agreements)) {
9296
throw new \Magento\Framework\Exception\CouldNotSaveException(
9397
__('Please agree to all the terms and conditions before placing the order.')

app/code/Magento/CheckoutAgreements/Test/Unit/Model/Checkout/Plugin/ValidationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
8585
$this->repositoryMock->expects($this->once())->method('getList')->willReturn([1]);
8686
$this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements);
8787
$this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(true);
88-
$this->paymentMock->expects($this->once())
88+
$this->paymentMock->expects(static::atLeastOnce())
8989
->method('getExtensionAttributes')
9090
->willReturn($this->extensionAttributesMock);
9191
$this->model->beforeSavePaymentInformation($this->subjectMock, $cartId, $this->paymentMock, $this->addressMock);
@@ -107,7 +107,7 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
107107
$this->repositoryMock->expects($this->once())->method('getList')->willReturn([1]);
108108
$this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements);
109109
$this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(false);
110-
$this->paymentMock->expects($this->once())
110+
$this->paymentMock->expects(static::atLeastOnce())
111111
->method('getExtensionAttributes')
112112
->willReturn($this->extensionAttributesMock);
113113
$this->model->beforeSavePaymentInformation($this->subjectMock, $cartId, $this->paymentMock, $this->addressMock);
@@ -125,7 +125,7 @@ public function testBeforeSavePaymentInformation()
125125
$this->repositoryMock->expects($this->once())->method('getList')->willReturn([1]);
126126
$this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements);
127127
$this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(true);
128-
$this->paymentMock->expects($this->once())
128+
$this->paymentMock->expects(static::atLeastOnce())
129129
->method('getExtensionAttributes')
130130
->willReturn($this->extensionAttributesMock);
131131
$this->model->beforeSavePaymentInformation($this->subjectMock, $cartId, $this->paymentMock, $this->addressMock);

app/code/Magento/CheckoutAgreements/view/frontend/requirejs-config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ var config = {
99
mixins: {
1010
'Magento_Checkout/js/action/place-order': {
1111
'Magento_CheckoutAgreements/js/model/place-order-mixin': true
12+
},
13+
'Magento_Checkout/js/action/set-payment-information': {
14+
'Magento_CheckoutAgreements/js/model/set-payment-information-mixin': true
1215
}
1316
}
1417
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright © 2016 Magento. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
/*jshint browser:true jquery:true*/
7+
/*global alert*/
8+
define([
9+
'jquery'
10+
], function ($) {
11+
'use strict';
12+
13+
var agreementsConfig = window.checkoutConfig.checkoutAgreements;
14+
15+
/** Override default place order action and add agreement_ids to request */
16+
return function (paymentData) {
17+
var agreementForm,
18+
agreementData,
19+
agreementIds;
20+
21+
if (!agreementsConfig.isEnabled) {
22+
return;
23+
}
24+
25+
agreementForm = $('.payment-method._active div[data-role=checkout-agreements] input');
26+
agreementData = agreementForm.serializeArray();
27+
agreementIds = [];
28+
29+
agreementData.forEach(function (item) {
30+
agreementIds.push(item.value);
31+
});
32+
33+
if (paymentData['extension_attributes'] === undefined) {
34+
paymentData['extension_attributes'] = {};
35+
}
36+
37+
paymentData['extension_attributes']['agreement_ids'] = agreementIds;
38+
};
39+
});

app/code/Magento/CheckoutAgreements/view/frontend/web/js/model/place-order-mixin.js

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,16 @@
66
/*global alert*/
77
define([
88
'jquery',
9-
'mage/utils/wrapper'
10-
], function ($, wrapper) {
9+
'mage/utils/wrapper',
10+
'Magento_CheckoutAgreements/js/model/agreements-assigner'
11+
], function ($, wrapper, agreementsAssigner) {
1112
'use strict';
1213

13-
var agreementsConfig = window.checkoutConfig.checkoutAgreements;
14-
1514
return function (placeOrderAction) {
1615

1716
/** Override default place order action and add agreement_ids to request */
1817
return wrapper.wrap(placeOrderAction, function (originalAction, paymentData, messageContainer) {
19-
var agreementForm,
20-
agreementData,
21-
agreementIds;
22-
23-
if (!agreementsConfig.isEnabled) {
24-
return originalAction(paymentData, messageContainer);
25-
}
26-
27-
agreementForm = $('.payment-method._active form[data-role=checkout-agreements]');
28-
agreementData = agreementForm.serializeArray();
29-
agreementIds = [];
30-
31-
agreementData.forEach(function (item) {
32-
agreementIds.push(item.value);
33-
});
34-
35-
paymentData.extension_attributes = {
36-
agreement_ids: agreementIds
37-
};
18+
agreementsAssigner(paymentData);
3819

3920
return originalAction(paymentData, messageContainer);
4021
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright © 2016 Magento. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
/*jshint browser:true jquery:true*/
6+
/*global alert*/
7+
define([
8+
'jquery',
9+
'mage/utils/wrapper',
10+
'Magento_CheckoutAgreements/js/model/agreements-assigner'
11+
], function ($, wrapper, agreementsAssigner) {
12+
'use strict';
13+
14+
return function (placeOrderAction) {
15+
16+
/** Override place-order-mixin for set-payment-information action as they differs only by method signature */
17+
return wrapper.wrap(placeOrderAction, function (originalAction, messageContainer, paymentData) {
18+
agreementsAssigner(paymentData);
19+
20+
return originalAction(messageContainer, paymentData);
21+
});
22+
};
23+
});

app/code/Magento/Vault/view/frontend/web/template/payment/form.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232

3333
<div class="payment-method-content">
3434
<each args="getRegion('messages')" render=""></each>
35-
35+
<div class="checkout-agreements-block">
36+
<!-- ko foreach: $parent.getRegion('before-place-order') -->
37+
<!-- ko template: getTemplate() --><!-- /ko -->
38+
<!--/ko-->
39+
</div>
3640
<div class="actions-toolbar">
3741
<div class="primary">
3842
<button class="action primary checkout"

lib/internal/Magento/Framework/Model/ResourceModel/AbstractResource.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public function commit()
8282
call_user_func($callback);
8383
}
8484
} catch (\Exception $e) {
85-
echo $e;
8685
throw $e;
8786
}
8887
}

lib/internal/Magento/Framework/Model/Test/Unit/ResourceModel/AbstractResourceStub.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@
66
namespace Magento\Framework\Model\Test\Unit\ResourceModel;
77

88
use Magento\Framework\DataObject;
9+
use Magento\Framework\DB\Adapter\AdapterInterface;
910
use Magento\Framework\Model\ResourceModel\AbstractResource;
1011

1112
class AbstractResourceStub extends AbstractResource
1213
{
14+
/**
15+
* @var AdapterInterface
16+
*/
17+
private $connectionAdapter;
18+
1319
/**
1420
* Resource initialization
1521
*
@@ -23,11 +29,21 @@ protected function _construct()
2329
/**
2430
* Get connection
2531
*
26-
* @return \Magento\Framework\DB\Adapter\AdapterInterface
32+
* @return AdapterInterface
2733
*/
2834
protected function getConnection()
2935
{
30-
return null;
36+
return $this->connectionAdapter;
37+
}
38+
39+
/**
40+
* @param AdapterInterface $adapter
41+
*
42+
* @return void
43+
*/
44+
public function setConnection(AdapterInterface $adapter)
45+
{
46+
$this->connectionAdapter = $adapter;
3147
}
3248

3349
/**

lib/internal/Magento/Framework/Model/Test/Unit/ResourceModel/AbstractResourceTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Framework\Model\Test\Unit\ResourceModel;
77

88
use Magento\Framework\DataObject;
9+
use Magento\Framework\DB\Adapter\AdapterInterface;
910

1011
class AbstractResourceTest extends \PHPUnit_Framework_TestCase
1112
{
@@ -105,4 +106,96 @@ public function unserializableFieldsDataProvider()
105106
[[$dataObject, 'serialized_boolean_false', null], false]
106107
];
107108
}
109+
110+
public function testCommitZeroLevel()
111+
{
112+
/** @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject $connection */
113+
$connection = $this->getMock(AdapterInterface::class);
114+
/** @var DataObject|\PHPUnit_Framework_MockObject_MockObject $closureExpectation */
115+
$closureExpectation = $this->getMockBuilder(DataObject::class)
116+
->disableOriginalConstructor()
117+
->getMock();
118+
119+
$abstractResource = new AbstractResourceStub();
120+
$abstractResource->setConnection($connection);
121+
$abstractResource->addCommitCallback(
122+
function () use ($closureExpectation) {
123+
$closureExpectation->setData(1);
124+
}
125+
);
126+
127+
$abstractResource->addCommitCallback(
128+
function () use ($closureExpectation) {
129+
$closureExpectation->getData();
130+
}
131+
);
132+
133+
$connection->expects(static::once())
134+
->method('commit');
135+
$connection->expects(static::once())
136+
->method('getTransactionLevel')
137+
->willReturn(0);
138+
$closureExpectation->expects(static::once())
139+
->method('setData')
140+
->with(1);
141+
$closureExpectation->expects(static::once())
142+
->method('getData');
143+
144+
$abstractResource->commit();
145+
}
146+
147+
/**
148+
* @expectedException \Exception
149+
*/
150+
public function testCommitZeroLevelCallbackException()
151+
{
152+
/** @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject $connection */
153+
$connection = $this->getMock(AdapterInterface::class);
154+
155+
$abstractResource = new AbstractResourceStub();
156+
$abstractResource->setConnection($connection);
157+
$abstractResource->addCommitCallback(
158+
function () {
159+
throw new \Exception();
160+
}
161+
);
162+
163+
$connection->expects(static::once())
164+
->method('commit');
165+
$connection->expects(static::once())
166+
->method('getTransactionLevel')
167+
->willReturn(0);
168+
169+
$abstractResource->commit();
170+
}
171+
172+
public function testCommitNotCompletedTransaction()
173+
{
174+
/** @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject $connection */
175+
$connection = $this->getMock(AdapterInterface::class);
176+
/** @var DataObject|\PHPUnit_Framework_MockObject_MockObject $closureExpectation */
177+
$closureExpectation = $this->getMockBuilder(DataObject::class)
178+
->disableOriginalConstructor()
179+
->getMock();
180+
181+
$abstractResource = new AbstractResourceStub();
182+
$abstractResource->setConnection($connection);
183+
$abstractResource->addCommitCallback(
184+
function () use ($closureExpectation) {
185+
$closureExpectation->setData(1);
186+
}
187+
);
188+
189+
$connection->expects(static::once())
190+
->method('commit');
191+
$connection->expects(static::once())
192+
->method('getTransactionLevel')
193+
->willReturn(1);
194+
195+
$closureExpectation->expects(static::never())
196+
->method('setData')
197+
->with(1);
198+
199+
$abstractResource->commit();
200+
}
108201
}

0 commit comments

Comments
 (0)