You are here

public function CRMCoreContactUITestCase::testContactOperations in CRM Core 7

User with permissions 'administer crm_core_contact entities' should be able to create/edit/delete contacts of any contact type.

File

modules/crm_core_contact_ui/crm_core_contact_ui.test, line 23

Class

CRMCoreContactUITestCase

Code

public function testContactOperations() {

  // Create user and login.
  $user = $this
    ->drupalCreateUser(array(
    'administer crm_core_contact entities',
    'view any crm_core_contact entity',
  ));
  $this
    ->drupalLogin($user);

  // There should be no contacts available after fresh installation and
  // there is link to create new contacts.
  $this
    ->drupalGet('crm-core/contact');
  $this
    ->assertText(t('There are no contacts available. Add one now.'), t('No contacts available after fresh installation.'));
  $this
    ->assertLink(t('Add a contact'));

  // Open page crm-core/contact/add and assert standard contact types available.
  $this
    ->drupalGet('crm-core/contact/add');
  $this
    ->assertLink(t('Add Household'));
  $this
    ->assertLink(t('Add Individual'));
  $this
    ->assertLink(t('Add Organization'));

  // Create Household contact.
  $household_node = array(
    'contact_name[und][0][family]' => $this
      ->randomName(),
  );
  $this
    ->drupalPost('crm-core/contact/add/household', $household_node, crm_core_contact_ui_save_contact_button_name('household'));

  // Assert we were redirected back to the list of contacts.
  $this
    ->assertUrl('crm-core/contact');
  $this
    ->assertLink($household_node['contact_name[und][0][family]'], 0, t('Newly created contact title listed.'));
  $this
    ->assertText(t('Household'), t('Newly created contact type listed.'));

  // Create individual contact.
  $individual_node = array(
    'contact_name[und][0][title]' => 'Mr.',
    'contact_name[und][0][given]' => $this
      ->randomName(),
    'contact_name[und][0][middle]' => $this
      ->randomName(),
    'contact_name[und][0][family]' => $this
      ->randomName(),
    'contact_name[und][0][generational]' => 'IV',
    'contact_name[und][0][credentials]' => $this
      ->randomName(),
  );
  $this
    ->drupalPost('crm-core/contact/add/individual', $individual_node, crm_core_contact_ui_save_contact_button_name('individual'));

  // Assert we were redirected back to the list of contacts.
  $this
    ->assertUrl('crm-core/contact');
  $link_label = $this
    ->getIndividualContactTitle($individual_node);
  $this
    ->assertLink($link_label, 0, t('Newly created contact title listed.'));
  $this
    ->assertText(t('Individual'), t('Newly created contact type listed.'));

  // Create Organization contact.
  $organization_node = array(
    'contact_name[und][0][family]' => $this
      ->randomName(),
  );
  $this
    ->drupalPost('crm-core/contact/add/organization', $organization_node, crm_core_contact_ui_save_contact_button_name('organization'));

  // Assert we were redirected back to the list of contacts.
  $this
    ->assertUrl('crm-core/contact');
  $this
    ->assertLink($organization_node['contact_name[und][0][family]'], 0, t('Newly created contact title listed.'));
  $this
    ->assertText(t('Organization'), t('Newly created contact type listed.'));

  // Edit operations.
  // We know that created nodes houshold is id 1, individual is no 2,
  // organization is no 3. But we should have better API to find contact by name.
  $household_node = $this
    ->householdContactValues();
  $this
    ->drupalPost('crm-core/contact/1/edit', $household_node, crm_core_contact_ui_save_contact_button_name('household'));

  // Assert we were redirected back to the list of contacts.
  $this
    ->assertUrl('crm-core/contact/1');
  $this
    ->assertText($household_node['contact_name[und][0][family]'], 0, t('Contact updated.'));

  // Check listing page.
  $this
    ->drupalGet('crm-core/contact');
  $this
    ->assertLink($household_node['contact_name[und][0][family]'], 0, t('Updated contact title listed.'));

  // Delete household contact.
  $this
    ->drupalPost('crm-core/contact/1/delete', array(), t('Yes'));
  $this
    ->assertUrl('crm-core/contact');
  $this
    ->assertNoLink($household_node['contact_name[und][0][family]'], 0, t('Deleted contact title no more listed.'));

  // Edit individual contact.
  $individual_node = $this
    ->individualContactValues();
  $this
    ->drupalPost('crm-core/contact/2/edit', $individual_node, crm_core_contact_ui_save_contact_button_name('individual'));

  // Assert we were redirected back to the list of contacts.
  $this
    ->assertUrl('crm-core/contact/2');

  // Check listing page.
  $this
    ->drupalGet('crm-core/contact');
  $link_label = $this
    ->getIndividualContactTitle($individual_node);
  $this
    ->assertLink($link_label, 0, t('Updated individual contact title listed.'));

  // Delete individual contact.
  $this
    ->drupalPost('crm-core/contact/2/delete', array(), t('Yes'));
  $this
    ->assertUrl('crm-core/contact');
  $this
    ->assertNoLink($link_label, 0, t('Deleted individual contact title no more listed.'));

  // Edit organization contact.
  $organization_node = $this
    ->organizationContactValues();
  $this
    ->drupalPost('crm-core/contact/3/edit', $organization_node, crm_core_contact_ui_save_contact_button_name('organization'));

  // Assert we were redirected back to the list of contacts.
  $this
    ->assertUrl('crm-core/contact/3');
  $this
    ->assertText($organization_node['contact_name[und][0][family]'], 0, t('Contact updated.'));

  // Check listing page.
  $this
    ->drupalGet('crm-core/contact');
  $this
    ->assertLink($organization_node['contact_name[und][0][family]'], 0, t('Updated contact title listed.'));

  // Delete organization contact.
  $this
    ->drupalPost('crm-core/contact/3/delete', array(), t('Yes'));
  $this
    ->assertUrl('crm-core/contact');
  $this
    ->assertNoLink($organization_node['contact_name[und][0][family]'], 0, t('Deleted contact title no more listed.'));

  // Assert that there are no contacts left.
  $this
    ->assertText(t('There are no contacts available. Add one now.'), t('No contacts available after fresh installation.'));
}