diff --git a/code/Model/Recipient/EmailRecipient.php b/code/Model/Recipient/EmailRecipient.php index 01ae893..e95d72e 100644 --- a/code/Model/Recipient/EmailRecipient.php +++ b/code/Model/Recipient/EmailRecipient.php @@ -128,17 +128,24 @@ class EmailRecipient extends DataObject /** * Get instance of UserForm when editing in getCMSFields * - * @return UserDefinedForm|UserForm + * @return UserDefinedForm|UserForm|null */ protected function getFormParent() { + // If polymorphic relationship is actually defined, use it + if ($this->FormID && $this->FormClass) { + $formClass = $this->FormClass; + return $formClass::get()->byID($this->FormID); + } + + // Revert to checking for a form from the session // LeftAndMain::sessionNamespace is protected. @todo replace this with a non-deprecated equivalent. $sessionNamespace = $this->config()->get('session_namespace') ?: CMSMain::class; - $formID = $this->FormID ?: Controller::curr()->getRequest()->getSession()->get($sessionNamespace . '.currentPage'); - $formClass = $this->FormClass ?: UserDefinedForm::class; - - return $formClass::get()->byID($formID); + $formID = Controller::curr()->getRequest()->getSession()->get($sessionNamespace . '.currentPage'); + if ($formID) { + return UserDefinedForm::get()->byID($formID); + } } public function getTitle() diff --git a/tests/Control/UserDefinedFormControllerTest.php b/tests/Control/UserDefinedFormControllerTest.php index 6e2fc5a..f4f6171 100644 --- a/tests/Control/UserDefinedFormControllerTest.php +++ b/tests/Control/UserDefinedFormControllerTest.php @@ -2,6 +2,7 @@ namespace SilverStripe\UserForms\Tests\Control; +use SilverStripe\Control\HTTPResponse; use SilverStripe\Core\Config\Config; use SilverStripe\Dev\CSSContentParser; use SilverStripe\Dev\FunctionalTest; @@ -104,32 +105,26 @@ class UserDefinedFormControllerTest extends FunctionalTest // Post with no fields $this->get($form->URLSegment); + /** @var HTTPResponse $response */ $response = $this->submitForm('UserForm_Form_' . $form->ID, null, []); - $this->assertPartialMatchBySelector( - '.field .message', - ['This field is required'] - ); + $this->assertContains('This field is required', $response->getBody()); // Post with all fields, but invalid email $this->get($form->URLSegment); - $this->submitForm('UserForm_Form_' . $form->ID, null, [ + /** @var HTTPResponse $response */ + $response = $this->submitForm('UserForm_Form_' . $form->ID, null, [ 'required-email' => 'invalid', 'required-text' => 'bob' ]); - $this->assertPartialMatchBySelector( - '.field .message', - ['Please enter an email address'] - ); + $this->assertContains('Please enter an email address', $response->getBody()); // Post with only required $this->get($form->URLSegment); - $this->submitForm('UserForm_Form_' . $form->ID, null, [ + /** @var HTTPResponse $response */ + $response = $this->submitForm('UserForm_Form_' . $form->ID, null, [ 'required-text' => 'bob' ]); - $this->assertPartialMatchBySelector( - 'p', - ["Thanks, we've received your submission."] - ); + $this->assertContains("Thanks, we've received your submission.", $response->getBody()); } public function testFinished() diff --git a/tests/Model/UserDefinedFormTest.php b/tests/Model/UserDefinedFormTest.php index 625ad42..0658071 100644 --- a/tests/Model/UserDefinedFormTest.php +++ b/tests/Model/UserDefinedFormTest.php @@ -111,6 +111,7 @@ class UserDefinedFormTest extends FunctionalTest $popup = new EmailRecipient(); $popup->FormID = $form->ID; + $popup->FormClass = UserDefinedForm::class; $fields = $popup->getCMSFields(); @@ -167,6 +168,7 @@ class UserDefinedFormTest extends FunctionalTest $page = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page'); $recipient = new EmailRecipient(); $recipient->FormID = $page->ID; + $recipient->FormClass = UserDefinedForm::class; $result = $recipient->getEmailTemplateDropdownValues(); @@ -180,6 +182,7 @@ class UserDefinedFormTest extends FunctionalTest $page = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page'); $recipient = new EmailRecipient(); $recipient->FormID = $page->ID; + $recipient->FormClass = UserDefinedForm::class; // Set the default template $recipient->EmailTemplate = current(array_keys($recipient->getEmailTemplateDropdownValues()));