API Remove UserFormRecipientEmail and use core Email class. Minor test fixes.

This commit is contained in:
Robbie Averill 2017-08-15 10:49:13 +12:00
parent cba7c26950
commit 59827e2011
7 changed files with 38 additions and 61 deletions

View File

@ -37,7 +37,6 @@ mappings:
EditableTextField: SilverStripe\UserForms\Model\EditableFormField\EditableTextField
UserDefinedForm_EmailRecipient: SilverStripe\UserForms\Model\Recipient\EmailRecipient
UserDefinedForm_EmailRecipientCondition: SilverStripe\UserForms\Model\Recipient\EmailRecipientCondition
UserFormRecipientEmail: SilverStripe\UserForms\Model\Recipient\UserFormRecipientEmail
UserFormRecipientItemRequest: SilverStripe\UserForms\Model\Recipient\UserFormRecipientItemRequest
SubmittedFileField: SilverStripe\UserForms\Model\Submission\SubmittedFileField
SubmittedForm: SilverStripe\UserForms\Model\Submission\SubmittedForm

View File

@ -424,7 +424,7 @@ class EditableFormField extends DataObject
{
do {
// Generate a new random name after this class (handles namespaces)
$classNamePieces = explode('\\', __CLASS__);
$classNamePieces = explode('\\', static::class);
$class = array_pop($classNamePieces);
$entropy = substr(sha1(uniqid()), 0, 5);
$name = "{$class}_{$entropy}";
@ -951,7 +951,7 @@ class EditableFormField extends DataObject
public function getErrorMessage()
{
$title = strip_tags("'". ($this->Title ? $this->Title : $this->Name) . "'");
$standard = sprintf(_t('SilverStripe\\Forms\\Form.FIELDISREQUIRED', '%s is required').'.', $title);
$standard = _t('SilverStripe\\Forms\\Form.FIELDISREQUIRED', '{field} is required.', ['field' => $title]);
// only use CustomErrorMessage if it has a non empty value
$errorMessage = (!empty($this->CustomErrorMessage)) ? $this->CustomErrorMessage : $standard;
@ -1062,7 +1062,8 @@ class EditableFormField extends DataObject
$editableFieldClasses = [];
foreach ($classes as $class) {
// Skip abstract / hidden classes
if (Config::inst()->get($class, 'abstract', Config::UNINHERITED) || Config::inst()->get($class, 'hidden')
if (Config::inst()->get($class, 'abstract', Config::UNINHERITED)
|| Config::inst()->get($class, 'hidden')
) {
continue;
}
@ -1136,10 +1137,10 @@ class EditableFormField extends DataObject
$fieldToWatch = $formFieldWatch->getSelectorFieldOnly();
$expression = $rule->buildExpression();
if (! in_array($fieldToWatch, $result['selectors'])) {
if (!in_array($fieldToWatch, $result['selectors'])) {
$result['selectors'][] = $fieldToWatch;
}
if (! in_array($expression['event'], $result['events'])) {
if (!in_array($expression['event'], $result['events'])) {
$result['events'][] = $expression['event'];
}
$result['operations'][] = $expression['operation'];

View File

@ -1,38 +0,0 @@
<?php
namespace SilverStripe\UserForms\Model\Recipient;
use SilverStripe\Control\Email\Email;
/**
* Email that gets sent to the people listed in the Email Recipients when a
* submission is made.
*
* @package userforms
*/
class UserFormRecipientEmail extends Email
{
protected $ss_template = 'SubmittedFormEmail';
protected $data;
public function __construct($submittedFields = null)
{
parent::__construct($submittedFields = null);
}
/**
* Set the "Reply-To" header with an email address rather than append as
* {@link Email::replyTo} does.
*
* @param string|array $address
* @param string|null $name
* @return $this
*/
public function setReplyTo($address, $name = null)
{
$this->customHeaders['Reply-To'] = $email;
return $this;
}
}

View File

@ -6,6 +6,7 @@ use PageController;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Upload;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Email\Email;
use SilverStripe\Control\HTTP;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Forms\Form;
@ -13,10 +14,9 @@ use SilverStripe\i18n\i18n;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Security\Member;
use SilverStripe\Security\Security;
use SilverStripe\UserForms\Form\UserForm;
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
use SilverStripe\UserForms\Model\Recipient\UserFormRecipientEmail;
use SilverStripe\UserForms\Model\Submission\SubmittedForm;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
@ -184,7 +184,7 @@ JS
public function process($data, $form)
{
$submittedForm = SubmittedForm::create();
$submittedForm->SubmittedByID = ($id = Member::currentUserID()) ? $id : 0;
$submittedForm->SubmittedByID = Security::getCurrentUser() ? Security::getCurrentUser()->ID : 0;
$submittedForm->ParentID = $this->ID;
// if saving is not disabled save now to generate the ID
@ -253,7 +253,7 @@ JS
}
$emailData = [
'Sender' => Member::currentUser(),
'Sender' => Security::getCurrentUser(),
'Fields' => $submittedFields
];
@ -262,7 +262,10 @@ JS
// email users on submit.
if ($recipients = $this->FilteredEmailRecipients($data, $form)) {
foreach ($recipients as $recipient) {
$email = UserFormRecipientEmail::create($submittedFields);
$email = Email::create()
->setHTMLTemplate('email/SubmittedFormEmail.ss')
->setPlainTemplate('email/SubmittedFormEmail.ss');
$mergeFields = $this->getMergeFieldsMap($emailData['Fields']);
if ($attachments) {
@ -282,11 +285,14 @@ JS
$parsedBody = SSViewer::execute_string($recipient->getEmailBodyContent(), $mergeFields);
if (!$recipient->SendPlain && $recipient->emailTemplateExists()) {
$email->setTemplate($recipient->EmailTemplate);
$email->setHTMLTemplate($recipient->EmailTemplate);
}
$email->setData($recipient);
foreach ($emailData as $key => $value) {
$email->addData($key, $value);
}
$email->populateTemplate($recipient);
$email->populateTemplate($emailData);
$email->setFrom($recipient->EmailFrom);
$email->setBody($parsedBody);
$email->setTo($recipient->EmailAddress);
@ -378,7 +384,7 @@ JS
* @param ArrayList fields
* @return ArrayData
*/
protected function getMergeFieldsMap($fields = array())
protected function getMergeFieldsMap($fields = [])
{
$data = ArrayData::create([]);

View File

@ -2,11 +2,13 @@
namespace SilverStripe\UserForms\Test\Model\EditableFormField;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\OptionsetField;
use SilverStripe\Security\Member;
use SilverStripe\Security\IdentityStore;
use SilverStripe\UserForms\Model\EditableFormField\EditableCheckbox;
use SilverStripe\UserForms\Model\EditableFormField\EditableDropdown;
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
@ -43,8 +45,7 @@ class EditableFormFieldTest extends FunctionalTest
$this->assertTrue($text->canEdit());
$this->assertTrue($text->canDelete());
$member = Member::currentUser();
$member->logOut();
Injector::inst()->get(IdentityStore::class)->logOut(Controller::curr()->getRequest());
$this->logInWithPermission('SITETREE_VIEW_ALL');
$this->assertFalse($text->canCreate());

View File

@ -24,6 +24,8 @@ class UserDefinedFormControllerTest extends FunctionalTest
{
protected static $fixture_file = 'UserDefinedFormTest.yml';
protected static $use_draft_site = true;
public function testProcess()
{
$form = $this->setupFormFrontend();
@ -56,7 +58,7 @@ class UserDefinedFormControllerTest extends FunctionalTest
$email = $this->findEmail('test@example.com', 'no-reply@example.com', 'Email Subject');
// assert that the email has the field title and the value html email
$parser = new CSSContentParser($email['content']);
$parser = new CSSContentParser($email['Content']);
$title = $parser->getBySelector('strong');
$this->assertEquals('Basic Text Field', (string) $title[0], 'Email contains the field name');
@ -68,13 +70,13 @@ class UserDefinedFormControllerTest extends FunctionalTest
$this->assertEmailSent('nohtml@example.com', 'no-reply@example.com', 'Email Subject');
$nohtml = $this->findEmail('nohtml@example.com', 'no-reply@example.com', 'Email Subject');
$this->assertContains('Basic Text Field: Basic Value', $nohtml['content'], 'Email contains no html');
$this->assertContains('Basic Text Field: Basic Value', $nohtml['Content'], 'Email contains no html');
// no data
$this->assertEmailSent('nodata@example.com', 'no-reply@example.com', 'Email Subject');
$nodata = $this->findEmail('nodata@example.com', 'no-reply@example.com', 'Email Subject');
$parser = new CSSContentParser($nodata['content']);
$parser = new CSSContentParser($nodata['Content']);
$list = $parser->getBySelector('dl');
$this->assertFalse(isset($list[0]), 'Email contains no fields');

View File

@ -12,6 +12,7 @@ use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\ORM\DB;
use SilverStripe\Security\Member;
use SilverStripe\UserForms\Extension\UserFormFieldEditorExtension;
use SilverStripe\UserForms\Extension\UserFormValidator;
use SilverStripe\UserForms\Model\EditableCustomRule;
use SilverStripe\UserForms\Model\EditableFormField\EditableEmailField;
@ -30,6 +31,10 @@ class UserDefinedFormTest extends FunctionalTest
{
protected static $fixture_file = 'UserDefinedFormTest.yml';
protected static $required_extensions = [
UserDefinedForm::class => [UserFormFieldEditorExtension::class],
];
public function testRollbackToVersion()
{
$this->markTestSkipped(
@ -492,8 +497,9 @@ class UserDefinedFormTest extends FunctionalTest
);
$result = $recipient->validate();
$this->assertFalse($result->isValid());
$this->assertContains('filtered.example.com', $result->getMessages());
$this->assertNotContains('filtered2@example.com', $result->getMessages());
$this->assertNotEmpty($result->getMessages());
$this->assertContains('filtered.example.com', $result->getMessages()[0]['message']);
$this->assertNotContains('filtered2@example.com', $result->getMessages()[0]['message']);
// test valid email addresses pass validation
$recipient = $this->objFromFixture(