123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?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);
- }
- }
|