Skip to content

Commit

Permalink
🔃 [Magento Community Engineering] Community Contributions - GraphQL
Browse files Browse the repository at this point in the history
Accepted Community Pull Requests:
 - magento/graphql-ce#873: graphQl-509: `save_in_address_book` has no impact on Address Book (by @kisroman)
 - magento/graphql-ce#991: graphQl-987: [Test coverage] Cover exceptions in Magento\QuoteGraphQl� (by @kisroman)
 - magento/graphql-ce#995: magento/graphql-ce#985: Remove unnecessary exceptions (by @atwixfirster)
 - magento/graphql-ce#997: magento/graphql-ce#975: [Test coverage] Cover CartAddressTypeResolver (by @atwixfirster)
 - magento/graphql-ce#990: Extend test coverage for CustomerDownloadableGraphQl (by @TomashKhamlai)
 - magento/graphql-ce#983: #981 Extend test coverage for BraintreeGraphQl (by @TomashKhamlai)
 - magento/graphql-ce#973: GraphQl-972: added support of the Global scope in the config fixture (by @VitaliyBoyko)


Fixed GitHub Issues:
 - #975: [Question] Have traits been considered for the Interceptor classes? (reported by @fooman) has been fixed in magento/graphql-ce#997 by @atwixfirster in 2.3-develop branch
   Related commits:
     1. 493d744

 - #989: make build artifacts accessable (reported by @Flyingmana) has been fixed in magento/graphql-ce#990 by @TomashKhamlai in 2.3-develop branch
   Related commits:
     1. bccd287

 - #972: Pub/Static directory is empty (reported by @mrugeshrocks) has been fixed in magento/graphql-ce#973 by @VitaliyBoyko in 2.3-develop branch
   Related commits:
     1. 3489da5
     2. 63bd232
     3. b64a485
  • Loading branch information
magento-engcom-team authored Oct 13, 2019
2 parents c60cf10 + 44cc3c5 commit 14d6388
Show file tree
Hide file tree
Showing 22 changed files with 839 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@

use Magento\Customer\Model\AuthenticationInterface;
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\State\UserLockedException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;

/**
* Check customer password
Expand All @@ -41,8 +37,6 @@ public function __construct(
* @param string $password
* @param int $customerId
* @throws GraphQlAuthenticationException
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function execute(string $password, int $customerId)
{
Expand All @@ -52,10 +46,6 @@ public function execute(string $password, int $customerId)
throw new GraphQlAuthenticationException(__($e->getMessage()), $e);
} catch (UserLockedException $e) {
throw new GraphQlAuthenticationException(__($e->getMessage()), $e);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart\Address;

use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Customer\Api\Data\AddressInterfaceFactory;
use Magento\Customer\Api\Data\RegionInterface;
use Magento\Customer\Api\Data\RegionInterfaceFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Quote\Model\Quote\Address as QuoteAddress;

/**
* Save Address to Customer Address Book.
*/
class SaveQuoteAddressToCustomerAddressBook
{
/**
* @var AddressInterfaceFactory
*/
private $addressFactory;

/**
* @var AddressRepositoryInterface
*/
private $addressRepository;

/**
* @var RegionInterfaceFactory
*/
private $regionFactory;

/**
* @param AddressInterfaceFactory $addressFactory
* @param AddressRepositoryInterface $addressRepository
* @param RegionInterfaceFactory $regionFactory
*/
public function __construct(
AddressInterfaceFactory $addressFactory,
AddressRepositoryInterface $addressRepository,
RegionInterfaceFactory $regionFactory
) {
$this->addressFactory = $addressFactory;
$this->addressRepository = $addressRepository;
$this->regionFactory = $regionFactory;
}

/**
* Save Address to Customer Address Book.
*
* @param QuoteAddress $quoteAddress
* @param int $customerId
*
* @return void
* @throws GraphQlInputException
*/
public function execute(QuoteAddress $quoteAddress, int $customerId): void
{
try {
/** @var AddressInterface $customerAddress */
$customerAddress = $this->addressFactory->create();
$customerAddress->setFirstname($quoteAddress->getFirstname())
->setLastname($quoteAddress->getLastname())
->setMiddlename($quoteAddress->getMiddlename())
->setPrefix($quoteAddress->getPrefix())
->setSuffix($quoteAddress->getSuffix())
->setVatId($quoteAddress->getVatId())
->setCountryId($quoteAddress->getCountryId())
->setCompany($quoteAddress->getCompany())
->setRegionId($quoteAddress->getRegionId())
->setFax($quoteAddress->getFax())
->setCity($quoteAddress->getCity())
->setPostcode($quoteAddress->getPostcode())
->setStreet($quoteAddress->getStreet())
->setTelephone($quoteAddress->getTelephone())
->setCustomerId($customerId);

/** @var RegionInterface $region */
$region = $this->regionFactory->create();
$region->setRegionCode($quoteAddress->getRegionCode())
->setRegion($quoteAddress->getRegion())
->setRegionId($quoteAddress->getRegionId());
$customerAddress->setRegion($region);

$this->addressRepository->save($customerAddress);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}
}
}
46 changes: 43 additions & 3 deletions app/code/Magento/QuoteGraphQl/Model/Cart/GetShippingAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Model\Quote\Address;
use Magento\QuoteGraphQl\Model\Cart\Address\SaveQuoteAddressToCustomerAddressBook;

/**
* Get shipping address
Expand All @@ -23,12 +24,21 @@ class GetShippingAddress
*/
private $quoteAddressFactory;

/**
* @var SaveQuoteAddressToCustomerAddressBook
*/
private $saveQuoteAddressToCustomerAddressBook;

/**
* @param QuoteAddressFactory $quoteAddressFactory
* @param SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
*/
public function __construct(QuoteAddressFactory $quoteAddressFactory)
{
public function __construct(
QuoteAddressFactory $quoteAddressFactory,
SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->saveQuoteAddressToCustomerAddressBook = $saveQuoteAddressToCustomerAddressBook;
}

/**
Expand Down Expand Up @@ -62,16 +72,46 @@ public function execute(ContextInterface $context, array $shippingAddressInput):
);
}

$shippingAddress = $this->createShippingAddress($context, $customerAddressId, $addressInput);

return $shippingAddress;
}

/**
* Create shipping address.
*
* @param ContextInterface $context
* @param int|null $customerAddressId
* @param array|null $addressInput
*
* @return \Magento\Quote\Model\Quote\Address
* @throws GraphQlAuthorizationException
*/
private function createShippingAddress(
ContextInterface $context,
?int $customerAddressId,
?array $addressInput
) {
$customerId = $context->getUserId();

if (null === $customerAddressId) {
$shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);

// need to save address only for registered user and if save_in_address_book = true
if (0 !== $customerId
&& isset($addressInput['save_in_address_book'])
&& (bool)$addressInput['save_in_address_book'] === true
) {
$this->saveQuoteAddressToCustomerAddressBook->execute($shippingAddress, $customerId);
}
} else {
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
(int)$customerAddressId,
$context->getUserId()
$customerId
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Address;
use Magento\QuoteGraphQl\Model\Cart\Address\SaveQuoteAddressToCustomerAddressBook;

/**
* Set billing address for a specified shopping cart
Expand All @@ -29,16 +30,24 @@ class SetBillingAddressOnCart
*/
private $assignBillingAddressToCart;

/**
* @var SaveQuoteAddressToCustomerAddressBook
*/
private $saveQuoteAddressToCustomerAddressBook;

/**
* @param QuoteAddressFactory $quoteAddressFactory
* @param AssignBillingAddressToCart $assignBillingAddressToCart
* @param SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
*/
public function __construct(
QuoteAddressFactory $quoteAddressFactory,
AssignBillingAddressToCart $assignBillingAddressToCart
AssignBillingAddressToCart $assignBillingAddressToCart,
SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->assignBillingAddressToCart = $assignBillingAddressToCart;
$this->saveQuoteAddressToCustomerAddressBook = $saveQuoteAddressToCustomerAddressBook;
}

/**
Expand Down Expand Up @@ -101,6 +110,15 @@ private function createBillingAddress(
): Address {
if (null === $customerAddressId) {
$billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);

$customerId = $context->getUserId();
// need to save address only for registered user and if save_in_address_book = true
if (0 !== $customerId
&& isset($addressInput['save_in_address_book'])
&& (bool)$addressInput['save_in_address_book'] === true
) {
$this->saveQuoteAddressToCustomerAddressBook->execute($billingAddress, $customerId);
}
} else {
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
Expand All @@ -111,6 +129,7 @@ private function createBillingAddress(
(int)$context->getUserId()
);
}

return $billingAddress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\Quote\Api\Data\CartInterface;
Expand All @@ -21,6 +20,7 @@ class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
* @var AssignShippingAddressToCart
*/
private $assignShippingAddressToCart;

/**
* @var GetShippingAddress
*/
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ input CartAddressInput {
postcode: String
country_code: String!
telephone: String!
save_in_address_book: Boolean!
save_in_address_book: Boolean
}

input SetShippingMethodsOnCartInput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Config\Model\Config;
use Magento\Config\Model\ResourceModel\Config as ConfigResource;
use Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Store\Model\StoreManagerInterface;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -156,4 +157,31 @@ private function getStoreIdByCode(string $storeCode): int
$store = $storeManager->getStore($storeCode);
return (int)$store->getId();
}

/**
* @inheritDoc
*/
protected function _setConfigValue($configPath, $value, $storeCode = false)
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
if ($storeCode === false) {
$objectManager->get(
\Magento\TestFramework\App\ApiMutableScopeConfig::class
)->setValue(
$configPath,
$value,
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
);

return;
}
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\TestFramework\App\ApiMutableScopeConfig::class
)->setValue(
$configPath,
$value,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeCode
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* @inheritdoc
*/
class MutableScopeConfig implements MutableScopeConfigInterface
class ApiMutableScopeConfig implements MutableScopeConfigInterface
{
/**
* @var Config
Expand Down Expand Up @@ -56,7 +56,6 @@ public function setValue(
/**
* Clean app config cache
*
* @param string|null $type
* @return void
*/
public function clean()
Expand Down Expand Up @@ -89,19 +88,13 @@ private function getTestAppConfig()
private function persistConfig($path, $value, $scopeType, $scopeCode): void
{
$pathParts = explode('/', $path);
$store = '';
if ($scopeType === \Magento\Store\Model\ScopeInterface::SCOPE_STORE) {
if ($scopeCode !== null) {
$store = ObjectManager::getInstance()
$store = 0;
if ($scopeType === \Magento\Store\Model\ScopeInterface::SCOPE_STORE
&& $scopeCode !== null) {
$store = ObjectManager::getInstance()
->get(\Magento\Store\Api\StoreRepositoryInterface::class)
->get($scopeCode)
->getId();
} else {
$store = ObjectManager::getInstance()
->get(\Magento\Store\Model\StoreManagerInterface::class)
->getStore()
->getId();
}
}
$configData = [
'section' => $pathParts[0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CreateBraintreeClientTokenTest extends GraphQlAbstract
* @magentoConfigFixture default_store payment/braintree/active 1
* @magentoConfigFixture default_store payment/braintree/environment sandbox
* @magentoConfigFixture default_store payment/braintree/merchant_id def_merchant_id
* @magentoConfigFixture default_store payment/braintree/merchant_account_id def_merchant_id
* @magentoConfigFixture default_store payment/braintree/public_key def_public_key
* @magentoConfigFixture default_store payment/braintree/private_key def_private_key
*/
Expand Down
Loading

0 comments on commit 14d6388

Please sign in to comment.