|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\LoginAsCustomerAssistance\Plugin; |
| 9 | + |
| 10 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 11 | +use Magento\Customer\Api\Data\CustomerInterface as Customer; |
| 12 | +use Magento\Customer\Model\CustomerRegistry; |
| 13 | +use Magento\Framework\Api\ExtensibleDataInterface; |
| 14 | +use Magento\Framework\Reflection\DataObjectProcessor; |
| 15 | +use Magento\Framework\Webapi\Rest\Request; |
| 16 | +use Magento\Integration\Model\Oauth\TokenFactory; |
| 17 | +use Magento\LoginAsCustomerAssistance\Api\IsAssistanceEnabledInterface; |
| 18 | +use Magento\LoginAsCustomerAssistance\Model\ResourceModel\GetLoginAsCustomerAssistanceAllowed; |
| 19 | +use Magento\TestFramework\Helper\Bootstrap; |
| 20 | +use Magento\TestFramework\TestCase\WebapiAbstract; |
| 21 | + |
| 22 | +/** |
| 23 | + * Api test for @see \Magento\LoginAsCustomerAssistance\Plugin\CustomerPlugin::afterSave. |
| 24 | + */ |
| 25 | +class CustomerMeTest extends WebapiAbstract |
| 26 | +{ |
| 27 | + const SERVICE_VERSION = 'V1'; |
| 28 | + const SERVICE_NAME = 'customerCustomerRepositoryV1'; |
| 29 | + const RESOURCE_PATH = '/V1/customers/me'; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var DataObjectProcessor |
| 33 | + */ |
| 34 | + private $dataObjectProcessor; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var CustomerRepositoryInterface |
| 38 | + */ |
| 39 | + private $customerRepository; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var CustomerRegistry |
| 43 | + */ |
| 44 | + private $customerRegistry; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var GetLoginAsCustomerAssistanceAllowed |
| 48 | + */ |
| 49 | + private $isAssistanceEnabled; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var TokenFactory |
| 53 | + */ |
| 54 | + private $tokenFactory; |
| 55 | + |
| 56 | + /** |
| 57 | + * @inheritDoc |
| 58 | + */ |
| 59 | + protected function setUp(): void |
| 60 | + { |
| 61 | + $objectManager = Bootstrap::getObjectManager(); |
| 62 | + $this->dataObjectProcessor = $objectManager->get(DataObjectProcessor::class); |
| 63 | + $this->customerRepository = $objectManager->get(CustomerRepositoryInterface::class); |
| 64 | + $this->customerRegistry = $objectManager->get(CustomerRegistry::class); |
| 65 | + $this->isAssistanceEnabled = $objectManager->get(GetLoginAsCustomerAssistanceAllowed::class); |
| 66 | + $this->tokenFactory = $objectManager->get(TokenFactory::class); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Check that 'assistance_allowed' set as expected. |
| 71 | + * |
| 72 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 73 | + * @dataProvider assistanceStatesDataProvider |
| 74 | + * |
| 75 | + * @param int $state |
| 76 | + * @param bool $expected |
| 77 | + * @return void |
| 78 | + */ |
| 79 | + public function testUpdateSelf(int $state, bool $expected): void |
| 80 | + { |
| 81 | + $customerId = (int)$this->customerRepository->get('customer@example.com')->getId(); |
| 82 | + $tokenModel = $this->tokenFactory->create(); |
| 83 | + $customerToken = $tokenModel->createCustomerToken($customerId)->getToken(); |
| 84 | + |
| 85 | + $updatedLastname = 'Updated lastname'; |
| 86 | + $customer = $this->getCustomerData($customerId); |
| 87 | + $customerData = $this->dataObjectProcessor->buildOutputDataArray($customer, Customer::class); |
| 88 | + $customerData[Customer::LASTNAME] = $updatedLastname; |
| 89 | + $customerData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]['assistance_allowed'] = $state; |
| 90 | + |
| 91 | + $requestData['customer'] = TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP |
| 92 | + ? $customerData |
| 93 | + : [ |
| 94 | + Customer::EMAIL => $customerData['email'], |
| 95 | + Customer::FIRSTNAME => $customerData['firstname'], |
| 96 | + Customer::LASTNAME => $updatedLastname, |
| 97 | + Customer::EXTENSION_ATTRIBUTES_KEY => ['assistance_allowed' => $state], |
| 98 | + ]; |
| 99 | + |
| 100 | + $serviceInfo = $this->getServiceInfo('SaveSelf', $customerToken); |
| 101 | + $response = $this->_webApiCall($serviceInfo, $requestData); |
| 102 | + $this->assertNotNull($response); |
| 103 | + |
| 104 | + $existingCustomerDataObject = $this->getCustomerData($customerId); |
| 105 | + $this->assertEquals($updatedLastname, $existingCustomerDataObject->getLastname()); |
| 106 | + $this->assertEquals($expected, $this->isAssistanceEnabled->execute($customerId)); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @param string $operation |
| 111 | + * @param string $token |
| 112 | + * @return array |
| 113 | + */ |
| 114 | + private function getServiceInfo(string $operation, string $token): array |
| 115 | + { |
| 116 | + return [ |
| 117 | + 'rest' => [ |
| 118 | + 'resourcePath' => self::RESOURCE_PATH, |
| 119 | + 'httpMethod' => Request::HTTP_METHOD_PUT, |
| 120 | + 'token' => $token, |
| 121 | + ], |
| 122 | + 'soap' => [ |
| 123 | + 'service' => self::SERVICE_NAME, |
| 124 | + 'serviceVersion' => self::SERVICE_VERSION, |
| 125 | + 'operation' => self::SERVICE_NAME . $operation, |
| 126 | + 'token' => $token, |
| 127 | + ], |
| 128 | + ]; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Retrieve customer data by Id. |
| 133 | + * |
| 134 | + * @param int $customerId |
| 135 | + * @return Customer |
| 136 | + */ |
| 137 | + private function getCustomerData(int $customerId): Customer |
| 138 | + { |
| 139 | + $customerData = $this->customerRepository->getById($customerId); |
| 140 | + $this->customerRegistry->remove($customerId); |
| 141 | + |
| 142 | + return $customerData; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @return array |
| 147 | + */ |
| 148 | + public function assistanceStatesDataProvider(): array |
| 149 | + { |
| 150 | + return [ |
| 151 | + 'Assistance Allowed' => [IsAssistanceEnabledInterface::ALLOWED, true], |
| 152 | + 'Assistance Denied' => [IsAssistanceEnabledInterface::DENIED, false], |
| 153 | + ]; |
| 154 | + } |
| 155 | +} |
0 commit comments