2008-08-18 07:13:46 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Tab;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\Forms\EmailField;
|
|
|
|
use SilverStripe\Forms\FieldGroup;
|
|
|
|
use SilverStripe\Forms\TextareaField;
|
|
|
|
use SilverStripe\Forms\TabSet;
|
|
|
|
use SilverStripe\Forms\NumericField;
|
|
|
|
use SilverStripe\Forms\CompositeField;
|
|
|
|
use SilverStripe\Forms\FormAction;
|
|
|
|
use SilverStripe\Forms\HiddenField;
|
|
|
|
|
2008-08-18 07:13:46 +02:00
|
|
|
/**
|
2011-05-11 09:51:54 +02:00
|
|
|
* Tests for FieldList
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @skipUpgrade
|
2011-05-11 09:51:54 +02:00
|
|
|
* @todo test for {@link FieldList->setValues()}. Need to check
|
2008-11-11 03:35:54 +01:00
|
|
|
* that the values that were set are the correct ones given back.
|
2011-05-11 09:51:54 +02:00
|
|
|
* @todo test for {@link FieldList->transform()} and {@link FieldList->makeReadonly()}.
|
|
|
|
* Need to ensure that it correctly transforms the FieldList object.
|
|
|
|
* @todo test for {@link FieldList->HiddenFields()}. Need to check
|
2008-11-11 03:35:54 +01:00
|
|
|
* the fields returned are the correct HiddenField objects for a
|
2011-05-11 09:51:54 +02:00
|
|
|
* given FieldList instance.
|
|
|
|
* @todo test for {@link FieldList->dataFields()}.
|
|
|
|
* @todo test for {@link FieldList->findOrMakeTab()}.
|
2008-11-11 03:35:54 +01:00
|
|
|
* @todo the same as above with insertBefore() and insertAfter()
|
2008-08-25 10:15:22 +02:00
|
|
|
*
|
2008-08-18 07:13:46 +02:00
|
|
|
*/
|
2011-05-11 09:51:54 +02:00
|
|
|
class FieldListTest extends SapphireTest {
|
2008-08-18 07:13:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test adding a field to a tab in a set.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAddFieldToTab() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList();
|
2008-08-18 07:13:46 +02:00
|
|
|
$tab = new Tab('Root');
|
|
|
|
$fields->push($tab);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
/* We add field objects to the FieldList, using two different methods */
|
2008-08-18 07:13:46 +02:00
|
|
|
$fields->addFieldToTab('Root', new TextField('Country'));
|
|
|
|
$fields->addFieldsToTab('Root', array(
|
|
|
|
new EmailField('Email'),
|
|
|
|
new TextField('Name'),
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-18 07:13:46 +02:00
|
|
|
/* Check that the field objects were created */
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('Country'));
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('Email'));
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('Name'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-18 07:13:46 +02:00
|
|
|
/* The field objects in the set should be the same as the ones we created */
|
|
|
|
$this->assertSame($fields->dataFieldByName('Country'), $tab->fieldByName('Country'));
|
|
|
|
$this->assertSame($fields->dataFieldByName('Email'), $tab->fieldByName('Email'));
|
|
|
|
$this->assertSame($fields->dataFieldByName('Name'), $tab->fieldByName('Name'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-18 07:13:46 +02:00
|
|
|
/* We'll have 3 fields inside the tab */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(3, $tab->Fields()->count());
|
2008-08-18 07:13:46 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-17 04:38:22 +02:00
|
|
|
/**
|
|
|
|
* Test that groups can be added to a fieldlist
|
|
|
|
*/
|
|
|
|
public function testFieldgroup() {
|
|
|
|
$fields = new FieldList();
|
|
|
|
$tab = new Tab('Root');
|
|
|
|
$fields->push($tab);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-17 04:38:22 +02:00
|
|
|
$fields->addFieldsToTab('Root', array(
|
|
|
|
$group1 = new FieldGroup(
|
|
|
|
new TextField('Name'),
|
|
|
|
new EmailField('Email')
|
|
|
|
),
|
|
|
|
$group2 = new FieldGroup(
|
|
|
|
new TextField('Company'),
|
|
|
|
new TextareaField('Address')
|
|
|
|
)
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-17 04:38:22 +02:00
|
|
|
/* Check that the field objects were created */
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('Name'));
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('Email'));
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('Company'));
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('Address'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-17 04:38:22 +02:00
|
|
|
/* The field objects in the set should be the same as the ones we created */
|
|
|
|
$this->assertSame($fields->dataFieldByName('Name'), $group1->fieldByName('Name'));
|
|
|
|
$this->assertSame($fields->dataFieldByName('Email'), $group1->fieldByName('Email'));
|
|
|
|
$this->assertSame($fields->dataFieldByName('Company'), $group2->fieldByName('Company'));
|
|
|
|
$this->assertSame($fields->dataFieldByName('Address'), $group2->fieldByName('Address'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-17 04:38:22 +02:00
|
|
|
/* We'll have 2 fields directly inside the tab */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(2, $tab->Fields()->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2013-10-17 04:38:22 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-18 07:13:46 +02:00
|
|
|
/**
|
|
|
|
* Test removing a single field from a tab in a set.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRemoveSingleFieldFromTab() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList();
|
2008-08-18 07:13:46 +02:00
|
|
|
$tab = new Tab('Root');
|
|
|
|
$fields->push($tab);
|
|
|
|
|
|
|
|
/* We add a field to the "Root" tab */
|
|
|
|
$fields->addFieldToTab('Root', new TextField('Country'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-18 07:13:46 +02:00
|
|
|
/* We have 1 field inside the tab, which is the field we just created */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(1, $tab->Fields()->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-18 07:13:46 +02:00
|
|
|
/* We remove the field from the tab */
|
|
|
|
$fields->removeFieldFromTab('Root', 'Country');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-18 07:13:46 +02:00
|
|
|
/* We'll have no fields in the tab now */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(0, $tab->Fields()->count());
|
2008-08-18 07:13:46 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRemoveTab() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList(new TabSet(
|
2008-10-03 17:55:14 +02:00
|
|
|
'Root',
|
|
|
|
$tab1 = new Tab('Tab1'),
|
|
|
|
$tab2 = new Tab('Tab2'),
|
|
|
|
$tab3 = new Tab('Tab3')
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-03 17:55:14 +02:00
|
|
|
$fields->removeByName('Tab2');
|
|
|
|
$this->assertNull($fields->fieldByName('Root')->fieldByName('Tab2'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-03 17:55:14 +02:00
|
|
|
$this->assertEquals($tab1, $fields->fieldByName('Root')->fieldByName('Tab1'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testHasTabSet() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$untabbedFields = new FieldList(
|
2008-10-03 20:29:43 +02:00
|
|
|
new TextField('Field1')
|
|
|
|
);
|
|
|
|
$this->assertFalse($untabbedFields->hasTabSet());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$tabbedFields = new FieldList(
|
2008-10-10 18:00:50 +02:00
|
|
|
new TabSet('Root',
|
|
|
|
new Tab('Tab1')
|
|
|
|
)
|
|
|
|
);
|
2008-10-03 20:29:43 +02:00
|
|
|
$this->assertTrue($tabbedFields->hasTabSet());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:15:22 +02:00
|
|
|
/**
|
|
|
|
* Test removing an array of fields from a tab in a set.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRemoveMultipleFieldsFromTab() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList();
|
2008-08-25 10:15:22 +02:00
|
|
|
$tab = new Tab('Root');
|
|
|
|
$fields->push($tab);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:15:22 +02:00
|
|
|
/* We add an array of fields, using addFieldsToTab() */
|
|
|
|
$fields->addFieldsToTab('Root', array(
|
|
|
|
new TextField('Name', 'Your name'),
|
|
|
|
new EmailField('Email', 'Email address'),
|
|
|
|
new NumericField('Number', 'Insert a number')
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:15:22 +02:00
|
|
|
/* We have 3 fields inside the tab, which we just created */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(3, $tab->Fields()->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:15:22 +02:00
|
|
|
/* We remove the 3 fields from the tab */
|
|
|
|
$fields->removeFieldsFromTab('Root', array(
|
|
|
|
'Name',
|
|
|
|
'Email',
|
|
|
|
'Number'
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:15:22 +02:00
|
|
|
/* We have no fields in the tab now */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(0, $tab->Fields()->count());
|
2008-08-25 10:15:22 +02:00
|
|
|
}
|
2013-05-26 01:09:03 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRemoveFieldByName() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList();
|
2008-08-25 10:15:22 +02:00
|
|
|
$fields->push(new TextField('Name', 'Your name'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(1, $fields->count());
|
2008-08-25 10:15:22 +02:00
|
|
|
$fields->removeByName('Name');
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(0, $fields->count());
|
2013-05-26 01:09:03 +02:00
|
|
|
|
|
|
|
$fields->push(new TextField('Name[Field]', 'Your name'));
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(1, $fields->count());
|
2013-05-26 01:09:03 +02:00
|
|
|
$fields->removeByName('Name[Field]');
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(0, $fields->count());
|
2008-08-25 10:15:22 +02:00
|
|
|
}
|
2013-05-26 01:09:03 +02:00
|
|
|
|
|
|
|
public function testDataFieldByName() {
|
|
|
|
$fields = new FieldList();
|
|
|
|
$fields->push($basic = new TextField('Name', 'Your name'));
|
|
|
|
$fields->push($brack = new TextField('Name[Field]', 'Your name'));
|
|
|
|
|
|
|
|
$this->assertEquals($basic, $fields->dataFieldByName('Name'));
|
|
|
|
$this->assertEquals($brack, $fields->dataFieldByName('Name[Field]'));
|
2008-08-25 10:15:22 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-08 18:14:21 +02:00
|
|
|
/**
|
|
|
|
* Test removing multiple fields from a set by their names in an array.
|
|
|
|
*/
|
|
|
|
public function testRemoveFieldsByName() {
|
|
|
|
$fields = new FieldList();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-08 18:14:21 +02:00
|
|
|
/* First of all, we add some fields into our FieldList object */
|
|
|
|
$fields->push(new TextField('Name', 'Your name'));
|
|
|
|
$fields->push(new TextField('Email', 'Your email'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-08 18:14:21 +02:00
|
|
|
/* We have 2 fields in our set now */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(2, $fields->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-08 18:14:21 +02:00
|
|
|
/* Then, we call up removeByName() to take it out again */
|
|
|
|
$fields->removeByName(array('Name', 'Email'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-08 18:14:21 +02:00
|
|
|
/* We have 0 fields in our set now, as we've just removed the one we added */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(0, $fields->count());
|
2013-05-08 18:14:21 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-18 07:13:46 +02:00
|
|
|
/**
|
|
|
|
* Test replacing a field with another one.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testReplaceField() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList();
|
2008-08-18 07:13:46 +02:00
|
|
|
$tab = new Tab('Root');
|
|
|
|
$fields->push($tab);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* A field gets added to the set */
|
2008-08-18 07:13:46 +02:00
|
|
|
$fields->addFieldToTab('Root', new TextField('Country'));
|
|
|
|
|
|
|
|
$this->assertSame($fields->dataFieldByName('Country'), $tab->fieldByName('Country'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-18 07:13:46 +02:00
|
|
|
$fields->replaceField('Country', new EmailField('Email'));
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(1, $tab->Fields()->count());
|
2013-05-26 01:09:03 +02:00
|
|
|
|
|
|
|
$fields = new FieldList();
|
|
|
|
$fields->push(new TextField('Name', 'Your name'));
|
|
|
|
$brack = new TextField('Name[Field]', 'Your name');
|
|
|
|
|
|
|
|
$fields->replaceField('Name', $brack);
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(1, $fields->count());
|
2013-05-26 01:09:03 +02:00
|
|
|
|
|
|
|
$this->assertEquals('Name[Field]', $fields->first()->getName());
|
2008-08-18 07:13:46 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRenameField() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList();
|
2009-01-05 07:19:48 +01:00
|
|
|
$nameField = new TextField('Name', 'Before title');
|
|
|
|
$fields->push($nameField);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
/* The title of the field object is the same as what we put in */
|
|
|
|
$this->assertSame('Before title', $nameField->Title());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
/* The field gets renamed to a different title */
|
|
|
|
$fields->renameField('Name', 'After title');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
/* The title of the field object is the title we renamed to, this
|
|
|
|
includes the original object we created ($nameField), and getting
|
|
|
|
the field back out of the set */
|
|
|
|
$this->assertSame('After title', $nameField->Title());
|
|
|
|
$this->assertSame('After title', $fields->dataFieldByName('Name')->Title());
|
|
|
|
}
|
2008-09-11 02:02:49 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testReplaceAFieldInADifferentTab() {
|
2011-05-11 09:51:54 +02:00
|
|
|
/* A FieldList gets created with a TabSet and some field objects */
|
|
|
|
$FieldList = new FieldList(
|
2008-09-11 02:02:49 +02:00
|
|
|
new TabSet('Root', $main = new Tab('Main',
|
|
|
|
new TextField('A'),
|
|
|
|
new TextField('B')
|
|
|
|
), $other = new Tab('Other',
|
|
|
|
new TextField('C'),
|
|
|
|
new TextField('D')
|
|
|
|
))
|
2014-08-15 08:53:05 +02:00
|
|
|
);
|
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
/* The field "A" gets added to the FieldList we just created created */
|
|
|
|
$FieldList->addFieldToTab('Root.Other', $newA = new TextField('A', 'New Title'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
/* The field named "A" has been removed from the Main tab to make way for our new field named "A" in
|
|
|
|
* Other tab. */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(1, $main->Fields()->count());
|
|
|
|
$this->assertEquals(3, $other->Fields()->count());
|
2008-09-11 02:02:49 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-18 07:13:46 +02:00
|
|
|
/**
|
|
|
|
* Test finding a field that's inside a tabset, within another tab.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNestedTabsFindingFieldByName() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* 2 tabs get created within a TabSet inside our set */
|
2008-08-18 07:13:46 +02:00
|
|
|
$tab = new TabSet('Root',
|
2011-04-15 06:36:22 +02:00
|
|
|
new TabSet('MyContent',
|
2008-08-18 07:13:46 +02:00
|
|
|
$mainTab = new Tab('Main'),
|
|
|
|
$otherTab = new Tab('Others')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$fields->push($tab);
|
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* Some fields get added to the 2 tabs we just created */
|
2011-04-15 06:36:22 +02:00
|
|
|
$fields->addFieldToTab('Root.MyContent.Main', new TextField('Country'));
|
|
|
|
$fields->addFieldToTab('Root.MyContent.Others', new TextField('Email'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* The fields we just added actually exists in the set */
|
2008-08-18 07:13:46 +02:00
|
|
|
$this->assertNotNull($fields->dataFieldByName('Country'));
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('Email'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* The fields we just added actually exist in the tabs */
|
2008-08-18 07:13:46 +02:00
|
|
|
$this->assertNotNull($mainTab->fieldByName('Country'));
|
|
|
|
$this->assertNotNull($otherTab->fieldByName('Email'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* We have 1 field for each of the tabs */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(1, $mainTab->Fields()->count());
|
|
|
|
$this->assertEquals(1, $otherTab->Fields()->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-04-15 06:36:22 +02:00
|
|
|
$this->assertNotNull($fields->fieldByName('Root.MyContent'));
|
|
|
|
$this->assertNotNull($fields->fieldByName('Root.MyContent'));
|
2008-08-18 07:13:46 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testTabTitles() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$set = new FieldList(
|
2008-10-10 18:00:50 +02:00
|
|
|
$rootTabSet = new TabSet('Root',
|
|
|
|
$tabSetWithoutTitle = new TabSet('TabSetWithoutTitle'),
|
|
|
|
$tabSetWithTitle = new TabSet('TabSetWithTitle', 'My TabSet Title',
|
|
|
|
new Tab('ExistingChildTab')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-10 18:00:50 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
$tabSetWithTitle->Title(),
|
|
|
|
'My TabSet Title',
|
|
|
|
'Automatic conversion of tab identifiers through findOrMakeTab() with FormField::name_to_label()'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-10 18:00:50 +02:00
|
|
|
$tabWithoutTitle = $set->findOrMakeTab('Root.TabWithoutTitle');
|
|
|
|
$this->assertEquals(
|
|
|
|
$tabWithoutTitle->Title(),
|
|
|
|
'Tab Without Title',
|
|
|
|
'Automatic conversion of tab identifiers through findOrMakeTab() with FormField::name_to_label()'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-10 18:00:50 +02:00
|
|
|
$tabWithTitle = $set->findOrMakeTab('Root.TabWithTitle', 'My Tab with Title');
|
|
|
|
$this->assertEquals(
|
|
|
|
$tabWithTitle->Title(),
|
|
|
|
'My Tab with Title',
|
|
|
|
'Setting of simple tab titles through findOrMakeTab()'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-10 18:00:50 +02:00
|
|
|
$childTabWithTitle = $set->findOrMakeTab('Root.TabSetWithoutTitle.NewChildTab', 'My Child Tab Title');
|
|
|
|
$this->assertEquals(
|
|
|
|
$childTabWithTitle->Title(),
|
|
|
|
'My Child Tab Title',
|
|
|
|
'Setting of nested tab titles through findOrMakeTab() works on last child tab'
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-18 07:13:46 +02:00
|
|
|
/**
|
|
|
|
* Test pushing a field to a set.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-05-11 09:51:54 +02:00
|
|
|
* This tests {@link FieldList->push()}.
|
2008-08-18 07:13:46 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testPushFieldToSet() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* A field named Country is added to the set */
|
2008-08-18 07:13:46 +02:00
|
|
|
$fields->push(new TextField('Country'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* We only have 1 field in the set */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(1, $fields->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* Another field called Email is added to the set */
|
2008-08-18 07:13:46 +02:00
|
|
|
$fields->push(new EmailField('Email'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* There are now 2 fields in the set */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(2, $fields->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-15 03:30:14 +02:00
|
|
|
// Test that pushing a composite field without a name onto the set works
|
|
|
|
// See ticket #2932
|
|
|
|
$fields->push(new CompositeField(
|
|
|
|
new TextField('Test1'),
|
|
|
|
new TextField('Test2')
|
2015-12-14 17:18:57 +01:00
|
|
|
));
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(3, $fields->count());
|
2015-12-14 17:18:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test pushing a field to the beginning of a set.
|
|
|
|
*
|
|
|
|
* This tests {@link FieldList->unshift()}.
|
|
|
|
*/
|
|
|
|
public function testPushFieldToBeginningOfSet() {
|
|
|
|
$fields = new FieldList();
|
|
|
|
|
|
|
|
/* A field named Country is added to the set */
|
|
|
|
$fields->unshift(new TextField('Country'));
|
|
|
|
|
|
|
|
/* We only have 1 field in the set */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(1, $fields->count());
|
2015-12-14 17:18:57 +01:00
|
|
|
|
|
|
|
/* Another field called Email is added to the set */
|
|
|
|
$fields->unshift(new EmailField('Email'));
|
|
|
|
|
|
|
|
/* There are now 2 fields in the set */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(2, $fields->count());
|
2015-12-14 17:18:57 +01:00
|
|
|
|
|
|
|
/* The most recently added field is at the beginning of the set */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals('Email', $fields->first()->getName());
|
2015-12-14 17:18:57 +01:00
|
|
|
|
|
|
|
// Test that pushing a composite field without a name onto the set works
|
|
|
|
$fields->unshift(new CompositeField(
|
|
|
|
new TextField('Test1'),
|
|
|
|
new TextField('Test2')
|
2008-10-15 03:30:14 +02:00
|
|
|
));
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(3, $fields->count());
|
2008-08-18 07:13:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test inserting a field before another in a set.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-05-11 09:51:54 +02:00
|
|
|
* This tests {@link FieldList->insertBefore()}.
|
2008-08-18 07:13:46 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testInsertBeforeFieldToSet() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* 3 fields are added to the set */
|
2008-08-18 07:13:46 +02:00
|
|
|
$fields->push(new TextField('Country'));
|
|
|
|
$fields->push(new TextField('Email'));
|
|
|
|
$fields->push(new TextField('FirstName'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* We now have 3 fields in the set */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(3, $fields->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* We insert another field called Title before the FirstName field */
|
2016-10-14 03:30:05 +02:00
|
|
|
$fields->insertBefore('FirstName', new TextField('Title'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* The field we just added actually exists in the set */
|
2008-08-18 07:13:46 +02:00
|
|
|
$this->assertNotNull($fields->dataFieldByName('Title'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* We now have 4 fields in the set */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(4, $fields->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* The position of the Title field is at number 3 */
|
2011-10-29 06:01:52 +02:00
|
|
|
$this->assertEquals('Title', $fields[2]->getName());
|
2014-01-07 17:02:43 +01:00
|
|
|
|
|
|
|
/* Test arguments are accepted in either order */
|
|
|
|
$fields->insertBefore('FirstName', new TextField('Surname'));
|
|
|
|
|
|
|
|
/* The field we just added actually exists in the set */
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('Surname'));
|
|
|
|
|
|
|
|
/* We now have 5 fields in the set */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(5, $fields->count());
|
2014-01-07 17:02:43 +01:00
|
|
|
|
|
|
|
/* The position of the Surname field is at number 4 */
|
|
|
|
$this->assertEquals('Surname', $fields[3]->getName());
|
2008-08-18 07:13:46 +02:00
|
|
|
}
|
2010-12-11 04:38:11 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testInsertBeforeMultipleFields() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList(
|
2014-08-15 08:53:05 +02:00
|
|
|
$root = new TabSet("Root",
|
2010-12-11 04:38:11 +01:00
|
|
|
$main = new Tab("Main",
|
|
|
|
$a = new TextField("A"),
|
|
|
|
$b = new TextField("B")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$fields->addFieldsToTab('Root.Main', array(
|
|
|
|
new TextField('NewField1'),
|
|
|
|
new TextField('NewField2')
|
|
|
|
), 'B');
|
|
|
|
|
2010-12-11 06:39:06 +01:00
|
|
|
$this->assertEquals(array_keys($fields->dataFields()), array(
|
2010-12-11 04:38:11 +01:00
|
|
|
'A',
|
|
|
|
'NewField1',
|
|
|
|
'NewField2',
|
|
|
|
'B'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2008-08-18 07:13:46 +02:00
|
|
|
/**
|
|
|
|
* Test inserting a field after another in a set.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testInsertAfterFieldToSet() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* 3 fields are added to the set */
|
2008-08-18 07:13:46 +02:00
|
|
|
$fields->push(new TextField('Country'));
|
|
|
|
$fields->push(new TextField('Email'));
|
|
|
|
$fields->push(new TextField('FirstName'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* We now have 3 fields in the set */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(3, $fields->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* A field called Title is inserted after the Country field */
|
2016-10-14 03:30:05 +02:00
|
|
|
$fields->insertAfter('Country', new TextField('Title'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* The field we just added actually exists in the set */
|
2008-08-18 07:13:46 +02:00
|
|
|
$this->assertNotNull($fields->dataFieldByName('Title'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
/* We now have 4 fields in the FieldList */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(4, $fields->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* The position of the Title field should be at number 2 */
|
2011-10-29 06:01:52 +02:00
|
|
|
$this->assertEquals('Title', $fields[1]->getName());
|
2014-01-07 17:02:43 +01:00
|
|
|
|
|
|
|
/* Test arguments are accepted in either order */
|
|
|
|
$fields->insertAfter('FirstName', new TextField('Surname'));
|
|
|
|
|
|
|
|
/* The field we just added actually exists in the set */
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('Surname'));
|
|
|
|
|
|
|
|
/* We now have 5 fields in the set */
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(5, $fields->count());
|
2014-01-07 17:02:43 +01:00
|
|
|
|
|
|
|
/* The position of the Surname field is at number 5 */
|
|
|
|
$this->assertEquals('Surname', $fields[4]->getName());
|
2008-08-18 07:13:46 +02:00
|
|
|
}
|
2008-09-11 02:02:49 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testrootFieldList() {
|
2011-05-11 09:51:54 +02:00
|
|
|
/* Given a nested set of FormField, CompositeField, and FieldList objects */
|
|
|
|
$FieldList = new FieldList(
|
2014-08-15 08:53:05 +02:00
|
|
|
$root = new TabSet("Root",
|
2008-09-11 02:02:49 +02:00
|
|
|
$main = new Tab("Main",
|
|
|
|
$a = new TextField("A"),
|
|
|
|
$b = new TextField("B")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
/* rootFieldList() should always evaluate to the same object: the topmost FieldList */
|
2012-05-14 04:42:54 +02:00
|
|
|
$this->assertSame($FieldList, $FieldList->rootFieldList());
|
|
|
|
$this->assertSame($FieldList, $root->rootFieldList());
|
|
|
|
$this->assertSame($FieldList, $main->rootFieldList());
|
|
|
|
$this->assertSame($FieldList, $a->rootFieldList());
|
|
|
|
$this->assertSame($FieldList, $b->rootFieldList());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-14 04:42:54 +02:00
|
|
|
/* If we push additional fields, they should also have the same rootFieldList() */
|
2008-09-11 02:02:49 +02:00
|
|
|
$root->push($other = new Tab("Other"));
|
|
|
|
$other->push($c = new TextField("C"));
|
|
|
|
$root->push($third = new Tab("Third", $d = new TextField("D")));
|
|
|
|
|
2012-05-14 04:42:54 +02:00
|
|
|
$this->assertSame($FieldList, $other->rootFieldList());
|
|
|
|
$this->assertSame($FieldList, $third->rootFieldList());
|
|
|
|
$this->assertSame($FieldList, $c->rootFieldList());
|
|
|
|
$this->assertSame($FieldList, $d->rootFieldList());
|
2008-09-11 02:02:49 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAddingDuplicateReplacesOldField() {
|
2011-05-11 09:51:54 +02:00
|
|
|
/* Given a nested set of FormField, CompositeField, and FieldList objects */
|
|
|
|
$FieldList = new FieldList(
|
2014-08-15 08:53:05 +02:00
|
|
|
$root = new TabSet("Root",
|
2008-09-11 02:02:49 +02:00
|
|
|
$main = new Tab("Main",
|
|
|
|
$a = new TextField("A"),
|
|
|
|
$b = new TextField("B")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-11 02:02:49 +02:00
|
|
|
/* Adding new fields of the same names should replace the original fields */
|
|
|
|
$newA = new TextField("A", "New A");
|
|
|
|
$newB = new TextField("B", "New B");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->addFieldToTab("Root.Main", $newA);
|
|
|
|
$FieldList->addFieldToTab("Root.Other", $newB);
|
2008-09-11 02:02:49 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$this->assertSame($newA, $FieldList->dataFieldByName("A"));
|
|
|
|
$this->assertSame($newB, $FieldList->dataFieldByName("B"));
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(1, $main->Fields()->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-11 02:02:49 +02:00
|
|
|
/* Pushing fields on the end of the field set should remove them from the tab */
|
|
|
|
$thirdA = new TextField("A", "Third A");
|
|
|
|
$thirdB = new TextField("B", "Third B");
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->push($thirdA);
|
|
|
|
$FieldList->push($thirdB);
|
2008-09-11 02:02:49 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$this->assertSame($thirdA, $FieldList->fieldByName("A"));
|
|
|
|
$this->assertSame($thirdB, $FieldList->fieldByName("B"));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(0, $main->Fields()->count());
|
2008-09-11 02:02:49 +02:00
|
|
|
}
|
2008-09-12 06:21:41 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAddingFieldToNonExistentTabCreatesThatTab() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList = new FieldList(
|
2014-08-15 08:53:05 +02:00
|
|
|
$root = new TabSet("Root",
|
2008-09-12 06:21:41 +02:00
|
|
|
$main = new Tab("Main",
|
|
|
|
$a = new TextField("A")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2008-09-12 06:42:24 +02:00
|
|
|
|
|
|
|
/* Add a field to a non-existent tab, and it will be created */
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->addFieldToTab("Root.Other", $b = new TextField("B"));
|
|
|
|
$this->assertNotNull($FieldList->fieldByName('Root')->fieldByName('Other'));
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertSame($b, $FieldList->fieldByName('Root')->fieldByName('Other')->Fields()->first());
|
2008-09-12 06:21:41 +02:00
|
|
|
}
|
2008-09-12 06:42:24 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAddingFieldToATabWithTheSameNameAsTheField() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList = new FieldList(
|
2014-08-15 08:53:05 +02:00
|
|
|
$root = new TabSet("Root",
|
2008-09-12 06:42:24 +02:00
|
|
|
$main = new Tab("Main",
|
|
|
|
$a = new TextField("A")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
/* If you have a tab with the same name as the field, then technically it's a duplicate. However, it's
|
2012-09-26 23:34:00 +02:00
|
|
|
* allowed because tab isn't a data field. Only duplicate data fields are problematic */
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->addFieldToTab("Root.MyName", $myName = new TextField("MyName"));
|
|
|
|
$this->assertNotNull($FieldList->fieldByName('Root')->fieldByName('MyName'));
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertSame($myName, $FieldList->fieldByName('Root')->fieldByName('MyName')->Fields()->first());
|
2008-09-12 06:42:24 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testInsertBeforeWithNestedCompositeFields() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList = new FieldList(
|
2008-11-11 03:35:54 +01:00
|
|
|
new TextField('A_pre'),
|
|
|
|
new TextField('A'),
|
|
|
|
new TextField('A_post'),
|
|
|
|
$compositeA = new CompositeField(
|
|
|
|
new TextField('B_pre'),
|
|
|
|
new TextField('B'),
|
|
|
|
new TextField('B_post'),
|
|
|
|
$compositeB = new CompositeField(
|
|
|
|
new TextField('C_pre'),
|
|
|
|
new TextField('C'),
|
|
|
|
new TextField('C_post')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->insertBefore(
|
2016-10-14 03:30:05 +02:00
|
|
|
'A',
|
|
|
|
$A_insertbefore = new TextField('A_insertbefore')
|
2008-11-11 03:35:54 +01:00
|
|
|
);
|
|
|
|
$this->assertSame(
|
|
|
|
$A_insertbefore,
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->dataFieldByName('A_insertbefore'),
|
|
|
|
'Field on toplevel FieldList can be inserted'
|
2008-11-11 03:35:54 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->insertBefore(
|
2016-10-14 03:30:05 +02:00
|
|
|
'B',
|
|
|
|
$B_insertbefore = new TextField('B_insertbefore')
|
2008-11-11 03:35:54 +01:00
|
|
|
);
|
|
|
|
$this->assertSame(
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->dataFieldByName('B_insertbefore'),
|
2008-11-11 03:35:54 +01:00
|
|
|
$B_insertbefore,
|
2011-05-11 09:51:54 +02:00
|
|
|
'Field on one nesting level FieldList can be inserted'
|
2008-11-11 03:35:54 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->insertBefore(
|
2016-10-14 03:30:05 +02:00
|
|
|
'C',
|
|
|
|
$C_insertbefore = new TextField('C_insertbefore')
|
2008-11-11 03:35:54 +01:00
|
|
|
);
|
|
|
|
$this->assertSame(
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->dataFieldByName('C_insertbefore'),
|
2008-11-11 03:35:54 +01:00
|
|
|
$C_insertbefore,
|
2011-05-11 09:51:54 +02:00
|
|
|
'Field on two nesting levels FieldList can be inserted'
|
2008-11-11 03:35:54 +01:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-18 07:13:46 +02:00
|
|
|
/**
|
2008-11-11 03:35:54 +01:00
|
|
|
* @todo check actual placement of fields
|
2008-08-18 07:13:46 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testInsertBeforeWithNestedTabsets() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldListA = new FieldList(
|
2008-11-11 03:35:54 +01:00
|
|
|
$tabSetA = new TabSet('TabSet_A',
|
|
|
|
$tabA1 = new Tab('Tab_A1',
|
|
|
|
new TextField('A_pre'),
|
|
|
|
new TextField('A'),
|
|
|
|
new TextField('A_post')
|
|
|
|
),
|
|
|
|
$tabB1 = new Tab('Tab_B1',
|
|
|
|
new TextField('B')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$tabSetA->insertBefore(
|
2016-10-14 03:30:05 +02:00
|
|
|
'A',
|
|
|
|
$A_insertbefore = new TextField('A_insertbefore')
|
2008-11-11 03:35:54 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldListA->dataFieldByName('A_insertbefore'),
|
2008-11-11 03:35:54 +01:00
|
|
|
$A_insertbefore,
|
|
|
|
'Field on toplevel tab can be inserted'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-11 03:35:54 +01:00
|
|
|
$this->assertEquals(0, $tabA1->fieldPosition('A_pre'));
|
|
|
|
$this->assertEquals(1, $tabA1->fieldPosition('A_insertbefore'));
|
|
|
|
$this->assertEquals(2, $tabA1->fieldPosition('A'));
|
|
|
|
$this->assertEquals(3, $tabA1->fieldPosition('A_post'));
|
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldListB = new FieldList(
|
2008-11-11 03:35:54 +01:00
|
|
|
new TabSet('TabSet_A',
|
|
|
|
$tabsetB = new TabSet('TabSet_B',
|
|
|
|
$tabB1 = new Tab('Tab_B1',
|
|
|
|
new TextField('C')
|
|
|
|
),
|
|
|
|
$tabB2 = new Tab('Tab_B2',
|
|
|
|
new TextField('B_pre'),
|
|
|
|
new TextField('B'),
|
|
|
|
new TextField('B_post')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldListB->insertBefore(
|
2016-10-14 03:30:05 +02:00
|
|
|
'B',
|
|
|
|
$B_insertbefore = new TextField('B_insertbefore')
|
2008-11-11 03:35:54 +01:00
|
|
|
);
|
|
|
|
$this->assertSame(
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldListB->dataFieldByName('B_insertbefore'),
|
2008-11-11 03:35:54 +01:00
|
|
|
$B_insertbefore,
|
|
|
|
'Field on nested tab can be inserted'
|
|
|
|
);
|
|
|
|
$this->assertEquals(0, $tabB2->fieldPosition('B_pre'));
|
|
|
|
$this->assertEquals(1, $tabB2->fieldPosition('B_insertbefore'));
|
|
|
|
$this->assertEquals(2, $tabB2->fieldPosition('B'));
|
|
|
|
$this->assertEquals(3, $tabB2->fieldPosition('B_post'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testInsertAfterWithNestedCompositeFields() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList = new FieldList(
|
2008-11-11 03:35:54 +01:00
|
|
|
new TextField('A_pre'),
|
|
|
|
new TextField('A'),
|
|
|
|
new TextField('A_post'),
|
|
|
|
$compositeA = new CompositeField(
|
|
|
|
new TextField('B_pre'),
|
|
|
|
new TextField('B'),
|
|
|
|
new TextField('B_post'),
|
|
|
|
$compositeB = new CompositeField(
|
|
|
|
new TextField('C_pre'),
|
|
|
|
new TextField('C'),
|
|
|
|
new TextField('C_post')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->insertAfter(
|
2016-10-14 03:30:05 +02:00
|
|
|
'A',
|
|
|
|
$A_insertafter = new TextField('A_insertafter')
|
2008-11-11 03:35:54 +01:00
|
|
|
);
|
|
|
|
$this->assertSame(
|
|
|
|
$A_insertafter,
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->dataFieldByName('A_insertafter'),
|
|
|
|
'Field on toplevel FieldList can be inserted after'
|
2008-11-11 03:35:54 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->insertAfter(
|
2016-10-14 03:30:05 +02:00
|
|
|
'B',
|
|
|
|
$B_insertafter = new TextField('B_insertafter')
|
2008-11-11 03:35:54 +01:00
|
|
|
);
|
|
|
|
$this->assertSame(
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->dataFieldByName('B_insertafter'),
|
2008-11-11 03:35:54 +01:00
|
|
|
$B_insertafter,
|
2011-05-11 09:51:54 +02:00
|
|
|
'Field on one nesting level FieldList can be inserted after'
|
2008-11-11 03:35:54 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->insertAfter(
|
2016-10-14 03:30:05 +02:00
|
|
|
'C',
|
|
|
|
$C_insertafter = new TextField('C_insertafter')
|
2008-11-11 03:35:54 +01:00
|
|
|
);
|
|
|
|
$this->assertSame(
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->dataFieldByName('C_insertafter'),
|
2008-11-11 03:35:54 +01:00
|
|
|
$C_insertafter,
|
2011-05-11 09:51:54 +02:00
|
|
|
'Field on two nesting levels FieldList can be inserted after'
|
2008-11-11 03:35:54 +01:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-11 03:35:54 +01:00
|
|
|
/**
|
|
|
|
* @todo check actual placement of fields
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testInsertAfterWithNestedTabsets() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldListA = new FieldList(
|
2008-11-11 03:35:54 +01:00
|
|
|
$tabSetA = new TabSet('TabSet_A',
|
|
|
|
$tabA1 = new Tab('Tab_A1',
|
|
|
|
new TextField('A_pre'),
|
|
|
|
new TextField('A'),
|
|
|
|
new TextField('A_post')
|
|
|
|
),
|
|
|
|
$tabB1 = new Tab('Tab_B1',
|
|
|
|
new TextField('B')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$tabSetA->insertAfter(
|
2016-10-14 03:30:05 +02:00
|
|
|
'A',
|
|
|
|
$A_insertafter = new TextField('A_insertafter')
|
2008-11-11 03:35:54 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldListA->dataFieldByName('A_insertafter'),
|
2008-11-11 03:35:54 +01:00
|
|
|
$A_insertafter,
|
|
|
|
'Field on toplevel tab can be inserted after'
|
|
|
|
);
|
|
|
|
$this->assertEquals(0, $tabA1->fieldPosition('A_pre'));
|
|
|
|
$this->assertEquals(1, $tabA1->fieldPosition('A'));
|
|
|
|
$this->assertEquals(2, $tabA1->fieldPosition('A_insertafter'));
|
|
|
|
$this->assertEquals(3, $tabA1->fieldPosition('A_post'));
|
2008-08-18 07:13:46 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldListB = new FieldList(
|
2008-11-11 03:35:54 +01:00
|
|
|
new TabSet('TabSet_A',
|
|
|
|
$tabsetB = new TabSet('TabSet_B',
|
|
|
|
$tabB1 = new Tab('Tab_B1',
|
|
|
|
new TextField('C')
|
|
|
|
),
|
|
|
|
$tabB2 = new Tab('Tab_B2',
|
|
|
|
new TextField('B_pre'),
|
|
|
|
new TextField('B'),
|
|
|
|
new TextField('B_post')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldListB->insertAfter(
|
2016-10-14 03:30:05 +02:00
|
|
|
'B',
|
|
|
|
$B_insertafter = new TextField('B_insertafter')
|
2008-11-11 03:35:54 +01:00
|
|
|
);
|
|
|
|
$this->assertSame(
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldListB->dataFieldByName('B_insertafter'),
|
2008-11-11 03:35:54 +01:00
|
|
|
$B_insertafter,
|
|
|
|
'Field on nested tab can be inserted after'
|
|
|
|
);
|
|
|
|
$this->assertEquals(0, $tabB2->fieldPosition('B_pre'));
|
|
|
|
$this->assertEquals(1, $tabB2->fieldPosition('B'));
|
|
|
|
$this->assertEquals(2, $tabB2->fieldPosition('B_insertafter'));
|
|
|
|
$this->assertEquals(3, $tabB2->fieldPosition('B_post'));
|
|
|
|
}
|
2015-05-17 00:49:11 +02:00
|
|
|
/**
|
|
|
|
* FieldList::changeFieldOrder() should place specified fields in given
|
|
|
|
* order then add any unspecified remainders at the end. Can be given an
|
|
|
|
* array or list of arguments.
|
|
|
|
*/
|
|
|
|
public function testChangeFieldOrder() {
|
|
|
|
$fieldNames = array('A','B','C','D','E');
|
|
|
|
$setArray = new FieldList();
|
|
|
|
$setArgs = new FieldList();
|
|
|
|
foreach ($fieldNames as $fN) {
|
|
|
|
$setArray->push(new TextField($fN));
|
|
|
|
$setArgs->push(new TextField($fN));
|
|
|
|
}
|
|
|
|
|
|
|
|
$setArray->changeFieldOrder(array('D','B','E'));
|
|
|
|
$this->assertEquals(0, $setArray->fieldPosition('D'));
|
|
|
|
$this->assertEquals(1, $setArray->fieldPosition('B'));
|
|
|
|
$this->assertEquals(2, $setArray->fieldPosition('E'));
|
|
|
|
$this->assertEquals(3, $setArray->fieldPosition('A'));
|
|
|
|
$this->assertEquals(4, $setArray->fieldPosition('C'));
|
|
|
|
|
|
|
|
$setArgs->changeFieldOrder('D','B','E');
|
|
|
|
$this->assertEquals(0, $setArgs->fieldPosition('D'));
|
|
|
|
$this->assertEquals(1, $setArgs->fieldPosition('B'));
|
|
|
|
$this->assertEquals(2, $setArgs->fieldPosition('E'));
|
|
|
|
$this->assertEquals(3, $setArgs->fieldPosition('A'));
|
|
|
|
$this->assertEquals(4, $setArgs->fieldPosition('C'));
|
|
|
|
|
|
|
|
unset($setArray, $setArgs);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldPosition() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$set = new FieldList(
|
2008-11-11 03:35:54 +01:00
|
|
|
new TextField('A'),
|
|
|
|
new TextField('B'),
|
|
|
|
new TextField('C')
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-11 03:35:54 +01:00
|
|
|
$this->assertEquals(0, $set->fieldPosition('A'));
|
|
|
|
$this->assertEquals(1, $set->fieldPosition('B'));
|
|
|
|
$this->assertEquals(2, $set->fieldPosition('C'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$set->insertBefore('B', new TextField('AB'));
|
2008-11-11 03:35:54 +01:00
|
|
|
$this->assertEquals(0, $set->fieldPosition('A'));
|
|
|
|
$this->assertEquals(1, $set->fieldPosition('AB'));
|
|
|
|
$this->assertEquals(2, $set->fieldPosition('B'));
|
|
|
|
$this->assertEquals(3, $set->fieldPosition('C'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-11 03:35:54 +01:00
|
|
|
unset($set);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-07 22:00:54 +01:00
|
|
|
/**
|
|
|
|
* FieldList::forTemplate() returns a concatenation of FieldHolder values.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testForTemplate() {
|
2012-03-07 22:00:54 +01:00
|
|
|
$set = new FieldList(
|
|
|
|
$a = new TextField('A'),
|
|
|
|
$b = new TextField('B')
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 17:03:02 +01:00
|
|
|
$this->assertEquals($a->FieldHolder() . $b->FieldHolder(), $set->forTemplate());
|
2012-03-07 22:02:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FieldList::forTemplate() for an action list returns a concatenation of Field values.
|
|
|
|
* Internally, this works by having FormAction::FieldHolder return just the field, but it's an important
|
|
|
|
* use-case to test.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testForTemplateForActionList() {
|
2012-03-07 22:02:57 +01:00
|
|
|
$set = new FieldList(
|
|
|
|
$a = new FormAction('A'),
|
|
|
|
$b = new FormAction('B')
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 17:03:02 +01:00
|
|
|
$this->assertEquals($a->Field() . $b->Field(), $set->forTemplate());
|
2012-03-07 22:00:54 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testMakeFieldReadonly() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList = new FieldList(
|
2008-12-04 23:38:32 +01:00
|
|
|
new TabSet('Root', new Tab('Main',
|
|
|
|
new TextField('A'),
|
|
|
|
new TextField('B')
|
|
|
|
)
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->makeFieldReadonly('A');
|
2008-12-04 23:38:32 +01:00
|
|
|
$this->assertTrue(
|
2011-05-11 09:51:54 +02:00
|
|
|
$FieldList->dataFieldByName('A')->isReadonly(),
|
|
|
|
'Field nested inside a TabSet and FieldList can be marked readonly by FieldList->makeFieldReadonly()'
|
2008-12-04 23:38:32 +01:00
|
|
|
);
|
|
|
|
}
|
2012-03-07 21:54:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test VisibleFields and HiddenFields
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testVisibleAndHiddenFields() {
|
2012-03-07 21:54:31 +01:00
|
|
|
$fields = new FieldList(
|
|
|
|
new TextField("A"),
|
|
|
|
new TextField("B"),
|
|
|
|
new HiddenField("C"),
|
|
|
|
new Tabset("Root",
|
2014-08-15 08:53:05 +02:00
|
|
|
new Tab("D",
|
2012-03-07 21:54:31 +01:00
|
|
|
new TextField("D1"),
|
|
|
|
new HiddenField("D2")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$hidden = $fields->HiddenFields();
|
|
|
|
// Inside hidden fields, all HiddenField objects are included, even nested ones
|
|
|
|
$this->assertNotNull($hidden->dataFieldByName('C'));
|
|
|
|
$this->assertNotNull($hidden->dataFieldByName('D2'));
|
|
|
|
// Visible fields are not
|
|
|
|
$this->assertNull($hidden->dataFieldByName('B'));
|
|
|
|
$this->assertNull($hidden->dataFieldByName('D1'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-07 21:54:31 +01:00
|
|
|
$visible = $fields->VisibleFields();
|
|
|
|
// Visible fields exclude top level HiddenField objects
|
|
|
|
$this->assertNotNull($visible->dataFieldByName('A'));
|
|
|
|
$this->assertNull($visible->dataFieldByName('C'));
|
|
|
|
// But they don't exclude nested HiddenField objects. This is a limitation; you should
|
|
|
|
// put all your HiddenFields at the top level.
|
|
|
|
$this->assertNotNull($visible->dataFieldByName('D2'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2008-11-11 03:35:54 +01:00
|
|
|
}
|