public function IndividualUiTest::testIndividualOperations in CRM Core 8.2
Same name and namespace in other branches
- 8 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\TestsCode
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]' => '',
'sex' => 'male',
'email[type]' => 'private',
'email[value]' => 'john.smith@admin.com',
'birth_date[0][value][year]' => 2000,
'birth_date[0][value][month]' => 1,
'birth_date[0][value][day]' => 2,
];
$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
->assertText('Sex');
$this
->assertText('Birth date');
$this
->assertText('Email');
$this
->assertRaw('john.smith@admin.com', t('Email of individual found.'));
$this
->assertOptionSelected('edit-email-type', 'private', t('Email type of individual selected.'));
$this
->assertOptionSelected('edit-sex', 'male', t('Sex for individual selected.'));
$this
->assertOptionSelected('edit-birth-date-0-value-year', '2000', t('Year of birth date for individual selected.'));
$this
->assertOptionSelected('edit-birth-date-0-value-month', '1', t('Month of birth date for individual selected.'));
$this
->assertOptionSelected('edit-birth-date-0-value-day', '2', t('Day of birth date for individual selected.'));
$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
->setSex('unknown');
$individual
->addEmail([
'value' => 'unk@admin.com',
'type' => 'private',
]);
$individual
->addEmail([
'value' => 'unk@administrator.com',
'type' => 'private',
]);
$individual
->setMainEmail([
'value' => 'unknown@admin.com',
'type' => 'corporate',
]);
$individual
->setBirthDate('2011-02-07');
$individual
->save();
$unchanged = \Drupal::entityTypeManager()
->getStorage('crm_core_individual')
->loadUnchanged($individual
->id());
$this
->assertEqual($unchanged
->getSex(), 'unknown');
$this
->assertEqual($unchanged
->getMainEmail()['value'], 'unknown@admin.com');
$this
->assertEqual($unchanged
->getMainEmail()['type'], 'corporate');
$this
->assertEqual($unchanged
->getEmailList()[1]['value'], 'unk@admin.com');
$this
->assertEqual($unchanged
->getEmailList()[1]['type'], 'private');
$this
->assertEqual($unchanged
->getEmailList()[2]['value'], 'unk@administrator.com');
$this
->assertEqual($unchanged
->getEmailList()[2]['type'], 'private');
$this
->assertEqual($unchanged
->getBirthDate(), '2011-02-07');
// 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());
}