tests working with emails alsog
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user