-
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.
LYNX-86: Move functionality from ContactGraphQlPwa to ContractGraphQl…
… (new module) (#86)
- Loading branch information
Showing
10 changed files
with
384 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
app/code/Magento/ContactGraphQl/Model/ContactUsValidator.php
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ContactGraphQl\Model; | ||
|
||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\Validator\EmailAddress; | ||
|
||
class ContactUsValidator | ||
{ | ||
/** | ||
* @var EmailAddress | ||
*/ | ||
private EmailAddress $emailValidator; | ||
|
||
/** | ||
* @param EmailAddress $emailValidator | ||
*/ | ||
public function __construct( | ||
EmailAddress $emailValidator | ||
) { | ||
$this->emailValidator = $emailValidator; | ||
} | ||
|
||
/** | ||
* Validate input data | ||
* | ||
* @param string[] $input | ||
* @return void | ||
* @throws GraphQlInputException | ||
*/ | ||
public function execute(array $input): void | ||
{ | ||
if (!$this->emailValidator->isValid($input['email'])) { | ||
throw new GraphQlInputException( | ||
__('The email address is invalid. Verify the email address and try again.') | ||
); | ||
} | ||
|
||
if ($input['name'] === '') { | ||
throw new GraphQlInputException(__('Name field is required.')); | ||
} | ||
|
||
if ($input['comment'] === '') { | ||
throw new GraphQlInputException(__('Comment field is required.')); | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
app/code/Magento/ContactGraphQl/Model/Resolver/ContactUs.php
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\ContactGraphQl\Model\Resolver; | ||
|
||
use Magento\Contact\Model\ConfigInterface; | ||
use Magento\Contact\Model\MailInterface; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Psr\Log\LoggerInterface; | ||
use Magento\ContactGraphQl\Model\ContactUsValidator; | ||
|
||
class ContactUs implements ResolverInterface | ||
{ | ||
/** | ||
* @var MailInterface | ||
*/ | ||
private MailInterface $mail; | ||
|
||
/** | ||
* @var ConfigInterface | ||
*/ | ||
private ConfigInterface $contactConfig; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private LoggerInterface $logger; | ||
|
||
/** | ||
* @var ContactUsValidator | ||
*/ | ||
private ContactUsValidator $validator; | ||
|
||
/** | ||
* @param MailInterface $mail | ||
* @param ConfigInterface $contactConfig | ||
* @param LoggerInterface $logger | ||
* @param ContactUsValidator $validator | ||
*/ | ||
public function __construct( | ||
MailInterface $mail, | ||
ConfigInterface $contactConfig, | ||
LoggerInterface $logger, | ||
ContactUsValidator $validator | ||
) { | ||
$this->mail = $mail; | ||
$this->contactConfig = $contactConfig; | ||
$this->logger = $logger; | ||
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) { | ||
if (!$this->contactConfig->isEnabled()) { | ||
throw new GraphQlInputException( | ||
__('The contact form is unavailable.') | ||
); | ||
} | ||
|
||
$input = array_map(function ($field) { | ||
return $field === null ? '' : trim($field); | ||
}, $args['input']); | ||
$this->validator->execute($input); | ||
|
||
try { | ||
$this->mail->send($input['email'], ['data' => $input]); | ||
} catch (\Exception $e) { | ||
$this->logger->critical($e); | ||
throw new GraphQlInputException( | ||
__('An error occurred while processing your form. Please try again later.') | ||
); | ||
} | ||
|
||
return [ | ||
'status' => true | ||
]; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# ContactGraphQlPwa | ||
|
||
**ContactGraphQlPwa** provides GraphQL support for `magento/module-contact`. |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "magento/module-contact-graph-ql", | ||
"description": "N/A", | ||
"type": "magento2-module", | ||
"config": { | ||
"sort-packages": true | ||
}, | ||
"require": { | ||
"php": "~8.1.0||~8.2.0", | ||
"magento/framework": "*", | ||
"magento/module-contact": "*" | ||
}, | ||
"suggest": { | ||
"magento/module-graph-ql": "*" | ||
}, | ||
"license": [ | ||
"OSL-3.0", | ||
"AFL-3.0" | ||
], | ||
"autoload": { | ||
"files": [ | ||
"registration.php" | ||
], | ||
"psr-4": { | ||
"Magento\\ContactGraphQl\\": "" | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<type name="Magento\StoreGraphQl\Model\Resolver\Store\StoreConfigDataProvider"> | ||
<arguments> | ||
<argument name="extendedConfigData" xsi:type="array"> | ||
<item name="contact_enabled" xsi:type="string">contact/contact/enabled</item> | ||
</argument> | ||
</arguments> | ||
</type> | ||
</config> |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="Magento_ContactGraphQl" /> | ||
</config> |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright © Magento, Inc. All rights reserved. | ||
# See COPYING.txt for license details. | ||
|
||
type Mutation { | ||
contactUs( | ||
input: ContactUsInput! @doc(description: "An input object that defines shopper information.") | ||
): ContactUsOutput @doc(description: "Send a 'Contact Us' email to the merchant.") @resolver(class: "Magento\\ContactGraphQl\\Model\\Resolver\\ContactUs") | ||
} | ||
|
||
input ContactUsInput { | ||
email: String! @doc(description: "The email address of the shopper.") | ||
name: String! @doc(description: "The full name of the shopper.") | ||
telephone: String @doc(description: "The shopper's telephone number.") | ||
comment: String! @doc(description: "The shopper's comment to the merchant.") | ||
} | ||
|
||
type ContactUsOutput @doc(description: "Contains the status of the request."){ | ||
status: Boolean! @doc(description: "Indicates whether the request was successful.") | ||
} | ||
|
||
type StoreConfig { | ||
contact_enabled: Boolean! @doc(description: "Indicates whether the Contact Us form in enabled.") | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
use Magento\Framework\Component\ComponentRegistrar; | ||
|
||
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_ContactGraphQl', __DIR__); |
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
146 changes: 146 additions & 0 deletions
146
dev/tests/api-functional/testsuite/Magento/GraphQl/ContactUs/ContactUsTest.php
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 |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl\ContactUs; | ||
|
||
use Magento\TestFramework\Fixture\Config; | ||
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
|
||
#[ | ||
Config("contact/contact/enabled", "1") | ||
] | ||
class ContactUsTest extends GraphQlAbstract | ||
{ | ||
/** | ||
* Successfuly send contact us form | ||
*/ | ||
public function testContactUsSuccess() | ||
{ | ||
$query = <<<MUTATION | ||
mutation { | ||
contactUs(input: { | ||
comment:"Test Contact Us", | ||
email:"test@adobe.com", | ||
name:"John Doe", | ||
telephone:"1111111111" | ||
}) | ||
{ | ||
status | ||
} | ||
} | ||
MUTATION; | ||
|
||
$expected = [ | ||
"contactUs" => [ | ||
"status" => true | ||
] | ||
]; | ||
$response = $this->graphQlMutation($query, [], '', []); | ||
$this->assertEquals($expected, $response, "Contact Us form can not be send"); | ||
} | ||
|
||
/** | ||
* Failed send contact us form - missing email | ||
*/ | ||
public function testContactUsBadEmail() | ||
{ | ||
$query = <<<MUTATION | ||
mutation { | ||
contactUs(input: { | ||
comment:"Test Contact Us", | ||
name:"John Doe", | ||
email:"adobe.com", | ||
telephone:"1111111111" | ||
}) | ||
{ | ||
status | ||
} | ||
} | ||
MUTATION; | ||
$this->expectException(ResponseContainsErrorsException::class); | ||
$this->expectExceptionMessage( | ||
'GraphQL response contains errors: The email address is invalid. Verify the email address and try again.' | ||
); | ||
$this->graphQlMutation($query, [], '', []); | ||
} | ||
|
||
/** | ||
* Failed send contact us form - missing name | ||
*/ | ||
public function testContactUsMissingName() | ||
{ | ||
$query = <<<MUTATION | ||
mutation { | ||
contactUs(input: { | ||
comment:"Test Contact Us", | ||
email:"test@adobe.com", | ||
telephone:"1111111111" | ||
}) | ||
{ | ||
status | ||
} | ||
} | ||
MUTATION; | ||
$this->expectException(ResponseContainsErrorsException::class); | ||
$this->expectExceptionMessage( | ||
'GraphQL response contains errors: Field ContactUsInput.name of required type String! was not provided.' | ||
); | ||
$this->graphQlMutation($query, [], '', []); | ||
} | ||
|
||
/** | ||
* Failed send contact us form - missing name | ||
*/ | ||
public function testContactUsMissingComment() | ||
{ | ||
$query = <<<MUTATION | ||
mutation { | ||
contactUs(input: { | ||
email:"test@adobe.com", | ||
name:"John Doe", | ||
telephone:"1111111111" | ||
}) | ||
{ | ||
status | ||
} | ||
} | ||
MUTATION; | ||
$this->expectException(ResponseContainsErrorsException::class); | ||
$this->expectExceptionMessage( | ||
'GraphQL response contains errors: Field ContactUsInput.comment of required type String! was not provided.' | ||
); | ||
$this->graphQlMutation($query, [], '', []); | ||
} | ||
|
||
/** | ||
* Failed send contact us form - missing name | ||
*/ | ||
#[ | ||
Config("contact/contact/enabled", "0") | ||
] | ||
public function testContactUsDisabled() | ||
{ | ||
$query = <<<MUTATION | ||
mutation { | ||
contactUs(input: { | ||
comment:"Test Contact Us", | ||
email:"test@adobe.com", | ||
name:"John Doe", | ||
telephone:"1111111111" | ||
}) | ||
{ | ||
status | ||
} | ||
} | ||
MUTATION; | ||
|
||
$this->expectException(ResponseContainsErrorsException::class); | ||
$this->expectExceptionMessage('GraphQL response contains errors: The contact form is unavailable.'); | ||
$this->graphQlMutation($query, [], '', []); | ||
} | ||
} |