You are here

public static function UcAddressesTestCase::generateAddressFieldValue in Ubercart Addresses 7

Same name and namespace in other branches
  1. 6.2 tests/UcAddressesTestCase.test \UcAddressesTestCase::generateAddressFieldValue()

Generates a value for an address field.

Parameters

string $fieldname: The field to generate a value for

array $values: The field values already generated

Return value

string The generated value

2 calls to UcAddressesTestCase::generateAddressFieldValue()
UcAddressesApiTestCase::doHandlerTests in tests/uc_addresses.api.test
Do handler tests.
UcAddressesTestCase::getEditAddressValues in tests/UcAddressesTestCase.test
Generates an array of values to post into an address form

File

tests/UcAddressesTestCase.test, line 415
Contains base class for Ubercart Addresses tests.

Class

UcAddressesTestCase
Base class for Ubercart Addresses tests.

Code

public static function generateAddressFieldValue($fieldname, &$values) {
  switch ($fieldname) {
    case 'address_name':

      // By default an empty address name is returned to avoid name collisions
      // when that aspect is not tested.
      return '';
    case 'default_shipping':
    case 'default_billing':

      // Don't make addresses the default if this aspect is not tested.
      return 0;
    case 'postal_code':

      // A numeric code for postal codes.
      return mt_rand(10000, 99999);
    case 'country':

      // The default country in Ubercart.
      $country_id = uc_store_default_country();

      // This can be a cached old value, ensure the country exist.
      $result = db_query("SELECT COUNT(*) FROM {uc_countries} WHERE country_id = :id AND version > :version", array(
        ':id' => $country_id,
        ':version' => 0,
      ))
        ->fetchField();
      if ($result == 0) {
        $country_id = db_query_range("SELECT country_id FROM {uc_countries} WHERE version > :version", 0, 1, array(
          ':version' => 0,
        ))
          ->fetchField();
      }
      return $country_id;
    case 'zone':

      // Random zone based on the defined country.
      if (isset($values['country'])) {
        $country_id = $values['country'];
      }
      else {
        $country_id = self::generateAddressFieldValue('country', $values);
      }
      return db_query("SELECT zone_id FROM {uc_zones} WHERE zone_country_id = :zone_country_id ORDER BY rand()", array(
        ':zone_country_id' => $country_id,
      ))
        ->fetchField();
    default:

      // In all other cases it is assummed that it's a textfield that needs to be filled in.
      return self::randomString(12);
  }
}