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
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
class FormScaffolderTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
protected static $fixture_file = 'FormScaffolderTest.yml';
|
|
|
|
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $required_extensions = [
|
2016-12-16 05:34:21 +01:00
|
|
|
Article::class => [
|
|
|
|
ArticleExtension::class
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
protected static $extra_dataobjects = [
|
2016-12-16 05:34:21 +01:00
|
|
|
Article::class,
|
|
|
|
Tag::class,
|
|
|
|
Author::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
public function testGetCMSFieldsSingleton()
|
|
|
|
{
|
|
|
|
$article = new Article;
|
|
|
|
$fields = $article->getCMSFields();
|
2017-03-02 03:24:38 +01:00
|
|
|
$form = new Form(null, 'TestForm', $fields, new FieldList());
|
2016-12-16 05:34:21 +01:00
|
|
|
$form->loadDataFrom($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() doesn't include many_many fields if no ID is present"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetCMSFieldsInstance()
|
|
|
|
{
|
|
|
|
$article1 = $this->objFromFixture(Article::class, 'article1');
|
|
|
|
|
|
|
|
$fields = $article1->getCMSFields();
|
2017-03-02 03:24:38 +01:00
|
|
|
$form = new Form(null, 'TestForm', $fields, new FieldList());
|
2016-12-16 05:34:21 +01:00
|
|
|
$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'
|
|
|
|
);
|
|
|
|
$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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateCMSFields()
|
|
|
|
{
|
|
|
|
$article1 = $this->objFromFixture(Article::class, 'article1');
|
|
|
|
|
|
|
|
$fields = $article1->getCMSFields();
|
2017-03-02 03:24:38 +01:00
|
|
|
$form = new Form(null, 'TestForm', $fields, new FieldList());
|
2016-12-16 05:34:21 +01:00
|
|
|
$form->loadDataFrom($article1);
|
|
|
|
|
|
|
|
$this->assertNotNull(
|
|
|
|
$fields->dataFieldByName('AddedExtensionField'),
|
|
|
|
'getCMSFields() includes extended fields'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRestrictCMSFields()
|
|
|
|
{
|
|
|
|
$article1 = $this->objFromFixture(Article::class, 'article1');
|
|
|
|
|
|
|
|
$fields = $article1->scaffoldFormFields(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
'restrictFields' => ['Title']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2017-03-02 03:24:38 +01:00
|
|
|
$form = new Form(null, 'TestForm', $fields, new FieldList());
|
2016-12-16 05:34:21 +01:00
|
|
|
$form->loadDataFrom($article1);
|
|
|
|
|
|
|
|
$this->assertNotNull(
|
|
|
|
$fields->dataFieldByName('Title'),
|
2021-12-13 09:05:33 +01:00
|
|
|
'scaffoldCMSFields() includes explicitly defined "restrictFields"'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertNull(
|
|
|
|
$fields->dataFieldByName('Content'),
|
|
|
|
'getCMSFields() doesnt include fields left out in a "restrictFields" definition'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFieldClassesOnGetCMSFields()
|
|
|
|
{
|
|
|
|
$article1 = $this->objFromFixture(Article::class, 'article1');
|
|
|
|
|
|
|
|
$fields = $article1->scaffoldFormFields(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
'fieldClasses' => ['Title' => 'SilverStripe\\Forms\\HTMLEditor\\HTMLEditorField']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2017-03-02 03:24:38 +01:00
|
|
|
$form = new Form(null, 'TestForm', $fields, new FieldList());
|
2016-12-16 05:34:21 +01:00
|
|
|
$form->loadDataFrom($article1);
|
|
|
|
|
|
|
|
$this->assertNotNull(
|
|
|
|
$fields->dataFieldByName('Title')
|
|
|
|
);
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
HTMLEditorField::class,
|
|
|
|
$fields->dataFieldByName('Title'),
|
|
|
|
'getCMSFields() doesnt include fields left out in a "restrictFields" definition'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetFormFields()
|
|
|
|
{
|
|
|
|
$fields = Article::singleton()->getFrontEndFields();
|
2017-03-02 03:24:38 +01:00
|
|
|
$form = new Form(null, 'TestForm', $fields, new FieldList());
|
2016-12-16 05:34:21 +01:00
|
|
|
$form->loadDataFrom(singleton(Article::class));
|
|
|
|
|
|
|
|
$this->assertFalse($fields->hasTabSet(), 'getFrontEndFields() doesnt produce a TabSet by default');
|
|
|
|
}
|
2008-10-14 00:20:41 +02:00
|
|
|
}
|