Skip to content

Commit

Permalink
magento#607: added validation on "cart_id" length and changed impleme…
Browse files Browse the repository at this point in the history
…ntation by using data provider for validation
  • Loading branch information
Harniuk Bohdan committed Apr 16, 2019
1 parent e346a92 commit 82c5536
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
Expand Up @@ -73,11 +73,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
$maskedQuoteId = $args['input']['cart_id'];

if ($quoteIdMask->load($maskedQuoteId, 'masked_id') && $quoteIdMask->getQuoteId()) {
throw new GraphQlAlreadyExistsException(__('Specified "cart_id" already exists'));
throw new GraphQlAlreadyExistsException(__('Specified parameter "cart_id" is non unique.'));
}

if (strlen($maskedQuoteId) > 32) {
throw new GraphQlInputException(__('Specified "cart_id" max size is 32'));
throw new GraphQlInputException(__('"cart_id" length have to be less than or equal to 32.'));
}
}

Expand Down
Expand Up @@ -10,6 +10,7 @@
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Quote\Api\GuestCartRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Math\Random as RandomDataGenerator;

/**
Expand Down Expand Up @@ -76,24 +77,37 @@ public function testCreateEmptyCartWithCartId()
self::assertNull($guestCart->getCustomer()->getId());
}

public function testCartIdNotUnique()
/**
* @dataProvider dataProviderValidateCreateEmptyCartWithSpecifiedCartId
* @param string $input
* @param string $message
* @throws \Exception
*/
public function testValidateCreateEmptyCartWithSpecifiedCartId(string $input, string $message)
{
$uniqueHash = $this->addEmptyCartWithCartId();
$input = str_replace('provide_non_unique_id', $this->addEmptyCartWithCartId(), $input);
$input = str_replace('provide_hash_with_prefix', $this->randomDataGenerator->getUniqueHash('prefix'), $input);

$query = <<<QUERY
mutation {
createEmptyCart(
input : {
cart_id : "$uniqueHash"
{$input}
}
)
}
QUERY;

$this->expectExceptionMessage("Specified \"cart_id\" already exists");
$this->expectExceptionMessage($message);
$this->graphQlQuery($query);
}

/**
* Return masked id for created empty cart.
*
* @return mixed
* @throws LocalizedException
*/
private function addEmptyCartWithCartId()
{
$uniqueHash = $this->randomDataGenerator->getUniqueHash();
Expand All @@ -111,4 +125,21 @@ private function addEmptyCartWithCartId()

return $response['createEmptyCart'];
}

/**
* @return array
*/
public function dataProviderValidateCreateEmptyCartWithSpecifiedCartId(): array
{
return [
'cart_id_unique_checking' => [
'cart_id: "provide_non_unique_id"',
'Specified parameter "cart_id" is non unique.'
],
'cart_id_length_checking' => [
'cart_id: "provide_hash_with_prefix"',
'"cart_id" length have to be less than or equal to 32.'
],
];
}
}

0 comments on commit 82c5536

Please sign in to comment.