2008-10-14 00:20:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for DataObject FormField scaffolding
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-10-14 00:20:41 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class FormScaffolderTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'FormScaffolderTest.yml';
|
2010-04-12 04:03:16 +02:00
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'FormScaffolderTest_Article',
|
|
|
|
'FormScaffolderTest_Tag',
|
|
|
|
'FormScaffolderTest_Author',
|
|
|
|
);
|
2014-08-15 08:53:05 +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
|
|
|
|
2012-09-26 23:34:00 +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'),
|
2013-07-10 02:44:24 +02:00
|
|
|
"getCMSFields() doesn't include many_many fields if no ID is present");
|
2008-10-14 00:20:41 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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);
|
|
|
|
|
2012-09-26 23:34:00 +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');
|
2013-07-10 02:44:24 +02:00
|
|
|
$this->assertNotNull($fields->dataFieldByName('SubjectOfArticles'),
|
|
|
|
'getCMSFields() includes polymorphic has_many fields if ID is present on instances');
|
|
|
|
$this->assertNull($fields->dataFieldByName('Subject'),
|
|
|
|
"getCMSFields() doesn't include polymorphic has_one field");
|
|
|
|
$this->assertNull($fields->dataFieldByName('SubjectID'),
|
|
|
|
"getCMSFields() doesn't include polymorphic has_one id field");
|
|
|
|
$this->assertNull($fields->dataFieldByName('SubjectClass'),
|
|
|
|
"getCMSFields() doesn't include polymorphic has_one class field");
|
2008-10-14 00:20:41 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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');
|
2014-08-15 08:53:05 +02: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);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +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);
|
|
|
|
|
2012-09-26 23:34:00 +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');
|
2008-10-14 00:20:41 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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'
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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 {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Title' => 'Varchar',
|
2008-10-14 00:20:41 +02:00
|
|
|
'Content' => 'HTMLText'
|
|
|
|
);
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2013-07-10 02:44:24 +02:00
|
|
|
'Author' => 'FormScaffolderTest_Author',
|
|
|
|
'Subject' => 'DataObject'
|
2008-10-14 00:20:41 +02:00
|
|
|
);
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Tags' => 'FormScaffolderTest_Tag',
|
2008-10-14 00:20:41 +02:00
|
|
|
);
|
2013-07-10 02:44:24 +02:00
|
|
|
private static $has_many = array(
|
|
|
|
'SubjectOfArticles' => 'FormScaffolderTest_Article.Subject'
|
|
|
|
);
|
2008-10-14 00:20:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class FormScaffolderTest_Author extends Member implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2008-10-14 00:20:41 +02:00
|
|
|
'ProfileImage' => 'Image'
|
|
|
|
);
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_many = array(
|
2013-07-10 02:44:24 +02:00
|
|
|
'Articles' => 'FormScaffolderTest_Article.Author',
|
|
|
|
'SubjectOfArticles' => 'FormScaffolderTest_Article.Subject'
|
2008-10-14 00:20:41 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
class FormScaffolderTest_Tag extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Title' => 'Varchar',
|
2008-10-14 00:20:41 +02:00
|
|
|
);
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $belongs_many_many = array(
|
2008-10-14 00:20:41 +02:00
|
|
|
'Articles' => 'FormScaffolderTest_Article'
|
|
|
|
);
|
2013-07-10 02:44:24 +02:00
|
|
|
private static $has_many = array(
|
|
|
|
'SubjectOfArticles' => 'FormScaffolderTest_Article.Subject'
|
|
|
|
);
|
2008-10-14 00:20:41 +02:00
|
|
|
}
|
2011-04-15 11:35:30 +02:00
|
|
|
class FormScaffolderTest_ArticleExtension extends DataExtension implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private 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
|
|
|
}
|
|
|
|
|
2012-10-09 05:55:37 +02:00
|
|
|
FormScaffolderTest_Article::add_extension('FormScaffolderTest_ArticleExtension');
|