You are here

protected function UCXFTestCase::getEditValues in Extra Fields Checkout Pane 7

Same name and namespace in other branches
  1. 6.2 uc_extra_fields_pane.test \UCXFTestCase::getEditValues()

Generates an array of values to post into an address form

Parameters

array $fields: An array of UCXF_Field objects.

array $parents: The parent form elements.

array $values: (Some of) the values to use in the address form.

string $prefix: Optionally prefixes every field name.

Return value

array An array with for each field a value.

3 calls to UCXFTestCase::getEditValues()
UCXFCheckoutTestCase::testSanitizing in ./uc_extra_fields_pane.test
Test if field values are properly sanitized.
UCXFOrderTestCase::testOrder in ./uc_extra_fields_pane.test
Test the order administration.
UCXFTestCase::checkout in ./uc_extra_fields_pane.test
Overrides UbercartTestHelper::checkout().

File

./uc_extra_fields_pane.test, line 217
Automated tests for Extra Fields Pane

Class

UCXFTestCase
Base class for all Extra Fields Pane test cases.

Code

protected function getEditValues($fields, $parents = array(), $values = array(), $prefix = '') {

  // Initialize values array
  $form_values = array();
  $extra_values = array();

  // Calculate parent string if needed.
  $parent_string = '';
  if (count($parents) > 0) {
    foreach ($parents as $parent) {
      if ($parent_string) {
        $parent_string = $parent_string . '[' . $parent . ']';
      }
      else {
        $parent_string = $parent;
      }
    }
  }

  // Fill in value for every field
  foreach ($fields as $field) {
    if (isset($values[$field->db_name])) {

      // The value is already set. Do not override it.
      continue;
    }
    $default = $this
      ->generateDefaultValue($field);
    if (!is_null($default)) {
      $values[$field->db_name] = $this
        ->generateDefaultValue($field);
    }
    elseif ($field->value_type == UCXF_Field::UCXF_WIDGET_TYPE_CONSTANT || $field->value_type == UCXF_Field::UCXF_WIDGET_TYPE_PHP) {
      switch ($field->db_name) {
        case $this->hiddenConstantField:
          $extra_values[$field->db_name] = 'hidden constant';
          break;
        case $this->hiddenPhpField:
          $extra_values[$field->db_name] = 'hidden string';
          break;
        default:
          $extra_values[$field->db_name] = $field
            ->generate_value();
          break;
      }
    }
  }

  // Prefix values and add parents
  foreach ($values as $fieldname => $value) {

    // Set in parents if needed
    $formfieldname = $prefix . $fieldname;
    if ($parent_string) {
      $formfieldname = $parent_string . '[' . $formfieldname . ']';
    }
    $form_values[$formfieldname] = $value;
  }
  return array(
    'form_values' => $form_values,
    'values' => array_merge($values, $extra_values),
  );
}