You are here

public function ConditionalFieldsTestCase::testNodeDependencies in Conditional Fields 7.3

Tests field dependencies on a node.

File

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

Class

ConditionalFieldsTestCase
@file Test Conditional Fields functionality and UI.

Code

public function testNodeDependencies() {

  // Try to submit a node with triggered dependencies.
  // The submit should fail because the dependent fields are required.
  $langcode = LANGUAGE_NONE;
  $edit = array(
    "dependee[{$langcode}]" => $this->dependee_on_value,
  );
  $this
    ->drupalPost('node/add/conditional-fields-test', $edit, t('Save'));
  foreach ($this->dependents as $dependent) {

    // Skip fields that always have a default value.
    if ($dependent['widget_type'] == 'options_buttons' && $dependent['field']['cardinality'] == 1 || in_array($dependent['widget_type'], array(
      'options_select',
      'options_buttons',
    )) && $dependent['field_type'] == 'taxonomy_term_reference') {
      continue;
    }

    // Multiple value textfields are dumb and can't find their own name.
    if ($dependent['field']['cardinality'] == -1 && in_array($dependent['widget_type'], array(
      'number',
      'text_textfield',
      'text_textarea',
      'text_textarea_with_summary',
    ))) {
      $name = '';
    }
    else {
      $name = $dependent['widget_type'] . '_' . $dependent['field_type'] . '_label';
      $name .= $dependent['field']['cardinality'] == -1 ? '_multiple' : '';
    }
    $this
      ->assertRaw(t('!name field is required.', array(
      '!name' => $name,
    )), 'Triggered ' . ($dependent['field']['cardinality'] == -1 ? 'multiple' : 'single') . ' value required ' . $dependent['field_type'] . ' dependent with widget ' . $dependent['widget_type'] . ' and no value fails validation');
  }

  // Fill the dependents with values and save the node.
  $edit = array(
    "dependee[{$langcode}]" => $this->dependee_on_value,
  );
  foreach ($this->dependents as $dependent) {

    // Text fields have structure field_name[langcode][delta][value].
    if (in_array($dependent['widget_type'], array(
      'number',
      'text_textfield',
      'text_textarea',
      'text_textarea_with_summary',
    ))) {
      $edit[$dependent['field']['field_name'] . "[{$langcode}][0][value]"] = '1';
    }
    elseif ($dependent['widget_type'] == 'options_select') {
      if ($dependent['field']['cardinality'] == 1) {

        // Single value select fields have structure field_name[langcode].
        $edit[$dependent['field']['field_name'] . "[{$langcode}]"] = '1';
      }
      else {

        // Multiple value select fields have structure field_name[langcode][].
        $edit[$dependent['field']['field_name'] . "[{$langcode}][]"] = '1';
      }
    }
    elseif (in_array($dependent['widget_type'], array(
      'options_buttons',
      'options_onoff',
      'taxonomy_autocomplete',
    ))) {
      if ($dependent['field']['cardinality'] == 1 || $dependent['widget_type'] == 'taxonomy_autocomplete') {

        // Radios and autocomplete fields have structure field_name[langcode].
        $edit[$dependent['field']['field_name'] . "[{$langcode}]"] = '1';
      }
      else {

        // Checkboxes have structure field_name[langcode][delta].
        $edit[$dependent['field']['field_name'] . "[{$langcode}][1]"] = '1';
      }
    }
    elseif (in_array($dependent['widget_type'], array(
      'file_generic',
      'image_image',
    ))) {

      // TODO.
      // $edit[$dependent['field']['field_name'] . "[$langcode][0][fid]"] = '1';
    }
  }
  $this
    ->drupalPost('node/add/conditional-fields-test', $edit, t('Save'));
  $this
    ->assertRaw(t('@type %title has been created.', array(
    '@type' => 'Conditional Fields Test Node Type',
    '%title' => '',
  )), 'Node was created with triggered dependencies.');

  // Verify that the fields are visible on node view.
  foreach ($this->dependents as $dependent) {
    $this
      ->assertText($dependent['instance']['label'] . ':', 'Triggered ' . ($dependent['field']['cardinality'] == -1 ? 'multiple' : 'single') . ' value ' . $dependent['field_type'] . ' dependent with widget ' . $dependent['widget_type'] . ' is visible on node view');
  }

  // Untrigger the dependency and verify that node is updated.
  $edit = array(
    "dependee[{$langcode}]" => $this->dependee_off_value,
  );
  $this
    ->drupalPost('node/1/edit', $edit, t('Save'));
  $this
    ->assertRaw(t('@type %title has been updated.', array(
    '@type' => 'Conditional Fields Test Node Type',
    '%title' => '',
  )), 'Node was updated with untriggered dependencies.');

  // Verify that fields are invisible on node view.
  foreach ($this->dependents as $dependent) {
    $this
      ->assertNoText($dependent['instance']['label'] . ':', 'Triggered ' . ($dependent['field']['cardinality'] == -1 ? 'multiple' : 'single') . ' value ' . $dependent['field_type'] . ' dependent with widget ' . $dependent['widget_type'] . ' is invisible on node view');
  }
}