Skip to content

Commit

Permalink
MC-18023: Magento 2.3 contact form not sending emails( No log entry).
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Kilo committed Apr 13, 2021
1 parent 9a68d68 commit f765273
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
30 changes: 16 additions & 14 deletions app/code/Magento/Contact/Controller/Index/Post.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Contact\Controller\Index;

use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Contact\Model\ConfigInterface;
use Magento\Contact\Model\MailInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;

/**
* Post user question controller.
*/
class Post extends \Magento\Contact\Controller\Index implements HttpPostActionInterface
{
/**
* @var DataPersistorInterface
*/
private $dataPersistor;

/**
* @var Context
*/
private $context;

/**
* @var MailInterface
*/
Expand All @@ -45,7 +43,7 @@ class Post extends \Magento\Contact\Controller\Index implements HttpPostActionIn
* @param ConfigInterface $contactsConfig
* @param MailInterface $mail
* @param DataPersistorInterface $dataPersistor
* @param LoggerInterface $logger
* @param LoggerInterface|null $logger
*/
public function __construct(
Context $context,
Expand All @@ -55,14 +53,13 @@ public function __construct(
LoggerInterface $logger = null
) {
parent::__construct($context, $contactsConfig);
$this->context = $context;
$this->mail = $mail;
$this->dataPersistor = $dataPersistor;
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
}

/**
* Post user question
* Post user question.
*
* @return Redirect
*/
Expand All @@ -78,6 +75,7 @@ public function execute()
);
$this->dataPersistor->clear('contact_us');
} catch (LocalizedException $e) {
$this->logger->error($e);
$this->messageManager->addErrorMessage($e->getMessage());
$this->dataPersistor->set('contact_us', $this->getRequest()->getParams());
} catch (\Exception $e) {
Expand All @@ -91,10 +89,12 @@ public function execute()
}

/**
* Send port data.
*
* @param array $post Post data from contact form
* @return void
*/
private function sendEmail($post)
private function sendEmail(array $post): void
{
$this->mail->send(
$post['email'],
Expand All @@ -103,10 +103,12 @@ private function sendEmail($post)
}

/**
* Validate form data.
*
* @return array
* @throws \Exception
*/
private function validatedParams()
private function validatedParams(): array
{
$request = $this->getRequest();
if (trim($request->getParam('name')) === '') {
Expand Down
45 changes: 40 additions & 5 deletions app/code/Magento/Contact/Test/Unit/Controller/Index/PostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Framework\UrlInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

/**
* @covers \Magento\Contact\Controller\Index\Post
Expand Down Expand Up @@ -70,7 +72,12 @@ class PostTest extends TestCase
private $mailMock;

/**
* test setup
* @var MockObject|LoggerInterface
*/
private $loggerMock;

/**
* @inheirtdoc
*/
protected function setUp(): void
{
Expand Down Expand Up @@ -116,13 +123,18 @@ protected function setUp(): void
$contextMock->expects($this->once())
->method('getResultRedirectFactory')
->willReturn($this->redirectResultFactoryMock);
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
->disableOriginalConstructor()
->onlyMethods(['error'])
->getMockForAbstractClass();

$this->controller = (new ObjectManagerHelper($this))->getObject(
Post::class,
[
'context' => $contextMock,
'mail' => $this->mailMock,
'dataPersistor' => $this->dataPersistorMock
'dataPersistor' => $this->dataPersistorMock,
'logger' => $this->loggerMock,
]
);
}
Expand All @@ -137,7 +149,8 @@ public function testExecuteEmptyPost(): void
}

/**
* Test exceute post validation
* Test execute post validation.
*
* @param array $postData
* @param bool $exceptionExpected
* @dataProvider postDataProvider
Expand All @@ -158,7 +171,7 @@ public function testExecutePostValidation($postData, $exceptionExpected): void
}

/**
* Data provider for test exceute post validation
* Data provider for test execute post validation.
*/
public function postDataProvider(): array
{
Expand All @@ -181,7 +194,7 @@ public function testExecuteValidPost(): void
'name' => 'Name',
'comment' => 'Comment',
'email' => 'valid@mail.com',
'hideit' => null
'hideit' => null,
];

$this->dataPersistorMock->expects($this->once())
Expand All @@ -193,6 +206,28 @@ public function testExecuteValidPost(): void
$this->controller->execute();
}

/**
* Verify localized exception is handled and logged.
*
* @return void
*/
public function testExecuteWithException(): void
{
$this->mailMock->expects(self::once())
->method('send')
->willThrowException(new LocalizedException(__('Test exception')));
$this->loggerMock->expects(self::once())->method('error');
$this->messageManagerMock->expects(self::once())->method('addErrorMessage')->with('Test exception');
$postData = [
'name' => 'John Doe',
'comment' => 'test comment',
'email' => 'john.doe@example.com',
'hideit' => null,
];
$this->stubRequestPostData($postData);
$this->controller->execute();
}

/**
* Stub request for post data
*
Expand Down

0 comments on commit f765273

Please sign in to comment.