Skip to content

Commit

Permalink
Merge pull request #5172 from magento-engcom/2.4-develop-forward-port-2
Browse files Browse the repository at this point in the history
[Magento Community Engineering] Community Contributions - GraphQL - 2.4-develop forward-port
  • Loading branch information
lenaorobei authored Jan 6, 2020
2 parents 3e23510 + 1690aae commit 87b0a4b
Show file tree
Hide file tree
Showing 14 changed files with 673 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
namespace Magento\CustomerGraphQl\Model\Customer;

use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\Validator\EmailAddress as EmailAddressValidator;

/**
* Class ValidateCustomerData
* Customer data validation used during customer account creation and updating
*/
class ValidateCustomerData
{
Expand All @@ -21,14 +22,23 @@ class ValidateCustomerData
*/
private $getAllowedCustomerAttributes;

/**
* @var EmailAddressValidator
*/
private $emailAddressValidator;

/**
* ValidateCustomerData constructor.
*
* @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes
* @param EmailAddressValidator $emailAddressValidator
*/
public function __construct(GetAllowedCustomerAttributes $getAllowedCustomerAttributes)
{
public function __construct(
GetAllowedCustomerAttributes $getAllowedCustomerAttributes,
EmailAddressValidator $emailAddressValidator
) {
$this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes;
$this->emailAddressValidator = $emailAddressValidator;
}

/**
Expand Down Expand Up @@ -59,5 +69,11 @@ public function execute(array $customerData): void
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
);
}

if (isset($customerData['email']) && !$this->emailAddressValidator->isValid($customerData['email'])) {
throw new GraphQlInputException(
__('"%1" is not a valid email address.', $customerData['email'])
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\SwatchesGraphQl\Model\Resolver\Product\Options\DataProvider;

use Magento\Swatches\Helper\Data as SwatchData;
use Magento\Swatches\Helper\Media as SwatchesMedia;
use Magento\Swatches\Model\Swatch;

/**
* Data provider for options swatches.
*/
class SwatchDataProvider
{
/**
* @var SwatchData
*/
private $swatchHelper;

/**
* @var SwatchesMedia
*/
private $swatchMediaHelper;

/**
* SwatchDataProvider constructor.
*
* @param SwatchData $swatchHelper
* @param SwatchesMedia $swatchMediaHelper
*/
public function __construct(
SwatchData $swatchHelper,
SwatchesMedia $swatchMediaHelper
) {
$this->swatchHelper = $swatchHelper;
$this->swatchMediaHelper = $swatchMediaHelper;
}

/**
* Returns swatch data by option ID.
*
* @param string $optionId
* @return array|null
*/
public function getData(string $optionId): ?array
{
$swatches = $this->swatchHelper->getSwatchesByOptionsId([$optionId]);
if (!isset($swatches[$optionId]['type'], $swatches[$optionId]['value'])) {
return null;
}
$type = (int)$swatches[$optionId]['type'];
$value = $swatches[$optionId]['value'];
$data = ['value' => $value, 'type' => $type];
if ($type === Swatch::SWATCH_TYPE_VISUAL_IMAGE) {
$data['thumbnail'] = $this->swatchMediaHelper->getSwatchAttributeImage(
Swatch::SWATCH_THUMBNAIL_NAME,
$value
);
}
return $data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\SwatchesGraphQl\Model\Resolver\Product\Options;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\SwatchesGraphQl\Model\Resolver\Product\Options\DataProvider\SwatchDataProvider;

/**
* Class SwatchData
*
* Product swatch data resolver, used for GraphQL request processing
*/
class SwatchData implements ResolverInterface
{
/**
* @var SwatchDataProvider
*/
private $swatchDataProvider;

/**
* SwatchData constructor.
*
* @param SwatchDataProvider $swatchDataProvider
*/
public function __construct(
SwatchDataProvider $swatchDataProvider
) {
$this->swatchDataProvider = $swatchDataProvider;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
return $this->swatchDataProvider->getData($value['value_index']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\SwatchesGraphQl\Model\Resolver\Product\Options;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;
use Magento\Swatches\Model\Swatch;

/**
* Resolver for swatch data interface.
*/
class SwatchDataTypeResolver implements TypeResolverInterface
{
/**
* @inheritdoc
*/
public function resolveType(array $data): string
{
switch ($data['type']) {
case Swatch::SWATCH_TYPE_TEXTUAL:
return 'TextSwatchData';
case Swatch::SWATCH_TYPE_VISUAL_COLOR:
return 'ColorSwatchData';
case Swatch::SWATCH_TYPE_VISUAL_IMAGE:
return 'ImageSwatchData';
default:
throw new LocalizedException(__('Unsupported swatch type'));
}
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/SwatchesGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
</argument>
</arguments>
</type>
</config>
</config>
22 changes: 21 additions & 1 deletion app/code/Magento/SwatchesGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,24 @@ type SwatchLayerFilterItem implements LayerFilterItemInterface, SwatchLayerFilte
type SwatchData {
type: String @doc(description: "Type of swatch filter item: 1 - text, 2 - image")
value: String @doc(description: "Value for swatch item (text or image link)")
}
}

type ConfigurableProductOptionsValues {
swatch_data: SwatchDataInterface @doc(description: "Swatch data for configurable product option") @resolver(class: "Magento\\SwatchesGraphQl\\Model\\Resolver\\Product\\Options\\SwatchData")
}

interface SwatchDataInterface @typeResolver(class: "Magento\\SwatchesGraphQl\\Model\\Resolver\\Product\\Options\\SwatchDataTypeResolver") {
value: String @doc(description: "Value of swatch item (HEX color code, image link or textual value)")
}

type ImageSwatchData implements SwatchDataInterface {
thumbnail: String @doc(description: "Thumbnail swatch image URL")
}

type TextSwatchData implements SwatchDataInterface {

}

type ColorSwatchData implements SwatchDataInterface {

}
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,25 @@ public function testCreateCustomerIfEmailMissed()
}

/**
* @expectedException \Exception
* @expectedExceptionMessage "Email" is not a valid email address.
* @dataProvider invalidEmailAddressDataProvider
*
* @param string $email
* @throws \Exception
*/
public function testCreateCustomerIfEmailIsNotValid()
public function testCreateCustomerIfEmailIsNotValid(string $email)
{
$newFirstname = 'Richard';
$newLastname = 'Rowe';
$currentPassword = 'test123#';
$newEmail = 'email';
$firstname = 'Richard';
$lastname = 'Rowe';
$password = 'test123#';

$query = <<<QUERY
mutation {
createCustomer(
input: {
firstname: "{$newFirstname}"
lastname: "{$newLastname}"
email: "{$newEmail}"
password: "{$currentPassword}"
firstname: "{$firstname}"
lastname: "{$lastname}"
email: "{$email}"
password: "{$password}"
is_subscribed: true
}
) {
Expand All @@ -203,9 +204,29 @@ public function testCreateCustomerIfEmailIsNotValid()
}
}
QUERY;
$this->expectExceptionMessage('"' . $email . '" is not a valid email address.');
$this->graphQlMutation($query);
}

/**
* @return array
*/
public function invalidEmailAddressDataProvider(): array
{
return [
['plainaddress'],
['[email protected]'],
['#@%^%#$@#$@#.com'],
['@example.com'],
['Joe Smith <[email protected]>'],
['email.example.com'],
['email@[email protected]'],
['[email protected] (Joe Smith)'],
['email@example'],
['“email”@example.com'],
];
}

/**
* @expectedException \Exception
* @expectedExceptionMessage Field "test123" is not defined by type CustomerInput.
Expand Down
Loading

0 comments on commit 87b0a4b

Please sign in to comment.