Skip to content

Commit 6bb8952

Browse files
authored
ENGCOM-4226: [Backport] [SendFriend] Covering the Send to friend by integration tests #21156
2 parents 2fcf9b7 + da02bbf commit 6bb8952

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
namespace Magento\SendFriend\Controller;
9+
10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Customer\Model\Session;
13+
use Magento\Framework\Data\Form\FormKey;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use Magento\Framework\Message\MessageInterface;
16+
use Magento\TestFramework\Request;
17+
use Magento\TestFramework\TestCase\AbstractController;
18+
19+
/**
20+
* Class SendmailTest
21+
*/
22+
class SendmailTest extends AbstractController
23+
{
24+
/**
25+
* Share the product to friend as logged in customer
26+
*
27+
* @magentoDbIsolation enabled
28+
* @magentoAppIsolation enabled
29+
* @magentoDataFixture Magento/SendFriend/_files/disable_allow_guest_config.php
30+
* @magentoDataFixture Magento/Customer/_files/customer.php
31+
* @magentoDataFixture Magento/Catalog/_files/products.php
32+
*/
33+
public function testSendActionAsLoggedIn()
34+
{
35+
$product = $this->getProduct();
36+
$this->login(1);
37+
$this->prepareRequestData();
38+
39+
$this->dispatch('sendfriend/product/sendmail/id/' . $product->getId());
40+
$this->assertSessionMessages(
41+
$this->equalTo(['The link to a friend was sent.']),
42+
MessageInterface::TYPE_SUCCESS
43+
);
44+
}
45+
46+
/**
47+
* Share the product to friend as guest customer
48+
*
49+
* @magentoDbIsolation enabled
50+
* @magentoAppIsolation enabled
51+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
52+
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
53+
* @magentoDataFixture Magento/Catalog/_files/products.php
54+
*/
55+
public function testSendActionAsGuest()
56+
{
57+
$product = $this->getProduct();
58+
$this->prepareRequestData();
59+
60+
$this->dispatch('sendfriend/product/sendmail/id/' . $product->getId());
61+
$this->assertSessionMessages(
62+
$this->equalTo(['The link to a friend was sent.']),
63+
MessageInterface::TYPE_SUCCESS
64+
);
65+
}
66+
67+
/**
68+
* Share the product to friend as guest customer with invalid post data
69+
*
70+
* @magentoDbIsolation enabled
71+
* @magentoAppIsolation enabled
72+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
73+
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
74+
* @magentoDataFixture Magento/Catalog/_files/products.php
75+
*/
76+
public function testSendActionAsGuestWithInvalidData()
77+
{
78+
$product = $this->getProduct();
79+
$this->prepareRequestData(true);
80+
81+
$this->dispatch('sendfriend/product/sendmail/id/' . $product->getId());
82+
$this->assertSessionMessages(
83+
$this->equalTo(['Invalid Sender Email']),
84+
MessageInterface::TYPE_ERROR
85+
);
86+
}
87+
88+
/**
89+
* @return ProductInterface
90+
*/
91+
private function getProduct()
92+
{
93+
return $this->_objectManager->get(ProductRepositoryInterface::class)->get('custom-design-simple-product');
94+
}
95+
96+
/**
97+
* Login the user
98+
*
99+
* @param string $customerId Customer to mark as logged in for the session
100+
* @return void
101+
*/
102+
protected function login($customerId)
103+
{
104+
/** @var Session $session */
105+
$session = Bootstrap::getObjectManager()
106+
->get(Session::class);
107+
$session->loginById($customerId);
108+
}
109+
110+
/**
111+
* @param bool $invalidData
112+
* @return void
113+
*/
114+
private function prepareRequestData($invalidData = false)
115+
{
116+
/** @var FormKey $formKey */
117+
$formKey = $this->_objectManager->get(FormKey::class);
118+
$post = [
119+
'sender' => [
120+
'name' => 'Test',
121+
'email' => '[email protected]',
122+
'message' => 'Message',
123+
],
124+
'recipients' => [
125+
'name' => [
126+
'Recipient 1',
127+
'Recipient 2'
128+
],
129+
'email' => [
130+
131+
132+
]
133+
],
134+
'form_key' => $formKey->getFormKey(),
135+
];
136+
if ($invalidData) {
137+
unset($post['sender']['email']);
138+
}
139+
140+
$this->getRequest()->setMethod(Request::METHOD_POST);
141+
$this->getRequest()->setPostValue($post);
142+
}
143+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Framework\App\Config\Value;
8+
use Magento\TestFramework\Helper\Bootstrap;
9+
10+
/** @var Value $config */
11+
$config = Bootstrap::getObjectManager()->create(Value::class);
12+
$config->setPath('sendfriend/email/enabled');
13+
$config->setScope('default');
14+
$config->setScopeId(0);
15+
$config->setValue(1);
16+
$config->save();
17+
18+
/** @var Value $config */
19+
$config = Bootstrap::getObjectManager()->create(Value::class);
20+
$config->setPath('sendfriend/email/allow_guest');
21+
$config->setScope('default');
22+
$config->setScopeId(0);
23+
$config->setValue(0);
24+
$config->save();

0 commit comments

Comments
 (0)