2008-10-14 00:20:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for DataObject FormField scaffolding
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-10-14 00:20:41 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class FormScaffolderTest extends SapphireTest {
|
|
|
|
|
2011-03-30 08:49:11 +02:00
|
|
|
static $fixture_file = 'FormScaffolderTest.yml';
|
2010-04-12 04:03:16 +02:00
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'FormScaffolderTest_Article',
|
|
|
|
'FormScaffolderTest_Tag',
|
|
|
|
'FormScaffolderTest_Author',
|
|
|
|
);
|
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetCMSFieldsSingleton() {
|
2012-06-11 12:46:42 +02:00
|
|
|
$article = new FormScaffolderTest_Article;
|
|
|
|
$fields = $article->getCMSFields();
|
2011-05-11 09:51:54 +02:00
|
|
|
$form = new Form(new Controller(), 'TestForm', $fields, new FieldList());
|
2012-06-11 12:46:42 +02:00
|
|
|
$form->loadDataFrom($article);
|
2009-11-22 07:07:45 +01:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetCMSFieldsInstance() {
|
2008-10-14 00:20:41 +02:00
|
|
|
$article1 = $this->objFromFixture('FormScaffolderTest_Article', 'article1');
|
2009-11-22 07:07:45 +01:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
$fields = $article1->getCMSFields();
|
2011-05-11 09:51:54 +02:00
|
|
|
$form = new Form(new Controller(), 'TestForm', $fields, new FieldList());
|
2009-11-22 07:07:45 +01:00
|
|
|
$form->loadDataFrom($article1);
|
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testUpdateCMSFields() {
|
2008-10-14 00:20:41 +02:00
|
|
|
$article1 = $this->objFromFixture('FormScaffolderTest_Article', 'article1');
|
2009-11-22 07:07:45 +01:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
$fields = $article1->getCMSFields();
|
2011-05-11 09:51:54 +02:00
|
|
|
$form = new Form(new Controller(), 'TestForm', $fields, new FieldList());
|
2009-11-22 07:07:45 +01:00
|
|
|
$form->loadDataFrom($article1);
|
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
$this->assertNotNull(
|
2011-04-15 11:35:30 +02:00
|
|
|
$fields->dataFieldByName('AddedExtensionField'),
|
|
|
|
'getCMSFields() includes extended fields'
|
2008-10-14 00:20:41 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRestrictCMSFields() {
|
2008-10-14 00:20:41 +02:00
|
|
|
$article1 = $this->objFromFixture('FormScaffolderTest_Article', 'article1');
|
2009-11-22 07:07:45 +01:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
$fields = $article1->scaffoldFormFields(array(
|
|
|
|
'restrictFields' => array('Title')
|
|
|
|
));
|
2011-05-11 09:51:54 +02:00
|
|
|
$form = new Form(new Controller(), 'TestForm', $fields, new FieldList());
|
2009-11-22 07:07:45 +01:00
|
|
|
$form->loadDataFrom($article1);
|
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldClassesOnGetCMSFields() {
|
2008-10-14 00:20:41 +02:00
|
|
|
$article1 = $this->objFromFixture('FormScaffolderTest_Article', 'article1');
|
2009-11-22 07:07:45 +01:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
$fields = $article1->scaffoldFormFields(array(
|
|
|
|
'fieldClasses' => array('Title' => 'HtmlEditorField')
|
|
|
|
));
|
2011-05-11 09:51:54 +02:00
|
|
|
$form = new Form(new Controller(), 'TestForm', $fields, new FieldList());
|
2009-11-22 07:07:45 +01:00
|
|
|
$form->loadDataFrom($article1);
|
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
$this->assertNotNull(
|
|
|
|
$fields->dataFieldByName('Title')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
get_class($fields->dataFieldByName('Title')),
|
|
|
|
'HtmlEditorField',
|
|
|
|
'getCMSFields() doesnt include fields left out in a "restrictFields" definition'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetFormFields() {
|
2008-10-14 00:20:41 +02:00
|
|
|
$fields = singleton('FormScaffolderTest_Article')->getFrontEndFields();
|
2011-05-11 09:51:54 +02:00
|
|
|
$form = new Form(new Controller(), 'TestForm', $fields, new FieldList());
|
2009-11-22 07:07:45 +01:00
|
|
|
$form->loadDataFrom(singleton('FormScaffolderTest_Article'));
|
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
$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'
|
|
|
|
);
|
|
|
|
}
|
2011-04-15 11:35:30 +02:00
|
|
|
class FormScaffolderTest_ArticleExtension extends DataExtension implements TestOnly {
|
2008-10-14 00:20:41 +02:00
|
|
|
static $db = array(
|
2011-04-15 11:35:30 +02:00
|
|
|
'ExtendedField' => 'Varchar'
|
2008-10-14 00:20:41 +02:00
|
|
|
);
|
2012-03-27 06:04:11 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function updateCMSFields(FieldList $fields) {
|
2008-10-14 00:20:41 +02:00
|
|
|
$fields->addFieldToTab('Root.Main',
|
2011-04-15 11:35:30 +02:00
|
|
|
new TextField('AddedExtensionField')
|
2008-10-14 00:20:41 +02:00
|
|
|
);
|
|
|
|
}
|
2012-03-27 06:04:11 +02:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
}
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
DataObject::add_extension('FormScaffolderTest_Article', 'FormScaffolderTest_ArticleExtension');
|