From 8769188605ddd1852a69b55349f70c7e5efda439 Mon Sep 17 00:00:00 2001 From: magento-engcom-team Date: Wed, 8 Nov 2017 15:24:15 +0200 Subject: [PATCH 1/4] 11552: Admin orders account information showing guest for customer name. --- .../Plugin/AddCustomerInfo.php | 70 ++++++++++++ .../Plugin/AddCustomerInfoTest.php | 99 +++++++++++++++++ app/code/Magento/Quote/etc/webapi_rest/di.xml | 1 + .../Magento/Quote/Api/CartRepositoryTest.php | 49 +++++++++ .../Plugin/AddCustomerInfoTest.php | 101 ++++++++++++++++++ 5 files changed, 320 insertions(+) create mode 100644 app/code/Magento/Quote/Model/QuoteRepository/Plugin/AddCustomerInfo.php create mode 100644 app/code/Magento/Quote/Test/Unit/Model/QuoteRepository/Plugin/AddCustomerInfoTest.php create mode 100644 dev/tests/integration/testsuite/Magento/Quote/Model/QuoteRepository/Plugin/AddCustomerInfoTest.php diff --git a/app/code/Magento/Quote/Model/QuoteRepository/Plugin/AddCustomerInfo.php b/app/code/Magento/Quote/Model/QuoteRepository/Plugin/AddCustomerInfo.php new file mode 100644 index 0000000000000..7473d5f0ef444 --- /dev/null +++ b/app/code/Magento/Quote/Model/QuoteRepository/Plugin/AddCustomerInfo.php @@ -0,0 +1,70 @@ +customerRepository = $customerRepository; + } + + /** + * Add to quote customer necessary information, if needed. + * + * @param CartRepositoryInterface $subject + * @param CartInterface $quote + * @return null + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function beforeSave(CartRepositoryInterface $subject, CartInterface $quote) + { + if ($quote->getCustomerId() !== null) { + foreach ($this->fields as $property) { + if (!$quote->getData($property)) { + $customer = $this->customerRepository->getById($quote->getCustomerId()); + $quote->setCustomer($customer); + $quote->setCustomerIsGuest(false); + break; + } + } + } + + return null; + } +} diff --git a/app/code/Magento/Quote/Test/Unit/Model/QuoteRepository/Plugin/AddCustomerInfoTest.php b/app/code/Magento/Quote/Test/Unit/Model/QuoteRepository/Plugin/AddCustomerInfoTest.php new file mode 100644 index 0000000000000..75c0a66a06af0 --- /dev/null +++ b/app/code/Magento/Quote/Test/Unit/Model/QuoteRepository/Plugin/AddCustomerInfoTest.php @@ -0,0 +1,99 @@ +customerRepository = $this->getMockBuilder(CustomerRepositoryInterface::class) + ->disableOriginalConstructor() + ->setMethods(['getById']) + ->getMockForAbstractClass(); + $objectManager = new ObjectManager($this); + $this->testSubject = $objectManager->getObject( + AddCustomerInfo::class, + ['customerRepository' => $this->customerRepository] + ); + } + + /** + * Test all necessary customer info will be added to cart, if absent. + * + * @return void + */ + public function testBeforeSave() + { + $customerId = 1; + + /** @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customer */ + $customer = $this->getMockBuilder(CustomerInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->customerRepository->expects(self::once()) + ->method('getById') + ->with(self::identicalTo($customerId)) + ->willReturn($customer); + + /** @var CartRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject $cartRepository */ + $cartRepository = $this->getMockBuilder(CartRepositoryInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + /** @var CartInterface|\PHPUnit_Framework_MockObject_MockObject $cart */ + $cart = $this->getMockBuilder(CartInterface::class) + ->disableOriginalConstructor() + ->setMethods(['getCustomerIsGuest', 'getCustomerId', 'getData', 'setCustomer', 'setCustomerIsGuest']) + ->getMockForAbstractClass(); + $cart->expects(self::once()) + ->method('getCustomerIsGuest') + ->willReturn(false); + $cart->expects(self::exactly(2)) + ->method('getCustomerId') + ->willReturn($customerId); + $cart->expects(self::once()) + ->method('getData') + ->with(self::identicalTo(OrderInterface::CUSTOMER_EMAIL)) + ->willReturn(null); + $cart->expects(self::once()) + ->method('setCustomer') + ->with(self::identicalTo($customer)) + ->willReturnSelf(); + $cart->expects(self::once()) + ->method('setCustomerIsGuest') + ->with(self::identicalTo(false)) + ->willReturnSelf(); + + $this->testSubject->beforeSave($cartRepository, $cart); + } +} diff --git a/app/code/Magento/Quote/etc/webapi_rest/di.xml b/app/code/Magento/Quote/etc/webapi_rest/di.xml index 2c8da7c775ec1..ab98d43b1dbd5 100644 --- a/app/code/Magento/Quote/etc/webapi_rest/di.xml +++ b/app/code/Magento/Quote/etc/webapi_rest/di.xml @@ -11,5 +11,6 @@ + diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartRepositoryTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartRepositoryTest.php index 8cb82f5c8f206..dc3eb78fd330e 100644 --- a/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartRepositoryTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartRepositoryTest.php @@ -318,6 +318,55 @@ public function testSaveQuote() $this->assertEquals($requestData["quote"]["customer"]["email"], $quote->getCustomerEmail()); } + /** + * Saving quote without full customer information, just customer id. + * + * @magentoApiDataFixture Magento/Sales/_files/quote.php + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @return void + */ + public function testSaveQuoteWithMinimalCustomerInfo() + { + $this->_markTestAsRestOnly(); + $token = $this->getToken(); + /** @var Quote $quote */ + $quote = $this->getCart('test01'); + $requestData['quote'] = [ + 'id' => $quote->getId(), + 'customer' => [ + 'id' => 1, + ], + ]; + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::$mineCartUrl, + 'httpMethod' => Request::HTTP_METHOD_PUT, + 'token' => $token + ], + 'soap' => [ + 'service' => 'quoteCartRepositoryV1', + 'serviceVersion' => 'V1', + 'operation' => 'quoteCartRepositoryV1Save', + 'token' => $token + ] + ]; + + $this->_webApiCall($serviceInfo, $requestData); + + // Reload target quote + $quote = $this->getCart('test01'); + /** @var $repository \Magento\Customer\Api\CustomerRepositoryInterface */ + $repository = $this->objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class); + /** @var $customer \Magento\Customer\Api\Data\CustomerInterface */ + $customer = $repository->get('customer@example.com'); + $this->assertEquals(0, $quote->getCustomerIsGuest()); + $this->assertEquals($customer->getId(), $quote->getCustomerId()); + $this->assertEquals($customer->getFirstname(), $quote->getCustomerFirstname()); + $this->assertEquals($customer->getLastname(), $quote->getCustomerLastname()); + $this->assertEquals($customer->getEmail(), $quote->getCustomerEmail()); + $this->assertEquals($customer->getGroupId(), $quote->getCustomerGroupId()); + } + /** * Request to api for the current user token. * diff --git a/dev/tests/integration/testsuite/Magento/Quote/Model/QuoteRepository/Plugin/AddCustomerInfoTest.php b/dev/tests/integration/testsuite/Magento/Quote/Model/QuoteRepository/Plugin/AddCustomerInfoTest.php new file mode 100644 index 0000000000000..b80501cbda911 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Quote/Model/QuoteRepository/Plugin/AddCustomerInfoTest.php @@ -0,0 +1,101 @@ +get(State::class); + $state->setAreaCode(Area::AREA_WEBAPI_REST); + $pluginInfo = Bootstrap::getObjectManager()->get(PluginList::class)->get(QuoteRepository::class, []); + self::assertSame(AddCustomerInfo::class, $pluginInfo['add_customer_info']['instance']); + } + + /** + * Test AddCustomerInfo plugin adds all necessary customer information, if quote has customer id. + * + * @magentoDataFixture Magento/Sales/_files/quote.php + * @magentoDataFixture Magento/Customer/_files/customer.php + */ + public function testAfterSave() + { + /** @var State $state */ + $state = Bootstrap::getObjectManager()->get(State::class); + $state->setAreaCode(Area::AREA_WEBAPI_REST); + /** @var CustomerRepositoryInterface $customerRepository */ + $customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class); + /** @var CustomerInterface $customer */ + $customer = $customerRepository->get('customer@example.com'); + $this->mockUserContext($customer->getId()); + /** @var QuoteRepository $quoteRepository */ + $quoteRepository = Bootstrap::getObjectManager()->create(QuoteRepository::class); + $quote = $this->loadQuote(); + $quote->setCustomerId($customer->getId()); + $quoteRepository->save($quote); + $quote = $this->loadQuote(); + self::assertEquals(0, $quote->getCustomerIsGuest()); + self::assertEquals($customer->getId(), $quote->getCustomerId()); + self::assertEquals($customer->getFirstname(), $quote->getCustomerFirstname()); + self::assertEquals($customer->getLastname(), $quote->getCustomerLastname()); + self::assertEquals($customer->getEmail(), $quote->getCustomerEmail()); + self::assertEquals($customer->getGroupId(), $quote->getCustomerGroupId()); + } + + /** + * @return \Magento\Quote\Model\Quote + */ + private function loadQuote() + { + /** @var $quote \Magento\Quote\Model\Quote */ + $quote = Bootstrap::getObjectManager()->get(\Magento\Quote\Model\Quote::class); + $quote->load('test01', 'reserved_order_id'); + + return $quote; + } + + /** + * Mock User context to bypass AccessChangeQuoteControl plugin. + * + * @param string $customerId + * @return void + */ + private function mockUserContext(string $customerId) + { + Bootstrap::getObjectManager()->removeSharedInstance(CompositeUserContext::class); + /** @var UserContextInterface|\PHPUnit_Framework_MockObject_MockObject $userContext */ + $userContext = $this->getMockBuilder(CompositeUserContext::class) + ->setMethods(['getUserType', 'getUserId']) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + $userContext->expects(self::any()) + ->method('getUserType') + ->willReturn(UserContextInterface::USER_TYPE_CUSTOMER); + $userContext->expects(self::any()) + ->method('getUserId') + ->willReturn($customerId); + Bootstrap::getObjectManager()->addSharedInstance($userContext, CompositeUserContext::class); + } +} From 8cd2ec498a34ce10728b30474afa3c600b4fda9e Mon Sep 17 00:00:00 2001 From: magento-engcom-team Date: Wed, 8 Nov 2017 16:38:47 +0200 Subject: [PATCH 2/4] 11552: Admin orders account information showing guest for customer name. --- .../Plugin/AddCustomerInfo.php | 37 +++++++++++++--- .../Plugin/AddCustomerInfoTest.php | 43 +++++++++++++++---- 2 files changed, 65 insertions(+), 15 deletions(-) diff --git a/app/code/Magento/Quote/Model/QuoteRepository/Plugin/AddCustomerInfo.php b/app/code/Magento/Quote/Model/QuoteRepository/Plugin/AddCustomerInfo.php index 7473d5f0ef444..4eca2d9388806 100644 --- a/app/code/Magento/Quote/Model/QuoteRepository/Plugin/AddCustomerInfo.php +++ b/app/code/Magento/Quote/Model/QuoteRepository/Plugin/AddCustomerInfo.php @@ -7,6 +7,7 @@ namespace Magento\Quote\Model\QuoteRepository\Plugin; use Magento\Customer\Api\CustomerRepositoryInterface; +use Magento\Customer\Api\Data\CustomerInterface; use Magento\Quote\Api\CartRepositoryInterface; use Magento\Quote\Api\Data\CartInterface; use Magento\Sales\Api\Data\OrderInterface; @@ -25,7 +26,6 @@ class AddCustomerInfo OrderInterface::CUSTOMER_EMAIL, OrderInterface::CUSTOMER_FIRSTNAME, OrderInterface::CUSTOMER_LASTNAME, - OrderInterface::CUSTOMER_GROUP_ID, ]; /** @@ -55,16 +55,41 @@ public function __construct(CustomerRepositoryInterface $customerRepository) public function beforeSave(CartRepositoryInterface $subject, CartInterface $quote) { if ($quote->getCustomerId() !== null) { + $customer = $this->customerRepository->getById($quote->getCustomerId()); foreach ($this->fields as $property) { - if (!$quote->getData($property)) { - $customer = $this->customerRepository->getById($quote->getCustomerId()); - $quote->setCustomer($customer); - $quote->setCustomerIsGuest(false); - break; + if ($quote->getData($property) === null) { + $value = $this->getCustomerData($customer, $property); + $quote->setData($property, $value); } } + $quote->setCustomerGroupId($customer->getGroupId()); + $quote->setCustomerIsGuest(false); } return null; } + + /** + * @param CustomerInterface $customer + * @param string $property + * + * @return null|string + */ + private function getCustomerData(CustomerInterface $customer, string $property) + { + $result = ''; + switch ($property) { + case OrderInterface::CUSTOMER_EMAIL : + $result = $customer->getEmail(); + break; + case OrderInterface::CUSTOMER_FIRSTNAME: + $result = $customer->getFirstname(); + break; + case OrderInterface::CUSTOMER_LASTNAME: + $result = $customer->getLastname(); + break; + } + + return $result; + } } diff --git a/app/code/Magento/Quote/Test/Unit/Model/QuoteRepository/Plugin/AddCustomerInfoTest.php b/app/code/Magento/Quote/Test/Unit/Model/QuoteRepository/Plugin/AddCustomerInfoTest.php index 75c0a66a06af0..d70dc8f07ab6f 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/QuoteRepository/Plugin/AddCustomerInfoTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/QuoteRepository/Plugin/AddCustomerInfoTest.php @@ -53,12 +53,28 @@ protected function setUp() */ public function testBeforeSave() { - $customerId = 1; + $customerId = '1'; + $customerEmail = 'customer@exampel.com'; + $customerFirstName = 'John'; + $customerLastName = 'Doe'; + $customerGroupId = 1; /** @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customer */ $customer = $this->getMockBuilder(CustomerInterface::class) ->disableOriginalConstructor() ->getMockForAbstractClass(); + $customer->expects(self::once()) + ->method('getEmail') + ->willReturn($customerEmail); + $customer->expects(self::once()) + ->method('getFirstName') + ->willReturn($customerFirstName); + $customer->expects(self::once()) + ->method('getLastName') + ->willReturn($customerLastName); + $customer->expects(self::once()) + ->method('getGroupId') + ->willReturn($customerGroupId); $this->customerRepository->expects(self::once()) ->method('getById') @@ -73,21 +89,30 @@ public function testBeforeSave() /** @var CartInterface|\PHPUnit_Framework_MockObject_MockObject $cart */ $cart = $this->getMockBuilder(CartInterface::class) ->disableOriginalConstructor() - ->setMethods(['getCustomerIsGuest', 'getCustomerId', 'getData', 'setCustomer', 'setCustomerIsGuest']) + ->setMethods(['getCustomerId', 'getData', 'setCustomerIsGuest', 'setData', 'setCustomerGroupId']) ->getMockForAbstractClass(); - $cart->expects(self::once()) - ->method('getCustomerIsGuest') - ->willReturn(false); $cart->expects(self::exactly(2)) ->method('getCustomerId') ->willReturn($customerId); - $cart->expects(self::once()) + $cart->expects(self::exactly(3)) ->method('getData') - ->with(self::identicalTo(OrderInterface::CUSTOMER_EMAIL)) + ->withConsecutive( + self::identicalTo(OrderInterface::CUSTOMER_EMAIL), + self::identicalTo(OrderInterface::CUSTOMER_FIRSTNAME), + self::identicalTo(OrderInterface::CUSTOMER_LASTNAME) + ) ->willReturn(null); + $cart->expects(self::exactly(3)) + ->method('setData') + ->withConsecutive( + self::identicalTo(OrderInterface::CUSTOMER_EMAIL), + self::identicalTo(OrderInterface::CUSTOMER_FIRSTNAME), + self::identicalTo(OrderInterface::CUSTOMER_LASTNAME) + ) + ->willReturnSelf(); $cart->expects(self::once()) - ->method('setCustomer') - ->with(self::identicalTo($customer)) + ->method('setCustomerGroupId') + ->with($customerGroupId) ->willReturnSelf(); $cart->expects(self::once()) ->method('setCustomerIsGuest') From 73b0633ead01bde30e205d88d9eba0b2421e4430 Mon Sep 17 00:00:00 2001 From: magento-engcom-team Date: Wed, 8 Nov 2017 16:56:04 +0200 Subject: [PATCH 3/4] 11552: Admin orders account information showing guest for customer name. --- .../Quote/Model/QuoteRepository/Plugin/AddCustomerInfo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Quote/Model/QuoteRepository/Plugin/AddCustomerInfo.php b/app/code/Magento/Quote/Model/QuoteRepository/Plugin/AddCustomerInfo.php index 4eca2d9388806..2ba4b7166bcd0 100644 --- a/app/code/Magento/Quote/Model/QuoteRepository/Plugin/AddCustomerInfo.php +++ b/app/code/Magento/Quote/Model/QuoteRepository/Plugin/AddCustomerInfo.php @@ -79,7 +79,7 @@ private function getCustomerData(CustomerInterface $customer, string $property) { $result = ''; switch ($property) { - case OrderInterface::CUSTOMER_EMAIL : + case OrderInterface::CUSTOMER_EMAIL: $result = $customer->getEmail(); break; case OrderInterface::CUSTOMER_FIRSTNAME: From bcda941eba3a09d86b0b7fa5fe315d08b6786464 Mon Sep 17 00:00:00 2001 From: magento-engcom-team Date: Wed, 8 Nov 2017 17:52:47 +0200 Subject: [PATCH 4/4] 11552: Admin orders account information showing guest for customer name. --- .../testsuite/Magento/Quote/Api/CartRepositoryTest.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartRepositoryTest.php b/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartRepositoryTest.php index dc3eb78fd330e..e0caa3acbe180 100644 --- a/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartRepositoryTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Quote/Api/CartRepositoryTest.php @@ -327,6 +327,7 @@ public function testSaveQuote() */ public function testSaveQuoteWithMinimalCustomerInfo() { + //for SOAP all necessary fields for customer are mandatory. $this->_markTestAsRestOnly(); $token = $this->getToken(); /** @var Quote $quote */ @@ -342,12 +343,6 @@ public function testSaveQuoteWithMinimalCustomerInfo() 'resourcePath' => self::$mineCartUrl, 'httpMethod' => Request::HTTP_METHOD_PUT, 'token' => $token - ], - 'soap' => [ - 'service' => 'quoteCartRepositoryV1', - 'serviceVersion' => 'V1', - 'operation' => 'quoteCartRepositoryV1Save', - 'token' => $token ] ];