Skip to content

Commit

Permalink
ENGCOM-5097: 678 Add Tests Email Sender empty send product wich not v…
Browse files Browse the repository at this point in the history
…isible in catalog magento#696
  • Loading branch information
lenaorobei authored May 23, 2019
2 parents a9bbba7 + 918ca45 commit e52226a
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\GraphQl\SendFriend;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\SendFriend\Model\SendFriend;
use Magento\SendFriend\Model\SendFriendFactory;
use Magento\TestFramework\Helper\Bootstrap;
Expand All @@ -22,52 +23,32 @@ class SendFriendTest extends GraphQlAbstract
* @var SendFriendFactory
*/
private $sendFriendFactory;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;

protected function setUp()
{
$this->sendFriendFactory = Bootstrap::getObjectManager()->get(SendFriendFactory::class);
$this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
*/
public function testSendFriend()
{
$query =
<<<QUERY
mutation {
sendEmailToFriend(
input: {
product_id: 1
sender: {
name: "Name"
email: "[email protected]"
message: "Lorem Ipsum"
}
recipients: [
{
$productId = (int)$this->productRepository->get('simple_product')->getId();
$recipients = '{
name: "Recipient Name 1"
email:"[email protected]"
},
{
name: "Recipient Name 2"
email:"[email protected]"
}
]
}
) {
sender {
name
email
message
}
recipients {
name
email
}
}
}
QUERY;
}';
$query = $this->getQuery($productId, $recipients);

$response = $this->graphQlMutation($query);
self::assertEquals('Name', $response['sendEmailToFriend']['sender']['name']);
Expand All @@ -81,41 +62,17 @@ public function testSendFriend()

public function testSendWithoutExistProduct()
{
$query =
<<<QUERY
mutation {
sendEmailToFriend(
input: {
product_id: 2018
sender: {
name: "Name"
email: "[email protected]"
message: "Lorem Ipsum"
}
recipients: [
{
$productId = 2018;
$recipients = '{
name: "Recipient Name 1"
email:"[email protected]"
},
{
name: "Recipient Name 2"
email:"[email protected]"
}
]
}
) {
sender {
name
email
message
}
recipients {
name
email
}
}
}
QUERY;
}';
$query = $this->getQuery($productId, $recipients);

$this->expectException(\Exception::class);
$this->expectExceptionMessage(
'The product that was requested doesn\'t exist. Verify the product and try again.'
Expand All @@ -124,26 +81,15 @@ public function testSendWithoutExistProduct()
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
*/
public function testMaxSendEmailToFriend()
{
/** @var SendFriend $sendFriend */
$sendFriend = $this->sendFriendFactory->create();

$query =
<<<QUERY
mutation {
sendEmailToFriend(
input: {
product_id: 1
sender: {
name: "Name"
email: "[email protected]"
message: "Lorem Ipsum"
}
recipients: [
{
$productId = (int)$this->productRepository->get('simple_product')->getId();
$recipients = '{
name: "Recipient Name 1"
email:"[email protected]"
},
Expand All @@ -166,22 +112,10 @@ public function testMaxSendEmailToFriend()
{
name: "Recipient Name 1"
email:"[email protected]"
}
]
}
) {
sender {
name
email
message
}
recipients {
name
email
}
}
}
QUERY;
}';

$query = $this->getQuery($productId, $recipients);

$this->expectException(\Exception::class);
$this->expectExceptionMessage("No more than {$sendFriend->getMaxRecipients()} emails can be sent at a time.");
$this->graphQlMutation($query);
Expand Down Expand Up @@ -221,7 +155,7 @@ public function testErrors(string $input, string $errorMessage)
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* TODO: use magentoApiConfigFixture (to be merged https://github.com/magento/graphql-ce/pull/351)
* @magentoApiDataFixture Magento/SendFriend/Fixtures/sendfriend_configuration.php
*/
Expand All @@ -231,42 +165,17 @@ public function testLimitMessagesPerHour()
/** @var SendFriend $sendFriend */
$sendFriend = $this->sendFriendFactory->create();

$query =
<<<QUERY
mutation {
sendEmailToFriend(
input: {
product_id: 1
sender: {
name: "Name"
email: "[email protected]"
message: "Lorem Ipsum"
}
recipients: [
{
$productId = (int)$this->productRepository->get('simple_product')->getId();
$recipients = '{
name: "Recipient Name 1"
email:"[email protected]"
},
{
{
name: "Recipient Name 2"
email:"[email protected]"
}
}';
$query = $this->getQuery($productId, $recipients);

]
}
) {
sender {
name
email
message
}
recipients {
name
email
}
}
}
QUERY;
$this->expectException(\Exception::class);
$this->expectExceptionMessage(
"You can't send messages more than {$sendFriend->getMaxSendsToFriend()} times an hour."
Expand All @@ -278,6 +187,49 @@ public function testLimitMessagesPerHour()
}
}

/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
*/
public function testSendProductWithoutSenderEmail()
{
$productId = (int)$this->productRepository->get('simple_product')->getId();
$recipients = '{
name: "Recipient Name 1"
email:""
}';
$query = $this->getQuery($productId, $recipients);

$this->expectException(\Exception::class);
$this->expectExceptionMessage('GraphQL response contains errors: Please provide Email for all of recipients.');
$this->graphQlMutation($query);
}

/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product_without_visibility.php
*/
public function testSendProductWithoutVisibility()
{
$productId = (int)$this->productRepository->get('simple_product_without_visibility')->getId();
$recipients = '{
name: "Recipient Name 1"
email:"[email protected]"
},
{
name: "Recipient Name 2"
email:"[email protected]"
}';
$query = $this->getQuery($productId, $recipients);

$response = $this->graphQlMutation($query);
self::assertEquals('Name', $response['sendEmailToFriend']['sender']['name']);
self::assertEquals('[email protected]', $response['sendEmailToFriend']['sender']['email']);
self::assertEquals('Lorem Ipsum', $response['sendEmailToFriend']['sender']['message']);
self::assertEquals('Recipient Name 1', $response['sendEmailToFriend']['recipients'][0]['name']);
self::assertEquals('[email protected]', $response['sendEmailToFriend']['recipients'][0]['email']);
self::assertEquals('Recipient Name 2', $response['sendEmailToFriend']['recipients'][1]['name']);
self::assertEquals('[email protected]', $response['sendEmailToFriend']['recipients'][1]['email']);
}

/**
* @return array
*/
Expand Down Expand Up @@ -358,4 +310,38 @@ public function sendFriendsErrorsDataProvider()
]
];
}

/**
* @param int $productId
* @param string $recipients
* @return string
*/
private function getQuery(int $productId, string $recipients): string
{
return <<<QUERY
mutation {
sendEmailToFriend(
input: {
product_id: {$productId}
sender: {
name: "Name"
email: "[email protected]"
message: "Lorem Ipsum"
}
recipients: [{$recipients}]
}
) {
sender {
name
email
message
}
recipients {
name
email
}
}
}
QUERY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Framework\Api\DataObjectHelper;
use Magento\TestFramework\Helper\Bootstrap;

$objectManager = Bootstrap::getObjectManager();
/** @var ProductInterfaceFactory $productFactory */
$productFactory = $objectManager->get(ProductInterfaceFactory::class);
/** @var DataObjectHelper $dataObjectHelper */
$dataObjectHelper = Bootstrap::getObjectManager()->get(DataObjectHelper::class);
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->get(ProductRepositoryInterface::class);

$product = $productFactory->create();
$productData = [
ProductInterface::TYPE_ID => Type::TYPE_SIMPLE,
ProductInterface::ATTRIBUTE_SET_ID => 4,
ProductInterface::SKU => 'simple_product_without_visibility',
ProductInterface::NAME => 'Simple Product Not Visible',
ProductInterface::PRICE => 10,
ProductInterface::VISIBILITY => Visibility::VISIBILITY_NOT_VISIBLE,
ProductInterface::STATUS => Status::STATUS_ENABLED,
];
$dataObjectHelper->populateWithArray($product, $productData, ProductInterface::class);
/** Out of interface */
$product
->setWebsiteIds([1])
->setStockData([
'qty' => 85.5,
'is_in_stock' => true,
'manage_stock' => true,
'is_qty_decimal' => true
]);
$productRepository->save($product);
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Registry;
use Magento\TestFramework\Helper\Bootstrap;

$objectManager = Bootstrap::getObjectManager();
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
/** @var Registry $registry */
$registry = $objectManager->get(Registry::class);

$currentArea = $registry->registry('isSecureArea');
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);

try {
$productRepository->deleteById('simple_product_without_visibility');
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
/**
* Tests which are wrapped with MySQL transaction clear all data by transaction rollback.
*/
}

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', $currentArea);

0 comments on commit e52226a

Please sign in to comment.