![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ declare(strict_types=1); namespace Magento\GraphQl\Customer; use Exception; use Magento\Customer\Api\CustomerRepositoryInterface; use Magento\Customer\Api\Data\AddressInterface; use Magento\Customer\Api\Data\CustomerInterface; use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\ObjectManager; use Magento\TestFramework\TestCase\GraphQlAbstract; use Magento\Integration\Api\CustomerTokenServiceInterface; /** * Test for customer address retrieval. */ class GetAddressesTest extends GraphQlAbstract { /** * @var CustomerTokenServiceInterface */ private $customerTokenService; /** * @var LockCustomer */ private $lockCustomer; protected function setUp(): void { parent::setUp(); $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class); $this->lockCustomer = Bootstrap::getObjectManager()->get(LockCustomer::class); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php */ public function testGetCustomerWithAddresses() { $query = $this->getQuery(); $userName = '[email protected]'; $password = 'password'; $customerToken = $this->customerTokenService->createCustomerAccessToken($userName, $password); $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; /** @var CustomerRepositoryInterface $customerRepository */ $customerRepository = ObjectManager::getInstance()->get(CustomerRepositoryInterface::class); $customer = $customerRepository->get('[email protected]'); $response = $this->graphQlQuery($query, [], '', $headerMap); $this->assertArrayHasKey('customer', $response); $this->assertArrayHasKey('addresses', $response['customer']); $this->assertIsArray([$response['customer']['addresses']], " Addresses field must be of an array type." ); self::assertNull($response['customer']['id']); $this->assertCustomerAddressesFields($customer, $response); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/Customer/_files/customer_address.php */ public function testGetCustomerAddressIfAccountIsLocked() { $this->expectException(\Exception::class); $this->expectExceptionMessage('GraphQL response contains errors: The account is locked.'); $query = $this->getQuery(); $userName = '[email protected]'; $password = 'password'; $this->lockCustomer->execute(1); $customerToken = $this->customerTokenService->createCustomerAccessToken($userName, $password); $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; $this->graphQlQuery($query, [], '', $headerMap); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/Customer/_files/customer_address.php */ public function testGetCustomerAddressIfUserIsNotAuthorized() { $this->expectException(\Exception::class); $this->expectExceptionMessage('GraphQL response contains errors: The current customer isn\'t authorized.'); $query = $this->getQuery(); $this->graphQlQuery($query); } /** * Verify the fields for CustomerAddress object * * @param CustomerInterface $customer * @param array $actualResponse */ private function assertCustomerAddressesFields($customer, $actualResponse) { /** @var AddressInterface $addresses */ $addresses = $customer->getAddresses(); foreach ($addresses as $addressKey => $addressValue) { $this->assertNotEmpty($addressValue); $assertionMap = [ ['response_field' => 'id', 'expected_value' => $addresses[$addressKey]->getId()], ['response_field' => 'customer_id', 'expected_value' => 0], ['response_field' => 'region_id', 'expected_value' => $addresses[$addressKey]->getRegionId()], ['response_field' => 'country_id', 'expected_value' => $addresses[$addressKey]->getCountryId()], ['response_field' => 'telephone', 'expected_value' => $addresses[$addressKey]->getTelephone()], ['response_field' => 'postcode', 'expected_value' => $addresses[$addressKey]->getPostcode()], ['response_field' => 'city', 'expected_value' => $addresses[$addressKey]->getCity()], ['response_field' => 'firstname', 'expected_value' => $addresses[$addressKey]->getFirstname()], ['response_field' => 'lastname', 'expected_value' => $addresses[$addressKey]->getLastname()] ]; $this->assertResponseFields($actualResponse['customer']['addresses'][$addressKey], $assertionMap); } } /** * @return string */ private function getQuery(): string { $query = <<<QUERY { customer { id addresses { id customer_id region_id country_id telephone postcode city firstname lastname } } } QUERY; return $query; } }