You are here

public function ConditionalFieldsTestCase::setUp in Conditional Fields 7.3

Sets up a Drupal site for running functional and integration tests.

Generates a random database prefix and installs Drupal with the specified installation profile in DrupalWebTestCase::$profile into the prefixed database. Afterwards, installs any additional modules specified by the test.

After installation all caches are flushed and several configuration values are reset to the values of the parent site executing the test, since the default values may be incompatible with the environment in which tests are being executed.

Parameters

...: List of modules to enable for the duration of the test. This can be either a single array or a variable number of string arguments.

Overrides DrupalWebTestCase::setUp

See also

DrupalWebTestCase::prepareDatabasePrefix()

DrupalWebTestCase::changeDatabasePrefix()

DrupalWebTestCase::prepareEnvironment()

File

tests/conditional_fields.test, line 21
Test Conditional Fields functionality and UI.

Class

ConditionalFieldsTestCase
@file Test Conditional Fields functionality and UI.

Code

public function setUp() {

  // Load all core modules that define field types.
  parent::setUp('conditional_fields_test', 'list', 'text', 'number', 'taxonomy', 'image');

  // Create and log in user.
  $web_user = $this
    ->drupalCreateUser(array(
    'create conditional_fields_test content',
    'edit any conditional_fields_test content',
  ));
  $this
    ->drupalLogin($web_user);

  // Create a vocabulary and two terms to test the term reference field.
  $vocabulary = new stdClass();
  $vocabulary->name = $this
    ->randomName();
  $vocabulary->machine_name = drupal_strtolower($this
    ->randomName());
  taxonomy_vocabulary_save($vocabulary);
  $term = new stdClass();
  $term->name = 'Foo';
  $term->vid = $vocabulary->vid;
  taxonomy_term_save($term);
  $term = new stdClass();
  $term->name = 'Bar';
  $term->vid = $vocabulary->vid;
  taxonomy_term_save($term);

  // Create dependee.
  $this->dependee_on_value = $this
    ->randomString();
  $this->dependee_off_value = $this
    ->randomString();
  $dependee = array(
    'field_name' => 'dependee',
    'type' => 'list_text',
    'settings' => array(
      'allowed_values' => array(
        $this->dependee_on_value => t('Dependent fields are visible'),
        $this->dependee_off_value => t('Dependent fields are invisible'),
      ),
    ),
  );
  field_create_field($dependee);
  $dependee_instance = array(
    'field_name' => 'dependee',
    'entity_type' => 'node',
    'bundle' => 'conditional_fields_test',
    'label' => 'dependee_label',
  );
  field_create_instance($dependee_instance);

  // Reload the instance because we need the instance id.
  $dependee_instance = field_info_instance('node', 'dependee', 'conditional_fields_test');

  // Prepare one field for each widget type/field type combination.
  foreach (field_info_widget_types() as $widget_type => $widget_info) {

    // TODO: test files and images.
    if ($widget_type == 'file_generic' || $widget_type == 'image_image') {
      continue;
    }
    foreach ($widget_info['field types'] as $field_type) {
      $field_name = 'dependent_' . drupal_strtolower($this
        ->randomName());
      $dependent = array(
        'field_name' => $field_name,
        'type' => $field_type,
        'cardinality' => 1,
      );

      // Some fields need allowed values to function properly.
      if ($widget_info['module'] == 'options') {
        $dependent['settings']['allowed_values'] = array(
          'Foo',
          'Bar',
        );
      }
      if ($field_type == 'taxonomy_term_reference') {
        $dependent['settings']['allowed_values'] = array(
          array(
            'vocabulary' => $vocabulary->machine_name,
            'parent' => '0',
          ),
        );
      }
      $dependent_instance = array(
        'field_name' => $field_name,
        'entity_type' => 'node',
        'bundle' => 'conditional_fields_test',
        'widget' => array(
          'type' => $widget_type,
          'settings' => array(
            'display_label' => 1,
          ),
        ),
        'required' => TRUE,
        'label' => $widget_type . '_' . $field_type . '_label',
      );

      // Radios spit an error if submitted with no value.
      if ($widget_type == 'options_buttons' && $widget_info['module'] == 'list') {

        // This is the "Bar" option.
        $dependent_instance['default_value'] = 1;
      }
      $this->dependents[] = array(
        'field_type' => $field_type,
        'widget_type' => $widget_type,
        'widget' => $widget_info,
        'field' => $dependent,
        'instance' => $dependent_instance,
      );

      // Create a multiple value version of most fields.
      if ($widget_type == 'options_onoff') {
        continue;
      }
      $dependent['cardinality'] = -1;
      $dependent['field_name'] = $dependent_instance['field_name'] = $field_name . '_multiple';
      $dependent_instance['label'] .= '_multiple';
      $this->dependents[] = array(
        'field_type' => $field_type,
        'widget_type' => $widget_type,
        'widget' => $widget_info,
        'field' => $dependent,
        'instance' => $dependent_instance,
      );
    }
  }

  // Create fields and dependencies.
  $dependency_options = array(
    'value_form' => $this->dependee_on_value,
    'value' => array(
      array(
        'value' => $this->dependee_on_value,
      ),
    ),
  );
  foreach ($this->dependents as $dependent) {
    field_create_field($dependent['field']);
    field_create_instance($dependent['instance']);
    $dependent_instance = field_info_instance('node', $dependent['field']['field_name'], 'conditional_fields_test');
    conditional_fields_dependency_insert($dependee_instance['id'], $dependent_instance['id'], $dependency_options);
  }
}