You are here

public function UcAddressesEntityCase::testEntityCrud in Ubercart Addresses 7

Test Ubercart Addresses Entity CRUD.

File

tests/uc_addresses.entity.test, line 38
Test cases address entity.

Class

UcAddressesEntityCase
Tests for Entity API integration.

Code

public function testEntityCrud() {
  $this
    ->drupalLogin($this->adminUser);

  // CREATE TESTS.
  $values = self::getEditAddressValues();
  $address_values = $values['values'];
  $address_values['uid'] = $this->adminUser->uid;
  $entity = entity_create('uc_addresses', $address_values);
  $address = $this
    ->getEntityAddress($entity);

  // Ensure that the address inside the entity contains all set values.
  if ($address instanceof UcAddressesAddress) {
    foreach ($address_values as $field => $value) {
      $this
        ->assertEqual($address
        ->getField($field), $address_values[$field], t('The field %field was set correctly with entity_create().', array(
        '%field' => $field,
      )));
    }
  }

  // Save the entity and ensure it is saved to the database.
  $old_id = $entity
    ->identifier();
  entity_save('uc_addresses', $entity);
  $this
    ->assertEntityHookInvoked('entity_presave', $old_id);
  $this
    ->assertEntityHookInvoked('entity_insert', $entity
    ->identifier());
  $this
    ->assertTrue(self::checkAddressValuesInDatabase($address_values), 'The address was correctly saved using entity_save().');
  if ($address instanceof UcAddressesAddress) {

    // Do tests with other entity API functions.
    $this
      ->doOtherEntityTests($entity, $address);
  }

  // Test the Entity Wrapper.
  $this
    ->doEntityWrapperTests($entity, $address_values);

  // LOAD TESTS.
  $aid = $this
    ->createAddress($this->customer);

  // Load this address through the address book.
  $address = UcAddressesAddressBook::get($this->customer->uid)
    ->getAddressById($aid);

  // Now load through Entity API.
  $entity = entity_load_single('uc_addresses', $aid);
  $this
    ->assertEntityHookInvoked('entity_load', $aid);

  // Ensure these two are equal.
  $this
    ->assertTrue($address === $this
    ->getEntityAddress($entity), 'Address loaded via Address Book API is equal to address loaded via Entity API.');

  // Do tests with other entity API functions.
  $this
    ->doOtherEntityTests($entity, $address);

  // Test the Entity Wrapper.
  $this
    ->doEntityWrapperTests($entity, $address
    ->getRawFieldData());

  // VIEW TESTS.
  // Load addresses and entities first.
  $addressBook = UcAddressesAddressBook::get($this->customer);
  $addresses = $addressBook
    ->getAddresses();
  $addressBook
    ->reset();
  $aids = array();
  foreach ($addresses as $address) {
    $aids[] = $address
      ->getId();
  }
  $entities = entity_load('uc_addresses', $aids);

  // View and render the entities.
  $content = entity_view('uc_addresses', $entities);
  $output = drupal_render($content);
  $this
    ->verbose($output);

  // Test if all addresses are displayed.
  foreach ($addresses as $address) {
    $this
      ->doAddressValuesDisplayedTests($address
      ->getRawFieldData(), 'address_view', $output);
  }

  // UPDATE TESTS.
  // Set some fields directly.
  $values = self::getEditAddressValues();
  $address_update_values = $values['values'];
  foreach ($address_update_values as $field => $value) {
    $entity->{$field} = $value;
  }
  $address = $this
    ->getEntityAddress($entity);
  if ($address instanceof UcAddressesAddress) {
    foreach ($address_update_values as $field => $value) {
      $this
        ->assertEqual($address
        ->getField($field), $address_update_values[$field], t('The field %field was set correctly when setting it directly with $entity->$field.', array(
        '%field' => $field,
      )));
    }

    // Use the 'aid' from the address to ensure later that the address
    // got updated instead of inserted.
    $address_update_values['aid'] = $address
      ->getId();
  }

  // Save the entity and ensure it is updated in the database.
  entity_save('uc_addresses', $entity);
  $this
    ->assertEntityHookInvoked('entity_presave', $entity
    ->identifier());
  $this
    ->assertEntityHookInvoked('entity_update', $entity
    ->identifier());
  $this
    ->assertTrue(self::checkAddressValuesInDatabase($address_update_values), 'The address was correctly updated using entity_save().');
  if ($address instanceof UcAddressesAddress) {

    // Do tests with other entity API functions.
    $this
      ->doOtherEntityTests($entity, $address);
  }

  // Test the Entity Wrapper.
  $this
    ->doEntityWrapperTests($entity, $address_update_values);

  // DELETE TESTS.
  $aid = $this
    ->createAddress($this->customer);
  entity_delete('uc_addresses', $aid);
  $this
    ->assertEntityHookInvoked('entity_delete', $aid);

  // Ensure the address is removed from the database.
  $result = (int) db_select('uc_addresses')
    ->fields('uc_addresses', array(
    'aid',
  ))
    ->condition('aid', $aid)
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertTrue($result === 0, 'The address was deleted using entity_delete().');

  // Ensure the address has been removed from the address book too.
  $deleted_address = UcAddressesAddressBook::get($this->customer->uid)
    ->getAddressById($aid);
  $this
    ->assertFalse($deleted_address, 'The address was deleted from the address book entity_delete().');
}