You are here

function ReferenceOptionLimitEntityreferenceTestCase::getEditArray in Reference field option limit 7

Helper to get the values for POST and AJAX operations.

The structure of this varies according to the widgets in use.

Parameters

$country_node_title: The title of the country node that should be present in the POST array.

$settings: The array of field settings passed to helperTestNodeCreateForm().

Return value

An array containing the following properties:

  • 'edit': The $edit array suitable for drupalPostAJAX() or drupalPost().
  • 'triggering_element': The name of the triggering element suitable for drupalPostAJAX().
2 calls to ReferenceOptionLimitEntityreferenceTestCase::getEditArray()
ReferenceOptionLimitEntityreferenceTestCase::helperTestNodeCreateForm in tests/reference_option_limit.test
Helper to run tests.
ReferenceOptionLimitEntityreferenceTestCase::rolPostAJAXCountryField in tests/reference_option_limit.test
Wrapper around drupalPostAJAX() for updating the country field.

File

tests/reference_option_limit.test, line 536
Contains tests for the Reference option limit module.

Class

ReferenceOptionLimitEntityreferenceTestCase
Test use of the module with entityreference fields.

Code

function getEditArray($country_node_title, $settings) {
  $node = $this
    ->getNodeByTitle($country_node_title);
  $return = array();

  // The structure of the $edit array we pass to to a post depends on the
  // form element, and thus on the field widget and cardinality. Set the key
  // to use in the $edit array accordingly.
  switch ($settings['widget'] . '-' . $settings['cardinality']) {
    case 'options_select-1':

      // Single select list.
      $return['edit'] = array(
        'test_rol_er_country[und]' => $node->nid,
      );
      $return['triggering_element'] = 'test_rol_er_country[und]';
      break;
    case 'options_select-' . FIELD_CARDINALITY_UNLIMITED:

      // Multi-select.
      $return['edit'] = array(
        'test_rol_er_country[und][]' => $node->nid,
      );
      $return['triggering_element'] = 'test_rol_er_country[und][]';
      break;
    case 'options_buttons-1':

      // Radios.
      $return['edit'] = array(
        'test_rol_er_country[und]' => $node->nid,
      );
      $return['triggering_element'] = 'test_rol_er_country[und]';
      break;
    case 'options_buttons-' . FIELD_CARDINALITY_UNLIMITED:

      // Checkboxes.
      $return['edit'] = array(
        "test_rol_er_country[und][{$node->nid}]" => $node->nid,
      );

      // Checkboxes have a special problem when there is a default value on
      // one of them. Because they are all separate elements, drupalPost()
      // tries to preserve their individual values when it fills out the $edit
      // array. And because drupalPostAJAX() doesn't update the internal
      // browser's HTML with the change (i.e., it doesn't change the
      // checkbox's state), a checkbox that is checked by default will keep
      // getting added to the $edit array as being checked in drupalPost().
      // So we have to explicitly uncheck it here every time.
      // This is (I believe) a bug in Drupal core: see
      // https://www.drupal.org/node/2423159
      foreach ($settings['default'] as $default_node_title) {
        if ($country_node_title == $default_node_title) {

          // Obviously if it happens that the default checkbox is one we now
          // want to set, leave it.
          continue;
        }
        $default_node = $this
          ->getNodeByTitle($default_node_title);
        $return['edit']["test_rol_er_country[und][{$default_node->nid}]"] = FALSE;
      }
      $return['triggering_element'] = "test_rol_er_country[und][{$node->nid}]";
      break;
  }
  return $return;
}