You are here

public function IndividualUiTest::testIndividualOperations in CRM Core 8

Same name and namespace in other branches
  1. 8.2 modules/crm_core_contact/src/Tests/IndividualUiTest.php \Drupal\crm_core_contact\Tests\IndividualUiTest::testIndividualOperations()

Tests the individual operations.

User with permissions 'administer crm_core_individual entities' should be able to create/edit/delete individuals of any individual type.

File

modules/crm_core_contact/src/Tests/IndividualUiTest.php, line 55

Class

IndividualUiTest
Tests the UI for Individual CRUD operations.

Namespace

Drupal\crm_core_contact\Tests

Code

public function testIndividualOperations() {
  $this
    ->drupalGet('crm-core');
  $this
    ->assertResponse(403);
  $user = $this
    ->drupalCreateUser([
    'view any crm_core_individual entity',
  ]);
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('crm-core');
  $this
    ->assertLink('CRM Individuals');
  $this
    ->assertNoLink('CRM Activities');
  $user = $this
    ->drupalCreateUser([
    'view any crm_core_activity entity',
  ]);
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('crm-core');
  $this
    ->assertNoLink('CRM Individuals');
  $this
    ->assertLink('CRM Activities');
  $this
    ->assertNoLinkByHref('crm-core/individual/add/customer', 'User has no permission to create Customer individuals.');
  $this
    ->drupalGet('crm-core/individual/add/customer');
  $this
    ->assertResponse(403);

  // Create user and login.
  $user = $this
    ->drupalCreateUser([
    'delete any crm_core_individual entity of bundle customer',
    'create crm_core_individual entities of bundle customer',
    'view any crm_core_individual entity',
    'view any crm_core_activity entity',
  ]);
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('crm-core');
  $this
    ->assertTitle(t('CRM Core | Drupal'));
  $this
    ->assertLink(t('CRM Activities'));
  $this
    ->assertLink(t('CRM Individuals'));
  $this
    ->clickLink(t('CRM Individuals'));

  // There should be no individuals available after fresh installation and
  // there is a link to create new individuals.
  $this
    ->assertText(t('There are no individuals available.'), 'No individuals available after fresh installation.');
  $this
    ->assertLink(t('Add an individual'));
  $this
    ->drupalGet('crm-core/individual/add');
  $this
    ->assertUrl('crm-core/individual/add/customer');

  // Create individual customer.
  $user = $this
    ->drupalCreateUser([
    'delete any crm_core_individual entity of bundle customer',
    'create crm_core_individual entities',
    'edit any crm_core_individual entity',
    'administer individual types',
    'view any crm_core_individual entity',
  ]);
  $this
    ->drupalLogin($user);
  $customer_node = [
    'name[0][title]' => 'Mr.',
    'name[0][given]' => 'John',
    'name[0][middle]' => 'Emanuel',
    'name[0][family]' => 'Smith',
    'name[0][generational]' => 'IV',
    'name[0][credentials]' => '',
  ];
  $this
    ->drupalPostForm('crm-core/individual/add/customer', $customer_node, 'Save Customer');

  // Assert we were redirected back to the list of individuals.
  $this
    ->assertUrl('crm-core/individual');
  $this
    ->assertLink('John Smith', 0, 'Newly created individual title listed.');
  $this
    ->assertText(t('Customer'), 'Newly created individual type listed.');

  // Assert all view headers are available.
  $this
    ->assertLink('Name');
  $this
    ->assertLink('Individual Type');
  $this
    ->assertLink('Updated');
  $this
    ->assertText('Operations links');
  $count = $this
    ->xpath('//form[@class="views-exposed-form"]/div/div/label[text()="Name (given)"]');
  $this
    ->assertTrue($count, 1, 'Name given is an exposed filter.');
  $count = $this
    ->xpath('//form[@class="views-exposed-form"]/div/div/label[text()="Name (family)"]');
  $this
    ->assertTrue($count, 1, 'Name given is an exposed filter.');
  $count = $this
    ->xpath('//form[@class="views-exposed-form"]/div/div/label[text()="Type"]');
  $this
    ->assertTrue($count, 1, 'Contact type is an exposed filter.');
  $individuals = \Drupal::entityTypeManager()
    ->getStorage('crm_core_individual')
    ->loadByProperties([
    'name__given' => 'John',
    'name__family' => 'Smith',
  ]);
  $individual = current($individuals);
  $this
    ->assertLinkByHref('crm-core/individual/' . $individual
    ->id());
  $this
    ->assertRaw('crm-core/individual/' . $individual
    ->id() . '/edit', 'Edit link is available.');
  $this
    ->assertRaw('crm-core/individual/' . $individual
    ->id() . '/delete', 'Delete link is available.');
  $this
    ->assertText($this->container
    ->get('date.formatter')
    ->format($individual
    ->get('changed')->value, 'medium'), 'Individual updated date is available.');
  $this
    ->drupalGet('crm-core/individual/1/edit');
  $this
    ->assertRaw('crm-core/individual/1/delete" class="button button--danger" data-drupal-selector="edit-delete" id="edit-delete"', 'Delete link is available.');
  $this
    ->assertRaw('nav class="tabs" role="navigation" aria-label="Tabs"');
  $individual
    ->save();

  // Get test view data page.
  $this
    ->drupalGet('individual-view-data');
  $this
    ->assertText('Mr. John Emanuel Smith IV');

  // Edit customer individual.
  $customer_node = [
    'name[0][title]' => 'Mr.',
    'name[0][given]' => 'Maynard',
    'name[0][middle]' => 'James',
    'name[0][family]' => 'Keenan',
    'name[0][generational]' => 'I',
    'name[0][credentials]' => 'MJK',
  ];
  $individuals = $this->container
    ->get('entity_type.manager')
    ->getStorage('crm_core_individual')
    ->loadByProperties([
    'name__given' => 'John',
    'name__family' => 'Smith',
  ]);
  $individual = current($individuals);
  $this
    ->drupalPostForm('crm-core/individual/' . $individual
    ->id() . '/edit', $customer_node, 'Save Customer');

  // Assert we are viewing the updated entity after update.
  $this
    ->assertUrl('crm-core/individual/' . $individual
    ->id());
  $this
    ->assertRaw('data-drupal-link-system-path="crm-core/individual/' . $individual
    ->id() . '/delete"', 'Local task "Delete" is available.');

  // Check listing page.
  $this
    ->drupalGet('crm-core/individual');
  $this
    ->assertText('Maynard Keenan', 0, 'Updated customer individual title listed.');

  // Delete individual contact.
  $this
    ->drupalPostForm('crm-core/individual/' . $individual
    ->id() . '/delete', [], t('Delete'));
  $this
    ->assertUrl('crm-core/individual');
  $this
    ->assertNoLink('Maynard Keenan', 0, 'Deleted individual customer title no more listed.');

  // Assert that there are no contacts left.
  $this
    ->assertText(t('There are no individuals available.'), 'No individuals available after deleting all of them.');

  // Create a individual with no label.

  /** @var \Drupal\crm_core_contact\ContactInterface $individual */
  $individual = Individual::create([
    'type' => 'customer',
  ]);
  $individual
    ->save();

  // Create another user.
  $new_user = $this
    ->drupalCreateUser();

  // Test EntityOwnerTrait functions on contact.
  $this
    ->assertEqual($individual
    ->getOwnerId(), $user
    ->id());
  $this
    ->assertEqual($individual
    ->getOwner()
    ->id(), $user
    ->id());
  $individual
    ->setOwner($new_user);
  $this
    ->assertEqual($individual
    ->getOwnerId(), $new_user
    ->id());
  $this
    ->assertEqual($individual
    ->getOwner()
    ->id(), $new_user
    ->id());
  $individual
    ->setOwnerId($user
    ->id());
  $this
    ->assertEqual($individual
    ->getOwnerId(), $user
    ->id());
  $this
    ->assertEqual($individual
    ->getOwner()
    ->id(), $user
    ->id());

  // Go to overview page and assert there is a default label displayed.
  $this
    ->drupalGet('crm-core/individual');
  $this
    ->assertLink('Nameless #' . $individual
    ->id());
  $this
    ->assertLinkByHref('crm-core/individual/' . $individual
    ->id());
}