Skip to content

Commit e6e8cfe

Browse files
candemiralpRokPopovpeterojo
authored
[ECP-8758] Update collection condition before removal of giftcard state data (#2368)
* [ECP-8758] Update collection condition before removal * [ECP-8758] Create unit test for StateData helper * [ECP-8758] Write unit tests * [ECP-8758] Update unit test --------- Co-authored-by: Rok Popov Ledinski <[email protected]> Co-authored-by: Peter Ojo <[email protected]>
1 parent e33d9c0 commit e6e8cfe

File tree

5 files changed

+235
-13
lines changed

5 files changed

+235
-13
lines changed

Helper/StateData.php

+20-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Adyen\Payment\Model\ResourceModel\StateData\Collection as StateDataCollection;
1919
use Adyen\Payment\Model\StateDataFactory;
2020
use Magento\Framework\Exception\LocalizedException;
21+
use Magento\Framework\Exception\NoSuchEntityException;
2122

2223
class StateData
2324
{
@@ -97,22 +98,31 @@ public function saveStateData(string $stateData, int $quoteId): void
9798
$this->stateDataResourceModel->save($stateDataObj);
9899
}
99100

101+
/**
102+
* @throws NoSuchEntityException
103+
*/
100104
public function removeStateData(int $stateDataId, ?int $quoteId = null): bool
101105
{
102-
$stateDataObj = $this->stateDataFactory->create();
103-
$stateDataObj->setEntityId($stateDataId);
106+
$stateDataCollection = $this->stateDataCollection->addFieldToFilter('entity_id', $stateDataId);
104107

105108
if (isset($quoteId)) {
106-
$stateDataObj->setQuoteId($quoteId);
109+
$stateDataCollection->addFieldToFilter('quote_id', $quoteId);
107110
}
108111

109-
try {
110-
$this->stateDataResourceModel->delete($stateDataObj);
111-
} catch (\Exception $e) {
112-
$this->adyenLogger->error('An error occurred while deleting state data: ' . $e->getMessage());
113-
return false;
114-
}
112+
$stateDataCollection->getSelect();
113+
$stateDataObj = $stateDataCollection->getFirstItem();
114+
115+
if (empty($stateDataObj->getData())) {
116+
throw new NoSuchEntityException();
117+
} else {
118+
try {
119+
$this->stateDataResourceModel->delete($stateDataObj);
120+
} catch (\Exception $e) {
121+
$this->adyenLogger->error('An error occurred while deleting state data: ' . $e->getMessage());
122+
return false;
123+
}
115124

116-
return true;
125+
return true;
126+
}
117127
}
118128
}

Model/Api/GuestAdyenStateData.php

+28-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
use Adyen\Payment\Api\GuestAdyenStateDataInterface;
1616
use Adyen\Payment\Helper\StateData as StateDataHelper;
17+
use Magento\Framework\Exception\InputException;
18+
use Magento\Framework\Exception\LocalizedException;
19+
use Magento\Framework\Exception\NoSuchEntityException;
1720
use Magento\Quote\Model\QuoteIdMaskFactory;
1821

1922
class GuestAdyenStateData implements GuestAdyenStateDataInterface
@@ -29,19 +32,41 @@ public function __construct(
2932
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
3033
}
3134

35+
/**
36+
* @throws InputException
37+
* @throws LocalizedException
38+
*/
3239
public function save(string $stateData, string $cartId): void
3340
{
34-
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
35-
$quoteId = $quoteIdMask->getQuoteId();
41+
$quoteId = $this->getQuoteIdFromMaskedCartId($cartId);
3642

3743
$this->stateDataHelper->saveStateData($stateData, $quoteId);
3844
}
3945

46+
/**
47+
* @throws InputException
48+
* @throws NoSuchEntityException
49+
*/
4050
public function remove(int $stateDataId, string $cartId): bool
51+
{
52+
$quoteId = $this->getQuoteIdFromMaskedCartId($cartId);
53+
54+
return $this->stateDataHelper->removeStateData($stateDataId, $quoteId);
55+
}
56+
57+
/**
58+
* @throws InputException
59+
*/
60+
private function getQuoteIdFromMaskedCartId(string $cartId): int
4161
{
4262
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
4363
$quoteId = $quoteIdMask->getQuoteId();
4464

45-
return $this->stateDataHelper->removeStateData($stateDataId, $quoteId);
65+
if (is_null($quoteId)) {
66+
$errorMessage = __("An error occurred: missing required parameter :cartId!");
67+
throw new InputException($errorMessage);
68+
}
69+
70+
return $quoteId;
4671
}
4772
}

Test/Unit/Helper/StateDataTest.php

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
*
4+
* Adyen Payment module (https://www.adyen.com/)
5+
*
6+
* Copyright (c) 2023 Adyen N.V. (https://www.adyen.com/)
7+
* See LICENSE.txt for license details.
8+
*
9+
* Author: Adyen <[email protected]>
10+
*/
11+
12+
namespace Adyen\Payment\Test\Unit\Helper;
13+
14+
use Adyen\Payment\Helper\StateData;
15+
use Adyen\Payment\Model\ResourceModel\StateData as StateDataResourceModel;
16+
use Adyen\Payment\Model\ResourceModel\StateData\Collection as StateDataCollection;
17+
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
18+
use Magento\Framework\Exception\NoSuchEntityException;
19+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
20+
use Adyen\Payment\Model\StateDataFactory;
21+
22+
class StateDataTest extends AbstractAdyenTestCase
23+
{
24+
private $stateDataHelper;
25+
private $stateDataCollectionMock;
26+
private $stateDataFactoryMock;
27+
private $stateDataResourceModelMock;
28+
29+
protected function setUp(): void
30+
{
31+
$this->objectManager = new ObjectManager($this);
32+
$this->stateDataCollectionMock = $this->createMock(StateDataCollection::class);
33+
$this->stateDataResourceModelMock = $this->createMock(StateDataResourceModel::class);
34+
35+
$stateDataMock = $this->createMock(\Adyen\Payment\Model\StateData::class);
36+
37+
$this->stateDataFactoryMock = $this->createGeneratedMock(StateDataFactory::class, ['create']);
38+
$this->stateDataFactoryMock->method('create')->willReturn($stateDataMock);
39+
40+
$this->stateDataHelper = $this->objectManager->getObject(StateData::class, [
41+
'stateDataCollection' => $this->stateDataCollectionMock,
42+
'stateDataResourceModel' => $this->stateDataResourceModelMock,
43+
'stateDataFactory' => $this->stateDataFactoryMock
44+
]);
45+
}
46+
47+
public function testSaveStateDataSuccessful()
48+
{
49+
$stateData = '{"stateData":"dummyData"}';
50+
$quoteId = 1;
51+
52+
$stateDataMock = $this->createConfiguredMock(\Adyen\Payment\Model\StateData::class, [
53+
'getData' => ['entity_id' => 1, 'quote_id' => 1]
54+
]);
55+
56+
$this->stateDataCollectionMock->method('addFieldToFilter')->willReturnSelf();
57+
$this->stateDataCollectionMock->method('getFirstItem')->willReturn($stateDataMock);
58+
$this->stateDataResourceModelMock->expects($this->once())->method('save');
59+
60+
$this->stateDataHelper->saveStateData($stateData, $quoteId);
61+
}
62+
63+
public function testRemoveStateDataSuccessful()
64+
{
65+
$stateDataId = 1;
66+
$quoteId = 1;
67+
68+
$stateDataMock = $this->createConfiguredMock(\Adyen\Payment\Model\StateData::class, [
69+
'getData' => ['entity_id' => 1, 'quote_id' => 1]
70+
]);
71+
72+
$this->stateDataCollectionMock->method('addFieldToFilter')->willReturnSelf();
73+
$this->stateDataCollectionMock->method('getFirstItem')->willReturn($stateDataMock);
74+
75+
$this->assertTrue($this->stateDataHelper->removeStateData($stateDataId, $quoteId));
76+
}
77+
78+
public function testRemoveStateDataException()
79+
{
80+
$this->expectException(NoSuchEntityException::class);
81+
82+
$stateDataId = 1;
83+
$quoteId = 1;
84+
85+
$stateDataMock = $this->createConfiguredMock(\Adyen\Payment\Model\StateData::class, [
86+
'getData' => null
87+
]);
88+
89+
$this->stateDataCollectionMock->method('addFieldToFilter')->willReturnSelf();
90+
$this->stateDataCollectionMock->method('getFirstItem')->willReturn($stateDataMock);
91+
92+
$this->stateDataHelper->removeStateData($stateDataId, $quoteId);
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
*
4+
* Adyen Payment module (https://www.adyen.com/)
5+
*
6+
* Copyright (c) 2023 Adyen N.V. (https://www.adyen.com/)
7+
* See LICENSE.txt for license details.
8+
*
9+
* Author: Adyen <[email protected]>
10+
*/
11+
12+
namespace Adyen\Payment\Test\Unit\Model\Api;
13+
14+
use Adyen\Payment\Helper\StateData;
15+
use Adyen\Payment\Model\Api\GuestAdyenStateData;
16+
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
17+
use Magento\Framework\Exception\InputException;
18+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
19+
use Magento\Quote\Model\QuoteIdMask;
20+
use Magento\Quote\Model\QuoteIdMaskFactory;
21+
22+
class GuestAdyenStateDataTest extends AbstractAdyenTestCase
23+
{
24+
private $objectManager;
25+
private $stateDataHelperMock;
26+
private $quoteIdMaskFactoryMock;
27+
private $quoteIdMaskMock;
28+
private $guestAdyenStateDataModel;
29+
30+
protected function setUp(): void
31+
{
32+
$this->objectManager = new ObjectManager($this);
33+
34+
$this->quoteIdMaskFactoryMock = $this->createGeneratedMock(QuoteIdMaskFactory::class, [
35+
'create'
36+
]);
37+
$this->stateDataHelperMock = $this->createMock(StateData::class);
38+
39+
$this->quoteIdMaskMock = $this->createGeneratedMock(QuoteIdMask::class, ['load', 'getQuoteId']);
40+
$this->quoteIdMaskMock->method('load')->willReturn($this->quoteIdMaskMock);
41+
$this->quoteIdMaskMock->method('getQuoteId')->willReturn(1);
42+
43+
$this->guestAdyenStateDataModel = $this->objectManager->getObject(GuestAdyenStateData::class, [
44+
'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock,
45+
'stateDataHelper' => $this->stateDataHelperMock
46+
]);
47+
}
48+
49+
public function testSaveSuccessful()
50+
{
51+
$stateData = '{"stateData":"dummyData"}';
52+
$cartId = 'ABC123456789';
53+
54+
$this->quoteIdMaskFactoryMock->method('create')->willReturn($this->quoteIdMaskMock);
55+
$this->stateDataHelperMock->expects($this->once())->method('saveStateData');
56+
57+
$this->guestAdyenStateDataModel->save($stateData, $cartId);
58+
}
59+
60+
public function testRemoveSuccessful()
61+
{
62+
$stateDataId = 1;
63+
$cartId = 'ABC123456789';
64+
65+
$this->quoteIdMaskFactoryMock->method('create')->willReturn($this->quoteIdMaskMock);
66+
$this->stateDataHelperMock->expects($this->once())->method('removeStateData');
67+
68+
$this->guestAdyenStateDataModel->remove($stateDataId, $cartId);
69+
}
70+
71+
public function testSaveException()
72+
{
73+
$this->expectException(InputException::class);
74+
75+
$stateData = '{"stateData":"dummyData"}';
76+
$cartId = '';
77+
78+
$quoteIdMaskMock = $this->createGeneratedMock(QuoteIdMask::class, ['load', 'getQuoteId']);
79+
$quoteIdMaskMock->method('load')->willReturn($quoteIdMaskMock);
80+
$quoteIdMaskMock->method('getQuoteId')->willReturn(null);
81+
82+
$this->quoteIdMaskFactoryMock->method('create')->willReturn($quoteIdMaskMock);
83+
84+
$this->guestAdyenStateDataModel->save($stateData, $cartId);
85+
}
86+
}

view/frontend/web/js/view/payment/method-renderer/adyen-giftcard-method.js

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ define(
2020
'Adyen_Payment/js/model/adyen-configuration',
2121
'Adyen_Payment/js/adyen',
2222
'Magento_Customer/js/customer-data',
23+
'Magento_Checkout/js/model/error-processor',
2324
'mage/translate'
2425
],
2526
function(
@@ -34,6 +35,7 @@ define(
3435
adyenConfiguration,
3536
adyenCheckout,
3637
customerData,
38+
errorProcessor,
3739
$t
3840
) {
3941
'use strict';
@@ -192,6 +194,11 @@ define(
192194
adyenPaymentService.removeStateData(data.stateDataId).done(function () {
193195
self.fetchRedeemedGiftcards();
194196
fullScreenLoader.stopLoader();
197+
}).fail(function(response) {
198+
fullScreenLoader.stopLoader();
199+
self.fetchRedeemedGiftcards();
200+
201+
errorProcessor.process(response, this.currentMessageContainer);
195202
});
196203
},
197204

0 commit comments

Comments
 (0)