Skip to content

Commit fad16f0

Browse files
committed
678 Add Tests
Email Sender empty send product wich not visible in catalog
1 parent 2ec065d commit fad16f0

File tree

3 files changed

+182
-120
lines changed

3 files changed

+182
-120
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php

Lines changed: 106 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\GraphQl\SendFriend;
99

10+
use Magento\Catalog\Api\ProductRepositoryInterface;
1011
use Magento\SendFriend\Model\SendFriend;
1112
use Magento\SendFriend\Model\SendFriendFactory;
1213
use Magento\TestFramework\Helper\Bootstrap;
@@ -22,52 +23,32 @@ class SendFriendTest extends GraphQlAbstract
2223
* @var SendFriendFactory
2324
*/
2425
private $sendFriendFactory;
26+
/**
27+
* @var ProductRepositoryInterface
28+
*/
29+
private $productRepository;
2530

2631
protected function setUp()
2732
{
2833
$this->sendFriendFactory = Bootstrap::getObjectManager()->get(SendFriendFactory::class);
34+
$this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
2935
}
3036

3137
/**
32-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
38+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
3339
*/
3440
public function testSendFriend()
3541
{
36-
$query =
37-
<<<QUERY
38-
mutation {
39-
sendEmailToFriend(
40-
input: {
41-
product_id: 1
42-
sender: {
43-
name: "Name"
44-
email: "e@mail.com"
45-
message: "Lorem Ipsum"
46-
}
47-
recipients: [
48-
{
42+
$productId = (int)$this->productRepository->get('simple_product')->getId();
43+
$recipients = '{
4944
name: "Recipient Name 1"
5045
email:"recipient1@mail.com"
5146
},
5247
{
5348
name: "Recipient Name 2"
5449
email:"recipient2@mail.com"
55-
}
56-
]
57-
}
58-
) {
59-
sender {
60-
name
61-
email
62-
message
63-
}
64-
recipients {
65-
name
66-
email
67-
}
68-
}
69-
}
70-
QUERY;
50+
}';
51+
$query = $this->getQuery($productId, $recipients);
7152

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

8263
public function testSendWithoutExistProduct()
8364
{
84-
$query =
85-
<<<QUERY
86-
mutation {
87-
sendEmailToFriend(
88-
input: {
89-
product_id: 2018
90-
sender: {
91-
name: "Name"
92-
email: "e@mail.com"
93-
message: "Lorem Ipsum"
94-
}
95-
recipients: [
96-
{
65+
$productId = 2018;
66+
$recipients = '{
9767
name: "Recipient Name 1"
9868
email:"recipient1@mail.com"
9969
},
10070
{
10171
name: "Recipient Name 2"
10272
email:"recipient2@mail.com"
103-
}
104-
]
105-
}
106-
) {
107-
sender {
108-
name
109-
email
110-
message
111-
}
112-
recipients {
113-
name
114-
email
115-
}
116-
}
117-
}
118-
QUERY;
73+
}';
74+
$query = $this->getQuery($productId, $recipients);
75+
11976
$this->expectException(\Exception::class);
12077
$this->expectExceptionMessage(
12178
'The product that was requested doesn\'t exist. Verify the product and try again.'
@@ -124,26 +81,15 @@ public function testSendWithoutExistProduct()
12481
}
12582

12683
/**
127-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
84+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
12885
*/
12986
public function testMaxSendEmailToFriend()
13087
{
13188
/** @var SendFriend $sendFriend */
13289
$sendFriend = $this->sendFriendFactory->create();
13390

134-
$query =
135-
<<<QUERY
136-
mutation {
137-
sendEmailToFriend(
138-
input: {
139-
product_id: 1
140-
sender: {
141-
name: "Name"
142-
email: "e@mail.com"
143-
message: "Lorem Ipsum"
144-
}
145-
recipients: [
146-
{
91+
$productId = (int)$this->productRepository->get('simple_product')->getId();
92+
$recipients = '{
14793
name: "Recipient Name 1"
14894
email:"recipient1@mail.com"
14995
},
@@ -166,22 +112,10 @@ public function testMaxSendEmailToFriend()
166112
{
167113
name: "Recipient Name 1"
168114
email:"recipient1@mail.com"
169-
}
170-
]
171-
}
172-
) {
173-
sender {
174-
name
175-
email
176-
message
177-
}
178-
recipients {
179-
name
180-
email
181-
}
182-
}
183-
}
184-
QUERY;
115+
}';
116+
117+
$query = $this->getQuery($productId, $recipients);
118+
185119
$this->expectException(\Exception::class);
186120
$this->expectExceptionMessage("No more than {$sendFriend->getMaxRecipients()} emails can be sent at a time.");
187121
$this->graphQlMutation($query);
@@ -221,7 +155,7 @@ public function testErrors(string $input, string $errorMessage)
221155
}
222156

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

234-
$query =
235-
<<<QUERY
236-
mutation {
237-
sendEmailToFriend(
238-
input: {
239-
product_id: 1
240-
sender: {
241-
name: "Name"
242-
email: "e@mail.com"
243-
message: "Lorem Ipsum"
244-
}
245-
recipients: [
246-
{
168+
$productId = (int)$this->productRepository->get('simple_product')->getId();
169+
$recipients = '{
247170
name: "Recipient Name 1"
248171
email:"recipient1@mail.com"
249172
},
250-
{
173+
{
251174
name: "Recipient Name 2"
252175
email:"recipient2@mail.com"
253-
}
176+
}';
177+
$query = $this->getQuery($productId, $recipients);
254178

255-
]
256-
}
257-
) {
258-
sender {
259-
name
260-
email
261-
message
262-
}
263-
recipients {
264-
name
265-
email
266-
}
267-
}
268-
}
269-
QUERY;
270179
$this->expectException(\Exception::class);
271180
$this->expectExceptionMessage(
272181
"You can't send messages more than {$sendFriend->getMaxSendsToFriend()} times an hour."
@@ -278,6 +187,49 @@ public function testLimitMessagesPerHour()
278187
}
279188
}
280189

190+
/**
191+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
192+
*/
193+
public function testSendProductWithoutSenderEmail()
194+
{
195+
$productId = (int)$this->productRepository->get('simple_product')->getId();
196+
$recipients = '{
197+
name: "Recipient Name 1"
198+
email:""
199+
}';
200+
$query = $this->getQuery($productId, $recipients);
201+
202+
$this->expectException(\Exception::class);
203+
$this->expectExceptionMessage('GraphQL response contains errors: Please provide Email for all of recipients.');
204+
$this->graphQlMutation($query);
205+
}
206+
207+
/**
208+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product_without_visibility.php
209+
*/
210+
public function testSendProductWithoutVisibility()
211+
{
212+
$productId = (int)$this->productRepository->get('simple_product_without_visibility')->getId();
213+
$recipients = '{
214+
name: "Recipient Name 1"
215+
email:"recipient1@mail.com"
216+
},
217+
{
218+
name: "Recipient Name 2"
219+
email:"recipient2@mail.com"
220+
}';
221+
$query = $this->getQuery($productId, $recipients);
222+
223+
$response = $this->graphQlMutation($query);
224+
self::assertEquals('Name', $response['sendEmailToFriend']['sender']['name']);
225+
self::assertEquals('e@mail.com', $response['sendEmailToFriend']['sender']['email']);
226+
self::assertEquals('Lorem Ipsum', $response['sendEmailToFriend']['sender']['message']);
227+
self::assertEquals('Recipient Name 1', $response['sendEmailToFriend']['recipients'][0]['name']);
228+
self::assertEquals('recipient1@mail.com', $response['sendEmailToFriend']['recipients'][0]['email']);
229+
self::assertEquals('Recipient Name 2', $response['sendEmailToFriend']['recipients'][1]['name']);
230+
self::assertEquals('recipient2@mail.com', $response['sendEmailToFriend']['recipients'][1]['email']);
231+
}
232+
281233
/**
282234
* @return array
283235
*/
@@ -358,4 +310,38 @@ public function sendFriendsErrorsDataProvider()
358310
]
359311
];
360312
}
313+
314+
/**
315+
* @param int $productId
316+
* @param string $recipients
317+
* @return string
318+
*/
319+
private function getQuery(int $productId, string $recipients): string
320+
{
321+
return <<<QUERY
322+
mutation {
323+
sendEmailToFriend(
324+
input: {
325+
product_id: {$productId}
326+
sender: {
327+
name: "Name"
328+
email: "e@mail.com"
329+
message: "Lorem Ipsum"
330+
}
331+
recipients: [{$recipients}]
332+
}
333+
) {
334+
sender {
335+
name
336+
email
337+
message
338+
}
339+
recipients {
340+
name
341+
email
342+
}
343+
}
344+
}
345+
QUERY;
346+
}
361347
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
12+
use Magento\Catalog\Model\Product\Type;
13+
use Magento\Catalog\Model\Product\Visibility;
14+
use Magento\Framework\Api\DataObjectHelper;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
17+
$objectManager = Bootstrap::getObjectManager();
18+
/** @var ProductInterfaceFactory $productFactory */
19+
$productFactory = $objectManager->get(ProductInterfaceFactory::class);
20+
/** @var DataObjectHelper $dataObjectHelper */
21+
$dataObjectHelper = Bootstrap::getObjectManager()->get(DataObjectHelper::class);
22+
/** @var ProductRepositoryInterface $productRepository */
23+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
24+
25+
$product = $productFactory->create();
26+
$productData = [
27+
ProductInterface::TYPE_ID => Type::TYPE_SIMPLE,
28+
ProductInterface::ATTRIBUTE_SET_ID => 4,
29+
ProductInterface::SKU => 'simple_product_without_visibility',
30+
ProductInterface::NAME => 'Simple Product Not Visible',
31+
ProductInterface::PRICE => 10,
32+
ProductInterface::VISIBILITY => Visibility::VISIBILITY_NOT_VISIBLE,
33+
ProductInterface::STATUS => Status::STATUS_ENABLED,
34+
];
35+
$dataObjectHelper->populateWithArray($product, $productData, ProductInterface::class);
36+
/** Out of interface */
37+
$product
38+
->setWebsiteIds([1])
39+
->setStockData([
40+
'qty' => 85.5,
41+
'is_in_stock' => true,
42+
'manage_stock' => true,
43+
'is_qty_decimal' => true
44+
]);
45+
$productRepository->save($product);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\Framework\Registry;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
12+
$objectManager = Bootstrap::getObjectManager();
13+
/** @var ProductRepositoryInterface $productRepository */
14+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
15+
/** @var Registry $registry */
16+
$registry = $objectManager->get(Registry::class);
17+
18+
$currentArea = $registry->registry('isSecureArea');
19+
$registry->unregister('isSecureArea');
20+
$registry->register('isSecureArea', true);
21+
22+
try {
23+
$productRepository->deleteById('simple_product_without_visibility');
24+
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
25+
/**
26+
* Tests which are wrapped with MySQL transaction clear all data by transaction rollback.
27+
*/
28+
}
29+
30+
$registry->unregister('isSecureArea');
31+
$registry->register('isSecureArea', $currentArea);

0 commit comments

Comments
 (0)