class FormBuilderFormBaseTest in Form Builder 7
Hierarchy
- class \DrupalTestCase
- class \DrupalUnitTestCase
- class \FormBuilderFormBaseTest
- class \DrupalUnitTestCase
Expanded class hierarchy of FormBuilderFormBaseTest
File
- tests/
FormBuilderFormBaseTest.php, line 3
View source
class FormBuilderFormBaseTest extends DrupalUnitTestCase {
public static function getInfo() {
return array(
'name' => 'FormBuilderFormBase unit tests.',
'description' => 'Tests form element handling.',
'group' => 'Form builder',
);
}
protected function emptyForm() {
return new FormBuilderFormBase('webform', 'test', NULL, array(), array(), NULL);
}
public function tearDown() {
parent::tearDown();
FormBuilderFormBase::purge(0);
FormBuilderLoader::instance()
->fromCache(NULL, NULL, NULL, TRUE);
}
/**
* @cover FormBuilderLoader::fromCache
* @cover FormBuilderFormBase::load
* @cover FormBuilderFormBase::save
*/
public function testSaveAndLoad() {
$loader = FormBuilderLoader::instance();
$form = $loader
->getForm('webform', 'test', 'test', array());
$form
->save();
$this
->assertEqual($form
->getFormArray(), $loader
->fromCache('webform', 'test', 'test')
->getFormArray());
}
/**
* @covers FormBuilderFormBase::setElementArray
* @covers FormBuilderFormBase::getElement
* @covers FormBuilderFormBase::getElementArray
* @covers FormBuilderFormBase::getFormArray
* @covers FormBuilderFormBase::addDefaults
*/
public function testSetElementArray() {
$form = $this
->emptyForm();
$a['#form_builder']['element_id'] = 'A';
$a['#key'] = 'a';
$a['#type'] = 'textfield';
$this
->assertEqual('A', $form
->setElementArray($a));
$rform = $form
->getFormArray();
$this
->assertArrayHasKey('a', $rform);
$a['#key'] = 'x';
$this
->assertEqual('A', $form
->setElementArray($a));
$rform = $form
->getFormArray();
$this
->assertArrayNotHasKey('a', $rform);
$this
->assertArrayHasKey('x', $rform);
$b['#key'] = 'b';
$b['#type'] = 'textfield';
$b['#form_builder'] = array(
'element_id' => 'B',
'parent_id' => 'A',
);
$this
->assertEqual('B', $form
->setElementArray($b));
$this
->assertArrayNotHasKey('b', $form
->getFormArray());
$this
->assertArrayHasKey('b', $form
->getElementArray('A'));
$b['#form_builder']['parent_id'] = 'NON EXISTING';
$this
->assertFalse($form
->setElementArray($b));
$this
->assertArrayHasKey('b', $form
->getElementArray('A'));
$b['#form_builder']['parent_id'] = FORM_BUILDER_ROOT;
$this
->assertEqual('B', $form
->setElementArray($b));
$this
->assertArrayHasKey('b', $form
->getFormArray());
$this
->assertArrayNotHasKey('b', $form
->getElementArray('A'));
}
/**
* @covers FormBuilderFormBase::getElementIds
* @covers FormBuilderFormBase::unsetElement
* @covers FormBuilderFormBase::unindexElements
*/
public function test_unsetElementArray() {
$form['a']['#type'] = 'textfield';
$form['a']['#form_builder'] = array(
'element_id' => 'A',
);
$form['a']['b'] = array(
'#type' => 'textfield',
);
$form['a']['b']['#form_builder'] = array(
'element_id' => 'B',
);
$form_obj = new FormBuilderFormBase('webform', 'test', NULL, array(), $form);
$this
->assertEqual(array(
'A',
'B',
), $form_obj
->getElementIds());
$form_obj
->unsetElement('A');
$this
->assertEqual(array(), $form_obj
->getElementIds());
}
/**
* @covers FormBuilderFormBase::__construct
* @covers FormBuilderFormBase::indexElements
*/
public function testElementIdIndexing() {
$form['a']['#type'] = 'textfield';
$form['a']['#form_builder'] = array(
'element_id' => 'A',
);
$form['a']['b'] = array(
'#type' => 'textfield',
);
$form['a']['b']['#form_builder'] = array(
'element_id' => 'B',
);
$form_obj = new FormBuilderFormBase('webform', 'test', NULL, array(), $form);
$this
->assertNotEmpty($form_obj
->getElementArray('A'));
$this
->assertNotEmpty($form_obj
->getElementArray('B'));
}
/**
* Integration test _form_builder_add_element().
*
* @covers ::_form_builder_add_element
* @covers ::form_builder_field_render
* @covers FormBuilderFormBase::load
* @covers FormBuilderFormBase::save
* @covers FormBuilderFormBase::serialize
* @covers FormBuilderFormBase::unserialize
*/
public function test_form_builder_add_element() {
module_load_include('inc', 'form_builder', 'includes/form_builder.admin');
$loader = FormBuilderLoader::instance();
$form = $loader
->getForm('webform', 'test', 'test', array());
$form
->save();
$data = _form_builder_add_element('webform', 'test', 'email', NULL, 'test', TRUE);
$this
->assertNotEmpty($data);
$this
->assertNotEmpty($data['html']);
}
/**
* Integration test: Render textfield inside fieldset.
*
* @covers ::_form_builder_add_element
* @covers ::form_builder_field_render
* @covers FormBuilderFormBase::load
* @covers FormBuilderFormBase::fromArray
* @covers FormBuilderFormBase::setElementArray
*/
public function test_render_fieldset() {
module_load_include('inc', 'form_builder', 'includes/form_builder.admin');
$loader = FormBuilderLoader::instance();
$form = $loader
->getForm('webform', 'test', 'test', array());
$form
->save();
drupal_static_reset('drupal_html_id');
$data = _form_builder_add_element('webform', 'test', 'fieldset', NULL, 'test', TRUE);
$wrapper = simplexml_load_string($data['html']);
// Test if element is properly wrapped.
$this
->assertEqual('form-builder-wrapper', (string) $wrapper['class']);
$this
->assertEqual('form-builder-title-bar', (string) $wrapper->div[0]['class']);
$element = $wrapper->div[1];
$this
->assertEqual('form-builder-element form-builder-element-fieldset', (string) $element['class']);
$this
->assertNotEmpty($element->fieldset);
$fieldset_id = $data['elementId'];
// Add a textfield to the form.
$data = _form_builder_add_element('webform', 'test', 'textfield', NULL, 'test', TRUE);
$this
->assertNotEquals($fieldset_id, $data['elementId']);
$textfield_id = $data['elementId'];
$form = $loader
->fromCache('webform', 'test', 'test');
// Move the textfield inside the fieldset.
$element = $form
->getElementArray($textfield_id);
$element['#weight'] = 1;
$element['#form_builder']['parent_id'] = $fieldset_id;
$this
->assertEqual($textfield_id, $form
->setElementArray($element));
$form_array = $form
->getFormArray();
$this
->assertEqual(array(
$fieldset_id,
), element_children($form_array));
$this
->assertEqual(array(
$textfield_id,
), element_children($form_array[$fieldset_id]));
}
public function testChangeElementKey() {
$a['#type'] = 'textfield';
$a['#form_builder'] = array(
'element_id' => 'A',
);
$form_obj = new FormBuilderFormBase('webform', 'test', NULL, array(), array(
'a' => $a,
));
$a['#key'] = 'b';
$form_obj
->setElementArray($a);
$form = $form_obj
->getFormArray();
$this
->assertArrayHasKey('b', $form);
$this
->assertArrayNotHasKey('a', $form);
}
protected function eArray($type, $id, $key, $weight = 0, $parent_id = FORM_BUILDER_ROOT) {
return array(
'#type' => $type,
'#key' => $key,
'#form_builder' => array(
'element_id' => $id,
'parent_id' => $parent_id,
),
'#weight' => $weight,
);
}
public function test_getElementsInPreOrder() {
$form['a'] = $this
->eArray('textfield', 'a', 'a', 1);
$form['fieldset'] = $this
->eArray('fieldset', 'fs', 'fieldset');
$form['fieldset']['b'] = $this
->eArray('textfield', 'b', 'b', 0, 'fs');
$form['fieldset']['c'] = array(
'#markup' => 'Not a form_builder element',
);
$form_obj = new FormBuilderFormBase('webform', 'test', NULL, array(), $form);
$expected = array(
'fs',
'b',
'a',
);
$this
->assertEqual($expected, array_keys($form_obj
->getElementsInPreOrder()));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DrupalTestCase:: |
protected | property | Assertions thrown in that test case. | |
DrupalTestCase:: |
protected | property | The database prefix of this test run. | |
DrupalTestCase:: |
protected | property | The original file directory, before it was changed for testing purposes. | |
DrupalTestCase:: |
public | property | Current results of this test case. | |
DrupalTestCase:: |
protected | property | Flag to indicate whether the test has been set up. | |
DrupalTestCase:: |
protected | property | ||
DrupalTestCase:: |
protected | property | ||
DrupalTestCase:: |
protected | property | This class is skipped when looking for the source of an assertion. | |
DrupalTestCase:: |
protected | property | The test run ID. | |
DrupalTestCase:: |
protected | property | Time limit for the test. | |
DrupalTestCase:: |
public | property | Whether to cache the installation part of the setUp() method. | |
DrupalTestCase:: |
public | property | Whether to cache the modules installation part of the setUp() method. | |
DrupalTestCase:: |
protected | property | URL to the verbose output file directory. | |
DrupalTestCase:: |
protected | function | Internal helper: stores the assert. | |
DrupalTestCase:: |
protected | function | Check to see if two values are equal. | |
DrupalTestCase:: |
protected | function | Check to see if a value is false (an empty string, 0, NULL, or FALSE). | |
DrupalTestCase:: |
protected | function | Check to see if two values are identical. | |
DrupalTestCase:: |
protected | function | Check to see if two values are not equal. | |
DrupalTestCase:: |
protected | function | Check to see if two values are not identical. | |
DrupalTestCase:: |
protected | function | Check to see if a value is not NULL. | |
DrupalTestCase:: |
protected | function | Check to see if a value is NULL. | |
DrupalTestCase:: |
protected | function | Check to see if a value is not false (not an empty string, 0, NULL, or FALSE). | |
DrupalTestCase:: |
public static | function | Delete an assertion record by message ID. | |
DrupalTestCase:: |
protected | function | Fire an error assertion. | 1 |
DrupalTestCase:: |
public | function | Handle errors during test runs. | 1 |
DrupalTestCase:: |
protected | function | Handle exceptions. | |
DrupalTestCase:: |
protected | function | Fire an assertion that is always negative. | |
DrupalTestCase:: |
public static | function | Converts a list of possible parameters into a stack of permutations. | |
DrupalTestCase:: |
protected | function | Cycles through backtrace until the first non-assertion method is found. | |
DrupalTestCase:: |
public static | function | Returns the database connection to the site running Simpletest. | |
DrupalTestCase:: |
public static | function | Store an assertion from outside the testing context. | |
DrupalTestCase:: |
protected | function | Fire an assertion that is always positive. | |
DrupalTestCase:: |
public static | function | Generates a random string containing letters and numbers. | |
DrupalTestCase:: |
public static | function | Generates a random string of ASCII characters of codes 32 to 126. | |
DrupalTestCase:: |
public | function | Run all tests in this class. | |
DrupalTestCase:: |
protected | function | Logs a verbose message in a text file. | |
DrupalUnitTestCase:: |
protected | function | Sets up unit test environment. | 9 |
DrupalUnitTestCase:: |
function |
Constructor for DrupalUnitTestCase. Overrides DrupalTestCase:: |
||
FormBuilderFormBaseTest:: |
protected | function | ||
FormBuilderFormBaseTest:: |
protected | function | ||
FormBuilderFormBaseTest:: |
public static | function | ||
FormBuilderFormBaseTest:: |
public | function |
Overrides DrupalUnitTestCase:: |
|
FormBuilderFormBaseTest:: |
public | function | ||
FormBuilderFormBaseTest:: |
public | function | @covers FormBuilderFormBase::__construct @covers FormBuilderFormBase::indexElements | |
FormBuilderFormBaseTest:: |
public | function | @cover FormBuilderLoader::fromCache @cover FormBuilderFormBase::load @cover FormBuilderFormBase::save | |
FormBuilderFormBaseTest:: |
public | function | @covers FormBuilderFormBase::setElementArray @covers FormBuilderFormBase::getElement @covers FormBuilderFormBase::getElementArray @covers FormBuilderFormBase::getFormArray @covers FormBuilderFormBase::addDefaults | |
FormBuilderFormBaseTest:: |
public | function | Integration test _form_builder_add_element(). | |
FormBuilderFormBaseTest:: |
public | function | ||
FormBuilderFormBaseTest:: |
public | function | Integration test: Render textfield inside fieldset. | |
FormBuilderFormBaseTest:: |
public | function | @covers FormBuilderFormBase::getElementIds @covers FormBuilderFormBase::unsetElement @covers FormBuilderFormBase::unindexElements |