Skip to content

Commit

Permalink
LYNX-86: Move functionality from ContactGraphQlPwa to ContractGraphQl…
Browse files Browse the repository at this point in the history
… (new module) (#86)
  • Loading branch information
bl4de authored Mar 9, 2023
1 parent 78f9844 commit c1a93c3
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 0 deletions.
52 changes: 52 additions & 0 deletions app/code/Magento/ContactGraphQl/Model/ContactUsValidator.php
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 app/code/Magento/ContactGraphQl/Model/Resolver/ContactUs.php
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
];
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/ContactGraphQl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ContactGraphQlPwa

**ContactGraphQlPwa** provides GraphQL support for `magento/module-contact`.
28 changes: 28 additions & 0 deletions app/code/Magento/ContactGraphQl/composer.json
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\\": ""
}
}
}
16 changes: 16 additions & 0 deletions app/code/Magento/ContactGraphQl/etc/graphql/di.xml
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>
10 changes: 10 additions & 0 deletions app/code/Magento/ContactGraphQl/etc/module.xml
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>
23 changes: 23 additions & 0 deletions app/code/Magento/ContactGraphQl/etc/schema.graphqls
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.")
}
10 changes: 10 additions & 0 deletions app/code/Magento/ContactGraphQl/registration.php
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__);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
"magento/module-configurable-product": "*",
"magento/module-configurable-product-sales": "*",
"magento/module-contact": "*",
"magento/module-contact-graph-ql": "*",
"magento/module-cookie": "*",
"magento/module-cron": "*",
"magento/module-currency-symbol": "*",
Expand Down
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, [], '', []);
}
}

0 comments on commit c1a93c3

Please sign in to comment.