2010-09-03 07:06:13 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package userforms
|
|
|
|
*/
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
class UserDefinedFormControllerTest extends FunctionalTest
|
|
|
|
{
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public static $fixture_file = 'UserDefinedFormTest.yml';
|
2010-09-03 07:06:13 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testProcess()
|
|
|
|
{
|
|
|
|
$form = $this->setupFormFrontend();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($form);
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->autoFollowRedirection = false;
|
|
|
|
$this->clearEmails();
|
2015-08-11 22:57:19 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// load the form
|
|
|
|
$this->get($form->URLSegment);
|
2015-08-11 22:57:19 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$field = $this->objFromFixture('EditableTextField', 'basic-text');
|
2015-08-11 22:57:19 +02:00
|
|
|
|
2017-04-18 03:11:32 +02:00
|
|
|
$response = $this->submitForm('UserForm_Form_' . $form->ID, null, array($field->Name => 'Basic Value'));
|
2010-09-03 07:06:13 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// should have a submitted form field now
|
|
|
|
$submitted = DataObject::get('SubmittedFormField', "\"Name\" = 'basic-text-name'");
|
|
|
|
$this->assertDOSAllMatch(array('Name' => 'basic-text-name', 'Value' => 'Basic Value', 'Title' => 'Basic Text Field'), $submitted);
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// check emails
|
|
|
|
$this->assertEmailSent('test@example.com', 'no-reply@example.com', 'Email Subject');
|
|
|
|
$email = $this->findEmail('test@example.com', 'no-reply@example.com', 'Email Subject');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// assert that the email has the field title and the value html email
|
|
|
|
$parser = new CSSContentParser($email['content']);
|
|
|
|
$title = $parser->getBySelector('strong');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals('Basic Text Field', (string) $title[0], 'Email contains the field name');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$value = $parser->getBySelector('dd');
|
|
|
|
$this->assertEquals('Basic Value', (string) $value[0], 'Email contains the value');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// no html
|
|
|
|
$this->assertEmailSent('nohtml@example.com', 'no-reply@example.com', 'Email Subject');
|
|
|
|
$nohtml = $this->findEmail('nohtml@example.com', 'no-reply@example.com', 'Email Subject');
|
2010-09-03 07:06:13 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertContains('Basic Text Field: Basic Value', $nohtml['content'], 'Email contains no html');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// 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');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$parser = new CSSContentParser($nodata['content']);
|
|
|
|
$list = $parser->getBySelector('dl');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertFalse(isset($list[0]), 'Email contains no fields');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// check to see if the user was redirected (301)
|
|
|
|
$this->assertEquals($response->getStatusCode(), 302);
|
|
|
|
$this->assertStringEndsWith('finished#uff', $response->getHeader('Location'));
|
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testValidation()
|
|
|
|
{
|
|
|
|
$form = $this->setupFormFrontend('email-form');
|
2015-12-11 05:38:31 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Post with no fields
|
|
|
|
$this->get($form->URLSegment);
|
2017-04-18 03:11:32 +02:00
|
|
|
$response = $this->submitForm('UserForm_Form_' . $form->ID, null, array());
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertPartialMatchBySelector(
|
|
|
|
'.field .message',
|
|
|
|
array('This field is required')
|
|
|
|
);
|
2015-12-11 05:38:31 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Post with all fields, but invalid email
|
|
|
|
$this->get($form->URLSegment);
|
2017-04-18 03:11:32 +02:00
|
|
|
$this->submitForm('UserForm_Form_' . $form->ID, null, array(
|
2016-07-21 07:53:59 +02:00
|
|
|
'required-email' => 'invalid',
|
|
|
|
'required-text' => 'bob'
|
|
|
|
));
|
|
|
|
$this->assertPartialMatchBySelector(
|
|
|
|
'.field .message',
|
|
|
|
array('Please enter an email address')
|
|
|
|
);
|
2015-12-11 05:38:31 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Post with only required
|
|
|
|
$this->get($form->URLSegment);
|
2017-04-18 03:11:32 +02:00
|
|
|
$this->submitForm('UserForm_Form_' . $form->ID, null, array(
|
2016-07-21 07:53:59 +02:00
|
|
|
'required-text' => 'bob'
|
|
|
|
));
|
|
|
|
$this->assertPartialMatchBySelector(
|
|
|
|
'p',
|
|
|
|
array("Thanks, we've received your submission.")
|
|
|
|
);
|
|
|
|
}
|
2015-12-11 05:38:31 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testFinished()
|
|
|
|
{
|
|
|
|
$form = $this->setupFormFrontend();
|
2014-01-06 03:15:01 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// set formProcessed and SecurityID to replicate the form being filled out
|
|
|
|
$this->session()->inst_set('SecurityID', 1);
|
|
|
|
$this->session()->inst_set('FormProcessed', 1);
|
2014-01-06 03:15:01 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$response = $this->get($form->URLSegment.'/finished');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertContains($form->OnCompleteMessage, $response->getBody());
|
|
|
|
}
|
2014-01-06 03:15:01 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testAppendingFinished()
|
|
|
|
{
|
|
|
|
$form = $this->setupFormFrontend();
|
2014-01-06 03:15:01 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// replicate finished being added to the end of the form URL without the form being filled out
|
|
|
|
$this->session()->inst_set('SecurityID', 1);
|
|
|
|
$this->session()->inst_set('FormProcessed', null);
|
2014-01-06 03:15:01 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$response = $this->get($form->URLSegment.'/finished');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertNotContains($form->OnCompleteMessage, $response->getBody());
|
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testForm()
|
|
|
|
{
|
|
|
|
$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page');
|
2015-08-10 07:03:36 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($form);
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// test form
|
2017-04-18 03:11:32 +02:00
|
|
|
$this->assertEquals($controller->Form()->getName(), 'Form_' . $form->ID, 'The form is referenced as Form');
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals($controller->Form()->Fields()->Count(), 1); // disabled SecurityID token fields
|
|
|
|
$this->assertEquals($controller->Form()->Actions()->Count(), 1);
|
|
|
|
$this->assertEquals(count($controller->Form()->getValidator()->getRequired()), 0);
|
2010-09-03 07:06:13 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$requiredForm = $this->objFromFixture('UserDefinedForm', 'validation-form');
|
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($requiredForm);
|
2010-09-03 07:06:13 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals($controller->Form()->Fields()->Count(), 1); // disabled SecurityID token fields
|
|
|
|
$this->assertEquals($controller->Form()->Actions()->Count(), 1);
|
|
|
|
$this->assertEquals(count($controller->Form()->getValidator()->getRequired()), 1);
|
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testGetFormFields()
|
|
|
|
{
|
|
|
|
// generating the fieldset of fields
|
|
|
|
$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($form);
|
|
|
|
|
|
|
|
$formSteps = $controller->Form()->getFormFields();
|
|
|
|
$firstStep = $formSteps->first();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals($formSteps->Count(), 1);
|
|
|
|
$this->assertEquals($firstStep->getChildren()->Count(), 1);
|
2015-08-10 07:03:36 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// custom error message on a form field
|
|
|
|
$requiredForm = $this->objFromFixture('UserDefinedForm', 'validation-form');
|
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($requiredForm);
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
UserDefinedForm::config()->required_identifier = "*";
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$formSteps = $controller->Form()->getFormFields();
|
|
|
|
$firstStep = $formSteps->first();
|
|
|
|
$firstField = $firstStep->getChildren()->first();
|
2015-08-10 07:03:36 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals('Custom Error Message', $firstField->getCustomValidationMessage());
|
|
|
|
$this->assertEquals($firstField->Title(), 'Required Text Field <span class=\'required-identifier\'>*</span>');
|
2015-08-10 07:03:36 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// test custom right title
|
|
|
|
$field = $form->Fields()->limit(1, 1)->First();
|
|
|
|
$field->RightTitle = 'Right Title';
|
|
|
|
$field->write();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($form);
|
|
|
|
$formSteps = $controller->Form()->getFormFields();
|
|
|
|
$firstStep = $formSteps->first();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals($firstStep->getChildren()->First()->RightTitle(), "Right Title");
|
2010-09-08 00:44:43 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// test empty form
|
|
|
|
$emptyForm = $this->objFromFixture('UserDefinedForm', 'empty-form');
|
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($emptyForm);
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertFalse($controller->Form()->getFormFields()->exists());
|
|
|
|
}
|
2015-07-24 04:37:48 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testGetFormActions()
|
|
|
|
{
|
|
|
|
// generating the fieldset of actions
|
|
|
|
$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($form);
|
|
|
|
$actions = $controller->Form()->getFormActions();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// by default will have 1 submit button which links to process
|
|
|
|
$expected = new FieldList(new FormAction('process', 'Submit'));
|
|
|
|
$expected->setForm($controller->Form());
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals($actions, $expected);
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// the custom popup should have a reset button and a custom text
|
|
|
|
$custom = $this->objFromFixture('UserDefinedForm', 'form-with-reset-and-custom-action');
|
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($custom);
|
|
|
|
$actions = $controller->Form()->getFormActions();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$expected = new FieldList(new FormAction('process', 'Custom Button'));
|
|
|
|
$expected->push(new ResetFormAction("clearForm", "Clear"));
|
|
|
|
$expected->setForm($controller->Form());
|
2010-09-03 07:06:13 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals($actions, $expected);
|
|
|
|
}
|
2015-07-24 04:37:48 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testRenderingIntoFormTemplate()
|
|
|
|
{
|
|
|
|
$form = $this->setupFormFrontend();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$form->Content = 'This is some content without a form nested between it';
|
|
|
|
$form->doPublish();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($form);
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// check to see if $Form is replaced to inside the content
|
|
|
|
$index = new ArrayData($controller->index());
|
|
|
|
$parser = new CSSContentParser($index->renderWith(array('UserDefinedFormControllerTest')));
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2017-04-18 03:11:32 +02:00
|
|
|
$this->checkTemplateIsCorrect($parser, $form);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2010-09-03 07:06:13 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testRenderingIntoTemplateWithSubstringReplacement()
|
|
|
|
{
|
|
|
|
$form = $this->setupFormFrontend();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($form);
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// check to see if $Form is replaced to inside the content
|
|
|
|
$index = new ArrayData($controller->index());
|
|
|
|
$parser = new CSSContentParser($index->renderWith(array('UserDefinedFormControllerTest')));
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2017-04-18 03:11:32 +02:00
|
|
|
$this->checkTemplateIsCorrect($parser, $form);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Publish a form for use on the frontend
|
|
|
|
*
|
|
|
|
* @param string $fixtureName
|
|
|
|
* @return UserDefinedForm
|
|
|
|
*/
|
|
|
|
protected function setupFormFrontend($fixtureName = 'basic-form-page')
|
|
|
|
{
|
|
|
|
$form = $this->objFromFixture('UserDefinedForm', $fixtureName);
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$form->doPublish();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$member = Member::currentUser();
|
|
|
|
$member->logOut();
|
2010-09-03 07:06:13 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
return $form;
|
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2017-04-18 03:11:32 +02:00
|
|
|
public function checkTemplateIsCorrect($parser, $form)
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
2017-04-18 03:11:32 +02:00
|
|
|
$this->assertArrayHasKey(0, $parser->getBySelector('form#UserForm_Form_' . $form->ID));
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// check for the input
|
|
|
|
$this->assertArrayHasKey(0, $parser->getBySelector('input.text'));
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// check for the label and the text
|
|
|
|
$label = $parser->getBySelector('label.left');
|
|
|
|
$this->assertArrayHasKey(0, $label);
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals((string) $label[0][0], "Basic Text Field", "Label contains correct field name");
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// check for the action
|
|
|
|
$action = $parser->getBySelector('input.action');
|
|
|
|
$this->assertArrayHasKey(0, $action);
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals((string) $action[0]['value'], "Submit", "Submit button has default text");
|
|
|
|
}
|
2010-11-01 03:22:29 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
class UserDefinedFormControllerTest_Controller extends UserDefinedForm_Controller implements TestOnly
|
|
|
|
{
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* Overloaded to avoid inconsistencies between 2.4.2 and 2.4.3 (disables all security tokens in unit tests by default)
|
|
|
|
*/
|
|
|
|
public function Form()
|
|
|
|
{
|
|
|
|
$form = parent::Form();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
if ($form) {
|
|
|
|
$form->disableSecurityToken();
|
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
return $form;
|
|
|
|
}
|
2012-06-22 02:21:08 +02:00
|
|
|
}
|