Compare commits

...

4 Commits

Author SHA1 Message Date
Guy Sartorelli
06cd3eecd9
ENH Update code to reflect changes in template layer 2024-10-22 13:12:00 +13:00
Guy Sartorelli
574ca14ee7
ENH Use config instead of runtime code to remove menu item (#1337) 2024-10-21 11:56:02 +13:00
Guy Sartorelli
4f821d1188
Merge pull request #1336 from creative-commoners/pulls/7/field-validators
MNT Fix unit test
2024-10-15 15:10:40 +13:00
Steve Boyd
6f0b4f0b99 MNT Fix unit test 2024-10-15 11:03:34 +13:00
7 changed files with 54 additions and 25 deletions

View File

@ -1,6 +0,0 @@
<?php
use SilverStripe\Admin\CMSMenu;
use SilverStripe\UserForms\Control\UserDefinedFormAdmin;
CMSMenu::remove_menu_class(UserDefinedFormAdmin::class);

View File

@ -47,6 +47,8 @@ class UserDefinedFormAdmin extends LeftAndMain
private static $url_segment = 'user-forms'; private static $url_segment = 'user-forms';
private static $ignore_menuitem = true;
/** /**
* @var string The name of the folder where form submissions will be placed by default * @var string The name of the folder where form submissions will be placed by default
*/ */

View File

@ -31,8 +31,9 @@ use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
use SilverStripe\Model\ArrayData; use SilverStripe\Model\ArrayData;
use SilverStripe\View\Requirements; use SilverStripe\View\Requirements;
use SilverStripe\View\SSViewer;
use SilverStripe\Model\ModelData; use SilverStripe\Model\ModelData;
use SilverStripe\View\TemplateEngine;
use SilverStripe\View\ViewLayerData;
use Swift_RfcComplianceException; use Swift_RfcComplianceException;
/** /**
@ -421,7 +422,8 @@ JS
// Include any parsed merge field references from the CMS editor - this is already escaped // Include any parsed merge field references from the CMS editor - this is already escaped
// This string substitution works for both HTML and plain text emails. // This string substitution works for both HTML and plain text emails.
// $recipient->getEmailBodyContent() will retrieve the relevant version of the email // $recipient->getEmailBodyContent() will retrieve the relevant version of the email
$emailData['Body'] = SSViewer::execute_string($recipient->getEmailBodyContent(), $mergeFields); $engine = Injector::inst()->create(TemplateEngine::class);
$emailData['Body'] = $engine->renderString($recipient->getEmailBodyContent(), ViewLayerData::create($mergeFields));
// only include visible fields if recipient visibility flag is set // only include visible fields if recipient visibility flag is set
if ((bool) $recipient->HideInvisibleFields) { if ((bool) $recipient->HideInvisibleFields) {
$emailData['Fields'] = $visibleSubmittedFields; $emailData['Fields'] = $visibleSubmittedFields;
@ -487,10 +489,10 @@ JS
if ($submittedFormField && trim($submittedFormField->Value ?? '')) { if ($submittedFormField && trim($submittedFormField->Value ?? '')) {
$email->setSubject($submittedFormField->Value); $email->setSubject($submittedFormField->Value);
} else { } else {
$email->setSubject(SSViewer::execute_string($recipient->EmailSubject, $mergeFields)); $email->setSubject($engine->renderString($recipient->EmailSubject, ViewLayerData::create($mergeFields)));
} }
} else { } else {
$email->setSubject(SSViewer::execute_string($recipient->EmailSubject, $mergeFields)); $email->setSubject($engine->renderString($recipient->EmailSubject, ViewLayerData::create($mergeFields)));
} }
$this->extend('updateEmail', $email, $recipient, $emailData); $this->extend('updateEmail', $email, $recipient, $emailData);
@ -660,7 +662,7 @@ EOS;
// Hide the step jump button if the FormStep has is initially hidden. // Hide the step jump button if the FormStep has is initially hidden.
// This is particularly important beacause the next/prev page buttons logic is controlled by // This is particularly important beacause the next/prev page buttons logic is controlled by
// the visibility of the FormStep buttons // the visibility of the FormStep buttons
// The HTML for the FormStep buttons is defined in UserFormProgress.ss // The HTML for the FormStep buttons is defined in the UserFormProgress template
$id = str_replace('#', '', $target ?? ''); $id = str_replace('#', '', $target ?? '');
$result .= <<<EOS $result .= <<<EOS
$('.step-button-wrapper[data-for="{$id}"]').addClass('hide'); $('.step-button-wrapper[data-for="{$id}"]').addClass('hide');

View File

@ -12,10 +12,10 @@ class UserFormsCheckboxSetField extends CheckboxSetField
{ {
/** /**
* If your project uses a custom UserFormsCheckboxSetField.ss, ensure that it includes * If your project uses a custom UserFormsCheckboxSetField template, ensure that it includes
* `$Top.getValidationAttributesHTML().RAW` so that custom validation messages work * `$Top.getValidationAttributesHTML().RAW` so that custom validation messages work
* For further details see * For further details see
* templates/SilverStripe/UserForms/FormField/UserFormsCheckboxSetField.ss * templates/SilverStripe/UserForms/FormField/UserFormsCheckboxSetField template
* *
* Use on a template with .RAW - single and double quoted strings will be safely escaped * Use on a template with .RAW - single and double quoted strings will be safely escaped
* *

View File

@ -12,10 +12,10 @@ class UserFormsOptionSetField extends OptionsetField
{ {
/** /**
* If your project uses a custom UserFormsCheckboxSetField.ss, ensure that it includes * If your project uses a custom UserFormsCheckboxSetField template, ensure that it includes
* `$Top.getValidationAttributesHTML().RAW` so that custom validation messages work * `$Top.getValidationAttributesHTML().RAW` so that custom validation messages work
* For further details see * For further details see
* templates/SilverStripe/UserForms/FormField/UserFormsCheckboxSetField.ss * templates/SilverStripe/UserForms/FormField/UserFormsCheckboxSetField template
* *
* Use on a template with .RAW - single and double quoted strings will be safely escaped * Use on a template with .RAW - single and double quoted strings will be safely escaped
* *

View File

@ -137,6 +137,12 @@ class EmailRecipient extends DataObject
*/ */
private static $allow_unbound_recipient_fields = false; private static $allow_unbound_recipient_fields = false;
/**
* The regex used to find template files for rendering emails.
* By default this finds ss template files.
*/
private static string $email_template_regex = '/^.*\.ss$/';
public function requireDefaultRecords() public function requireDefaultRecords()
{ {
parent::requireDefaultRecords(); parent::requireDefaultRecords();
@ -562,7 +568,7 @@ class EmailRecipient extends DataObject
$templates = []; $templates = [];
$finder = new FileFinder(); $finder = new FileFinder();
$finder->setOption('name_regex', '/^.*\.ss$/'); $finder->setOption('name_regex', static::config()->get('email_template_regex'));
$parent = $this->getFormParent(); $parent = $this->getFormParent();

View File

@ -584,16 +584,41 @@ class UserDefinedFormControllerTest extends FunctionalTest
public static function provideValidEmailsToArray() public static function provideValidEmailsToArray()
{ {
return [ return [
[[], [null]],
[[], [' , , ']],
[[], ['broken.email, broken@.email, broken2.@email']],
[ [
['broken@email', 'correctemail@email.com'], 'input' => [
[', broken@email, email@-email.com,correctemail@email.com,'] null
],
'expected' => [],
], ],
[ [
['correctemail1@email.com', 'correctemail2@email.com', 'correctemail3@email.com'], 'input' => [
['correctemail1@email.com, correctemail2@email.com, correctemail3@email.com'] ' , , '
],
'expected' => [],
],
[
'input' => [
'broken.email, broken@.email, broken2.@email'
],
'expected' => [],
],
[
'input' => [
', broken@email, email@-email.com,correctemail@email.com,'
],
'expected' => [
'correctemail@email.com'
],
],
[
'input' => [
'correctemail1@email.com, correctemail2@email.com, correctemail3@email.com'
],
'expected' => [
'correctemail1@email.com',
'correctemail2@email.com',
'correctemail3@email.com'
],
] ]
]; ];
} }
@ -602,7 +627,7 @@ class UserDefinedFormControllerTest extends FunctionalTest
* Test that provided email is valid * Test that provided email is valid
*/ */
#[DataProvider('provideValidEmailsToArray')] #[DataProvider('provideValidEmailsToArray')]
public function testValidEmailsToArray(array $expectedOutput, array $input) public function testValidEmailsToArray(array $input, array $expected)
{ {
$class = new ReflectionClass(UserDefinedFormController::class); $class = new ReflectionClass(UserDefinedFormController::class);
$method = $class->getMethod('validEmailsToArray'); $method = $class->getMethod('validEmailsToArray');
@ -610,6 +635,6 @@ class UserDefinedFormControllerTest extends FunctionalTest
$controller = new UserDefinedFormController(); $controller = new UserDefinedFormController();
$this->assertEquals($expectedOutput, $method->invokeArgs($controller, $input)); $this->assertEquals($expected, $method->invokeArgs($controller, $input));
} }
} }