You are here

class FormBuilderFormBaseTest in Form Builder 7

Hierarchy

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

Namesort descending Modifiers Type Description Overrides
DrupalTestCase::$assertions protected property Assertions thrown in that test case.
DrupalTestCase::$databasePrefix protected property The database prefix of this test run.
DrupalTestCase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
DrupalTestCase::$results public property Current results of this test case.
DrupalTestCase::$setup protected property Flag to indicate whether the test has been set up.
DrupalTestCase::$setupDatabasePrefix protected property
DrupalTestCase::$setupEnvironment protected property
DrupalTestCase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
DrupalTestCase::$testId protected property The test run ID.
DrupalTestCase::$timeLimit protected property Time limit for the test.
DrupalTestCase::$useSetupInstallationCache public property Whether to cache the installation part of the setUp() method.
DrupalTestCase::$useSetupModulesCache public property Whether to cache the modules installation part of the setUp() method.
DrupalTestCase::$verboseDirectoryUrl protected property URL to the verbose output file directory.
DrupalTestCase::assert protected function Internal helper: stores the assert.
DrupalTestCase::assertEqual protected function Check to see if two values are equal.
DrupalTestCase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
DrupalTestCase::assertIdentical protected function Check to see if two values are identical.
DrupalTestCase::assertNotEqual protected function Check to see if two values are not equal.
DrupalTestCase::assertNotIdentical protected function Check to see if two values are not identical.
DrupalTestCase::assertNotNull protected function Check to see if a value is not NULL.
DrupalTestCase::assertNull protected function Check to see if a value is NULL.
DrupalTestCase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
DrupalTestCase::deleteAssert public static function Delete an assertion record by message ID.
DrupalTestCase::error protected function Fire an error assertion. 1
DrupalTestCase::errorHandler public function Handle errors during test runs. 1
DrupalTestCase::exceptionHandler protected function Handle exceptions.
DrupalTestCase::fail protected function Fire an assertion that is always negative.
DrupalTestCase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
DrupalTestCase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
DrupalTestCase::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
DrupalTestCase::insertAssert public static function Store an assertion from outside the testing context.
DrupalTestCase::pass protected function Fire an assertion that is always positive.
DrupalTestCase::randomName public static function Generates a random string containing letters and numbers.
DrupalTestCase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
DrupalTestCase::run public function Run all tests in this class.
DrupalTestCase::verbose protected function Logs a verbose message in a text file.
DrupalUnitTestCase::setUp protected function Sets up unit test environment. 9
DrupalUnitTestCase::__construct function Constructor for DrupalUnitTestCase. Overrides DrupalTestCase::__construct
FormBuilderFormBaseTest::eArray protected function
FormBuilderFormBaseTest::emptyForm protected function
FormBuilderFormBaseTest::getInfo public static function
FormBuilderFormBaseTest::tearDown public function Overrides DrupalUnitTestCase::tearDown
FormBuilderFormBaseTest::testChangeElementKey public function
FormBuilderFormBaseTest::testElementIdIndexing public function @covers FormBuilderFormBase::__construct @covers FormBuilderFormBase::indexElements
FormBuilderFormBaseTest::testSaveAndLoad public function @cover FormBuilderLoader::fromCache @cover FormBuilderFormBase::load @cover FormBuilderFormBase::save
FormBuilderFormBaseTest::testSetElementArray public function @covers FormBuilderFormBase::setElementArray @covers FormBuilderFormBase::getElement @covers FormBuilderFormBase::getElementArray @covers FormBuilderFormBase::getFormArray @covers FormBuilderFormBase::addDefaults
FormBuilderFormBaseTest::test_form_builder_add_element public function Integration test _form_builder_add_element().
FormBuilderFormBaseTest::test_getElementsInPreOrder public function
FormBuilderFormBaseTest::test_render_fieldset public function Integration test: Render textfield inside fieldset.
FormBuilderFormBaseTest::test_unsetElementArray public function @covers FormBuilderFormBase::getElementIds @covers FormBuilderFormBase::unsetElement @covers FormBuilderFormBase::unindexElements