2008-08-18 07:13:46 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for FieldSet
|
2008-08-25 10:28:46 +02:00
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage tests
|
2008-08-18 07:13:46 +02:00
|
|
|
*
|
|
|
|
* @TODO test for {@link FieldSet->insertBeforeRecursive()}.
|
|
|
|
*
|
|
|
|
* @TODO test for {@link FieldSet->setValues()}. Need to check
|
|
|
|
* that the values that were set are the correct ones given back.
|
|
|
|
*
|
|
|
|
* @TODO test for {@link FieldSet->transform()} and {@link FieldSet->makeReadonly()}.
|
|
|
|
* Need to ensure that it correctly transforms the FieldSet object.
|
|
|
|
*
|
|
|
|
* @TODO test for {@link FieldSet->HiddenFields()}. Need to check
|
|
|
|
* the fields returned are the correct HiddenField objects for a
|
|
|
|
* given FieldSet instance.
|
|
|
|
*
|
|
|
|
* @TODO test for {@link FieldSet->dataFields()}.
|
|
|
|
*
|
|
|
|
* @TODO test for {@link FieldSet->findOrMakeTab()}.
|
2008-08-25 10:15:22 +02:00
|
|
|
*
|
2008-08-18 07:13:46 +02:00
|
|
|
*/
|
|
|
|
class FieldSetTest extends SapphireTest {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test adding a field to a tab in a set.
|
|
|
|
*/
|
|
|
|
function testAddFieldToTab() {
|
|
|
|
$fields = new FieldSet();
|
|
|
|
$tab = new Tab('Root');
|
|
|
|
$fields->push($tab);
|
|
|
|
|
|
|
|
/* We add field objects to the FieldSet, using two different methods */
|
|
|
|
$fields->addFieldToTab('Root', new TextField('Country'));
|
|
|
|
$fields->addFieldsToTab('Root', array(
|
|
|
|
new EmailField('Email'),
|
|
|
|
new TextField('Name'),
|
|
|
|
));
|
|
|
|
|
|
|
|
/* Check that the field objects were created */
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('Country'));
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('Email'));
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('Name'));
|
|
|
|
|
|
|
|
/* 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'));
|
|
|
|
|
|
|
|
/* We'll have 3 fields inside the tab */
|
|
|
|
$this->assertEquals(3, $tab->Fields()->Count());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test removing a single field from a tab in a set.
|
|
|
|
*/
|
2008-08-25 10:15:22 +02:00
|
|
|
function testRemoveSingleFieldFromTab() {
|
2008-08-18 07:13:46 +02:00
|
|
|
$fields = new FieldSet();
|
|
|
|
$tab = new Tab('Root');
|
|
|
|
$fields->push($tab);
|
|
|
|
|
|
|
|
/* We add a field to the "Root" tab */
|
|
|
|
$fields->addFieldToTab('Root', new TextField('Country'));
|
|
|
|
|
|
|
|
/* We have 1 field inside the tab, which is the field we just created */
|
|
|
|
$this->assertEquals(1, $tab->Fields()->Count());
|
|
|
|
|
|
|
|
/* We remove the field from the tab */
|
|
|
|
$fields->removeFieldFromTab('Root', 'Country');
|
|
|
|
|
|
|
|
/* We'll have no fields in the tab now */
|
|
|
|
$this->assertEquals(0, $tab->Fields()->Count());
|
|
|
|
}
|
|
|
|
|
2008-08-25 10:15:22 +02:00
|
|
|
/**
|
|
|
|
* Test removing an array of fields from a tab in a set.
|
|
|
|
*/
|
|
|
|
function testRemoveMultipleFieldsFromTab() {
|
|
|
|
$fields = new FieldSet();
|
|
|
|
$tab = new Tab('Root');
|
|
|
|
$fields->push($tab);
|
|
|
|
|
|
|
|
/* 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')
|
|
|
|
));
|
|
|
|
|
|
|
|
/* We have 3 fields inside the tab, which we just created */
|
|
|
|
$this->assertEquals(3, $tab->Fields()->Count());
|
|
|
|
|
|
|
|
/* We remove the 3 fields from the tab */
|
|
|
|
$fields->removeFieldsFromTab('Root', array(
|
|
|
|
'Name',
|
|
|
|
'Email',
|
|
|
|
'Number'
|
|
|
|
));
|
|
|
|
|
|
|
|
/* We have no fields in the tab now */
|
|
|
|
$this->assertEquals(0, $tab->Fields()->Count());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test removing a field from a set by it's name.
|
|
|
|
*/
|
|
|
|
function testRemoveFieldByName() {
|
|
|
|
$fields = new FieldSet();
|
|
|
|
|
|
|
|
/* First of all, we add a field into our FieldSet object */
|
|
|
|
$fields->push(new TextField('Name', 'Your name'));
|
|
|
|
|
|
|
|
/* We have 1 field in our set now */
|
|
|
|
$this->assertEquals(1, $fields->Count());
|
|
|
|
|
|
|
|
/* Then, we call up removeByName() to take it out again */
|
|
|
|
$fields->removeByName('Name');
|
|
|
|
|
|
|
|
/* We have 0 fields in our set now, as we've just removed the one we added */
|
|
|
|
$this->assertEquals(0, $fields->Count());
|
|
|
|
}
|
|
|
|
|
2008-08-18 07:13:46 +02:00
|
|
|
/**
|
|
|
|
* Test replacing a field with another one.
|
|
|
|
*/
|
2008-08-25 10:15:22 +02:00
|
|
|
function testReplaceField() {
|
2008-08-18 07:13:46 +02:00
|
|
|
$fields = new FieldSet();
|
|
|
|
$tab = new Tab('Root');
|
|
|
|
$fields->push($tab);
|
|
|
|
|
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'));
|
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* We have the same object as the one we pushed */
|
2008-08-18 07:13:46 +02:00
|
|
|
$this->assertSame($fields->dataFieldByName('Country'), $tab->fieldByName('Country'));
|
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* The field called Country is replaced by the field called Email */
|
2008-08-18 07:13:46 +02:00
|
|
|
$fields->replaceField('Country', new EmailField('Email'));
|
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* We have 1 field inside our tab */
|
2008-08-18 07:13:46 +02:00
|
|
|
$this->assertEquals(1, $tab->Fields()->Count());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test finding a field that's inside a tabset, within another tab.
|
|
|
|
*/
|
|
|
|
function testNestedTabsFindingFieldByName() {
|
|
|
|
$fields = new FieldSet();
|
|
|
|
|
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',
|
|
|
|
new TabSet('Content',
|
|
|
|
$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 */
|
2008-08-18 07:13:46 +02:00
|
|
|
$fields->addFieldToTab('Root.Content.Main', new TextField('Country'));
|
|
|
|
$fields->addFieldToTab('Root.Content.Others', new TextField('Email'));
|
|
|
|
|
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'));
|
|
|
|
|
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'));
|
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* We have 1 field for each of the tabs */
|
2008-08-18 07:13:46 +02:00
|
|
|
$this->assertEquals(1, $mainTab->Fields()->Count());
|
|
|
|
$this->assertEquals(1, $otherTab->Fields()->Count());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test pushing a field to a set.
|
|
|
|
*
|
|
|
|
* This tests {@link FieldSet->push()}.
|
|
|
|
*/
|
|
|
|
function testPushFieldToSet() {
|
|
|
|
$fields = new FieldSet();
|
|
|
|
|
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'));
|
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* We only have 1 field in the set */
|
2008-08-18 07:13:46 +02:00
|
|
|
$this->assertEquals(1, $fields->Count());
|
|
|
|
|
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'));
|
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* There are now 2 fields in the set */
|
2008-08-18 07:13:46 +02:00
|
|
|
$this->assertEquals(2, $fields->Count());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test inserting a field before another in a set.
|
|
|
|
*
|
|
|
|
* This tests {@link FieldSet->insertBefore()}.
|
|
|
|
*/
|
|
|
|
function testInsertBeforeFieldToSet() {
|
|
|
|
$fields = new FieldSet();
|
|
|
|
|
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'));
|
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* We now have 3 fields in the set */
|
2008-08-18 07:13:46 +02:00
|
|
|
$this->assertEquals(3, $fields->Count());
|
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* We insert another field called Title before the FirstName field */
|
2008-08-18 07:13:46 +02:00
|
|
|
$fields->insertBefore(new TextField('Title'), 'FirstName');
|
|
|
|
|
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'));
|
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* We now have 4 fields in the set */
|
2008-08-18 07:13:46 +02:00
|
|
|
$this->assertEquals(4, $fields->Count());
|
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* The position of the Title field is at number 3 */
|
2008-08-18 07:13:46 +02:00
|
|
|
$this->assertEquals(3, $fields->fieldByName('Title')->Pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test inserting a field after another in a set.
|
|
|
|
*/
|
|
|
|
function testInsertAfterFieldToSet() {
|
|
|
|
$fields = new FieldSet();
|
|
|
|
|
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'));
|
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* We now have 3 fields in the set */
|
2008-08-18 07:13:46 +02:00
|
|
|
$this->assertEquals(3, $fields->Count());
|
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* A field called Title is inserted after the Country field */
|
2008-08-18 07:13:46 +02:00
|
|
|
$fields->insertAfter(new TextField('Title'), 'Country');
|
|
|
|
|
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'));
|
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* We now have 4 fields in the FieldSet */
|
2008-08-18 07:13:46 +02:00
|
|
|
$this->assertEquals(4, $fields->Count());
|
|
|
|
|
2008-08-25 10:27:16 +02:00
|
|
|
/* The position of the Title field should be at number 2 */
|
2008-08-18 07:13:46 +02:00
|
|
|
$this->assertEquals(2, $fields->fieldByName('Title')->Pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @TODO test pushing a field replacing an existing one. (duplicate)
|
|
|
|
* @TODO the same as above with insertBefore() and insertAfter()
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|