Skip to content

Commit dffe94d

Browse files
authored
Merge pull request #4336 from magento-engcom/graphql-develop-prs
[Magento Community Engineering] Community Contributions - GraphQL
2 parents e11f0e4 + 299a4e8 commit dffe94d

21 files changed

+1153
-124
lines changed

app/code/Magento/CatalogGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ type CustomizableAreaValue @doc(description: "CustomizableAreaValue defines the
118118
}
119119

120120
type CategoryTree implements CategoryInterface @doc(description: "Category Tree implementation.") {
121-
children: [CategoryTree] @doc(description: "Child categories tree.") @resolve(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\CategoryTree")
121+
children: [CategoryTree] @doc(description: "Child categories tree.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\CategoryTree")
122122
}
123123

124124
type CustomizableDateOption implements CustomizableOptionInterface @doc(description: "CustomizableDateOption contains information about a date picker that is defined as part of a customizable option.") {

app/code/Magento/CustomerGraphQl/Model/Resolver/IsEmailAvailable.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1414
use Magento\Framework\GraphQl\Query\ResolverInterface;
1515
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\Framework\Validator\EmailAddress as EmailValidator;
1617

1718
/**
1819
* Is Customer Email Available
@@ -24,13 +25,21 @@ class IsEmailAvailable implements ResolverInterface
2425
*/
2526
private $accountManagement;
2627

28+
/**
29+
* @var EmailValidator
30+
*/
31+
private $emailValidator;
32+
2733
/**
2834
* @param AccountManagementInterface $accountManagement
35+
* @param EmailValidator $emailValidator
2936
*/
3037
public function __construct(
31-
AccountManagementInterface $accountManagement
38+
AccountManagementInterface $accountManagement,
39+
EmailValidator $emailValidator
3240
) {
3341
$this->accountManagement = $accountManagement;
42+
$this->emailValidator = $emailValidator;
3443
}
3544

3645
/**
@@ -47,6 +56,10 @@ public function resolve(
4756
throw new GraphQlInputException(__('Email must be specified'));
4857
}
4958

59+
if (!$this->emailValidator->isValid($args['email'])) {
60+
throw new GraphQlInputException(__('Email is invalid'));
61+
}
62+
5063
try {
5164
$isEmailAvailable = $this->accountManagement->isEmailAvailable($args['email']);
5265
} catch (LocalizedException $e) {

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductSearchTest.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,10 @@ public function testSearchWithFilterWithPageSizeEqualTotalCount()
381381
}
382382
QUERY;
383383
$this->expectException(\Exception::class);
384-
$this->expectExceptionMessage('GraphQL response contains errors: currentPage value 2 specified is greater ' .
385-
'than the 1 page(s) available');
384+
$this->expectExceptionMessage(
385+
'GraphQL response contains errors: currentPage value 2 specified is greater ' .
386+
'than the 1 page(s) available'
387+
);
386388
$this->graphQlQuery($query);
387389
}
388390

@@ -1043,8 +1045,10 @@ public function testQueryPageOutOfBoundException()
10431045
QUERY;
10441046

10451047
$this->expectException(\Exception::class);
1046-
$this->expectExceptionMessage('GraphQL response contains errors: currentPage value 2 specified is greater ' .
1047-
'than the 1 page(s) available.');
1048+
$this->expectExceptionMessage(
1049+
'GraphQL response contains errors: currentPage value 2 specified is greater ' .
1050+
'than the 1 page(s) available.'
1051+
);
10481052
$this->graphQlQuery($query);
10491053
}
10501054

@@ -1075,8 +1079,10 @@ public function testQueryWithNoSearchOrFilterArgumentException()
10751079
QUERY;
10761080

10771081
$this->expectException(\Exception::class);
1078-
$this->expectExceptionMessage('GraphQL response contains errors: \'search\' or \'filter\' input argument is ' .
1079-
'required.');
1082+
$this->expectExceptionMessage(
1083+
'GraphQL response contains errors: \'search\' or \'filter\' input argument is ' .
1084+
'required.'
1085+
);
10801086
$this->graphQlQuery($query);
10811087
}
10821088

@@ -1140,7 +1146,7 @@ public function testFilterProductsThatAreOutOfStockWithConfigSettings()
11401146
private function assertProductItems(array $filteredProducts, array $actualResponse)
11411147
{
11421148
$productItemsInResponse = array_map(null, $actualResponse['products']['items'], $filteredProducts);
1143-
1149+
// phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall
11441150
for ($itemIndex = 0; $itemIndex < count($filteredProducts); $itemIndex++) {
11451151
$this->assertNotEmpty($productItemsInResponse[$itemIndex]);
11461152
$this->assertResponseFields(

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/GenerateCustomerTokenTest.php

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
namespace Magento\GraphQl\Customer;
99

1010
use Magento\TestFramework\TestCase\GraphQlAbstract;
11-
use PHPUnit\Framework\TestResult;
1211

1312
/**
14-
* Class GenerateCustomerTokenTest
15-
* @package Magento\GraphQl\Customer
13+
* API-functional tests cases for generateCustomerToken mutation
1614
*/
1715
class GenerateCustomerTokenTest extends GraphQlAbstract
1816
{
@@ -23,87 +21,95 @@ class GenerateCustomerTokenTest extends GraphQlAbstract
2321
*/
2422
public function testGenerateCustomerValidToken()
2523
{
26-
$userName = 'customer@example.com';
24+
$email = 'customer@example.com';
2725
$password = 'password';
2826

29-
$mutation
30-
= <<<MUTATION
31-
mutation {
32-
generateCustomerToken(
33-
email: "{$userName}"
34-
password: "{$password}"
35-
) {
36-
token
37-
}
38-
}
39-
MUTATION;
27+
$mutation = $this->getQuery($email, $password);
4028

4129
$response = $this->graphQlMutation($mutation);
4230
$this->assertArrayHasKey('generateCustomerToken', $response);
4331
$this->assertInternalType('array', $response['generateCustomerToken']);
4432
}
4533

4634
/**
47-
* Verify customer with invalid credentials
35+
* Test customer with invalid data.
36+
*
37+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
38+
* @expectedException \Exception
39+
*
40+
* @dataProvider dataProviderInvalidCustomerInfo
41+
* @param string $email
42+
* @param string $password
43+
* @param string $message
4844
*/
49-
public function testGenerateCustomerTokenWithInvalidCredentials()
45+
public function testGenerateCustomerTokenInvalidData(string $email, string $password, string $message)
5046
{
51-
$userName = 'customer@example.com';
52-
$password = 'bad-password';
53-
54-
$mutation
55-
= <<<MUTATION
56-
mutation {
57-
generateCustomerToken(
58-
email: "{$userName}"
59-
password: "{$password}"
60-
) {
61-
token
62-
}
63-
}
64-
MUTATION;
65-
66-
$this->expectException(\Exception::class);
67-
$this->expectExceptionMessage('GraphQL response contains errors: The account sign-in' . ' ' .
68-
'was incorrect or your account is disabled temporarily. Please wait and try again later.');
47+
$mutation = $this->getQuery($email, $password);
48+
$this->expectExceptionMessage($message);
6949
$this->graphQlMutation($mutation);
7050
}
7151

7252
/**
73-
* Verify customer with empty email
53+
* Test customer token regeneration.
54+
*
55+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
7456
*/
75-
public function testGenerateCustomerTokenWithEmptyEmail()
57+
public function testRegenerateCustomerToken()
7658
{
77-
$email = '';
78-
$password = 'bad-password';
59+
$email = 'customer@example.com';
60+
$password = 'password';
7961

80-
$mutation
81-
= <<<MUTATION
82-
mutation {
83-
generateCustomerToken(
84-
email: "{$email}"
85-
password: "{$password}"
86-
) {
87-
token
88-
}
89-
}
90-
MUTATION;
62+
$mutation = $this->getQuery($email, $password);
9163

92-
$this->expectException(\Exception::class);
93-
$this->expectExceptionMessage('GraphQL response contains errors: Specify the "email" value.');
94-
$this->graphQlMutation($mutation);
64+
$response1 = $this->graphQlMutation($mutation);
65+
$token1 = $response1['generateCustomerToken']['token'];
66+
67+
$response2 = $this->graphQlMutation($mutation);
68+
$token2 = $response2['generateCustomerToken']['token'];
69+
70+
$this->assertNotEquals($token1, $token2, 'Tokens should not be identical!');
9571
}
9672

9773
/**
98-
* Verify customer with empty password
74+
* @return array
9975
*/
100-
public function testGenerateCustomerTokenWithEmptyPassword()
76+
public function dataProviderInvalidCustomerInfo(): array
10177
{
102-
$email = 'customer@example.com';
103-
$password = '';
78+
return [
79+
'invalid_email' => [
80+
'invalid_email@example.com',
81+
'password',
82+
'The account sign-in was incorrect or your account is disabled temporarily. ' .
83+
'Please wait and try again later.'
84+
],
85+
'empty_email' => [
86+
'',
87+
'password',
88+
'Specify the "email" value.'
89+
],
90+
'invalid_password' => [
91+
'customer@example.com',
92+
'invalid_password',
93+
'The account sign-in was incorrect or your account is disabled temporarily. ' .
94+
'Please wait and try again later.'
95+
],
96+
'empty_password' => [
97+
'customer@example.com',
98+
'',
99+
'Specify the "password" value.'
100+
101+
]
102+
];
103+
}
104104

105-
$mutation
106-
= <<<MUTATION
105+
/**
106+
* @param string $email
107+
* @param string $password
108+
* @return string
109+
*/
110+
private function getQuery(string $email, string $password) : string
111+
{
112+
return <<<MUTATION
107113
mutation {
108114
generateCustomerToken(
109115
email: "{$email}"
@@ -113,9 +119,5 @@ public function testGenerateCustomerTokenWithEmptyPassword()
113119
}
114120
}
115121
MUTATION;
116-
117-
$this->expectException(\Exception::class);
118-
$this->expectExceptionMessage('GraphQL response contains errors: Specify the "password" value.');
119-
$this->graphQlMutation($mutation);
120122
}
121123
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/IsEmailAvailableTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
use Magento\TestFramework\TestCase\GraphQlAbstract;
1111

12+
/**
13+
* Test email availability functionality
14+
*/
1215
class IsEmailAvailableTest extends GraphQlAbstract
1316
{
1417
/**
@@ -31,6 +34,9 @@ public function testEmailNotAvailable()
3134
self::assertFalse($response['isEmailAvailable']['is_email_available']);
3235
}
3336

37+
/**
38+
* Verify email availability
39+
*/
3440
public function testEmailAvailable()
3541
{
3642
$query =
@@ -47,4 +53,55 @@ public function testEmailAvailable()
4753
self::assertArrayHasKey('is_email_available', $response['isEmailAvailable']);
4854
self::assertTrue($response['isEmailAvailable']['is_email_available']);
4955
}
56+
57+
/**
58+
* @expectedException \Exception
59+
* @expectedExceptionMessage GraphQL response contains errors: Email must be specified
60+
*/
61+
public function testEmailAvailableEmptyValue()
62+
{
63+
$query =
64+
<<<QUERY
65+
{
66+
isEmailAvailable(email: "") {
67+
is_email_available
68+
}
69+
}
70+
QUERY;
71+
$this->graphQlQuery($query);
72+
}
73+
74+
/**
75+
* @expectedException \Exception
76+
* @expectedExceptionMessage Field "isEmailAvailable" argument "email" of type "String!" is required
77+
*/
78+
public function testEmailAvailableMissingValue()
79+
{
80+
$query =
81+
<<<QUERY
82+
{
83+
isEmailAvailable {
84+
is_email_available
85+
}
86+
}
87+
QUERY;
88+
$this->graphQlQuery($query);
89+
}
90+
91+
/**
92+
* @expectedException \Exception
93+
* @expectedExceptionMessage GraphQL response contains errors: Email is invalid
94+
*/
95+
public function testEmailAvailableInvalidValue()
96+
{
97+
$query =
98+
<<<QUERY
99+
{
100+
isEmailAvailable(email: "invalid-email") {
101+
is_email_available
102+
}
103+
}
104+
QUERY;
105+
$this->graphQlQuery($query);
106+
}
50107
}

0 commit comments

Comments
 (0)