2008-04-06 05:52:14 +02:00
|
|
|
|
<?php
|
2013-05-26 01:09:03 +02:00
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
|
|
|
|
|
use SilverStripe\Forms\Tests\FormTest\TestController;
|
|
|
|
|
use SilverStripe\Forms\Tests\FormTest\ControllerWithSecurityToken;
|
|
|
|
|
use SilverStripe\Forms\Tests\FormTest\ControllerWithStrictPostCheck;
|
|
|
|
|
use SilverStripe\Forms\Tests\FormTest\Player;
|
|
|
|
|
use SilverStripe\Forms\Tests\FormTest\Team;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
use SilverStripe\ORM\DataModel;
|
2016-10-14 03:30:05 +02:00
|
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\Team;
|
2016-06-23 01:37:22 +02:00
|
|
|
|
use SilverStripe\Security\SecurityToken;
|
|
|
|
|
use SilverStripe\Security\RandomGenerator;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
|
use SilverStripe\Control\Controller;
|
2016-09-09 08:43:05 +02:00
|
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
|
use SilverStripe\Forms\HeaderField;
|
|
|
|
|
use SilverStripe\Forms\TextareaField;
|
|
|
|
|
use SilverStripe\Forms\DateField;
|
|
|
|
|
use SilverStripe\Forms\NumericField;
|
|
|
|
|
use SilverStripe\Forms\LookupField;
|
|
|
|
|
use SilverStripe\Forms\FileField;
|
|
|
|
|
use SilverStripe\Forms\FormAction;
|
|
|
|
|
use SilverStripe\View\SSViewer;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
|
|
2008-06-15 15:33:53 +02:00
|
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
|
* @skipUpgrade
|
2008-06-15 15:33:53 +02:00
|
|
|
|
*/
|
2008-10-06 16:58:01 +02:00
|
|
|
|
class FormTest extends FunctionalTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
|
protected static $fixture_file = 'FormTest.yml';
|
2010-04-12 04:03:16 +02:00
|
|
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
2016-10-14 03:30:05 +02:00
|
|
|
|
Player::class,
|
|
|
|
|
Team::class,
|
2010-04-12 04:03:16 +02:00
|
|
|
|
);
|
2013-01-29 14:14:47 +01:00
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
|
protected $extraControllers = [
|
|
|
|
|
TestController::class,
|
|
|
|
|
ControllerWithSecurityToken::class,
|
|
|
|
|
ControllerWithStrictPostCheck::class,
|
|
|
|
|
];
|
|
|
|
|
|
2013-05-30 11:05:39 +02:00
|
|
|
|
public function setUp() {
|
2013-01-29 14:14:47 +01:00
|
|
|
|
parent::setUp();
|
|
|
|
|
|
2015-09-17 04:23:52 +02:00
|
|
|
|
// Suppress themes
|
2016-10-14 03:30:05 +02:00
|
|
|
|
SSViewer::set_themes([
|
|
|
|
|
SSViewer::DEFAULT_THEME
|
|
|
|
|
]);
|
2013-01-29 14:14:47 +01:00
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2008-04-06 05:52:14 +02:00
|
|
|
|
public function testLoadDataFromRequest() {
|
|
|
|
|
$form = new Form(
|
|
|
|
|
new Controller(),
|
|
|
|
|
'Form',
|
2011-05-11 09:51:54 +02:00
|
|
|
|
new FieldList(
|
2008-04-06 05:52:14 +02:00
|
|
|
|
new TextField('key1'),
|
|
|
|
|
new TextField('namespace[key2]'),
|
|
|
|
|
new TextField('namespace[key3][key4]'),
|
|
|
|
|
new TextField('othernamespace[key5][key6][key7]')
|
|
|
|
|
),
|
2011-05-11 09:51:54 +02:00
|
|
|
|
new FieldList()
|
2008-04-06 05:52:14 +02:00
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2008-04-06 05:52:14 +02:00
|
|
|
|
// url would be ?key1=val1&namespace[key2]=val2&namespace[key3][key4]=val4&othernamespace[key5][key6][key7]=val7
|
|
|
|
|
$requestData = array(
|
|
|
|
|
'key1' => 'val1',
|
|
|
|
|
'namespace' => array(
|
|
|
|
|
'key2' => 'val2',
|
|
|
|
|
'key3' => array(
|
|
|
|
|
'key4' => 'val4',
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
'othernamespace' => array(
|
|
|
|
|
'key5' => array(
|
|
|
|
|
'key6' =>array(
|
|
|
|
|
'key7' => 'val7'
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2008-04-06 05:52:14 +02:00
|
|
|
|
$form->loadDataFrom($requestData);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2008-04-06 05:52:14 +02:00
|
|
|
|
$fields = $form->Fields();
|
|
|
|
|
$this->assertEquals($fields->fieldByName('key1')->Value(), 'val1');
|
|
|
|
|
$this->assertEquals($fields->fieldByName('namespace[key2]')->Value(), 'val2');
|
|
|
|
|
$this->assertEquals($fields->fieldByName('namespace[key3][key4]')->Value(), 'val4');
|
|
|
|
|
$this->assertEquals($fields->fieldByName('othernamespace[key5][key6][key7]')->Value(), 'val7');
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2008-10-07 19:44:12 +02:00
|
|
|
|
public function testLoadDataFromUnchangedHandling() {
|
|
|
|
|
$form = new Form(
|
|
|
|
|
new Controller(),
|
|
|
|
|
'Form',
|
2011-05-11 09:51:54 +02:00
|
|
|
|
new FieldList(
|
2008-10-07 19:44:12 +02:00
|
|
|
|
new TextField('key1'),
|
|
|
|
|
new TextField('key2')
|
|
|
|
|
),
|
2011-05-11 09:51:54 +02:00
|
|
|
|
new FieldList()
|
2008-10-07 19:44:12 +02:00
|
|
|
|
);
|
|
|
|
|
$form->loadDataFrom(array(
|
|
|
|
|
'key1' => 'save',
|
|
|
|
|
'key2' => 'dontsave',
|
|
|
|
|
'key2_unchanged' => '1'
|
|
|
|
|
));
|
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
|
$form->getData(),
|
2008-10-07 19:44:12 +02:00
|
|
|
|
array(
|
|
|
|
|
'key1' => 'save',
|
|
|
|
|
'key2' => null,
|
|
|
|
|
),
|
|
|
|
|
'loadDataFrom() doesnt save a field if a matching "<fieldname>_unchanged" flag is set'
|
|
|
|
|
);
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2008-10-07 19:23:50 +02:00
|
|
|
|
public function testLoadDataFromObject() {
|
|
|
|
|
$form = new Form(
|
2013-05-23 09:01:58 +02:00
|
|
|
|
new Controller(),
|
2008-10-07 19:23:50 +02:00
|
|
|
|
'Form',
|
2011-05-11 09:51:54 +02:00
|
|
|
|
new FieldList(
|
2008-10-16 15:26:50 +02:00
|
|
|
|
new HeaderField('MyPlayerHeader','My Player'),
|
2008-10-07 19:23:50 +02:00
|
|
|
|
new TextField('Name'), // appears in both Player and Team
|
|
|
|
|
new TextareaField('Biography'),
|
|
|
|
|
new DateField('Birthday'),
|
|
|
|
|
new NumericField('BirthdayYear') // dynamic property
|
|
|
|
|
),
|
2011-05-11 09:51:54 +02:00
|
|
|
|
new FieldList()
|
2008-10-07 19:23:50 +02:00
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
|
$captainWithDetails = $this->objFromFixture(Player::class, 'captainWithDetails');
|
2008-10-09 16:40:13 +02:00
|
|
|
|
$form->loadDataFrom($captainWithDetails);
|
2008-10-07 19:23:50 +02:00
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
|
$form->getData(),
|
2008-10-07 19:23:50 +02:00
|
|
|
|
array(
|
2008-10-09 16:40:13 +02:00
|
|
|
|
'Name' => 'Captain Details',
|
2008-10-07 19:23:50 +02:00
|
|
|
|
'Biography' => 'Bio 1',
|
2014-08-15 08:53:05 +02:00
|
|
|
|
'Birthday' => '1982-01-01',
|
|
|
|
|
'BirthdayYear' => '1982',
|
2008-10-07 19:23:50 +02:00
|
|
|
|
),
|
|
|
|
|
'LoadDataFrom() loads simple fields and dynamic getters'
|
|
|
|
|
);
|
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
|
$captainNoDetails = $this->objFromFixture(Player::class, 'captainNoDetails');
|
2008-10-09 16:40:13 +02:00
|
|
|
|
$form->loadDataFrom($captainNoDetails);
|
2008-10-07 19:23:50 +02:00
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
|
$form->getData(),
|
2008-10-07 19:23:50 +02:00
|
|
|
|
array(
|
2008-10-09 16:40:13 +02:00
|
|
|
|
'Name' => 'Captain No Details',
|
|
|
|
|
'Biography' => null,
|
2014-08-15 08:53:05 +02:00
|
|
|
|
'Birthday' => null,
|
|
|
|
|
'BirthdayYear' => 0,
|
2008-10-09 16:40:13 +02:00
|
|
|
|
),
|
|
|
|
|
'LoadNonBlankDataFrom() loads only fields with values, and doesnt overwrite existing values'
|
|
|
|
|
);
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2008-10-09 16:40:13 +02:00
|
|
|
|
public function testLoadDataFromClearMissingFields() {
|
|
|
|
|
$form = new Form(
|
|
|
|
|
new Controller(),
|
|
|
|
|
'Form',
|
2011-05-11 09:51:54 +02:00
|
|
|
|
new FieldList(
|
2008-10-16 15:26:50 +02:00
|
|
|
|
new HeaderField('MyPlayerHeader','My Player'),
|
2008-10-09 16:40:13 +02:00
|
|
|
|
new TextField('Name'), // appears in both Player and Team
|
|
|
|
|
new TextareaField('Biography'),
|
|
|
|
|
new DateField('Birthday'),
|
|
|
|
|
new NumericField('BirthdayYear'), // dynamic property
|
|
|
|
|
$unrelatedField = new TextField('UnrelatedFormField')
|
|
|
|
|
//new CheckboxSetField('Teams') // relation editing
|
|
|
|
|
),
|
2011-05-11 09:51:54 +02:00
|
|
|
|
new FieldList()
|
2008-10-09 16:40:13 +02:00
|
|
|
|
);
|
|
|
|
|
$unrelatedField->setValue("random value");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
|
$captainWithDetails = $this->objFromFixture(Player::class, 'captainWithDetails');
|
2008-10-09 16:40:13 +02:00
|
|
|
|
$form->loadDataFrom($captainWithDetails);
|
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
|
$form->getData(),
|
2008-10-09 16:40:13 +02:00
|
|
|
|
array(
|
|
|
|
|
'Name' => 'Captain Details',
|
2008-10-07 19:23:50 +02:00
|
|
|
|
'Biography' => 'Bio 1',
|
2014-08-15 08:53:05 +02:00
|
|
|
|
'Birthday' => '1982-01-01',
|
2008-10-09 16:40:13 +02:00
|
|
|
|
'BirthdayYear' => '1982',
|
|
|
|
|
'UnrelatedFormField' => 'random value',
|
2008-10-07 19:23:50 +02:00
|
|
|
|
),
|
2008-10-09 16:40:13 +02:00
|
|
|
|
'LoadDataFrom() doesnt overwrite fields not found in the object'
|
2008-10-07 19:23:50 +02:00
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
|
$captainWithDetails = $this->objFromFixture(Player::class, 'captainNoDetails');
|
|
|
|
|
$team2 = $this->objFromFixture(Team::class, 'team2');
|
2008-10-09 16:40:13 +02:00
|
|
|
|
$form->loadDataFrom($captainWithDetails);
|
2012-11-15 23:47:32 +01:00
|
|
|
|
$form->loadDataFrom($team2, Form::MERGE_CLEAR_MISSING);
|
2008-10-07 19:23:50 +02:00
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
|
$form->getData(),
|
2008-10-07 19:23:50 +02:00
|
|
|
|
array(
|
2008-10-09 16:40:13 +02:00
|
|
|
|
'Name' => 'Team 2',
|
2008-10-07 19:23:50 +02:00
|
|
|
|
'Biography' => '',
|
2014-08-15 08:53:05 +02:00
|
|
|
|
'Birthday' => '',
|
|
|
|
|
'BirthdayYear' => 0,
|
2008-10-09 16:40:13 +02:00
|
|
|
|
'UnrelatedFormField' => null,
|
2008-10-07 19:23:50 +02:00
|
|
|
|
),
|
2008-10-09 16:40:13 +02:00
|
|
|
|
'LoadDataFrom() overwrites fields not found in the object with $clearMissingFields=true'
|
2008-10-07 19:23:50 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
2012-11-15 23:47:32 +01:00
|
|
|
|
|
2015-12-24 04:30:50 +01:00
|
|
|
|
public function testLookupFieldDisabledSaving() {
|
2016-10-14 03:30:05 +02:00
|
|
|
|
$object = new Team();
|
2015-12-24 04:30:50 +01:00
|
|
|
|
$form = new Form(
|
|
|
|
|
new Controller(),
|
|
|
|
|
'Form',
|
|
|
|
|
new FieldList(
|
|
|
|
|
new LookupField('Players', 'Players')
|
|
|
|
|
),
|
|
|
|
|
new FieldList()
|
|
|
|
|
);
|
|
|
|
|
$form->loadDataFrom(array(
|
|
|
|
|
'Players' => array(
|
2016-01-06 00:34:58 +01:00
|
|
|
|
14,
|
|
|
|
|
18,
|
2015-12-24 04:30:50 +01:00
|
|
|
|
22
|
|
|
|
|
),
|
|
|
|
|
));
|
|
|
|
|
$form->saveInto($object);
|
|
|
|
|
$playersIds = $object->Players()->getIDList();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($form->validate());
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$playersIds,
|
|
|
|
|
array(),
|
|
|
|
|
'saveInto() should not save into the DataObject for the LookupField'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-15 23:47:32 +01:00
|
|
|
|
public function testLoadDataFromIgnoreFalseish() {
|
|
|
|
|
$form = new Form(
|
|
|
|
|
new Controller(),
|
|
|
|
|
'Form',
|
|
|
|
|
new FieldList(
|
|
|
|
|
new TextField('Biography', 'Biography', 'Custom Default')
|
|
|
|
|
),
|
|
|
|
|
new FieldList()
|
|
|
|
|
);
|
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
|
$captainNoDetails = $this->objFromFixture(Player::class, 'captainNoDetails');
|
|
|
|
|
$captainWithDetails = $this->objFromFixture(Player::class, 'captainWithDetails');
|
2012-11-15 23:47:32 +01:00
|
|
|
|
|
|
|
|
|
$form->loadDataFrom($captainNoDetails, Form::MERGE_IGNORE_FALSEISH);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$form->getData(),
|
|
|
|
|
array('Biography' => 'Custom Default'),
|
|
|
|
|
'LoadDataFrom() doesn\'t overwrite fields when MERGE_IGNORE_FALSEISH set and values are false-ish'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$form->loadDataFrom($captainWithDetails, Form::MERGE_IGNORE_FALSEISH);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$form->getData(),
|
|
|
|
|
array('Biography' => 'Bio 1'),
|
|
|
|
|
'LoadDataFrom() does overwrite fields when MERGE_IGNORE_FALSEISH set and values arent false-ish'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-06 16:58:01 +02:00
|
|
|
|
public function testFormMethodOverride() {
|
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
$form->setFormMethod('GET');
|
2012-01-02 15:03:35 +01:00
|
|
|
|
$this->assertNull($form->Fields()->dataFieldByName('_method'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2008-10-06 16:58:01 +02:00
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
$form->setFormMethod('PUT');
|
2013-05-09 02:27:16 +02:00
|
|
|
|
$this->assertEquals($form->Fields()->dataFieldByName('_method')->Value(), 'PUT',
|
2008-10-06 16:58:01 +02:00
|
|
|
|
'PUT override in forms has PUT in hiddenfield'
|
|
|
|
|
);
|
2013-05-09 02:27:16 +02:00
|
|
|
|
$this->assertEquals($form->FormMethod(), 'POST',
|
2008-10-06 16:58:01 +02:00
|
|
|
|
'PUT override in forms has POST in <form> tag'
|
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2008-10-06 16:58:01 +02:00
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
$form->setFormMethod('DELETE');
|
2013-05-09 02:27:16 +02:00
|
|
|
|
$this->assertEquals($form->Fields()->dataFieldByName('_method')->Value(), 'DELETE',
|
2008-10-06 16:58:01 +02:00
|
|
|
|
'PUT override in forms has PUT in hiddenfield'
|
|
|
|
|
);
|
2013-05-09 02:27:16 +02:00
|
|
|
|
$this->assertEquals($form->FormMethod(), 'POST',
|
2008-10-06 16:58:01 +02:00
|
|
|
|
'PUT override in forms has POST in <form> tag'
|
|
|
|
|
);
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2014-11-26 10:49:37 +01:00
|
|
|
|
public function testValidationExemptActions() {
|
2016-10-14 03:30:05 +02:00
|
|
|
|
$this->get('FormTest_Controller');
|
2014-11-26 10:49:37 +01:00
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
|
$this->submitForm(
|
2014-11-26 10:49:37 +01:00
|
|
|
|
'Form_Form',
|
|
|
|
|
'action_doSubmit',
|
|
|
|
|
array(
|
|
|
|
|
'Email' => 'test@test.com'
|
|
|
|
|
)
|
|
|
|
|
);
|
2016-03-08 21:50:18 +01:00
|
|
|
|
|
2014-11-26 10:49:37 +01:00
|
|
|
|
// Firstly, assert that required fields still work when not using an exempt action
|
|
|
|
|
$this->assertPartialMatchBySelector(
|
|
|
|
|
'#Form_Form_SomeRequiredField_Holder .required',
|
|
|
|
|
array('"Some Required Field" is required'),
|
|
|
|
|
'Required fields show a notification on field when left blank'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Re-submit the form using validation-exempt button
|
2016-10-14 03:30:05 +02:00
|
|
|
|
$this->submitForm(
|
2014-11-26 10:49:37 +01:00
|
|
|
|
'Form_Form',
|
|
|
|
|
'action_doSubmitValidationExempt',
|
|
|
|
|
array(
|
|
|
|
|
'Email' => 'test@test.com'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// The required message should be empty if validation was skipped
|
|
|
|
|
$items = $this->cssParser()->getBySelector('#Form_Form_SomeRequiredField_Holder .required');
|
|
|
|
|
$this->assertEmpty($items);
|
|
|
|
|
|
|
|
|
|
// And the session message should show up is submitted successfully
|
|
|
|
|
$this->assertPartialMatchBySelector(
|
|
|
|
|
'#Form_Form_error',
|
|
|
|
|
array(
|
|
|
|
|
'Validation skipped'
|
|
|
|
|
),
|
|
|
|
|
'Form->sessionMessage() shows up after reloading the form'
|
|
|
|
|
);
|
2016-04-22 04:46:18 +02:00
|
|
|
|
|
|
|
|
|
// Test this same behaviour, but with a form-action exempted via instance
|
2016-10-14 03:30:05 +02:00
|
|
|
|
$this->submitForm(
|
2016-04-22 04:46:18 +02:00
|
|
|
|
'Form_Form',
|
|
|
|
|
'action_doSubmitActionExempt',
|
|
|
|
|
array(
|
|
|
|
|
'Email' => 'test@test.com'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// The required message should be empty if validation was skipped
|
|
|
|
|
$items = $this->cssParser()->getBySelector('#Form_Form_SomeRequiredField_Holder .required');
|
|
|
|
|
$this->assertEmpty($items);
|
|
|
|
|
|
|
|
|
|
// And the session message should show up is submitted successfully
|
|
|
|
|
$this->assertPartialMatchBySelector(
|
|
|
|
|
'#Form_Form_error',
|
|
|
|
|
array(
|
|
|
|
|
'Validation bypassed!'
|
|
|
|
|
),
|
|
|
|
|
'Form->sessionMessage() shows up after reloading the form'
|
|
|
|
|
);
|
2014-11-26 10:49:37 +01:00
|
|
|
|
}
|
2016-03-08 21:50:18 +01:00
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testSessionValidationMessage() {
|
2008-10-14 23:37:51 +02:00
|
|
|
|
$this->get('FormTest_Controller');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-04-20 05:02:43 +02:00
|
|
|
|
$response = $this->post(
|
|
|
|
|
'FormTest_Controller/Form',
|
2008-10-14 23:37:51 +02:00
|
|
|
|
array(
|
|
|
|
|
'Email' => 'invalid',
|
2015-11-11 03:18:26 +01:00
|
|
|
|
'Number' => '<a href="http://mysite.com">link</a>' // XSS attempt
|
2008-10-14 23:37:51 +02:00
|
|
|
|
// leaving out "Required" field
|
|
|
|
|
)
|
|
|
|
|
);
|
2013-05-30 11:05:39 +02:00
|
|
|
|
|
2008-10-14 23:37:51 +02:00
|
|
|
|
$this->assertPartialMatchBySelector(
|
2013-05-30 11:05:39 +02:00
|
|
|
|
'#Form_Form_Email_Holder span.message',
|
2008-10-14 23:37:51 +02:00
|
|
|
|
array(
|
2012-06-01 03:13:06 +02:00
|
|
|
|
'Please enter an email address'
|
2008-10-14 23:37:51 +02:00
|
|
|
|
),
|
|
|
|
|
'Formfield validation shows note on field if invalid'
|
|
|
|
|
);
|
|
|
|
|
$this->assertPartialMatchBySelector(
|
2013-05-30 11:05:39 +02:00
|
|
|
|
'#Form_Form_SomeRequiredField_Holder span.required',
|
2008-10-14 23:37:51 +02:00
|
|
|
|
array(
|
2012-09-25 16:12:53 +02:00
|
|
|
|
'"Some Required Field" is required'
|
2008-10-14 23:37:51 +02:00
|
|
|
|
),
|
|
|
|
|
'Required fields show a notification on field when left blank'
|
|
|
|
|
);
|
2015-11-11 03:18:26 +01:00
|
|
|
|
|
|
|
|
|
$this->assertContains(
|
|
|
|
|
''<a href="http://mysite.com">link</a>' is not a number, only numbers can be accepted for this field',
|
|
|
|
|
$response->getBody(),
|
|
|
|
|
"Validation messages are safely XML encoded"
|
|
|
|
|
);
|
|
|
|
|
$this->assertNotContains(
|
|
|
|
|
'<a href="http://mysite.com">link</a>',
|
|
|
|
|
$response->getBody(),
|
|
|
|
|
"Unsafe content is not emitted directly inside the response body"
|
|
|
|
|
);
|
2008-10-14 23:37:51 +02:00
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testSessionSuccessMessage() {
|
2008-10-14 23:37:51 +02:00
|
|
|
|
$this->get('FormTest_Controller');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-04-20 05:02:43 +02:00
|
|
|
|
$response = $this->post(
|
|
|
|
|
'FormTest_Controller/Form',
|
2008-10-14 23:37:51 +02:00
|
|
|
|
array(
|
|
|
|
|
'Email' => 'test@test.com',
|
|
|
|
|
'SomeRequiredField' => 'test',
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
$this->assertPartialMatchBySelector(
|
|
|
|
|
'#Form_Form_error',
|
|
|
|
|
array(
|
|
|
|
|
'Test save was successful'
|
|
|
|
|
),
|
|
|
|
|
'Form->sessionMessage() shows up after reloading the form'
|
|
|
|
|
);
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testGloballyDisabledSecurityTokenInheritsToNewForm() {
|
2010-12-05 09:22:57 +01:00
|
|
|
|
SecurityToken::enable();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2010-12-05 09:22:57 +01:00
|
|
|
|
$form1 = $this->getStubForm();
|
2016-06-23 01:37:22 +02:00
|
|
|
|
$this->assertInstanceOf('SilverStripe\\Security\\SecurityToken', $form1->getSecurityToken());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2010-12-05 09:22:57 +01:00
|
|
|
|
SecurityToken::disable();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2010-12-05 09:22:57 +01:00
|
|
|
|
$form2 = $this->getStubForm();
|
2016-06-23 01:37:22 +02:00
|
|
|
|
$this->assertInstanceOf('SilverStripe\\Security\\NullSecurityToken', $form2->getSecurityToken());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2010-12-05 09:22:57 +01:00
|
|
|
|
SecurityToken::enable();
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testDisableSecurityTokenDoesntAddTokenFormField() {
|
2010-12-05 09:22:57 +01:00
|
|
|
|
SecurityToken::enable();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2010-12-05 09:22:57 +01:00
|
|
|
|
$formWithToken = $this->getStubForm();
|
2012-05-09 12:43:22 +02:00
|
|
|
|
$this->assertInstanceOf(
|
2016-08-19 00:51:35 +02:00
|
|
|
|
'SilverStripe\\Forms\\HiddenField',
|
2010-12-05 09:22:57 +01:00
|
|
|
|
$formWithToken->Fields()->fieldByName(SecurityToken::get_default_name()),
|
|
|
|
|
'Token field added by default'
|
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2010-12-05 09:22:57 +01:00
|
|
|
|
$formWithoutToken = $this->getStubForm();
|
|
|
|
|
$formWithoutToken->disableSecurityToken();
|
|
|
|
|
$this->assertNull(
|
|
|
|
|
$formWithoutToken->Fields()->fieldByName(SecurityToken::get_default_name()),
|
|
|
|
|
'Token field not added if disableSecurityToken() is set'
|
|
|
|
|
);
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testDisableSecurityTokenAcceptsSubmissionWithoutToken() {
|
2010-12-05 09:22:57 +01:00
|
|
|
|
SecurityToken::enable();
|
2014-10-06 05:01:33 +02:00
|
|
|
|
$expectedToken = SecurityToken::inst()->getValue();
|
2016-01-06 00:34:58 +01:00
|
|
|
|
|
2010-12-05 09:22:57 +01:00
|
|
|
|
$response = $this->get('FormTest_ControllerWithSecurityToken');
|
|
|
|
|
// can't use submitForm() as it'll automatically insert SecurityID into the POST data
|
|
|
|
|
$response = $this->post(
|
|
|
|
|
'FormTest_ControllerWithSecurityToken/Form',
|
|
|
|
|
array(
|
|
|
|
|
'Email' => 'test@test.com',
|
|
|
|
|
'action_doSubmit' => 1
|
|
|
|
|
// leaving out security token
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(400, $response->getStatusCode(), 'Submission fails without security token');
|
2014-02-08 03:13:39 +01:00
|
|
|
|
|
2014-10-06 05:01:33 +02:00
|
|
|
|
// Generate a new token which doesn't match the current one
|
|
|
|
|
$generator = new RandomGenerator();
|
|
|
|
|
$invalidToken = $generator->randomToken('sha1');
|
|
|
|
|
$this->assertNotEquals($invalidToken, $expectedToken);
|
|
|
|
|
|
|
|
|
|
// Test token with request
|
2014-02-08 03:13:39 +01:00
|
|
|
|
$response = $this->get('FormTest_ControllerWithSecurityToken');
|
|
|
|
|
$response = $this->post(
|
|
|
|
|
'FormTest_ControllerWithSecurityToken/Form',
|
|
|
|
|
array(
|
|
|
|
|
'Email' => 'test@test.com',
|
|
|
|
|
'action_doSubmit' => 1,
|
2014-10-06 05:01:33 +02:00
|
|
|
|
'SecurityID' => $invalidToken
|
2014-02-08 03:13:39 +01:00
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(200, $response->getStatusCode(), 'Submission reloads form if security token invalid');
|
2014-10-06 05:01:33 +02:00
|
|
|
|
$this->assertTrue(
|
|
|
|
|
stripos($response->getBody(), 'name="SecurityID" value="'.$expectedToken.'"') !== false,
|
|
|
|
|
'Submission reloads with correct security token after failure'
|
|
|
|
|
);
|
|
|
|
|
$this->assertTrue(
|
|
|
|
|
stripos($response->getBody(), 'name="SecurityID" value="'.$invalidToken.'"') === false,
|
|
|
|
|
'Submission reloads without incorrect security token after failure'
|
|
|
|
|
);
|
2014-02-08 03:13:39 +01:00
|
|
|
|
|
|
|
|
|
$matched = $this->cssParser()->getBySelector('#Form_Form_Email');
|
|
|
|
|
$attrs = $matched[0]->attributes();
|
|
|
|
|
$this->assertEquals('test@test.com', (string)$attrs['value'], 'Submitted data is preserved');
|
|
|
|
|
|
2010-12-05 09:22:57 +01:00
|
|
|
|
$response = $this->get('FormTest_ControllerWithSecurityToken');
|
|
|
|
|
$tokenEls = $this->cssParser()->getBySelector('#Form_Form_SecurityID');
|
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
|
1,
|
|
|
|
|
count($tokenEls),
|
2010-12-05 09:22:57 +01:00
|
|
|
|
'Token form field added for controller without disableSecurityToken()'
|
|
|
|
|
);
|
|
|
|
|
$token = (string)$tokenEls[0];
|
|
|
|
|
$response = $this->submitForm(
|
|
|
|
|
'Form_Form',
|
|
|
|
|
null,
|
|
|
|
|
array(
|
|
|
|
|
'Email' => 'test@test.com',
|
|
|
|
|
'SecurityID' => $token
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(200, $response->getStatusCode(), 'Submission suceeds with security token');
|
|
|
|
|
}
|
2013-03-25 06:16:11 +01:00
|
|
|
|
|
|
|
|
|
public function testStrictFormMethodChecking() {
|
|
|
|
|
$response = $this->get('FormTest_ControllerWithStrictPostCheck');
|
|
|
|
|
$response = $this->get(
|
|
|
|
|
'FormTest_ControllerWithStrictPostCheck/Form/?Email=test@test.com&action_doSubmit=1'
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(405, $response->getStatusCode(), 'Submission fails with wrong method');
|
|
|
|
|
|
|
|
|
|
$response = $this->get('FormTest_ControllerWithStrictPostCheck');
|
|
|
|
|
$response = $this->post(
|
|
|
|
|
'FormTest_ControllerWithStrictPostCheck/Form',
|
|
|
|
|
array(
|
|
|
|
|
'Email' => 'test@test.com',
|
|
|
|
|
'action_doSubmit' => 1
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(200, $response->getStatusCode(), 'Submission succeeds with correct method');
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testEnableSecurityToken() {
|
2010-12-05 09:30:32 +01:00
|
|
|
|
SecurityToken::disable();
|
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
$this->assertFalse($form->getSecurityToken()->isEnabled());
|
|
|
|
|
$form->enableSecurityToken();
|
|
|
|
|
$this->assertTrue($form->getSecurityToken()->isEnabled());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2010-12-05 09:30:32 +01:00
|
|
|
|
SecurityToken::disable(); // restore original
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testDisableSecurityToken() {
|
2010-12-05 09:30:32 +01:00
|
|
|
|
SecurityToken::enable();
|
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
$this->assertTrue($form->getSecurityToken()->isEnabled());
|
|
|
|
|
$form->disableSecurityToken();
|
|
|
|
|
$this->assertFalse($form->getSecurityToken()->isEnabled());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2010-12-05 09:30:32 +01:00
|
|
|
|
SecurityToken::disable(); // restore original
|
|
|
|
|
}
|
2011-12-26 08:36:24 +01:00
|
|
|
|
|
|
|
|
|
public function testEncType() {
|
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
$this->assertEquals('application/x-www-form-urlencoded', $form->getEncType());
|
|
|
|
|
|
|
|
|
|
$form->setEncType(Form::ENC_TYPE_MULTIPART);
|
|
|
|
|
$this->assertEquals('multipart/form-data', $form->getEncType());
|
|
|
|
|
|
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
$form->Fields()->push(new FileField(null));
|
|
|
|
|
$this->assertEquals('multipart/form-data', $form->getEncType());
|
|
|
|
|
|
|
|
|
|
$form->setEncType(Form::ENC_TYPE_URLENCODED);
|
|
|
|
|
$this->assertEquals('application/x-www-form-urlencoded', $form->getEncType());
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-29 13:07:59 +02:00
|
|
|
|
public function testAddExtraClass() {
|
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
$form->addExtraClass('class1');
|
|
|
|
|
$form->addExtraClass('class2');
|
|
|
|
|
$this->assertStringEndsWith('class1 class2', $form->extraClass());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRemoveExtraClass() {
|
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
$form->addExtraClass('class1');
|
|
|
|
|
$form->addExtraClass('class2');
|
|
|
|
|
$this->assertStringEndsWith('class1 class2', $form->extraClass());
|
|
|
|
|
$form->removeExtraClass('class1');
|
|
|
|
|
$this->assertStringEndsWith('class2', $form->extraClass());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAddManyExtraClasses() {
|
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
//test we can split by a range of spaces and tabs
|
|
|
|
|
$form->addExtraClass('class1 class2 class3 class4 class5');
|
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
|
'class1 class2 class3 class4 class5',
|
|
|
|
|
$form->extraClass()
|
|
|
|
|
);
|
|
|
|
|
//test that duplicate classes don't get added
|
|
|
|
|
$form->addExtraClass('class1 class2');
|
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
|
'class1 class2 class3 class4 class5',
|
|
|
|
|
$form->extraClass()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRemoveManyExtraClasses() {
|
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
$form->addExtraClass('class1 class2 class3 class4 class5');
|
|
|
|
|
//test we can remove a single class we just added
|
|
|
|
|
$form->removeExtraClass('class3');
|
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
|
'class1 class2 class4 class5',
|
|
|
|
|
$form->extraClass()
|
|
|
|
|
);
|
|
|
|
|
//check we can remove many classes at once
|
|
|
|
|
$form->removeExtraClass('class1 class5');
|
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
|
'class2 class4',
|
|
|
|
|
$form->extraClass()
|
|
|
|
|
);
|
|
|
|
|
//check that removing a dud class is fine
|
|
|
|
|
$form->removeExtraClass('dudClass');
|
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
|
'class2 class4',
|
|
|
|
|
$form->extraClass()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-14 13:55:41 +01:00
|
|
|
|
public function testDefaultClasses() {
|
2016-08-19 00:51:35 +02:00
|
|
|
|
Form::config()->update('default_classes', array(
|
2015-01-14 13:55:41 +01:00
|
|
|
|
'class1',
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
|
|
|
|
|
$this->assertContains('class1', $form->extraClass(), 'Class list does not contain expected class');
|
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
|
Form::config()->update('default_classes', array(
|
2015-01-14 13:55:41 +01:00
|
|
|
|
'class1',
|
|
|
|
|
'class2',
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
|
|
|
|
|
$this->assertContains('class1 class2', $form->extraClass(), 'Class list does not contain expected class');
|
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
|
Form::config()->update('default_classes', array(
|
2015-01-14 13:55:41 +01:00
|
|
|
|
'class3',
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
|
|
|
|
|
$this->assertContains('class3', $form->extraClass(), 'Class list does not contain expected class');
|
|
|
|
|
|
|
|
|
|
$form->removeExtraClass('class3');
|
|
|
|
|
|
|
|
|
|
$this->assertNotContains('class3', $form->extraClass(), 'Class list contains unexpected class');
|
|
|
|
|
}
|
2011-12-22 18:05:20 +01:00
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testAttributes() {
|
2011-12-22 18:05:20 +01:00
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
$form->setAttribute('foo', 'bar');
|
|
|
|
|
$this->assertEquals('bar', $form->getAttribute('foo'));
|
|
|
|
|
$attrs = $form->getAttributes();
|
|
|
|
|
$this->assertArrayHasKey('foo', $attrs);
|
|
|
|
|
$this->assertEquals('bar', $attrs['foo']);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-08 02:10:01 +02:00
|
|
|
|
public function testButtonClicked() {
|
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
$action = $form->buttonClicked();
|
|
|
|
|
$this->assertNull($action);
|
2016-07-15 11:11:47 +02:00
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
|
$controller = new FormTest\TestController();
|
2016-07-15 11:11:47 +02:00
|
|
|
|
$form = $controller->Form();
|
2016-09-09 08:43:05 +02:00
|
|
|
|
$request = new HTTPRequest('POST', 'FormTest_Controller/Form', array(), array(
|
2016-07-15 11:11:47 +02:00
|
|
|
|
'Email' => 'test@test.com',
|
|
|
|
|
'SomeRequiredField' => 1,
|
|
|
|
|
'action_doSubmit' => 1
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$form->httpSubmission($request);
|
|
|
|
|
$button = $form->buttonClicked();
|
2016-08-19 00:51:35 +02:00
|
|
|
|
$this->assertInstanceOf('SilverStripe\\Forms\\FormAction', $button);
|
2016-07-15 11:11:47 +02:00
|
|
|
|
$this->assertEquals('doSubmit', $button->actionName());
|
2016-08-19 00:51:35 +02:00
|
|
|
|
/** @skipUpgrade */
|
2016-07-15 11:11:47 +02:00
|
|
|
|
$form = new Form(
|
|
|
|
|
$controller,
|
|
|
|
|
'Form',
|
|
|
|
|
new FieldList(new FormAction('doSubmit', 'Inline action')),
|
|
|
|
|
new FieldList()
|
|
|
|
|
);
|
|
|
|
|
$form->disableSecurityToken();
|
2016-09-09 08:43:05 +02:00
|
|
|
|
$request = new HTTPRequest('POST', 'FormTest_Controller/Form', array(), array(
|
2016-07-15 11:11:47 +02:00
|
|
|
|
'action_doSubmit' => 1
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$form->httpSubmission($request);
|
|
|
|
|
$button = $form->buttonClicked();
|
2016-08-19 00:51:35 +02:00
|
|
|
|
$this->assertInstanceOf('SilverStripe\\Forms\\FormAction', $button);
|
2016-07-15 11:11:47 +02:00
|
|
|
|
$this->assertEquals('doSubmit', $button->actionName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCheckAccessAction() {
|
2016-10-14 03:30:05 +02:00
|
|
|
|
$controller = new FormTest\TestController();
|
2016-07-15 11:11:47 +02:00
|
|
|
|
$form = new Form(
|
|
|
|
|
$controller,
|
|
|
|
|
'Form',
|
|
|
|
|
new FieldList(),
|
|
|
|
|
new FieldList(new FormAction('actionName', 'Action'))
|
|
|
|
|
);
|
|
|
|
|
$this->assertTrue($form->checkAccessAction('actionName'));
|
|
|
|
|
|
|
|
|
|
$form = new Form(
|
|
|
|
|
$controller,
|
|
|
|
|
'Form',
|
|
|
|
|
new FieldList(new FormAction('inlineAction', 'Inline action')),
|
|
|
|
|
new FieldList()
|
|
|
|
|
);
|
|
|
|
|
$this->assertTrue($form->checkAccessAction('inlineAction'));
|
2016-06-08 02:10:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
|
public function testAttributesHTML() {
|
2011-12-22 18:05:20 +01:00
|
|
|
|
$form = $this->getStubForm();
|
|
|
|
|
|
|
|
|
|
$form->setAttribute('foo', 'bar');
|
|
|
|
|
$this->assertContains('foo="bar"', $form->getAttributesHTML());
|
|
|
|
|
|
|
|
|
|
$form->setAttribute('foo', null);
|
|
|
|
|
$this->assertNotContains('foo="bar"', $form->getAttributesHTML());
|
|
|
|
|
|
|
|
|
|
$form->setAttribute('foo', true);
|
|
|
|
|
$this->assertContains('foo="foo"', $form->getAttributesHTML());
|
|
|
|
|
|
|
|
|
|
$form->setAttribute('one', 1);
|
|
|
|
|
$form->setAttribute('two', 2);
|
|
|
|
|
$form->setAttribute('three', 3);
|
|
|
|
|
$this->assertNotContains('one="1"', $form->getAttributesHTML('one', 'two'));
|
|
|
|
|
$this->assertNotContains('two="2"', $form->getAttributesHTML('one', 'two'));
|
|
|
|
|
$this->assertContains('three="3"', $form->getAttributesHTML('one', 'two'));
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2014-01-30 22:38:34 +01:00
|
|
|
|
function testMessageEscapeHtml() {
|
|
|
|
|
$form = $this->getStubForm();
|
2016-09-09 08:43:05 +02:00
|
|
|
|
$form->getController()->handleRequest(new HTTPRequest('GET', '/'), DataModel::inst()); // stub out request
|
2014-01-30 22:38:34 +01:00
|
|
|
|
$form->sessionMessage('<em>Escaped HTML</em>', 'good', true);
|
|
|
|
|
$parser = new CSSContentParser($form->forTemplate());
|
|
|
|
|
$messageEls = $parser->getBySelector('.message');
|
|
|
|
|
$this->assertContains(
|
|
|
|
|
'<em>Escaped HTML</em>',
|
|
|
|
|
$messageEls[0]->asXML()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$form = $this->getStubForm();
|
2016-09-09 08:43:05 +02:00
|
|
|
|
$form->getController()->handleRequest(new HTTPRequest('GET', '/'), DataModel::inst()); // stub out request
|
2014-01-30 22:38:34 +01:00
|
|
|
|
$form->sessionMessage('<em>Unescaped HTML</em>', 'good', false);
|
|
|
|
|
$parser = new CSSContentParser($form->forTemplate());
|
|
|
|
|
$messageEls = $parser->getBySelector('.message');
|
|
|
|
|
$this->assertContains(
|
|
|
|
|
'<em>Unescaped HTML</em>',
|
|
|
|
|
$messageEls[0]->asXML()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testFieldMessageEscapeHtml() {
|
|
|
|
|
$form = $this->getStubForm();
|
2016-09-09 08:43:05 +02:00
|
|
|
|
$form->getController()->handleRequest(new HTTPRequest('GET', '/'), DataModel::inst()); // stub out request
|
2014-01-30 22:38:34 +01:00
|
|
|
|
$form->addErrorMessage('key1', '<em>Escaped HTML</em>', 'good', true);
|
|
|
|
|
$form->setupFormErrors();
|
2014-11-18 23:05:07 +01:00
|
|
|
|
$parser = new CSSContentParser($result = $form->forTemplate());
|
|
|
|
|
$messageEls = $parser->getBySelector('#Form_Form_key1_Holder .message');
|
2014-01-30 22:38:34 +01:00
|
|
|
|
$this->assertContains(
|
|
|
|
|
'<em>Escaped HTML</em>',
|
|
|
|
|
$messageEls[0]->asXML()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$form = $this->getStubForm();
|
2016-09-09 08:43:05 +02:00
|
|
|
|
$form->getController()->handleRequest(new HTTPRequest('GET', '/'), DataModel::inst()); // stub out request
|
2014-01-30 22:38:34 +01:00
|
|
|
|
$form->addErrorMessage('key1', '<em>Unescaped HTML</em>', 'good', false);
|
|
|
|
|
$form->setupFormErrors();
|
|
|
|
|
$parser = new CSSContentParser($form->forTemplate());
|
2014-11-18 23:05:07 +01:00
|
|
|
|
$messageEls = $parser->getBySelector('#Form_Form_key1_Holder .message');
|
2014-01-30 22:38:34 +01:00
|
|
|
|
$this->assertContains(
|
|
|
|
|
'<em>Unescaped HTML</em>',
|
|
|
|
|
$messageEls[0]->asXML()
|
|
|
|
|
);
|
2011-12-22 18:05:20 +01:00
|
|
|
|
}
|
2015-08-27 18:56:22 +02:00
|
|
|
|
|
|
|
|
|
public function testGetExtraFields()
|
|
|
|
|
{
|
2016-10-14 03:30:05 +02:00
|
|
|
|
$form = new FormTest\ExtraFieldsForm(
|
|
|
|
|
new FormTest\TestController(),
|
2015-08-27 18:56:22 +02:00
|
|
|
|
'Form',
|
|
|
|
|
new FieldList(new TextField('key1')),
|
|
|
|
|
new FieldList()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
|
'key1' => 'test',
|
|
|
|
|
'ExtraFieldCheckbox' => false,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$form->loadDataFrom($data);
|
|
|
|
|
|
|
|
|
|
$formData = $form->getData();
|
|
|
|
|
$this->assertEmpty($formData['ExtraFieldCheckbox']);
|
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
|
2008-10-06 16:58:01 +02:00
|
|
|
|
protected function getStubForm() {
|
|
|
|
|
return new Form(
|
2016-10-14 03:30:05 +02:00
|
|
|
|
new FormTest\TestController(),
|
2008-10-06 16:58:01 +02:00
|
|
|
|
'Form',
|
2011-05-11 09:51:54 +02:00
|
|
|
|
new FieldList(new TextField('key1')),
|
|
|
|
|
new FieldList()
|
2008-10-06 16:58:01 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
2008-04-06 05:52:14 +02:00
|
|
|
|
}
|