You are here

public function UcAddressesApiTestCase::testPerformanceHintSetting in Ubercart Addresses 7

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

Tests the address book's performance hint setting.

File

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

Class

UcAddressesApiTestCase
Test cases for the api component.

Code

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

  // Add three addresses and save them all.
  for ($i = 0; $i < 3; $i++) {
    $address = $addressBook
      ->addAddress();
    $values = self::getEditAddressValues();
    $address
      ->setMultipleFields($values['values'], TRUE);
  }
  $addressBook
    ->save();

  // Get addresses for later use.
  $addresses = $addressBook
    ->getAddresses();

  // Make sure we have three addresses.
  $this
    ->assertEqual(count($addresses), 3, 'The address book contains 3 addresses.');

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

  // Ensure the performance hint is set to load a single address.
  $addressBook
    ->setPerformanceHint(UcAddressesAddressBook::PERF_HINT_LOAD_ONE);
  $this
    ->assertEqual($addressBook
    ->getPerformanceHint(), UcAddressesAddressBook::PERF_HINT_LOAD_ONE, 'Performance hint is set to PERF_HINT_LOAD_ONE.');

  // Load the first address.
  $address1 = reset($addresses);
  $addressBook
    ->getAddressById($address1
    ->getId());

  // Check if the only address that is loaded now is $address1.
  foreach ($addresses as $address) {
    if ($address
      ->getId() == $address1
      ->getId()) {

      // The address should be loaded.
      $this
        ->assertTrue($addressBook
        ->addressExists($address
        ->getId()), format_string('Address %aid is loaded.', array(
        '%aid' => $address
          ->getId(),
      )));
    }
    else {

      // The address should not be loaded.
      $this
        ->assertFalse($addressBook
        ->addressExists($address
        ->getId()), format_string('Address %aid is not loaded.', array(
        '%aid' => $address
          ->getId(),
      )));
    }
  }

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

  // Ensure the performance hint is set to load all address.
  $addressBook
    ->setPerformanceHint(UcAddressesAddressBook::PERF_HINT_LOAD_ALL);
  $this
    ->assertEqual($addressBook
    ->getPerformanceHint(), UcAddressesAddressBook::PERF_HINT_LOAD_ALL, 'Performance hint is set to PERF_HINT_LOAD_ALL.');

  // Load address1.
  $addressBook
    ->getAddressById($address1
    ->getId());

  // Check if all address are loaded.
  foreach ($addresses as $address) {

    // Each address should be loaded.
    $this
      ->assertTrue($addressBook
      ->addressExists($address
      ->getId()), format_string('Address %aid is loaded.', array(
      '%aid' => $address
        ->getId(),
    )));
  }

  // Set performance hint back to load one.
  $addressBook
    ->setPerformanceHint(UcAddressesAddressBook::PERF_HINT_LOAD_ONE);
}