2010-09-03 07:06:13 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-15 23:31:47 +02:00
|
|
|
namespace SilverStripe\UserForms\Tests\Model;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2017-08-11 02:20:12 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2018-02-13 18:21:03 +01:00
|
|
|
use SilverStripe\Control\Email\Email;
|
2017-08-11 02:20:12 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns;
|
2017-08-11 02:20:12 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2017-08-15 00:49:13 +02:00
|
|
|
use SilverStripe\UserForms\Extension\UserFormFieldEditorExtension;
|
2017-08-11 02:20:12 +02:00
|
|
|
use SilverStripe\UserForms\Extension\UserFormValidator;
|
|
|
|
use SilverStripe\UserForms\Model\EditableCustomRule;
|
2017-08-15 05:30:59 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableDropdown;
|
2018-01-24 03:17:03 +01:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableEmailField;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroup;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroupEnd;
|
2017-08-11 02:20:12 +02:00
|
|
|
use SilverStripe\UserForms\Model\Recipient\EmailRecipient;
|
|
|
|
use SilverStripe\UserForms\Model\UserDefinedForm;
|
|
|
|
use SilverStripe\Versioned\Versioned;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
/**
|
|
|
|
* @package userforms
|
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
class UserDefinedFormTest extends FunctionalTest
|
|
|
|
{
|
2018-06-20 07:08:20 +02:00
|
|
|
protected $usesTransactions = false;
|
|
|
|
|
2017-10-15 23:13:01 +02:00
|
|
|
protected static $fixture_file = '../UserFormsTest.yml';
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2017-08-15 00:49:13 +02:00
|
|
|
protected static $required_extensions = [
|
|
|
|
UserDefinedForm::class => [UserFormFieldEditorExtension::class],
|
|
|
|
];
|
|
|
|
|
2021-11-01 21:52:58 +01:00
|
|
|
protected function setUp(): void
|
2018-02-13 18:21:03 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2022-11-28 21:36:08 +01:00
|
|
|
Email::config()->set('admin_email', 'no-reply@example.com');
|
2018-02-13 18:21:03 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testRollbackToVersion()
|
|
|
|
{
|
|
|
|
$this->markTestSkipped(
|
|
|
|
'UserDefinedForm::rollback() has not been implemented completely'
|
|
|
|
);
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->logInWithPermission('ADMIN');
|
2022-11-28 21:36:08 +01:00
|
|
|
/** @var UserDefinedForm|Versioned $form */
|
2017-08-09 01:55:09 +02:00
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
|
2010-09-03 07:06:13 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$form->SubmitButtonText = 'Button Text';
|
|
|
|
$form->write();
|
2017-08-15 23:31:47 +02:00
|
|
|
$form->publishRecursive();
|
2016-07-21 07:53:59 +02:00
|
|
|
$origVersion = $form->Version;
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$form->SubmitButtonText = 'Updated Button Text';
|
|
|
|
$form->write();
|
2017-08-15 23:31:47 +02:00
|
|
|
$form->publishRecursive();
|
2010-09-03 07:06:13 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// check published site
|
2017-08-11 02:36:28 +02:00
|
|
|
$updated = Versioned::get_one_by_stage(UserDefinedForm::class, 'Stage', "\"UserDefinedForm\".\"ID\" = $form->ID");
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals($updated->SubmitButtonText, 'Updated Button Text');
|
2010-09-03 07:06:13 +02:00
|
|
|
|
2022-11-28 21:36:08 +01:00
|
|
|
$form->rollbackRecursive($origVersion);
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2017-08-11 02:20:12 +02:00
|
|
|
$orignal = Versioned::get_one_by_stage(UserDefinedForm::class, 'Stage', "\"UserDefinedForm\".\"ID\" = $form->ID");
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals($orignal->SubmitButtonText, 'Button Text');
|
|
|
|
}
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testGetCMSFields()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2017-08-09 01:55:09 +02:00
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$fields = $form->getCMSFields();
|
2010-09-03 07:06:13 +02:00
|
|
|
|
2018-02-13 18:21:03 +01:00
|
|
|
$this->assertNotNull($fields->dataFieldByName('Fields'));
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('EmailRecipients'));
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('Submissions'));
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('OnCompleteMessage'));
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2010-09-03 07:06:13 +02:00
|
|
|
|
2017-08-09 01:47:47 +02:00
|
|
|
|
|
|
|
public function testGetCMSFieldsShowInSummary()
|
2017-01-30 11:24:00 +01:00
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2017-08-09 01:55:09 +02:00
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'summary-rules-form');
|
2017-01-30 11:24:00 +01:00
|
|
|
|
|
|
|
$fields = $form->getCMSFields();
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
$this->assertInstanceOf(GridField::class, $fields->dataFieldByName('Submissions'));
|
2017-01-30 11:24:00 +01:00
|
|
|
|
|
|
|
$submissionsgrid = $fields->dataFieldByName('Submissions');
|
2017-08-09 01:55:09 +02:00
|
|
|
$gridFieldDataColumns = $submissionsgrid->getConfig()->getComponentByType(GridFieldDataColumns::class);
|
2017-01-30 11:24:00 +01:00
|
|
|
|
|
|
|
$summaryFields = $gridFieldDataColumns->getDisplayFields($submissionsgrid);
|
|
|
|
|
2022-04-13 03:52:56 +02:00
|
|
|
$this->assertContains('SummaryShow', array_keys($summaryFields ?? []), 'Summary field not showing displayed field');
|
|
|
|
$this->assertNotContains('SummaryHide', array_keys($summaryFields ?? []), 'Summary field showing displayed field');
|
2017-01-30 11:24:00 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testEmailRecipientPopup()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2017-08-11 02:20:12 +02:00
|
|
|
$popup = new EmailRecipient();
|
2016-07-21 07:53:59 +02:00
|
|
|
$popup->FormID = $form->ID;
|
2018-03-19 03:09:13 +01:00
|
|
|
$popup->FormClass = UserDefinedForm::class;
|
2019-05-03 05:55:59 +02:00
|
|
|
$popup->EmailAddress = 'test@example.com';
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$fields = $popup->getCMSFields();
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2018-02-13 18:21:03 +01:00
|
|
|
$this->assertNotNull($fields->dataFieldByName('EmailSubject'));
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('EmailFrom'));
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('EmailAddress'));
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('HideFormData'));
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('SendPlain'));
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('EmailBody'));
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// add an email field, it should now add a or from X address picker
|
2017-08-09 01:55:09 +02:00
|
|
|
$email = $this->objFromFixture(EditableEmailField::class, 'email-field');
|
2016-07-21 07:53:59 +02:00
|
|
|
$form->Fields()->add($email);
|
2010-09-08 00:44:43 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$popup->write();
|
2010-09-03 07:06:13 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$fields = $popup->getCMSFields();
|
2017-08-09 01:55:09 +02:00
|
|
|
$this->assertThat($fields->dataFieldByName('SendEmailToFieldID'), $this->isInstanceOf(DropdownField::class));
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2018-02-20 15:32:14 +01:00
|
|
|
// if the front end has checkboxes or dropdown they can select from that can also be used to send things
|
2017-08-09 01:55:09 +02:00
|
|
|
$dropdown = $this->objFromFixture(EditableDropdown::class, 'department-dropdown');
|
2016-07-21 07:53:59 +02:00
|
|
|
$form->Fields()->add($dropdown);
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$fields = $popup->getCMSFields();
|
|
|
|
$this->assertTrue($fields->dataFieldByName('SendEmailToFieldID') !== null);
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$popup->delete();
|
|
|
|
}
|
2015-07-16 05:06:53 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testGetEmailBodyContent()
|
|
|
|
{
|
2017-08-11 02:20:12 +02:00
|
|
|
$recipient = new EmailRecipient();
|
2019-05-03 05:55:59 +02:00
|
|
|
$recipient->EmailAddress = 'test@example.com';
|
2015-07-16 05:06:53 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$emailBody = 'not html';
|
|
|
|
$emailBodyHtml = '<p>html</p>';
|
2015-07-16 05:06:53 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$recipient->EmailBody = $emailBody;
|
|
|
|
$recipient->EmailBodyHtml = $emailBodyHtml;
|
|
|
|
$recipient->write();
|
2015-07-16 05:06:53 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals($recipient->SendPlain, 0);
|
|
|
|
$this->assertEquals($recipient->getEmailBodyContent(), $emailBodyHtml);
|
2015-07-16 05:06:53 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$recipient->SendPlain = 1;
|
|
|
|
$recipient->write();
|
2015-07-16 05:06:53 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals($recipient->getEmailBodyContent(), $emailBody);
|
2015-07-16 05:06:53 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$recipient->delete();
|
|
|
|
}
|
2015-07-16 05:06:53 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testGetEmailTemplateDropdownValues()
|
|
|
|
{
|
2017-12-04 11:42:58 +01:00
|
|
|
$page = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
|
2017-08-11 02:20:12 +02:00
|
|
|
$recipient = new EmailRecipient();
|
2017-12-04 11:42:58 +01:00
|
|
|
$recipient->FormID = $page->ID;
|
2018-03-19 03:09:13 +01:00
|
|
|
$recipient->FormClass = UserDefinedForm::class;
|
2015-07-16 05:06:53 +02:00
|
|
|
|
2017-12-04 11:42:58 +01:00
|
|
|
$result = $recipient->getEmailTemplateDropdownValues();
|
2015-07-16 05:06:53 +02:00
|
|
|
|
2017-12-04 11:42:58 +01:00
|
|
|
// Installation path can be as a project when testing in Travis, so check partial match
|
2020-05-27 03:41:51 +02:00
|
|
|
$foundKey = false;
|
2022-04-13 03:52:56 +02:00
|
|
|
foreach (array_keys($result ?? []) as $key) {
|
|
|
|
if (strpos($key ?? '', 'email' . DIRECTORY_SEPARATOR . 'SubmittedFormEmail') !== false) {
|
2020-05-27 03:41:51 +02:00
|
|
|
$foundKey = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->assertTrue($foundKey);
|
2022-04-13 03:52:56 +02:00
|
|
|
$this->assertTrue(in_array('SubmittedFormEmail', array_values($result ?? [])));
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2015-07-16 05:06:53 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testEmailTemplateExists()
|
|
|
|
{
|
2017-12-04 11:42:58 +01:00
|
|
|
$page = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
|
2017-08-11 02:20:12 +02:00
|
|
|
$recipient = new EmailRecipient();
|
2017-12-04 11:42:58 +01:00
|
|
|
$recipient->FormID = $page->ID;
|
2018-03-19 03:09:13 +01:00
|
|
|
$recipient->FormClass = UserDefinedForm::class;
|
2019-05-03 05:55:59 +02:00
|
|
|
$recipient->EmailAddress = 'test@example.com';
|
2015-07-16 05:06:53 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Set the default template
|
2022-04-13 03:52:56 +02:00
|
|
|
$recipient->EmailTemplate = current(array_keys($recipient->getEmailTemplateDropdownValues() ?? []));
|
2016-07-21 07:53:59 +02:00
|
|
|
$recipient->write();
|
2015-07-16 05:06:53 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// The default template exists
|
|
|
|
$this->assertTrue($recipient->emailTemplateExists());
|
2015-07-16 05:06:53 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// A made up template doesn't exists
|
|
|
|
$this->assertFalse($recipient->emailTemplateExists('MyTemplateThatsNotThere'));
|
2015-07-16 05:06:53 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$recipient->delete();
|
|
|
|
}
|
2015-07-16 05:06:53 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testCanEditAndDeleteRecipient()
|
|
|
|
{
|
2017-08-09 01:55:09 +02:00
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
|
2010-09-08 00:44:43 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
foreach ($form->EmailRecipients() as $recipient) {
|
|
|
|
$this->assertTrue($recipient->canEdit());
|
|
|
|
$this->assertTrue($recipient->canDelete());
|
|
|
|
}
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2017-12-04 11:42:58 +01:00
|
|
|
$this->logOut();
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->logInWithPermission('SITETREE_VIEW_ALL');
|
2017-12-04 11:42:58 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
foreach ($form->EmailRecipients() as $recipient) {
|
|
|
|
$this->assertFalse($recipient->canEdit());
|
|
|
|
$this->assertFalse($recipient->canDelete());
|
|
|
|
}
|
|
|
|
}
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testPublishing()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
|
2016-07-21 07:53:59 +02:00
|
|
|
$form->write();
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2017-08-15 23:31:47 +02:00
|
|
|
$form->publishRecursive();
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2017-08-11 02:20:12 +02:00
|
|
|
$live = Versioned::get_one_by_stage(UserDefinedForm::class, 'Live', "\"UserDefinedForm_Live\".\"ID\" = $form->ID");
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertNotNull($live);
|
|
|
|
$this->assertEquals(2, $live->Fields()->Count()); // one page and one field
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
$dropdown = $this->objFromFixture(EditableDropdown::class, 'basic-dropdown');
|
2016-07-21 07:53:59 +02:00
|
|
|
$form->Fields()->add($dropdown);
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2017-08-11 02:20:12 +02:00
|
|
|
$stage = Versioned::get_one_by_stage(UserDefinedForm::class, 'Stage', "\"UserDefinedForm\".\"ID\" = $form->ID");
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals(3, $stage->Fields()->Count());
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// should not have published the dropdown
|
2017-08-11 02:20:12 +02:00
|
|
|
$liveDropdown = Versioned::get_one_by_stage(EditableFormField::class, 'Live', "\"EditableFormField_Live\".\"ID\" = $dropdown->ID");
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertNull($liveDropdown);
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// when publishing it should have added it
|
2017-08-15 23:31:47 +02:00
|
|
|
$form->publishRecursive();
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2017-08-11 02:20:12 +02:00
|
|
|
$live = Versioned::get_one_by_stage(UserDefinedForm::class, 'Live', "\"UserDefinedForm_Live\".\"ID\" = $form->ID");
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals(3, $live->Fields()->Count());
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// edit the title
|
|
|
|
$text = $form->Fields()->limit(1, 1)->First();
|
|
|
|
$text->Title = 'Edited title';
|
|
|
|
$text->write();
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2017-08-11 02:20:12 +02:00
|
|
|
$liveText = Versioned::get_one_by_stage(EditableFormField::class, 'Live', "\"EditableFormField_Live\".\"ID\" = $text->ID");
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertFalse($liveText->Title == $text->Title);
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2017-08-15 23:31:47 +02:00
|
|
|
$form->publishRecursive();
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2017-08-11 02:20:12 +02:00
|
|
|
$liveText = Versioned::get_one_by_stage(EditableFormField::class, 'Live', "\"EditableFormField_Live\".\"ID\" = $text->ID");
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertTrue($liveText->Title == $text->Title);
|
2015-09-18 01:07:58 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Add a display rule to the dropdown
|
|
|
|
$displayRule = new EditableCustomRule();
|
|
|
|
$displayRule->ParentID = $dropdown->ID;
|
|
|
|
$displayRule->ConditionFieldID = $text->ID;
|
|
|
|
$displayRule->write();
|
|
|
|
$ruleID = $displayRule->ID;
|
2015-09-18 01:07:58 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Not live
|
2017-08-11 02:20:12 +02:00
|
|
|
$liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEmpty($liveRule);
|
2015-09-18 01:07:58 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Publish form, it's now live
|
2017-08-15 23:31:47 +02:00
|
|
|
$form->publishRecursive();
|
2017-08-11 02:20:12 +02:00
|
|
|
$liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertNotEmpty($liveRule);
|
2015-09-18 01:07:58 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Remove rule
|
|
|
|
$displayRule->delete();
|
2015-09-18 01:07:58 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Live rule still exists
|
2017-08-11 02:20:12 +02:00
|
|
|
$liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertNotEmpty($liveRule);
|
2015-09-18 01:07:58 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Publish form, it should remove this rule
|
2018-06-19 06:04:43 +02:00
|
|
|
$form->publishRecursive();
|
|
|
|
$liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
|
|
|
|
$this->assertEmpty($liveRule);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2015-07-13 05:36:20 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testUnpublishing()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2017-08-09 01:55:09 +02:00
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
|
2016-07-21 07:53:59 +02:00
|
|
|
$form->write();
|
|
|
|
$this->assertEquals(0, DB::query("SELECT COUNT(*) FROM \"EditableFormField_Live\"")->value());
|
2017-08-15 23:31:47 +02:00
|
|
|
$form->publishRecursive();
|
2010-09-03 07:06:13 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// assert that it exists and has a field
|
2018-06-19 06:14:36 +02:00
|
|
|
$live = Versioned::get_one_by_stage(UserDefinedForm::class, 'Live', "\"UserDefinedForm_Live\".\"ID\" = $form->ID", false);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
$this->assertTrue(isset($live));
|
|
|
|
$this->assertEquals(2, DB::query("SELECT COUNT(*) FROM \"EditableFormField_Live\"")->value());
|
|
|
|
|
|
|
|
// unpublish
|
|
|
|
$form->doUnpublish();
|
|
|
|
|
2018-06-19 06:14:36 +02:00
|
|
|
$this->assertNull(Versioned::get_one_by_stage(UserDefinedForm::class, 'Live', "\"UserDefinedForm_Live\".\"ID\" = $form->ID", false));
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals(0, DB::query("SELECT COUNT(*) FROM \"EditableFormField_Live\"")->value());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoRevertToLive()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2017-08-09 01:55:09 +02:00
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
|
2016-07-21 07:53:59 +02:00
|
|
|
$field = $form->Fields()->First();
|
|
|
|
|
|
|
|
$field->Title = 'Title';
|
|
|
|
$field->write();
|
|
|
|
|
2017-08-15 23:31:47 +02:00
|
|
|
$form->publishRecursive();
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
$field->Title = 'Edited title';
|
|
|
|
$field->write();
|
|
|
|
|
|
|
|
// check that the published version is not updated
|
2017-08-11 02:20:12 +02:00
|
|
|
$live = Versioned::get_one_by_stage(EditableFormField::class, 'Live', "\"EditableFormField_Live\".\"ID\" = $field->ID");
|
2017-08-14 00:52:48 +02:00
|
|
|
$this->assertInstanceOf(EditableFormField::class, $live);
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals('Title', $live->Title);
|
|
|
|
|
|
|
|
// revert back to the live data
|
|
|
|
$form->doRevertToLive();
|
|
|
|
$form->flushCache();
|
|
|
|
|
2017-08-11 02:20:12 +02:00
|
|
|
$check = Versioned::get_one_by_stage(EditableFormField::class, 'Stage', "\"EditableFormField\".\"ID\" = $field->ID");
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
$this->assertEquals('Title', $check->Title);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDuplicatingForm()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2017-08-09 01:55:09 +02:00
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
$duplicate = $form->duplicate();
|
|
|
|
|
|
|
|
$this->assertEquals($form->Fields()->Count(), $duplicate->Fields()->Count());
|
|
|
|
|
|
|
|
// can't compare object since the dates/ids change
|
|
|
|
$this->assertEquals($form->Fields()->First()->Title, $duplicate->Fields()->First()->Title);
|
|
|
|
|
|
|
|
// Test duplicate with group
|
2017-08-09 01:55:09 +02:00
|
|
|
$form2 = $this->objFromFixture(UserDefinedForm::class, 'page-with-group');
|
2016-07-21 07:53:59 +02:00
|
|
|
$form2Validator = new UserFormValidator();
|
2017-08-09 01:55:09 +02:00
|
|
|
$form2Validator->setForm(new Form(new Controller(), Form::class, new FieldList(), new FieldList()));
|
2024-08-28 01:32:21 +02:00
|
|
|
$this->assertTrue($form2Validator->php($form2->toMap()), json_encode($form2Validator->getResult()->getMessages()));
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
// Check field groups exist
|
2017-08-09 01:55:09 +02:00
|
|
|
$form2GroupStart = $form2->Fields()->filter('ClassName', EditableFieldGroup::class)->first();
|
|
|
|
$form2GroupEnd = $form2->Fields()->filter('ClassName', EditableFieldGroupEnd::class)->first();
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals($form2GroupEnd->ID, $form2GroupStart->EndID);
|
|
|
|
|
|
|
|
// Duplicate this
|
|
|
|
$form3 = $form2->duplicate();
|
|
|
|
$form3Validator = new UserFormValidator();
|
2017-08-09 01:55:09 +02:00
|
|
|
$form3Validator->setForm(new Form(new Controller(), Form::class, new FieldList(), new FieldList()));
|
2024-08-28 01:32:21 +02:00
|
|
|
$this->assertTrue($form3Validator->php($form3->toMap()), json_encode($form3Validator->getResult()->getMessages()));
|
2016-07-21 07:53:59 +02:00
|
|
|
// Check field groups exist
|
2017-08-09 01:55:09 +02:00
|
|
|
$form3GroupStart = $form3->Fields()->filter('ClassName', EditableFieldGroup::class)->first();
|
|
|
|
$form3GroupEnd = $form3->Fields()->filter('ClassName', EditableFieldGroupEnd::class)->first();
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals($form3GroupEnd->ID, $form3GroupStart->EndID);
|
|
|
|
$this->assertNotEquals($form2GroupEnd->ID, $form3GroupStart->EndID);
|
|
|
|
}
|
|
|
|
|
2018-03-22 23:43:50 +01:00
|
|
|
public function testDuplicateFormDuplicatesRecursively()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
/** @var UserDefinedForm $form */
|
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'form-with-multioptions');
|
|
|
|
|
2018-03-23 01:31:38 +01:00
|
|
|
$this->assertGreaterThanOrEqual(1, $form->Fields()->count(), 'Fixtured page has a field');
|
2018-03-22 23:43:50 +01:00
|
|
|
$this->assertCount(
|
|
|
|
2,
|
|
|
|
$form->Fields()->Last()->Options(),
|
|
|
|
'Fixtured multiple option field has two options'
|
|
|
|
);
|
|
|
|
|
|
|
|
$newForm = $form->duplicate();
|
|
|
|
$this->assertEquals(
|
|
|
|
$form->Fields()->count(),
|
|
|
|
$newForm->Fields()->count(),
|
|
|
|
'Duplicated page has same number of fields'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$form->Fields()->Last()->Options()->count(),
|
|
|
|
$newForm->Fields()->Last()->Options()->count(),
|
|
|
|
'Duplicated dropdown field from duplicated form has duplicated options'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testFormOptions()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2017-08-09 01:55:09 +02:00
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
$fields = $form->getFormOptions();
|
|
|
|
$submit = $fields->fieldByName('SubmitButtonText');
|
|
|
|
$reset = $fields->fieldByName('ShowClearButton');
|
|
|
|
|
|
|
|
$this->assertEquals($submit->Title(), 'Text on submit button:');
|
|
|
|
$this->assertEquals($reset->Title(), 'Show Clear Form Button');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmailRecipientFilters()
|
|
|
|
{
|
2017-05-15 04:05:18 +02:00
|
|
|
/** @var UserDefinedForm $form */
|
2017-08-09 01:55:09 +02:00
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'filtered-form-page');
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
// Check unfiltered recipients
|
|
|
|
$result0 = $form
|
|
|
|
->EmailRecipients()
|
|
|
|
->sort('EmailAddress')
|
|
|
|
->column('EmailAddress');
|
|
|
|
$this->assertEquals(
|
2017-08-11 02:20:12 +02:00
|
|
|
[
|
2016-07-21 07:53:59 +02:00
|
|
|
'filtered1@example.com',
|
|
|
|
'filtered2@example.com',
|
|
|
|
'unfiltered@example.com'
|
2017-08-11 02:20:12 +02:00
|
|
|
],
|
2016-07-21 07:53:59 +02:00
|
|
|
$result0
|
|
|
|
);
|
|
|
|
|
|
|
|
// check filters based on given data
|
|
|
|
$result1 = $form->FilteredEmailRecipients(
|
2017-08-11 02:20:12 +02:00
|
|
|
[
|
2016-07-21 07:53:59 +02:00
|
|
|
'your-name' => 'Value',
|
|
|
|
'address' => '',
|
|
|
|
'street' => 'Anything',
|
|
|
|
'city' => 'Matches Not Equals',
|
2017-08-11 02:20:12 +02:00
|
|
|
'colours' => ['Red'] // matches 2
|
2017-08-11 02:37:03 +02:00
|
|
|
],
|
|
|
|
null
|
2016-07-21 07:53:59 +02:00
|
|
|
)
|
|
|
|
->sort('EmailAddress')
|
|
|
|
->column('EmailAddress');
|
|
|
|
$this->assertEquals(
|
2017-08-11 02:20:12 +02:00
|
|
|
[
|
2016-07-21 07:53:59 +02:00
|
|
|
'filtered2@example.com',
|
|
|
|
'unfiltered@example.com'
|
2017-08-11 02:20:12 +02:00
|
|
|
],
|
2016-07-21 07:53:59 +02:00
|
|
|
$result1
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check all positive matches
|
|
|
|
$result2 = $form->FilteredEmailRecipients(
|
2017-08-11 02:20:12 +02:00
|
|
|
[
|
2016-07-21 07:53:59 +02:00
|
|
|
'your-name' => '',
|
|
|
|
'address' => 'Anything',
|
|
|
|
'street' => 'Matches Equals',
|
|
|
|
'city' => 'Anything',
|
2017-08-11 02:20:12 +02:00
|
|
|
'colours' => ['Red', 'Blue'] // matches 2
|
2017-08-11 02:37:03 +02:00
|
|
|
],
|
|
|
|
null
|
2016-07-21 07:53:59 +02:00
|
|
|
)
|
|
|
|
->sort('EmailAddress')
|
|
|
|
->column('EmailAddress');
|
|
|
|
$this->assertEquals(
|
2017-08-11 02:20:12 +02:00
|
|
|
[
|
2016-07-21 07:53:59 +02:00
|
|
|
'filtered1@example.com',
|
|
|
|
'filtered2@example.com',
|
|
|
|
'unfiltered@example.com'
|
2017-08-11 02:20:12 +02:00
|
|
|
],
|
2016-07-21 07:53:59 +02:00
|
|
|
$result2
|
|
|
|
);
|
|
|
|
|
|
|
|
$result3 = $form->FilteredEmailRecipients(
|
2017-08-11 02:20:12 +02:00
|
|
|
[
|
2016-07-21 07:53:59 +02:00
|
|
|
'your-name' => 'Should be blank but is not',
|
|
|
|
'address' => 'Anything',
|
|
|
|
'street' => 'Matches Equals',
|
|
|
|
'city' => 'Anything',
|
2017-08-11 02:20:12 +02:00
|
|
|
'colours' => ['Blue']
|
2017-08-11 02:37:03 +02:00
|
|
|
],
|
|
|
|
null
|
2016-07-21 07:53:59 +02:00
|
|
|
)->column('EmailAddress');
|
|
|
|
$this->assertEquals(
|
2017-08-11 02:20:12 +02:00
|
|
|
[
|
2016-07-21 07:53:59 +02:00
|
|
|
'unfiltered@example.com'
|
2017-08-11 02:20:12 +02:00
|
|
|
],
|
2016-07-21 07:53:59 +02:00
|
|
|
$result3
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$result4 = $form->FilteredEmailRecipients(
|
2017-08-11 02:20:12 +02:00
|
|
|
[
|
2016-07-21 07:53:59 +02:00
|
|
|
'your-name' => '',
|
|
|
|
'address' => 'Anything',
|
|
|
|
'street' => 'Wrong value for this field',
|
|
|
|
'city' => '',
|
2017-08-11 02:20:12 +02:00
|
|
|
'colours' => ['Blue', 'Green']
|
2017-08-11 02:37:03 +02:00
|
|
|
],
|
|
|
|
null
|
2016-07-21 07:53:59 +02:00
|
|
|
)->column('EmailAddress');
|
|
|
|
$this->assertEquals(
|
2017-08-11 02:20:12 +02:00
|
|
|
['unfiltered@example.com'],
|
2016-07-21 07:53:59 +02:00
|
|
|
$result4
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIndex()
|
|
|
|
{
|
|
|
|
// Test that the $UserDefinedForm is stripped out
|
2017-08-09 01:55:09 +02:00
|
|
|
$page = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
|
2022-11-28 21:36:08 +01:00
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
$result = $this->get($page->Link());
|
|
|
|
$body = Convert::nl2os($result->getBody(), ''); // strip out newlines
|
|
|
|
$this->assertFalse($result->isError());
|
2021-11-01 21:52:58 +01:00
|
|
|
$this->assertStringContainsString('<p>Here is my form</p><form', $body);
|
|
|
|
$this->assertStringContainsString('</form><p>Thank you for filling it out</p>', $body);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2021-11-01 21:52:58 +01:00
|
|
|
$this->assertStringNotContainsString('<p>$UserDefinedForm</p>', $body);
|
|
|
|
$this->assertStringNotContainsString('<p></p>', $body);
|
|
|
|
$this->assertStringNotContainsString('</p><p>Thank you for filling it out</p>', $body);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2017-01-08 23:06:17 +01:00
|
|
|
|
|
|
|
public function testEmailAddressValidation()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
|
|
|
|
// test invalid email addresses fail validation
|
2017-08-11 02:37:03 +02:00
|
|
|
$recipient = $this->objFromFixture(
|
|
|
|
EmailRecipient::class,
|
|
|
|
'invalid-recipient-list'
|
|
|
|
);
|
2017-01-08 23:06:17 +01:00
|
|
|
$result = $recipient->validate();
|
2017-08-14 00:52:48 +02:00
|
|
|
$this->assertFalse($result->isValid());
|
2017-08-15 00:49:13 +02:00
|
|
|
$this->assertNotEmpty($result->getMessages());
|
2021-11-01 21:52:58 +01:00
|
|
|
$this->assertStringContainsString('filtered.example.com', $result->getMessages()[0]['message']);
|
|
|
|
$this->assertStringNotContainsString('filtered2@example.com', $result->getMessages()[0]['message']);
|
2017-01-08 23:06:17 +01:00
|
|
|
|
|
|
|
// test valid email addresses pass validation
|
2017-08-11 02:37:03 +02:00
|
|
|
$recipient = $this->objFromFixture(
|
|
|
|
EmailRecipient::class,
|
|
|
|
'valid-recipient-list'
|
|
|
|
);
|
2017-01-08 23:06:17 +01:00
|
|
|
$result = $recipient->validate();
|
2017-08-14 00:52:48 +02:00
|
|
|
$this->assertTrue($result->isValid());
|
|
|
|
$this->assertEmpty($result->getMessages());
|
2017-01-08 23:06:17 +01:00
|
|
|
}
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|