You are here

public function UcAddressesApiTestCase::testAddressBookCrud in Ubercart Addresses 7

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

Tests if addresses can be added, saved and deleted.

File

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

Class

UcAddressesApiTestCase
Test cases for the api component.

Code

public function testAddressBookCrud() {

  // First, get the address book of the admin user.
  $addressBook = $this
    ->UcAddressesGetAddressBook($this->adminUser->uid);

  // Add a new address.
  $address1 = $addressBook
    ->addAddress();

  // Ensure the address book has exactly one address in its registry.
  $this
    ->assertEqual(count($addressBook
    ->getAddresses()), 1, 'The address book contains exactly one address.');

  // Try to delete the address again and test again how many addresses the address book contains (should be 0).
  $address1
    ->delete();
  $this
    ->assertEqual(count($addressBook
    ->getAddresses()), 0, 'The address book contains no addresses.');

  // Add a new address and ensure it is new.
  $address2 = $addressBook
    ->addAddress();
  $this
    ->assertTrue($address2
    ->isNew(), 'The address is new.');

  // Fill the address (all field values should be present).
  $values = self::getEditAddressValues();
  $address2
    ->setMultipleFields($values['values'], TRUE);

  // Save all addresses in the address book and ensure the address is no longer new (thus has a definitive address ID).
  $addressBook
    ->save();
  $this
    ->assertFalse($address2
    ->isNew(), 'The address is no longer new.');

  // Check if the address exists in the database.
  $this
    ->assertTrue(self::checkAddressValuesInDatabase($values['values']), 'The address is correctly saved to the database.');

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

  // Try to get the address for the user.
  $address2_2 = $addressBook
    ->getAddressById($address2
    ->getId());

  // Ensure these two addresses have the same ID.
  $this
    ->assertEqual($address2_2
    ->getId(), $address2
    ->getId(), format_string('Address %aid succesfully loaded from the database.', array(
    '%aid' => $address2
      ->getId(),
  )));

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

  // Try to delete the address.
  $this
    ->assertTrue($addressBook
    ->deleteAddressById($address2
    ->getId()), format_string('Address %aid is deleted.', array(
    '%aid' => $address2
      ->getId(),
  )));

  // Ensure the database table is empty now.
  $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.');
}