Skip to content

Commit

Permalink
add test for mail sending; improve param resoving for reference/set s…
Browse files Browse the repository at this point in the history
…teps
  • Loading branch information
gggeek committed Jul 30, 2017
1 parent 975ea79 commit 675380f
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 4 deletions.
10 changes: 8 additions & 2 deletions Core/Executor/MailExecutor.php
Expand Up @@ -74,8 +74,14 @@ protected function send($dsl, $context)
if (isset($dsl['body'])) {
$message->setBody($this->resolveReferencesInText($dsl['body']));
}
if (isset($dsl['attach'])) { /// @todo
$message->attach(Swift_Attachment::fromPath($this->resolveReferencesRecursively($dsl['attach'])));
if (isset($dsl['attach'])) {
$path = $this->resolveReferencesRecursively($dsl['attach']);
// we use the same logic as for the image/file fields in content: look up file 1st relative to the migration
$attachment = dirname($context['path']) . '/' . $path;
if (!is_file($attachment)) {
$attachment = $path;
}
$message->attach(Swift_Attachment::fromPath($attachment));
}

if (isset($dsl['priority'])) {
Expand Down
5 changes: 3 additions & 2 deletions Core/Executor/ReferenceExecutor.php
Expand Up @@ -56,8 +56,9 @@ protected function set($dsl, $context)
throw new \Exception("Invalid step definition: miss 'value' for setting reference");
}
$value = $dsl['value'];
if (preg_match('/%.+%$/', $value)) {
$value = $this->container->getParameter(trim($value, '%'));
if (preg_match('/.*%.+%.*$/', $value)) {
// we use the same parameter resolving rule as symfony, even though this means abusing the ContainerInterface
$value = $this->container->getParameterBag()->resolveString($value);
}
$overwrite = isset($dsl['overwrite']) ? $overwrite = $dsl['overwrite'] : false;
$this->referenceResolver->addReference($dsl['identifier'], $value, $overwrite);
Expand Down
File renamed without changes.
83 changes: 83 additions & 0 deletions Tests/dsl/good/UnitTestOK023_mail.yml
@@ -0,0 +1,83 @@
-
type: reference
mode: set
identifier: kmb_test_023_subject
value: testing

-
type: reference
mode: set
identifier: kmb_test_023_body
value: migrations

-
type: mail
mode: send
from: test@test.me
to: test@test.you
cc: test@test.him
bcc:
- test@test.her
- test@test.they
subject: "this is an email subject about [reference:kmb_test_023_subject] etc..."
body: "this is an email body about [reference:kmb_test_023_body] etc..."
attach: files/emptyfile.txt

# NB: this file is made available by a custom swiftmailer listener
-
type: file
mode: load
file: /tmp/mail.txt
references:
-
attribute: body
identifier: kmb_test_023_mailmessage

-
type: assert
target: reference
identifier: reference:kmb_test_023_mailmessage
test:
contains: "From: test@test.me"

-
type: assert
target: reference
identifier: reference:kmb_test_023_mailmessage
test:
contains: "To: test@test.you"

-
type: assert
target: reference
identifier: reference:kmb_test_023_mailmessage
test:
contains: "Cc: test@test.him"

-
type: assert
target: reference
identifier: reference:kmb_test_023_mailmessage
test:
contains: "Bcc: test@test.her, test@test.they"

-
type: assert
target: reference
identifier: reference:kmb_test_023_mailmessage
test:
contains: "Content-Type: multipart/mixed;"

-
type: assert
target: reference
identifier: reference:kmb_test_023_mailmessage
test:
contains: "this is an email body about migrations etc..."

-
type: assert
target: reference
identifier: reference:kmb_test_023_mailmessage
test:
contains: "Subject: this is an email subject about testing etc..."
1 change: 1 addition & 0 deletions Tests/ezpublish/config/config_behat_ezplatform.yml
Expand Up @@ -46,6 +46,7 @@ stash:
registerDoctrineAdapter: false

swiftmailer:
disable_delivery: false
spool:
type: file
path: '%kernel.root_dir%/spool'
Expand Up @@ -31,6 +31,7 @@ ez_publish_legacy:
root_dir: %kernel.root_dir%/../../ezpublish-legacy

swiftmailer:
disable_delivery: false
spool:
type: file
path: '%kernel.root_dir%/spool'
4 changes: 4 additions & 0 deletions Tests/ezpublish/config/services.yml
Expand Up @@ -23,3 +23,7 @@ services:
class: Kaliop\eZMigrationBundle\Tests\helper\CustomReferenceResolver
tags:
- { name: ez_migration_bundle.reference_resolver.customreference }
ez_migration_bundle.test.swiftmailer_message_logger:
class: Kaliop\eZMigrationBundle\Tests\helper\MailMessageLogger
tags:
- { name: swiftmailer.default.plugin }
19 changes: 19 additions & 0 deletions Tests/helper/MailMessageLogger.php
@@ -0,0 +1,19 @@
<?php

namespace Kaliop\eZMigrationBundle\Tests\helper;

use Swift_Plugins_MessageLogger;
use Swift_Events_SendEvent;

class MailMessageLogger extends Swift_Plugins_MessageLogger
{
/**
* @todo make the name of the written file configurable, and/or do set references to it on saving
* @param Swift_Events_SendEvent $evt
*/
public function sendPerformed(Swift_Events_SendEvent $evt)
{
$msg = $evt->getMessage();
file_put_contents('/tmp/mail.txt', $msg->toString());
}
}
4 changes: 4 additions & 0 deletions WHATSNEW.md
Expand Up @@ -3,6 +3,10 @@ Version 4.0

* New: allow to set references to contentType default_always_available, default_sort_field, default_sort_order, is_container

* Improved: parameter resolving in migration steps reference/set now works even when the parameter is not the full value of the string, eg: '%kernel.root_dir%/spool'

* Improved: path to attachment files can be specified relative to the migration file for migration steps mail/send


Version 4.0 RC-5
================
Expand Down

0 comments on commit 675380f

Please sign in to comment.