Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SendFriend] Covering the Send to friend by integration tests #21048

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\SendFriend\Controller;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\Data\Form\FormKey;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Framework\Message\MessageInterface;
use Magento\TestFramework\Request;
use Magento\TestFramework\TestCase\AbstractController;

/**
* Class SendmailTest
*/
class SendmailTest extends AbstractController
{
/**
* Share the product to friend as logged in customer
*
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
* @magentoDataFixture Magento/SendFriend/_files/disable_allow_guest_config.php
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Catalog/_files/products.php
*/
public function testSendActionAsLoggedIn()
{
$product = $this->getProduct();
$this->login(1);
$this->prepareRequestData();

$this->dispatch('sendfriend/product/sendmail/id/' . $product->getId());
$this->assertSessionMessages(
$this->equalTo(['The link to a friend was sent.']),
MessageInterface::TYPE_SUCCESS
);
}

/**
* Share the product to friend as guest customer
*
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
* @magentoConfigFixture default_store sendfriend/email/enabled 1
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
* @magentoDataFixture Magento/Catalog/_files/products.php
*/
public function testSendActionAsGuest()
{
$product = $this->getProduct();
$this->prepareRequestData();

$this->dispatch('sendfriend/product/sendmail/id/' . $product->getId());
$this->assertSessionMessages(
$this->equalTo(['The link to a friend was sent.']),
MessageInterface::TYPE_SUCCESS
);
}

/**
* Share the product to friend as guest customer with invalid post data
*
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
* @magentoConfigFixture default_store sendfriend/email/enabled 1
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
* @magentoDataFixture Magento/Catalog/_files/products.php
*/
public function testSendActionAsGuestWithInvalidData()
{
$product = $this->getProduct();
$this->prepareRequestData(true);

$this->dispatch('sendfriend/product/sendmail/id/' . $product->getId());
$this->assertSessionMessages(
$this->equalTo(['Invalid Sender Email']),
MessageInterface::TYPE_ERROR
);
}

/**
* @return ProductInterface
*/
private function getProduct()
{
return $this->_objectManager->get(ProductRepositoryInterface::class)->get('custom-design-simple-product');
}

/**
* Login the user
*
* @param string $customerId Customer to mark as logged in for the session
* @return void
*/
protected function login($customerId)
{
/** @var Session $session */
$session = Bootstrap::getObjectManager()
->get(Session::class);
$session->loginById($customerId);
}

/**
* @param bool $invalidData
* @return void
*/
private function prepareRequestData($invalidData = false)
{
/** @var FormKey $formKey */
$formKey = $this->_objectManager->get(FormKey::class);
$post = [
'sender' => [
'name' => 'Test',
'email' => 'test@example.com',
'message' => 'Message',
],
'recipients' => [
'name' => [
'Recipient 1',
'Recipient 2'
],
'email' => [
'r1@example.com',
'r2@example.com'
]
],
'form_key' => $formKey->getFormKey(),
];
if ($invalidData) {
unset($post['sender']['email']);
}

$this->getRequest()->setMethod(Request::METHOD_POST);
$this->getRequest()->setPostValue($post);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Framework\App\Config\Value;
use Magento\TestFramework\Helper\Bootstrap;

/** @var Value $config */
$config = Bootstrap::getObjectManager()->create(Value::class);
$config->setPath('sendfriend/email/enabled');
$config->setScope('default');
$config->setScopeId(0);
$config->setValue(1);
$config->save();

/** @var Value $config */
$config = Bootstrap::getObjectManager()->create(Value::class);
$config->setPath('sendfriend/email/allow_guest');
$config->setScope('default');
$config->setScopeId(0);
$config->setValue(0);
$config->save();