1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Quote\Model;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Model\Quote\Address\Rate;
use Magento\TestFramework\Helper\Bootstrap;
/**
* Class QuoteValidatorTest.
*
* @magentoDbIsolation enabled
*/
class QuoteValidatorTest extends \PHPUnit\Framework\TestCase
{
/**
* @var QuoteValidator
*/
private $quoteValidator;
/**
* @inheritdoc
*/
public function setUp()
{
$this->quoteValidator = Bootstrap::getObjectManager()->create(QuoteValidator::class);
}
/**
* @expectedException \Magento\Framework\Exception\LocalizedException
* @expectedExceptionMessage Please check the shipping address information.
*/
public function testValidateBeforeSubmitShippingAddressInvalid()
{
$quote = $this->getQuote();
$quote->getShippingAddress()->setPostcode('');
$this->quoteValidator->validateBeforeSubmit($quote);
}
/**
* @expectedException \Magento\Framework\Exception\LocalizedException
* @expectedExceptionMessage Some addresses can't be used due to the configurations for specific countries.
*/
public function testValidateBeforeSubmitCountryIsNotAllowed()
{
/** @magentoConfigFixture does not allow to change the value for the website scope */
Bootstrap::getObjectManager()->get(
\Magento\Framework\App\Config\MutableScopeConfigInterface::class
)->setValue(
'general/country/allow',
'US',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
$quote = $this->getQuote();
$quote->getShippingAddress()->setCountryId('AF');
$this->quoteValidator->validateBeforeSubmit($quote);
}
/**
* @expectedException \Magento\Framework\Exception\LocalizedException
* @expectedExceptionMessage The shipping method is missing. Select the shipping method and try again.
*/
public function testValidateBeforeSubmitShippingMethodInvalid()
{
$quote = $this->getQuote();
$quote->getShippingAddress()->setShippingMethod('NONE');
$this->quoteValidator->validateBeforeSubmit($quote);
}
/**
* @expectedException \Magento\Framework\Exception\LocalizedException
* @expectedExceptionMessage Please check the billing address information.
*/
public function testValidateBeforeSubmitBillingAddressInvalid()
{
$quote = $this->getQuote();
$quote->getBillingAddress()->setTelephone('');
$this->quoteValidator->validateBeforeSubmit($quote);
}
/**
* @expectedException \Magento\Framework\Exception\LocalizedException
* @expectedExceptionMessage Enter a valid payment method and try again.
*/
public function testValidateBeforeSubmitPaymentMethodInvalid()
{
$quote = $this->getQuote();
$quote->getPayment()->setMethod('');
$this->quoteValidator->validateBeforeSubmit($quote);
}
/**
* @expectedException \Magento\Framework\Exception\LocalizedException
* @magentoConfigFixture current_store sales/minimum_order/active 1
* @magentoConfigFixture current_store sales/minimum_order/amount 100
*/
public function testValidateBeforeSubmitMinimumAmountInvalid()
{
$quote = $this->getQuote();
$quote->getShippingAddress()
->setBaseSubtotal(0);
$this->quoteValidator->validateBeforeSubmit($quote);
}
/**
* @return void
*/
public function testValidateBeforeSubmitWithoutMinimumOrderAmount()
{
$this->quoteValidator->validateBeforeSubmit($this->getQuote());
}
/**
* @magentoConfigFixture current_store sales/minimum_order/active 1
* @magentoConfigFixture current_store sales/minimum_order/amount 100
*/
public function testValidateBeforeSubmitWithMinimumOrderAmount()
{
$quote = $this->getQuote();
$quote->getShippingAddress()
->setBaseSubtotal(200);
$this->quoteValidator->validateBeforeSubmit($quote);
}
/**
* Checks a case when the default website has country restrictions and the quote created
* for the another website with different country restrictions.
*
* @magentoDataFixture Magento/Quote/Fixtures/quote_sec_website.php
* @magentoDbIsolation disabled
*/
public function testValidateBeforeSubmit()
{
$quote = $this->getQuoteById('0000032134');
$this->quoteValidator->validateBeforeSubmit($quote);
}
/**
* @return Quote
*/
private function getQuote(): Quote
{
/** @var Quote $quote */
$quote = Bootstrap::getObjectManager()->create(Quote::class);
/** @var AddressInterface $billingAddress */
$billingAddress = Bootstrap::getObjectManager()->create(AddressInterface::class);
$billingAddress->setFirstname('Joe')
->setLastname('Doe')
->setCountryId('US')
->setRegion('TX')
->setCity('Austin')
->setStreet('1000 West Parmer Line')
->setPostcode('11501')
->setTelephone('123456789');
$quote->setBillingAddress($billingAddress);
/** @var AddressInterface $shippingAddress */
$shippingAddress = Bootstrap::getObjectManager()->create(AddressInterface::class);
$shippingAddress->setFirstname('Joe')
->setLastname('Doe')
->setCountryId('US')
->setRegion('TX')
->setCity('Austin')
->setStreet('1000 West Parmer Line')
->setPostcode('11501')
->setTelephone('123456789');
$quote->setShippingAddress($shippingAddress);
$quote->getShippingAddress()
->setShippingMethod('flatrate_flatrate')
->setCollectShippingRates(true);
/** @var Rate $shippingRate */
$shippingRate = Bootstrap::getObjectManager()->create(Rate::class);
$shippingRate->setMethod('flatrate')
->setCarrier('flatrate')
->setPrice('5')
->setCarrierTitle('Flat Rate')
->setCode('flatrate_flatrate');
$quote->getShippingAddress()
->addShippingRate($shippingRate);
$quote->getPayment()->setMethod('CC');
/** @var QuoteRepository $quoteRepository */
$quoteRepository = Bootstrap::getObjectManager()->create(QuoteRepository::class);
$quoteRepository->save($quote);
return $quote;
}
/**
* Gets quote entity by reserved order id.
*
* @param string $reservedOrderId
* @return Quote
*/
private function getQuoteById(string $reservedOrderId): Quote
{
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
$searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
$searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
->create();
/** @var CartRepositoryInterface $repository */
$repository = Bootstrap::getObjectManager()->get(CartRepositoryInterface::class);
$items = $repository->getList($searchCriteria)
->getItems();
return array_pop($items);
}
}