You are here

function CRMCoreContactTestCase::testCRUD in CRM Core 7

File

modules/crm_core_contact/tests/crm_core_contact.test, line 19

Class

CRMCoreContactTestCase
CRMCoreContactTestCase.

Code

function testCRUD() {
  $type = 'dog';

  // crm_core_contact_type_new().
  $contact_type = crm_core_contact_type_new($type);
  $this
    ->assertTrue(isset($contact_type->description) && empty($contact_type->description), t('New contact type description exists.'));
  $this
    ->assertTrue(isset($contact_type->name) && empty($contact_type->name), t('New contact type name exists.'));
  $this
    ->assertTrue(isset($contact_type->type) && $contact_type->type == $type, t('New contact type type exists.'));

  // crm_core_contact_type_save().
  $contact_type->name = $this
    ->randomName();
  $contact_type->description = $this
    ->randomString();
  crm_core_contact_type_save($contact_type);

  // crm_core_contact_type_load().
  $contact_type_load = crm_core_contact_type_load($type);
  $this
    ->assertEqual($contact_type->type, $contact_type_load->type, t('Loaded contact type has same type.'));
  $this
    ->assertEqual($contact_type->name, $contact_type_load->name, t('Loaded contact type has same name.'));
  $this
    ->assertEqual($contact_type->description, $contact_type_load->description, t('Loaded contact type has same description.'));
  $this
    ->assertTrue(!empty($contact_type_load->id), 'Loaded contact type has id.');

  // Update test.
  $new_description = $this
    ->randomString();
  $contact_type_load = crm_core_contact_type_load($type);
  $contact_type_load->description = $new_description;
  crm_core_contact_type_save($contact_type_load);
  $contact_type_load_updated = crm_core_contact_type_load($type);
  $this
    ->assertEqual($new_description, $contact_type_load_updated->description, 'Updated contact type description match.');

  // Delete contact type.
  crm_core_contact_type_delete($contact_type->id);

  // Avoid static cache.
  $contact_type_load = crm_core_contact_type_load($type, TRUE);
  $this
    ->assertTrue(empty($contact_type_load), t('Contact type deleted.'));
}