QuoteValidatorTest.php 7.3 KB

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