mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
319d2f4952
API CHANGE: A DataList can be passed as the 2nd argument to the constructor; this is the recommended approach for editing non-relations. API CHANGE: You can't set a custom query; only a custom DataList. API CHANGE: You can't have one of these fields editing data that doesn't correspond to a DataList - there must be some kind of DataObject behind it. API CHANGE: If the field's name corresponds to a relation on the object being edited, then the relation is used as the data set - all the source* parameters are ignored. API CHANGE: relationAutoSetting only works if your form has had the corresponding data object loaded with $form->loadDataFrom(). API CHANGE: relationAutoSetting can't be turned off; attach a non-relation DataList instead.
139 lines
4.5 KiB
PHP
139 lines
4.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Tests for DataObject FormField scaffolding
|
|
*
|
|
* @package sapphire
|
|
* @subpackage tests
|
|
*
|
|
*/
|
|
class FormScaffolderTest extends SapphireTest {
|
|
|
|
static $fixture_file = 'FormScaffolderTest.yml';
|
|
|
|
protected $extraDataObjects = array(
|
|
'FormScaffolderTest_Article',
|
|
'FormScaffolderTest_Tag',
|
|
'FormScaffolderTest_Author',
|
|
);
|
|
|
|
|
|
function testGetCMSFieldsSingleton() {
|
|
$fields = singleton('FormScaffolderTest_Article')->getCMSFields();
|
|
$form = new Form(new Controller(), 'TestForm', $fields, new FieldSet());
|
|
$form->loadDataFrom(singleton('FormScaffolderTest_Article'));
|
|
|
|
$this->assertTrue($fields->hasTabSet(), 'getCMSFields() produces a TabSet');
|
|
$this->assertNotNull($fields->dataFieldByName('Title'), 'getCMSFields() includes db fields');
|
|
$this->assertNotNull($fields->dataFieldByName('Content'), 'getCMSFields() includes db fields');
|
|
$this->assertNotNull($fields->dataFieldByName('AuthorID'), 'getCMSFields() includes has_one fields on singletons');
|
|
$this->assertNull($fields->dataFieldByName('Tags'), 'getCMSFields() doesnt include many_many fields if no ID is present');
|
|
}
|
|
|
|
function testGetCMSFieldsInstance() {
|
|
$article1 = $this->objFromFixture('FormScaffolderTest_Article', 'article1');
|
|
|
|
$fields = $article1->getCMSFields();
|
|
$form = new Form(new Controller(), 'TestForm', $fields, new FieldSet());
|
|
$form->loadDataFrom($article1);
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('AuthorID'), 'getCMSFields() includes has_one fields on instances');
|
|
$this->assertNotNull($fields->dataFieldByName('Tags'), 'getCMSFields() includes many_many fields if ID is present on instances');
|
|
}
|
|
|
|
function testUpdateCMSFields() {
|
|
$article1 = $this->objFromFixture('FormScaffolderTest_Article', 'article1');
|
|
|
|
$fields = $article1->getCMSFields();
|
|
$form = new Form(new Controller(), 'TestForm', $fields, new FieldSet());
|
|
$form->loadDataFrom($article1);
|
|
|
|
$this->assertNotNull(
|
|
$fields->dataFieldByName('AddedExtensionField'),
|
|
'getCMSFields() includes extended fields'
|
|
);
|
|
}
|
|
|
|
function testRestrictCMSFields() {
|
|
$article1 = $this->objFromFixture('FormScaffolderTest_Article', 'article1');
|
|
|
|
$fields = $article1->scaffoldFormFields(array(
|
|
'restrictFields' => array('Title')
|
|
));
|
|
$form = new Form(new Controller(), 'TestForm', $fields, new FieldSet());
|
|
$form->loadDataFrom($article1);
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('Title'), 'scaffoldCMSFields() includes explitly defined "restrictFields"');
|
|
$this->assertNull($fields->dataFieldByName('Content'), 'getCMSFields() doesnt include fields left out in a "restrictFields" definition');
|
|
}
|
|
|
|
function testFieldClassesOnGetCMSFields() {
|
|
$article1 = $this->objFromFixture('FormScaffolderTest_Article', 'article1');
|
|
|
|
$fields = $article1->scaffoldFormFields(array(
|
|
'fieldClasses' => array('Title' => 'HtmlEditorField')
|
|
));
|
|
$form = new Form(new Controller(), 'TestForm', $fields, new FieldSet());
|
|
$form->loadDataFrom($article1);
|
|
|
|
$this->assertNotNull(
|
|
$fields->dataFieldByName('Title')
|
|
);
|
|
$this->assertEquals(
|
|
get_class($fields->dataFieldByName('Title')),
|
|
'HtmlEditorField',
|
|
'getCMSFields() doesnt include fields left out in a "restrictFields" definition'
|
|
);
|
|
}
|
|
|
|
function testGetFormFields() {
|
|
$fields = singleton('FormScaffolderTest_Article')->getFrontEndFields();
|
|
$form = new Form(new Controller(), 'TestForm', $fields, new FieldSet());
|
|
$form->loadDataFrom(singleton('FormScaffolderTest_Article'));
|
|
|
|
$this->assertFalse($fields->hasTabSet(), 'getFrontEndFields() doesnt produce a TabSet by default');
|
|
}
|
|
}
|
|
|
|
class FormScaffolderTest_Article extends DataObject implements TestOnly {
|
|
static $db = array(
|
|
'Title' => 'Varchar',
|
|
'Content' => 'HTMLText'
|
|
);
|
|
static $has_one = array(
|
|
'Author' => 'FormScaffolderTest_Author'
|
|
);
|
|
static $many_many = array(
|
|
'Tags' => 'FormScaffolderTest_Tag',
|
|
);
|
|
}
|
|
|
|
class FormScaffolderTest_Author extends Member implements TestOnly {
|
|
static $has_one = array(
|
|
'ProfileImage' => 'Image'
|
|
);
|
|
static $has_many = array(
|
|
'Articles' => 'FormScaffolderTest_Article'
|
|
);
|
|
}
|
|
class FormScaffolderTest_Tag extends DataObject implements TestOnly {
|
|
static $db = array(
|
|
'Title' => 'Varchar',
|
|
);
|
|
static $belongs_many_many = array(
|
|
'Articles' => 'FormScaffolderTest_Article'
|
|
);
|
|
}
|
|
class FormScaffolderTest_ArticleExtension extends DataExtension implements TestOnly {
|
|
static $db = array(
|
|
'ExtendedField' => 'Varchar'
|
|
);
|
|
function updateCMSFields(&$fields) {
|
|
$fields->addFieldToTab('Root.Main',
|
|
new TextField('AddedExtensionField')
|
|
);
|
|
}
|
|
}
|
|
|
|
DataObject::add_extension('FormScaffolderTest_Article', 'FormScaffolderTest_ArticleExtension');
|
|
?>
|