-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #557 from magento-performance/ACPT-1502
ACPT-1502: Add Customer GraphQl mutations to GraphQlStateTest
- Loading branch information
Showing
2 changed files
with
707 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,11 +7,14 @@ | |
|
||
namespace Magento\GraphQl\App; | ||
|
||
use Magento\Customer\Api\CustomerRepositoryInterface; | ||
use Magento\Framework\App\Http as HttpApp; | ||
use Magento\Framework\App\Request\HttpFactory as RequestFactory; | ||
use Magento\Framework\App\Response\Http as HttpResponse; | ||
use Magento\Framework\ObjectManagerInterface; | ||
use Magento\Framework\Registry; | ||
use Magento\GraphQl\App\State\Comparator; | ||
use Magento\Integration\Api\CustomerTokenServiceInterface; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
|
||
/** | ||
|
@@ -38,6 +41,12 @@ class GraphQlStateTest extends \PHPUnit\Framework\TestCase | |
/** @var RequestFactory */ | ||
private RequestFactory $requestFactory; | ||
|
||
/** @var CustomerRepositoryInterface */ | ||
private CustomerRepositoryInterface $customerRepository; | ||
|
||
/** @var Registry */ | ||
private $registry; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
|
@@ -49,6 +58,33 @@ protected function setUp(): void | |
parent::setUp(); | ||
} | ||
|
||
/** | ||
* @magentoDataFixture Magento/Customer/_files/customer.php | ||
* @magentoDataFixture Magento/Customer/_files/customer_address.php | ||
* @dataProvider customerDataProvider | ||
* @return void | ||
* @throws \Exception | ||
*/ | ||
public function testCustomerState(string $query, array $variables, array $variables2, array $authInfo, string $operationName, string $expected) | ||
{ | ||
if ($operationName === 'createCustomer') { | ||
$this->customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class); | ||
$this->registry = $this->objectManager->get(Registry::class); | ||
$this->registry->register('isSecureArea', true); | ||
try { | ||
$customer = $this->customerRepository->get($variables['email']); | ||
$this->customerRepository->delete($customer); | ||
$customer2 = $this->customerRepository->get($variables2['email']); | ||
$this->customerRepository->delete($customer2); | ||
} catch (\Exception $e) { | ||
// Customer does not exist | ||
} finally { | ||
$this->registry->unregister('isSecureArea', false); | ||
} | ||
} | ||
$this->testState($query, $variables, $variables2, $authInfo, $operationName, $expected); | ||
} | ||
|
||
/** | ||
* Runs various GraphQL queries and checks if state of shared objects in Object Manager have changed | ||
* @magentoConfigFixture base_website btob/website_configuration/company_active 1 | ||
|
@@ -58,19 +94,20 @@ protected function setUp(): void | |
* @param string $query | ||
* @param array $variables | ||
* @param array $variables2 This is the second set of variables to be used in the second request | ||
* @param array $authInfo | ||
* @param string $operationName | ||
* @param string $expected | ||
* @return void | ||
* @throws \Exception | ||
*/ | ||
public function testState(string $query, array $variables, array $variables2, string $operationName, string $expected): void | ||
public function testState(string $query, array $variables, array $variables2, array $authInfo, string $operationName, string $expected): void | ||
{ | ||
$jsonEncodedRequest = json_encode([ | ||
'query' => $query, | ||
'variables' => $variables, | ||
'operationName' => $operationName | ||
]); | ||
$output1 = $this->request($jsonEncodedRequest, $operationName, true); | ||
$output1 = $this->request($jsonEncodedRequest, $operationName, $authInfo, true); | ||
$this->assertStringContainsString($expected, $output1); | ||
if ($variables2) { | ||
$jsonEncodedRequest = json_encode([ | ||
|
@@ -79,21 +116,22 @@ public function testState(string $query, array $variables, array $variables2, st | |
'operationName' => $operationName | ||
]); | ||
} | ||
$output2 = $this->request($jsonEncodedRequest, $operationName); | ||
$output2 = $this->request($jsonEncodedRequest, $operationName, $authInfo); | ||
$this->assertStringContainsString($expected, $output2); | ||
} | ||
|
||
/** | ||
* @param string $query | ||
* @param string $operationName | ||
* @param array $authInfo | ||
* @param bool $firstRequest | ||
* @return string | ||
* @throws \Exception | ||
*/ | ||
private function request(string $query, string $operationName, bool $firstRequest = false): string | ||
private function request(string $query, string $operationName, array $authInfo, bool $firstRequest = false): string | ||
{ | ||
$this->comparator->rememberObjectsStateBefore($firstRequest); | ||
$response = $this->doRequest($query); | ||
$response = $this->doRequest($query, $authInfo); | ||
$this->comparator->rememberObjectsStateAfter($firstRequest); | ||
$result = $this->comparator->compare($operationName); | ||
$this->assertEmpty( | ||
|
@@ -113,13 +151,20 @@ private function request(string $query, string $operationName, bool $firstReques | |
* @param string $query | ||
* @return string | ||
*/ | ||
private function doRequest(string $query) | ||
private function doRequest(string $query, array $authInfo) | ||
{ | ||
$request = $this->requestFactory->create(); | ||
$request->setContent($query); | ||
$request->setMethod('POST'); | ||
$request->setPathInfo('/graphql'); | ||
$request->getHeaders()->addHeaders(['content_type' => self::CONTENT_TYPE]); | ||
if ($authInfo) { | ||
$email = $authInfo['email']; | ||
$password = $authInfo['password']; | ||
$customerToken = $this->objectManager->get(CustomerTokenServiceInterface::class) | ||
->createCustomerAccessToken($email, $password); | ||
$request->getHeaders()->addHeaders(['Authorization' => 'Bearer ' . $customerToken]); | ||
} | ||
$unusedResponse = $this->objectManager->create(HttpResponse::class); | ||
$httpApp = $this->objectManager->create( | ||
HttpApp::class, | ||
|
@@ -170,6 +215,7 @@ public function queryDataProvider(): array | |
QUERY, | ||
['id' => 4], | ||
[], | ||
[], | ||
'navigationMenu', | ||
'"id":4,"name":"Category 1.1","product_count":2,' | ||
], | ||
|
@@ -220,6 +266,7 @@ public function queryDataProvider(): array | |
QUERY, | ||
['name' => 'Configurable%20Product', 'onServer' => false], | ||
[], | ||
[], | ||
'productDetailByName', | ||
'"sku":"configurable","name":"Configurable Product"' | ||
], | ||
|
@@ -269,6 +316,7 @@ public function queryDataProvider(): array | |
QUERY, | ||
['id' => 4, 'currentPage' => 1, 'pageSize' => 12], | ||
[], | ||
[], | ||
'category', | ||
'"url_key":"category-1-1","name":"Category 1.1"' | ||
], | ||
|
@@ -333,6 +381,7 @@ public function queryDataProvider(): array | |
QUERY, | ||
['name' => 'Simple Product1', 'onServer' => false], | ||
[], | ||
[], | ||
'productDetail', | ||
'"sku":"simple1","name":"Simple Product1"' | ||
], | ||
|
@@ -347,8 +396,175 @@ public function queryDataProvider(): array | |
QUERY, | ||
['urlKey' => 'no-route'], | ||
[], | ||
[], | ||
'resolveUrl', | ||
'"type":"CMS_PAGE","id":1' | ||
], | ||
]; | ||
} | ||
/** | ||
* Queries, variables, operation names, and expected responses for test | ||
* | ||
* @return array[] | ||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) | ||
*/ | ||
public function customerDataProvider(): array | ||
{ | ||
return [ | ||
'Create Customer' => [ | ||
<<<'QUERY' | ||
mutation($firstname: String!, $lastname: String!, $email: String!, $password: String!) { | ||
createCustomerV2( | ||
input: { | ||
firstname: $firstname, | ||
lastname: $lastname, | ||
email: $email, | ||
password: $password | ||
} | ||
) { | ||
customer { | ||
created_at | ||
prefix | ||
firstname | ||
middlename | ||
lastname | ||
suffix | ||
default_billing | ||
default_shipping | ||
date_of_birth | ||
taxvat | ||
is_subscribed | ||
gender | ||
allow_remote_shopping_assistance | ||
} | ||
} | ||
} | ||
QUERY, | ||
[ | ||
'firstname' => 'John', | ||
'lastname' => 'Doe', | ||
'email' => '[email protected]', | ||
'password' => 'Password-1', | ||
], | ||
[ | ||
'firstname' => 'John', | ||
'lastname' => 'Doe', | ||
'email' => '[email protected]', | ||
'password' => 'Password-2', | ||
], | ||
[], | ||
'createCustomer', | ||
'"email":"', | ||
], | ||
'Update Customer' => [ | ||
<<<'QUERY' | ||
mutation($allow: Boolean!) { | ||
updateCustomerV2( | ||
input: { | ||
allow_remote_shopping_assistance: $allow | ||
} | ||
) { | ||
customer { | ||
allow_remote_shopping_assistance | ||
} | ||
} | ||
} | ||
QUERY, | ||
['allow' => true], | ||
['allow' => false], | ||
['email' => '[email protected]', 'password' => 'password'], | ||
'updateCustomer', | ||
'allow_remote_shopping_assistance' | ||
], | ||
'Update Customer Address' => [ | ||
<<<'QUERY' | ||
mutation($addressId: Int!, $city: String!) { | ||
updateCustomerAddress(id: $addressId, input: { | ||
region: { | ||
region: "Alberta" | ||
region_id: 66 | ||
region_code: "AB" | ||
} | ||
country_code: CA | ||
street: ["Line 1 Street","Line 2"] | ||
company: "Company Name" | ||
telephone: "123456789" | ||
fax: "123123123" | ||
postcode: "7777" | ||
city: $city | ||
firstname: "Adam" | ||
lastname: "Phillis" | ||
middlename: "A" | ||
prefix: "Mr." | ||
suffix: "Jr." | ||
vat_id: "1" | ||
default_shipping: true | ||
default_billing: true | ||
}) { | ||
id | ||
customer_id | ||
region { | ||
region | ||
region_id | ||
region_code | ||
} | ||
country_code | ||
street | ||
company | ||
telephone | ||
fax | ||
postcode | ||
city | ||
firstname | ||
lastname | ||
middlename | ||
prefix | ||
suffix | ||
vat_id | ||
default_shipping | ||
default_billing | ||
} | ||
} | ||
QUERY, | ||
['addressId' => 1, 'city' => 'New York'], | ||
['addressId' => 1, 'city' => 'Austin'], | ||
['email' => '[email protected]', 'password' => 'password'], | ||
'updateCustomerAddress', | ||
'city' | ||
], | ||
'Update Customer Email' => [ | ||
<<<'QUERY' | ||
mutation($email: String!, $password: String!) { | ||
updateCustomerEmail( | ||
email: $email | ||
password: $password | ||
) { | ||
customer { | ||
} | ||
} | ||
} | ||
QUERY, | ||
['email' => '[email protected]', 'password' => 'password'], | ||
['email' => '[email protected]', 'password' => 'password'], | ||
['email' => '[email protected]', 'password' => 'password'], | ||
'updateCustomerEmail', | ||
'email' | ||
], | ||
'Generate Customer Token' => [ | ||
<<<'QUERY' | ||
mutation($email: String!, $password: String!) { | ||
generateCustomerToken(email: $email, password: $password) { | ||
token | ||
} | ||
} | ||
QUERY, | ||
['email' => '[email protected]', 'password' => 'password'], | ||
['email' => '[email protected]', 'password' => 'password'], | ||
[], | ||
'generateCustomerToken', | ||
'token' | ||
] | ||
]; | ||
} | ||
|
Oops, something went wrong.