FIX Show correct fields in email preview

Co-authored-by: Michael Caruana <mikeyc7m@users.noreply.github.com>
This commit is contained in:
Steve Boyd 2021-01-17 22:53:41 +13:00
parent ce2fe31bdd
commit 8801790b66
2 changed files with 51 additions and 8 deletions

View File

@ -53,16 +53,12 @@ class UserFormRecipientItemRequest extends GridFieldDetailForm_ItemRequest
protected function getPreviewFieldData()
{
$data = ArrayList::create();
$fields = $this->record->Form()->Fields()->filter(
'ClassName:not',
[
EditableLiteralField::class,
EditableFormHeading::class,
]
);
$fields = $this->record->Form()->Fields();
foreach ($fields as $field) {
if (!$field->showInReports()) {
continue;
}
$data->push(ArrayData::create([
'Name' => $field->dbObject('Name'),
'Title' => $field->dbObject('Title'),

View File

@ -0,0 +1,47 @@
<?php
namespace SilverStripe\UserForms\Tests\Model\Recipient;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\PolymorphicHasManyList;
use SilverStripe\UserForms\Model\Recipient\EmailRecipient;
use SilverStripe\UserForms\Model\Recipient\UserFormRecipientItemRequest;
use SilverStripe\UserForms\Model\UserDefinedForm;
class UserFormRecipientItemRequestTest extends SapphireTest
{
public function testShowInReportsAffectsPreview()
{
// classes where showInReports() returns false
$namespace = 'SilverStripe\UserForms\Model\EditableFormField';
$falseClasses = ['EditableFieldGroup', 'EditableFieldGroupEnd', 'EditableFormStep'];
// some classes where showInReports() returns true (inherits from EditableFormField)
$trueClasses = ['EditableTextField', 'EditableEmailField', 'EditableDateField'];
$form = new UserDefinedForm();
$form->write();
/** @var PolymorphicHasManyList $fields */
$fields = $form->Fields();
foreach (array_merge($falseClasses, $trueClasses) as $class) {
$fqcn = "$namespace\\$class";
$obj = new $fqcn();
$obj->Name = 'My' . $class;
$obj->write();
$fields->add($obj);
}
$recipient = new EmailRecipient();
$recipient->EmailAddress = 'to@example.com';
$recipient->EmailFrom = 'from@example.com';
$recipient->EmailTemplate = 'email/SubmittedFormEmail';
$recipient->Form = $form;
$recipient->write();
$recipient->setComponent('Form', $form);
$request = new UserFormRecipientItemRequest(null, null, $recipient, null, '');
$html = $request->preview()->getValue();
foreach ($falseClasses as $class) {
$this->assertNotContains('My' . $class, $html);
}
foreach ($trueClasses as $class) {
$this->assertContains('My' . $class, $html);
}
}
}