You are here

public function UcAddressesTokenTestCase::testAddressTokens in Ubercart Addresses 7

Test if tokens defined by Ubercart Addresses work.

File

tests/uc_addresses.tokens.test, line 29
Test cases for the token replacement.

Class

UcAddressesTokenTestCase
Test cases for the api component.

Code

public function testAddressTokens() {

  // Add two addresses, one default shipping, the other default billing.
  $addressBook = UcAddressesAddressBook::get($this->adminUser->uid);
  $shippingAddress = $addressBook
    ->addAddress();
  $values = self::getEditAddressValues();
  $shippingAddress
    ->setMultipleFields($values['values'], TRUE);
  $addressBook
    ->setAddressAsDefault($shippingAddress, 'shipping');
  $billingAddress = $addressBook
    ->addAddress();
  $values = self::getEditAddressValues();
  $billingAddress
    ->setMultipleFields($values['values'], TRUE);
  $addressBook
    ->setAddressAsDefault($billingAddress, 'billing');
  $addressBook
    ->save();

  // Setup all available tokens.
  $tokens = array(
    'first_name',
    'last_name',
    'phone',
    'company',
    'street1',
    'street2',
    'city',
    'country:country_name',
    'country:country_code2',
    'country:country_code3',
    'country:country_name_if',
    'country:country_code2_if',
    'country:country_code3_if',
    'country',
    'zone:zone_code',
    'zone:zone_name',
    'zone',
    'postal_code',
    'address_name',
    'default_shipping',
    'default_billing',
    'aid',
    'uid',
    'created',
    'modified',
  );

  // Test if address tokens get replaced properly.
  $data['uc_addresses'] = $billingAddress;
  $this
    ->doAddressTokenTests($tokens, $data, $billingAddress);

  // Test if address tokens in user context get replaced properly.
  $data['user'] = $this->adminUser;
  $this
    ->doAddressTokenTests($tokens, $data, $billingAddress, 'user:default-billing-address:');
  $this
    ->doAddressTokenTests($tokens, $data, $shippingAddress, 'user:default-shipping-address:');

  // Test if address tokens for the current user get replaced properly.
  global $user;
  $user = $this->adminUser;
  $data = array();
  $this
    ->doAddressTokenTests($tokens, $data, $billingAddress, 'current-user:default-billing-address:');
  $this
    ->doAddressTokenTests($tokens, $data, $shippingAddress, 'current-user:default-shipping-address:');

  // Test if all tokens in user context get replaced when used in combination.
  $text = '';
  $data['user'] = $this->adminUser;
  $prefixes = array(
    'user:default-billing-address:',
    'user:default-shipping-address:',
    'current-user:default-shipping-address:',
    'current-user:default-billing-address:',
  );
  foreach ($tokens as $token) {
    foreach ($prefixes as $prefix) {
      $text .= '[' . $prefix . $token . ']' . "\n";
    }
  }
  $replaced = token_replace($text, $data);
  foreach ($tokens as $token) {
    foreach ($prefixes as $prefix) {
      $full_token = '[' . $prefix . $token . ']';

      // The token itself should not be visible in the replaced text.
      $this
        ->assertFalse(strpos($replaced, $full_token), t('The token @token got replaced correctly when used in combination with other tokens.', array(
        '@token' => $full_token,
      )));
    }
  }

  // Output the total replaced text.
  $this
    ->verbose('<pre>' . $replaced . '</pre>');
}