diff --git a/src/Controller/Component/ContactUsComponent.php b/src/Controller/Component/ContactUsComponent.php index a2615fa..5ecc56f 100644 --- a/src/Controller/Component/ContactUsComponent.php +++ b/src/Controller/Component/ContactUsComponent.php @@ -77,7 +77,7 @@ class ContactUsComponent extends Component /** * @return EntityInterface|ContactUsFormSubmission */ - public function processContactUsForm(EntityInterface $contactUsFormSubmission, array $postData = null) + public function processContactUsForm(EntityInterface $contactUsFormSubmission, array|null $postData) { if (!isset($postData)) { $postData = $this->getController()->getRequest()->getData(); diff --git a/src/Mailer/ContactUsFormSubmissionsMailer.php b/src/Mailer/ContactUsFormSubmissionsMailer.php index 68e65f0..9e82681 100644 --- a/src/Mailer/ContactUsFormSubmissionsMailer.php +++ b/src/Mailer/ContactUsFormSubmissionsMailer.php @@ -22,10 +22,8 @@ class ContactUsFormSubmissionsMailer extends Mailer protected function confirmation(EntityInterface $contactUsFormSubmission, array $options = []) { $name = isset($contactUsFormSubmission['name']) ? $contactUsFormSubmission['name'] . ', ' : ''; - // un-hide the token to be able to send it in the email content $subject = __d('cake_contact_us', 'We have received your message'); - $this ->setTo($contactUsFormSubmission['email']) ->setSubject($name . $subject) @@ -46,13 +44,11 @@ class ContactUsFormSubmissionsMailer extends Mailer */ protected function backend(EntityInterface $contactUsFormSubmission, array $options = []) { - $name = isset($contactUsFormSubmission['name']) ? ' by ' . $contactUsFormSubmission['name'] : ''; - // un-hide the token to be able to send it in the email content $subject = __d('cake_contact_us', 'Contact Us Form Submitted'); - + $name = isset($contactUsFormSubmission['name']) ? ' by ' . $contactUsFormSubmission['name'] : ''; $this - ->setTo(Configure::readOrFail('ContactUs.email.backend.to')) + ->setTo(Configure::readOrFail('ContactUs.fields.email.backend.to')) ->setSubject($subject . $name) ->setEmailFormat(Message::MESSAGE_BOTH) ->setViewVars([ diff --git a/src/Model/Table/ContactUsFormSubmissionsTable.php b/src/Model/Table/ContactUsFormSubmissionsTable.php index 243ed59..03f2743 100644 --- a/src/Model/Table/ContactUsFormSubmissionsTable.php +++ b/src/Model/Table/ContactUsFormSubmissionsTable.php @@ -134,7 +134,6 @@ class ContactUsFormSubmissionsTable extends Table $this->getMailer($mailer)->send('confirmation', [$contactUsFormSubmission]); $updateData['confirm_email_sent'] = $now; } catch (CakeException $exception) { - } } @@ -144,11 +143,8 @@ class ContactUsFormSubmissionsTable extends Table $this->getMailer($mailer)->send('backend', [$contactUsFormSubmission]); $updateData['backend_email_sent'] = $now; } catch (CakeException $exception) { - } } - Log::debug(print_r('$updateData', true)); - Log::debug(print_r($updateData, true)); if ($updateData) { $contactUsFormSubmission = $this->patchEntity($contactUsFormSubmission, $updateData, ['validate' => false]); $this->saveOrFail($contactUsFormSubmission); diff --git a/templates/email/html/backend.php b/templates/email/html/backend.php index 1070ed5..2dd8476 100644 --- a/templates/email/html/backend.php +++ b/templates/email/html/backend.php @@ -6,20 +6,21 @@ use Cake\Core\Configure; * @var \CakeContactUs\Model\Entity\ContactUsFormSubmission $contactUsFormSubmission */ ?> +

submitted_at) ?>,

name); ?>

-email)) : ?> +email)) : ?>

Html->link($contactUsFormSubmission->email, 'mailto:' . $contactUsFormSubmission->email); ?>

-subject)) : ?> +subject)) : ?>

subject); ?> @@ -28,5 +29,4 @@ use Cake\Core\Configure;

message); ?> -

- +

\ No newline at end of file diff --git a/templates/email/html/confirmation.php b/templates/email/html/confirmation.php index 2a5126d..ac309a6 100644 --- a/templates/email/html/confirmation.php +++ b/templates/email/html/confirmation.php @@ -6,4 +6,4 @@

, -

+

\ No newline at end of file diff --git a/templates/email/text/backend.php b/templates/email/text/backend.php index 136ea08..c7c66e0 100644 --- a/templates/email/text/backend.php +++ b/templates/email/text/backend.php @@ -5,21 +5,22 @@ use Cake\Core\Configure; /** * @var \CakeContactUs\Model\Entity\ContactUsFormSubmission $contactUsFormSubmission */ + ?> submitted_at) ?>, name); ?> -email)) : ?> +email)) : ?> email; ?> -subject)) : ?> +subject)) : ?> subject); ?> -message); ?> +message); ?> \ No newline at end of file diff --git a/templates/email/text/confirmation.php b/templates/email/text/confirmation.php index 5332911..a8fc64f 100644 --- a/templates/email/text/confirmation.php +++ b/templates/email/text/confirmation.php @@ -9,9 +9,8 @@ * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ ?> -, +name) ? $contactUsFormSubmission->name : '') ?>, , - diff --git a/templates/layout/email/html/default.php b/templates/layout/email/html/default.php new file mode 100644 index 0000000..96b0e73 --- /dev/null +++ b/templates/layout/email/html/default.php @@ -0,0 +1,25 @@ + + + + + <?= $this->fetch('title') ?> + + + fetch('content') ?> + + diff --git a/templates/layout/email/text/default.php b/templates/layout/email/text/default.php new file mode 100644 index 0000000..cd51169 --- /dev/null +++ b/templates/layout/email/text/default.php @@ -0,0 +1,17 @@ +fetch('content'); diff --git a/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php b/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php index bac24d6..19ec8e6 100644 --- a/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php +++ b/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php @@ -5,10 +5,13 @@ namespace CakeContactUs\Test\TestCase\Controller; use App\Model\Table\ContactUsFormSubmissionsTable; use Cake\Controller\Controller; +use Cake\Core\Configure; use Cake\Event\EventList; use Cake\Event\EventManager; use Cake\Http\ServerRequest; +use Cake\Mailer\Transport\DebugTransport; use Cake\ORM\Table; +use Cake\TestSuite\EmailTrait; use Cake\TestSuite\IntegrationTestTrait; use Cake\TestSuite\TestCase; use CakeContactUs\CakeContactUsPlugin; @@ -23,6 +26,7 @@ use PHPUnit\Exception; class ContactUsFormSubmissionsControllerTest extends TestCase { use IntegrationTestTrait; + use EmailTrait; /** * @var ContactUsFormSubmissionsTable|Table @@ -87,6 +91,8 @@ class ContactUsFormSubmissionsControllerTest extends TestCase $cntAfter = $this->ContactUsFormSubmissions->find()->count(); $this->assertEquals($cntBefore, $cntAfter); + + $this->assertNoMailSent(); } /** @@ -99,10 +105,59 @@ class ContactUsFormSubmissionsControllerTest extends TestCase * * @uses ContactUsFormSubmissionsController::add */ - public function testAddPostSuccess(): void + public function testAddPostSuccessNoEmail(): void { $cntBefore = $this->ContactUsFormSubmissions->find()->count(); + $url = [ + 'plugin' => 'CakeContactUs', + 'controller' => 'ContactUsFormSubmissions', + 'action' => 'add', + ]; + $data = [ + 'name' => 'valid name', + 'email' => 'valid_email@test.com', + 'message' => 'valid message goes here', + ]; + $this->post($url, $data); + $this->assertResponseCode(302); + $cntAfter = $this->ContactUsFormSubmissions->find()->count(); + $this->assertEquals($cntBefore + 1, $cntAfter); + $this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED); + $this->assertNoMailSent(); + } + + /** + * Test add method + * + * Tests a POST request to the add action with a logged in user + * + * @return void + * @throws Exception + * + * @uses ContactUsFormSubmissionsController::add + */ + public function testAddPostSuccessBothEmailsSent(): void + { + Configure::write('ContactUs', [ + 'fields' => [ + 'captcha' => false, + 'email' => [ + 'confirmation' => true, + 'backend' => [ + 'to' => 'test@example.com', + ], + ], + ], + ]); + Configure::write('EmailTransport', [ + 'default' => [ + 'className' => DebugTransport::class, + ] + ]); + + $cntBefore = $this->ContactUsFormSubmissions->find()->count(); + $url = [ 'plugin' => 'CakeContactUs', 'controller' => 'ContactUsFormSubmissions', @@ -119,6 +174,102 @@ class ContactUsFormSubmissionsControllerTest extends TestCase $cntAfter = $this->ContactUsFormSubmissions->find()->count(); $this->assertEquals($cntBefore + 1, $cntAfter); $this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED); + + $this->assertMailCount(2); // confirmation + backend emails + } + + /** + * Test add method + * + * Tests a POST request to the add action with a logged in user + * + * @return void + * @throws Exception + * + * @uses ContactUsFormSubmissionsController::add + */ + public function testAddPostSuccessConfirmationEmailSent(): void + { + Configure::write('ContactUs', [ + 'fields' => [ + 'captcha' => false, + 'email' => [ + 'confirmation' => true, + 'backend' => false, + ], + ], + ]); + $cntBefore = $this->ContactUsFormSubmissions->find()->count(); + + $url = [ + 'plugin' => 'CakeContactUs', + 'controller' => 'ContactUsFormSubmissions', + 'action' => 'add', + ]; + $data = [ + 'name' => 'valid name', + 'email' => 'valid_email@test.com', + 'message' => 'valid message goes here', + ]; + $this->post($url, $data); + $this->assertResponseCode(302); + + $cntAfter = $this->ContactUsFormSubmissions->find()->count(); + $this->assertEquals($cntBefore + 1, $cntAfter); + $this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED); + + $this->assertMailCount(1); // confirmation only + } + + /** + * Test add method + * + * Tests a POST request to the add action with a logged in user + * + * @return void + * @throws Exception + * + * @uses ContactUsFormSubmissionsController::add + */ + public function testAddPostSuccessBackendEmailSent(): void + { + Configure::write('ContactUs', [ + 'fields' => [ + 'captcha' => false, + 'email' => [ + 'confirmation' => false, + 'backend' => [ + 'to' => 'test@example.com', + ], + ], + ], + ]); + + $cntBefore = $this->ContactUsFormSubmissions->find()->count(); + + $url = [ + 'plugin' => 'CakeContactUs', + 'controller' => 'ContactUsFormSubmissions', + 'action' => 'add', + ]; + $data = [ + 'name' => 'valid name', + 'email' => 'valid_email@test.com', + 'message' => 'valid message goes here', + ]; + $this->post($url, $data); + $responseBody = (string)$this->_response->getBody(); + $tmpLog = fopen(TMP . 'phpunit.log', 'w'); + if ($tmpLog) { + fwrite($tmpLog, $responseBody); + } + $this->assertResponseCode(302); + + $cntAfter = $this->ContactUsFormSubmissions->find()->count(); + $this->assertEquals($cntBefore + 1, $cntAfter); + $this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED); + + $this->assertMailCount(1); // backend email only } /** @@ -152,6 +303,6 @@ class ContactUsFormSubmissionsControllerTest extends TestCase $cntAfter = $this->ContactUsFormSubmissions->find()->count(); $this->assertEquals($cntBefore, $cntAfter); $this->assertEventFired(CakeContactUsPlugin::EVENT_BEFORE_CONTACT_US_FORM_SAVED); - + $this->assertNoMailSent(); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 87170ad..119c9d3 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -7,6 +7,9 @@ use Cake\Core\Configure; use Cake\Core\Plugin; use Cake\Database\Connection; use Cake\Datasource\ConnectionManager; +use Cake\Mailer\Mailer; +use Cake\Mailer\Transport\DebugTransport; +use Cake\Mailer\TransportFactory; use Cake\TestSuite\Fixture\SchemaLoader; use CakeContactUs\CakeContactUsPlugin; use Migrations\TestSuite\Migrator; @@ -66,7 +69,34 @@ Configure::write('ContactUs', [ ], ], ]); - +Configure::write('EmailTransport', [ + 'default' => [ + 'className' => DebugTransport::class, + ] +]); +TransportFactory::setConfig('default', [ + 'className' => DebugTransport::class, +]); +Configure::write('Email', [ + /* + * Email delivery profiles + * + * Delivery profiles allow you to predefine various properties about email + * messages from your application and give the settings a name. This saves + * duplication across your application and makes maintenance and development + * easier. Each profile accepts a number of keys. See `Cake\Mailer\Email` + * for more information. + */ + 'default' => [ + 'transport' => 'default', + 'from' => 'test@example.com', + /* + * Will by default be set to config value of App.encoding, if that exists otherwise to UTF-8. + */ + //'charset' => 'utf-8', + //'headerCharset' => 'utf-8', + ], +]); $cache = [ 'default' => [ 'engine' => 'File', @@ -89,6 +119,7 @@ $cache = [ ]; Cache::setConfig($cache); +Mailer::setConfig(Configure::consume('Email')); class_alias(AppController::class, 'App\Controller\AppController');