2008-10-14 00:20:41 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Forms\Tests\FormScaffolderTest\Article;
|
|
|
|
use SilverStripe\Forms\Tests\FormScaffolderTest\ArticleExtension;
|
|
|
|
use SilverStripe\Forms\Tests\FormScaffolderTest\Author;
|
|
|
|
use SilverStripe\Forms\Tests\FormScaffolderTest\Tag;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
/**
|
|
|
|
* Tests for DataObject FormField scaffolding
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
protected $requiredExtensions = [
|
|
|
|
Article::class => [
|
|
|
|
ArticleExtension::class
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
protected $extraDataObjects = array(
|
2016-10-14 03:30:05 +02:00
|
|
|
Article::class,
|
|
|
|
Tag::class,
|
|
|
|
Author::class,
|
2010-04-12 04:03:16 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetCMSFieldsSingleton() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$article = new Article;
|
2012-06-11 12:46:42 +02:00
|
|
|
$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() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$article1 = $this->objFromFixture(Article::class, '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() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$article1 = $this->objFromFixture(Article::class, '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() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$article1 = $this->objFromFixture(Article::class, '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() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$article1 = $this->objFromFixture(Article::class, 'article1');
|
2009-11-22 07:07:45 +01:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
$fields = $article1->scaffoldFormFields(array(
|
2016-08-19 00:51:35 +02:00
|
|
|
'fieldClasses' => array('Title' => 'SilverStripe\\Forms\\HTMLEditor\\HTMLEditorField')
|
2008-10-14 00:20:41 +02:00
|
|
|
));
|
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')
|
|
|
|
);
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertInstanceOf(
|
|
|
|
HTMLEditorField::class,
|
|
|
|
$fields->dataFieldByName('Title'),
|
2008-10-14 00:20:41 +02:00
|
|
|
'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() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$fields = Article::singleton()->getFrontEndFields();
|
2011-05-11 09:51:54 +02:00
|
|
|
$form = new Form(new Controller(), 'TestForm', $fields, new FieldList());
|
2016-10-14 03:30:05 +02:00
|
|
|
$form->loadDataFrom(singleton(Article::class));
|
2009-11-22 07:07:45 +01:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
$this->assertFalse($fields->hasTabSet(), 'getFrontEndFields() doesnt produce a TabSet by default');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|