You are here

public function UcAddressesEntityCase::testHooks in Ubercart Addresses 7

Test if entity hooks are invoked when using the address book API.

File

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

Class

UcAddressesEntityCase
Tests for Entity API integration.

Code

public function testHooks() {

  // Get the address book of the admin user.
  $addressBook = UcAddressesAddressBook::get($this->adminUser->uid);

  // Test presave and insert hook.
  $address = $addressBook
    ->addAddress();
  $address
    ->setField('last_name', self::randomName());
  $old_id = $address
    ->getId();
  $address
    ->save();
  $aid = $address
    ->getId();
  $this
    ->assertEntityHookInvoked('entity_presave', $old_id);
  $this
    ->assertEntityHookInvoked('entity_insert', $aid);

  // Ensure the load is NOT invoked, because the address
  // should be cached in memory.
  $addressBook
    ->getAddressById($aid);
  $this
    ->assertEntityHookNotInvoked('entity_load', $aid);

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

  // Test load hook.
  $address = $addressBook
    ->getAddressById($aid);
  $this
    ->assertEntityHookInvoked('entity_load', $aid);

  // Ensure the update is NOT invoked, because the address
  // didn't change.
  $address
    ->save();
  $this
    ->assertEntityHookNotInvoked('entity_update', $aid);

  // Now, change something on the address and save again.
  // This time the update hook should be invoked.
  $address
    ->setField('first_name', self::randomName());
  $address
    ->save();
  $this
    ->assertEntityHookInvoked('entity_update', $aid);

  // Test if the delete hook is invoked.
  // This address is not marked as a default address, so deleting
  // should go fine.
  $address
    ->delete();
  $this
    ->assertEntityHookInvoked('entity_delete', $aid);
}