Skip to content

Commit 04ec5ed

Browse files
committed
MC-32136: Email product to friend
1 parent 81bb39b commit 04ec5ed

File tree

8 files changed

+767
-112
lines changed

8 files changed

+767
-112
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\TestFramework\SendFriend\Model;
9+
10+
use Magento\SendFriend\Model\ResourceModel\SendFriend as SendFriendResource;
11+
12+
/**
13+
* Delete log rows by ip address
14+
*/
15+
class DeleteLogRowsByIp
16+
{
17+
/** @var SendFriendResource */
18+
private $sendFriendResource;
19+
20+
/**
21+
* @param SendFriendResource $sendFriendResource
22+
*/
23+
public function __construct(SendFriendResource $sendFriendResource)
24+
{
25+
$this->sendFriendResource = $sendFriendResource;
26+
}
27+
28+
/**
29+
* Delete rows from sendfriend_log table by ip address
30+
*
31+
* @param string $ipAddress
32+
* @return void
33+
*/
34+
public function execute(string $ipAddress): void
35+
{
36+
$connection = $this->sendFriendResource->getConnection();
37+
$condition = $connection->quoteInto('ip = ?', ip2long($ipAddress));
38+
$connection->delete($this->sendFriendResource->getMainTable(), $condition);
39+
}
40+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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\Block;
9+
10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Block\Product\View;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\Framework\Registry;
15+
use Magento\Framework\View\LayoutInterface;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* Class checks send friend link visibility
21+
*
22+
* @magentoAppArea frontend
23+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
24+
*/
25+
class ProductViewTest extends TestCase
26+
{
27+
/** @var ObjectManagerInterface */
28+
private $objectManager;
29+
30+
/** @var Registry */
31+
private $registry;
32+
33+
/** @var LayoutInterface */
34+
private $layout;
35+
36+
/** @var View */
37+
private $block;
38+
39+
/** @var ProductRepositoryInterface */
40+
private $productRepository;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp()
46+
{
47+
parent::setUp();
48+
49+
$this->objectManager = Bootstrap::getObjectManager();
50+
$this->layout = $this->objectManager->get(LayoutInterface::class);
51+
$this->block = $this->layout->createBlock(View::class);
52+
$this->block->setTemplate('Magento_Catalog::product/view/mailto.phtml');
53+
$this->registry = $this->objectManager->get(Registry::class);
54+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
55+
$this->productRepository->cleanCache();
56+
}
57+
58+
/**
59+
* @inheritdoc
60+
*/
61+
protected function tearDown()
62+
{
63+
parent::tearDown();
64+
65+
$this->registry->unregister('product');
66+
}
67+
68+
/**
69+
* @return void
70+
*/
71+
public function testSendFriendLinkDisabled(): void
72+
{
73+
$this->registerProduct('simple2');
74+
$this->assertEmpty($this->block->toHtml());
75+
}
76+
77+
/**
78+
* @magentoConfigFixture current_store sendfriend/email/enabled 1
79+
*
80+
* @return void
81+
*/
82+
public function testSendFriendLinkEnabled(): void
83+
{
84+
$product = $this->registerProduct('simple2');
85+
$html = $this->block->toHtml();
86+
$this->assertContains('sendfriend/product/send/id/' . $product->getId(), $html);
87+
$this->assertEquals('Email', trim(strip_tags($html)));
88+
}
89+
90+
/**
91+
* Register product by sku
92+
*
93+
* @param string $sku
94+
* @return ProductInterface
95+
*/
96+
private function registerProduct(string $sku): ProductInterface
97+
{
98+
$product = $this->productRepository->get($sku);
99+
$this->registry->unregister('product');
100+
$this->registry->register('product', $product);
101+
102+
return $product;
103+
}
104+
}

dev/tests/integration/testsuite/Magento/SendFriend/Block/SendTest.php

Lines changed: 97 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,94 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\SendFriend\Block;
79

10+
use Magento\Customer\Api\AccountManagementInterface;
11+
use Magento\Customer\Model\Session;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\Framework\View\LayoutInterface;
814
use Magento\TestFramework\Helper\Bootstrap;
15+
use Magento\TestFramework\Helper\Xpath;
16+
use PHPUnit\Framework\TestCase;
917

10-
class SendTest extends \PHPUnit\Framework\TestCase
18+
/**
19+
* Class checks send friend email block
20+
*
21+
* @see \Magento\SendFriend\Block\Send
22+
*
23+
* @magentoAppArea frontend
24+
*/
25+
class SendTest extends TestCase
1126
{
27+
/** @var array */
28+
private $elementsXpath = [
29+
'sender name field' => "//input[@name='sender[name]']",
30+
'sender email field' => "//input[@name='sender[email]']",
31+
'sender message field' => "//textarea[@name='sender[message]']",
32+
'recipient name field' => "//input[contains(@name, 'recipients[name]')]",
33+
'recipient email field' => "//input[contains(@name, 'recipients[email]')]",
34+
'submit button' => "//button[@type='submit']/span[contains(text(), 'Send Email')]",
35+
'notice massage' => "//div[@id='max-recipient-message']"
36+
. "/span[contains(text(), 'Maximum 1 email addresses allowed.')]"
37+
];
38+
39+
/** @var ObjectManagerInterface */
40+
private $objectManager;
41+
42+
/** @var LayoutInterface */
43+
private $layout;
44+
45+
/** @var Send */
46+
private $block;
47+
48+
/** @var Session */
49+
private $session;
50+
51+
/** @var AccountManagementInterface */
52+
private $accountManagement;
53+
1254
/**
13-
* @var \Magento\SendFriend\Block\Send
55+
* @inheritdoc
1456
*/
15-
protected $_block;
16-
1757
protected function setUp()
1858
{
19-
$this->_block = Bootstrap::getObjectManager()->create(\Magento\SendFriend\Block\Send::class);
59+
$this->objectManager = Bootstrap::getObjectManager();
60+
$this->layout = $this->objectManager->get(LayoutInterface::class);
61+
$this->block = $this->layout->createBlock(Send::class);
62+
$this->session = $this->objectManager->get(Session::class);
63+
$this->accountManagement = $this->objectManager->get(AccountManagementInterface::class);
64+
}
65+
66+
/**
67+
* @inheritdoc
68+
*/
69+
protected function tearDown()
70+
{
71+
parent::tearDown();
72+
73+
$this->session->logout();
2074
}
2175

2276
/**
77+
* @dataProvider formDataProvider
78+
*
2379
* @param string $field
2480
* @param string $value
25-
* @dataProvider formDataProvider
26-
* @covers \Magento\SendFriend\Block\Send::getUserName
27-
* @covers \Magento\SendFriend\Block\Send::getEmail
81+
* @return void
2882
*/
29-
public function testGetCustomerFieldFromFormData($field, $value)
83+
public function testGetCustomerFieldFromFormData(string $field, string $value): void
3084
{
3185
$formData = ['sender' => [$field => $value]];
32-
$this->_block->setFormData($formData);
86+
$this->block->setFormData($formData);
3387
$this->assertEquals(trim($value), $this->_callBlockMethod($field));
3488
}
3589

3690
/**
3791
* @return array
3892
*/
39-
public function formDataProvider()
93+
public function formDataProvider(): array
4094
{
4195
return [
4296
['name', 'Customer Form Name'],
@@ -45,49 +99,65 @@ public function formDataProvider()
4599
}
46100

47101
/**
102+
* @magentoDataFixture Magento/Customer/_files/customer.php
103+
*
104+
* @dataProvider customerSessionDataProvider
105+
*
106+
* @magentoAppIsolation enabled
107+
*
48108
* @param string $field
49109
* @param string $value
50-
* @dataProvider customerSessionDataProvider
51-
* @covers \Magento\SendFriend\Block\Send::getUserName
52-
* @covers \Magento\SendFriend\Block\Send::getEmail
53-
* @magentoDataFixture Magento/Customer/_files/customer.php
110+
* @return void
54111
*/
55-
public function testGetCustomerFieldFromSession($field, $value)
112+
public function testGetCustomerFieldFromSession(string $field, string $value): void
56113
{
57-
$logger = $this->createMock(\Psr\Log\LoggerInterface::class);
58-
/** @var $session \Magento\Customer\Model\Session */
59-
$session = Bootstrap::getObjectManager()->create(\Magento\Customer\Model\Session::class, [$logger]);
60-
/** @var \Magento\Customer\Api\AccountManagementInterface $service */
61-
$service = Bootstrap::getObjectManager()->create(\Magento\Customer\Api\AccountManagementInterface::class);
62-
$customer = $service->authenticate('customer@example.com', 'password');
63-
$session->setCustomerDataAsLoggedIn($customer);
114+
$customer = $this->accountManagement->authenticate('customer@example.com', 'password');
115+
$this->session->setCustomerDataAsLoggedIn($customer);
64116
$this->assertEquals($value, $this->_callBlockMethod($field));
65117
}
66118

67119
/**
68120
* @return array
69121
*/
70-
public function customerSessionDataProvider()
122+
public function customerSessionDataProvider(): array
71123
{
72124
return [
73125
['name', 'John Smith'],
74126
['email', 'customer@example.com']
75127
];
76128
}
77129

130+
/**
131+
* @magentoConfigFixture current_store sendfriend/email/max_recipients 1
132+
*
133+
* @return void
134+
*/
135+
public function testBlockAppearance(): void
136+
{
137+
$this->block->setTemplate('Magento_SendFriend::send.phtml');
138+
$html = preg_replace('#<script(.*?)>#i', '', $this->block->toHtml());
139+
foreach ($this->elementsXpath as $key => $xpath) {
140+
$this->assertEquals(
141+
1,
142+
Xpath::getElementsCountForXpath($xpath, $html),
143+
sprintf('The %s field is not found on the page', $key)
144+
);
145+
}
146+
}
147+
78148
/**
79149
* Call block method based on form field
80150
*
81151
* @param string $field
82152
* @return null|string
83153
*/
84-
protected function _callBlockMethod($field)
154+
protected function _callBlockMethod(string $field): ?string
85155
{
86156
switch ($field) {
87157
case 'name':
88-
return $this->_block->getUserName();
158+
return $this->block->getUserName();
89159
case 'email':
90-
return $this->_block->getEmail();
160+
return $this->block->getEmail();
91161
default:
92162
return null;
93163
}

0 commit comments

Comments
 (0)