Skip to content

Commit

Permalink
FIX getFormParent does not automatically look up arbitrary test stubs…
Browse files Browse the repository at this point in the history
…, remove theme dependency
  • Loading branch information
robbieaverill committed Mar 19, 2018
1 parent 9403ee2 commit d199140
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
17 changes: 12 additions & 5 deletions code/Model/Recipient/EmailRecipient.php
Expand Up @@ -128,17 +128,24 @@ public function summaryFields()
/**
* 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()
Expand Down
23 changes: 9 additions & 14 deletions tests/Control/UserDefinedFormControllerTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -104,32 +105,26 @@ public function testValidation()

// 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()
Expand Down
3 changes: 3 additions & 0 deletions tests/Model/UserDefinedFormTest.php
Expand Up @@ -111,6 +111,7 @@ public function testEmailRecipientPopup()

$popup = new EmailRecipient();
$popup->FormID = $form->ID;
$popup->FormClass = UserDefinedForm::class;

$fields = $popup->getCMSFields();

Expand Down Expand Up @@ -167,6 +168,7 @@ public function testGetEmailTemplateDropdownValues()
$page = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
$recipient = new EmailRecipient();
$recipient->FormID = $page->ID;
$recipient->FormClass = UserDefinedForm::class;

$result = $recipient->getEmailTemplateDropdownValues();

Expand All @@ -180,6 +182,7 @@ public function testEmailTemplateExists()
$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()));
Expand Down

0 comments on commit d199140

Please sign in to comment.