forked from mautic/mautic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mautic#1289 from acquia/MAUT-5131-2
MAUT 5131 - Send example email as a specified contact
- Loading branch information
Showing
6 changed files
with
419 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Used in data-lookup-callback attr of form field in ExampleSendType | ||
* Take a look at https://github.com/twitter/typeahead.js/ | ||
*/ | ||
Mautic.activateContactLookupField = function(fieldOptions, filterId) { | ||
|
||
const lookupElementId = 'example_send_contact'; | ||
const action = mQuery('#'+ lookupElementId).attr('data-chosen-lookup'); | ||
|
||
const options = { | ||
limit: 20, | ||
'searchKey': 'lead.lead', | ||
}; | ||
|
||
Mautic.activateFieldTypeahead(lookupElementId, filterId, options, action); | ||
|
||
mQuery('#'+ lookupElementId).on("change",function(event) { | ||
if (event.target.value === '') { | ||
// Delete selected contact ID from hidden field | ||
mQuery('#example_send_contact_id').val(''); | ||
} | ||
}); | ||
}; | ||
|
||
/** | ||
* Used in data-lookup-callback attr of form field in ExampleSendType | ||
*/ | ||
Mautic.updateContactLookupListFilter = function(field, item) { | ||
if (item && item.id) { | ||
mQuery('#example_send_contact_id').val(item.id); | ||
mQuery(field).val(item.value); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
app/bundles/EmailBundle/Tests/Controller/EmailExampleFunctionalTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* @copyright 2021 Mautic Contributors. All rights reserved | ||
* @author Mautic | ||
* | ||
* @link https://mautic.org | ||
* | ||
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html | ||
*/ | ||
|
||
namespace Mautic\EmailBundle\Tests\Controller; | ||
|
||
use DateTime; | ||
use Mautic\CoreBundle\Test\MauticMysqlTestCase; | ||
use Mautic\EmailBundle\Entity\Email; | ||
use Mautic\LeadBundle\Entity\Lead; | ||
use Swift_Events_EventListener; | ||
use Swift_Mime_SimpleMessage; | ||
use Swift_Transport; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class EmailExampleFunctionalTest extends MauticMysqlTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
$this->configParams['mailer_spool_type'] = 'file'; | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function testSendEmail(): void | ||
{ | ||
$this->container->set('swiftmailer.transport.real', $transport = $this->createTransportFake()); | ||
|
||
$lead = $this->createLead(); | ||
$email = $this->createEmail(); | ||
$this->em->flush(); | ||
|
||
$crawler = $this->client->request(Request::METHOD_GET, "/s/emails/sendExample/{$email->getId()}"); | ||
$formCrawler = $crawler->filter('form[name=example_send]'); | ||
self::assertSame(1, $formCrawler->count()); | ||
$form = $formCrawler->form(); | ||
$form->setValues([ | ||
'example_send[emails][list][0]' => '[email protected]', | ||
'example_send[contact]' => 'somebody', | ||
'example_send[contact_id]' => $lead->getId(), | ||
]); | ||
$this->client->submit($form); | ||
|
||
self::assertCount(1, $transport->messages); | ||
|
||
$message = $transport->messages[0]; | ||
|
||
// Asserting email data | ||
self::assertInstanceOf('Swift_Message', $message); | ||
self::assertSame('[email protected]', key($message->getTo())); | ||
self::assertContains('Email subject', $message->getSubject()); | ||
self::assertContains( | ||
'Contact emails is [email protected]', | ||
$message->getBody() | ||
); | ||
} | ||
|
||
private function createEmail(): Email | ||
{ | ||
$email = new Email(); | ||
$email->setDateAdded(new DateTime()); | ||
$email->setName('Email name'); | ||
$email->setSubject('Email subject'); | ||
$email->setTemplate('Blank'); | ||
$email->setCustomHtml('Contact emails is {contactfield=email}'); | ||
$this->em->persist($email); | ||
|
||
return $email; | ||
} | ||
|
||
private function createLead(): Lead | ||
{ | ||
$lead = new Lead(); | ||
$lead->setEmail('[email protected]'); | ||
$this->em->persist($lead); | ||
|
||
return $lead; | ||
} | ||
|
||
private function createTransportFake(): Swift_Transport | ||
{ | ||
return new class() implements Swift_Transport { | ||
/** | ||
* @var array | ||
*/ | ||
public $messages = []; | ||
|
||
public function isStarted(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function start(): void | ||
{ | ||
} | ||
|
||
public function stop(): void | ||
{ | ||
} | ||
|
||
public function ping(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null): int | ||
{ | ||
$this->messages[] = clone $message; | ||
|
||
return count((array) $message->getTo()) | ||
+ count((array) $message->getCc()) | ||
+ count((array) $message->getBcc()); | ||
} | ||
|
||
public function registerPlugin(Swift_Events_EventListener $plugin): void | ||
{ | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.