You are here

public function UcAddressesApiTestCase::testDefaultAddresses in Ubercart Addresses 7

Same name and namespace in other branches
  1. 6.2 tests/uc_addresses.api.test \UcAddressesApiTestCase::testDefaultAddresses()

Tests default addresses.

File

tests/uc_addresses.api.test, line 107
Test cases for the api component.

Class

UcAddressesApiTestCase
Test cases for the api component.

Code

public function testDefaultAddresses() {
  $addressBook = $this
    ->UcAddressesGetAddressBook($this->adminUser->uid);

  // Create a new address and mark it as default billing.
  $address1 = $addressBook
    ->addAddress();
  $addressBook
    ->setAddressAsDefault($address1, 'billing');
  $address1
    ->save();

  // Ensure that the address is the default billing address.
  $default_billing_aid = db_query("SELECT aid FROM {uc_addresses} WHERE default_billing = :default_billing", array(
    ':default_billing' => 1,
  ))
    ->fetchField();
  $this
    ->assertEqual($address1
    ->getId(), $default_billing_aid, 'The address is the default billing address.');

  // Reset the address book.
  $addressBook
    ->reset();

  // Try to delete address1. This should not be possible, because
  // deleting default addresses is not allowed.
  $this
    ->assertFalse($addressBook
    ->deleteAddressById($address1
    ->getId()), format_string('Address %aid is not deleted.', array(
    '%aid' => $address1
      ->getId(),
  )));

  // Create a new address and delete the first one.
  $address2 = $addressBook
    ->addAddress();
  $addressBook
    ->setAddressAsDefault($address2, 'billing');
  $this
    ->assertTrue($addressBook
    ->deleteAddressById($address1
    ->getId()), format_string('Address %aid is deleted.', array(
    '%aid' => $address1
      ->getId(),
  )));

  // Ensure the database table is empty now ($address2 is not yet saved).
  $number_of_addresses = db_query("SELECT COUNT(aid) AS number_of_addresses FROM {uc_addresses}")
    ->fetchField();
  $this
    ->assertEqual($number_of_addresses, 0, 'There are no addresses in the uc_addresses table.');

  // Now save $address2 and ensure that this address is the default
  // billing address in the database.
  $address2
    ->save();
  $default_billing_aid = db_query("SELECT aid FROM {uc_addresses} WHERE default_billing = :default_billing", array(
    ':default_billing' => 1,
  ))
    ->fetchField();
  $this
    ->assertEqual($address2
    ->getId(), $default_billing_aid, 'The address is the default billing address.');

  // Create a third address and mark this as default billing as well.
  $address3 = $addressBook
    ->addAddress();
  $addressBook
    ->setAddressAsDefault($address3, 'billing');
  $address3
    ->save();

  // Ensure the user has only one default billing address.
  $aids = db_select('uc_addresses')
    ->fields('uc_addresses', array(
    'aid',
  ))
    ->condition('default_billing', 1)
    ->execute()
    ->fetchAll();
  $this
    ->assertEqual(count($aids), 1, 'The database contains one default billing address.');
}