View source
<?php
namespace Drupal\crm_core_contact\Tests;
use Drupal\crm_core_contact\Entity\Individual;
use Drupal\crm_core_contact\Entity\IndividualType;
use Drupal\simpletest\WebTestBase;
class IndividualUiTest extends WebTestBase {
public static $modules = [
'crm_core_contact',
'crm_core_activity',
'crm_core_tests',
'block',
'views_ui',
'options',
'datetime',
];
public function setUp() {
parent::setUp();
IndividualType::create([
'name' => 'Customer',
'type' => 'customer',
'description' => 'A single customer.',
'primary_fields' => [],
])
->save();
$this
->drupalPlaceBlock('local_actions_block');
$this
->drupalPlaceBlock('local_tasks_block');
}
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);
$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'));
$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');
$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');
$this
->assertUrl('crm-core/individual');
$this
->assertLink('John Smith', 0, 'Newly created individual title listed.');
$this
->assertText(t('Customer'), 'Newly created individual type listed.');
$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');
$this
->drupalGet('individual-view-data');
$this
->assertText('Mr. John Emanuel Smith IV');
$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');
$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.');
$this
->drupalGet('crm-core/individual');
$this
->assertText('Maynard Keenan', 0, 'Updated customer individual title listed.');
$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.');
$this
->assertText(t('There are no individuals available.'), 'No individuals available after deleting all of them.');
$individual = Individual::create([
'type' => 'customer',
]);
$individual
->save();
$new_user = $this
->drupalCreateUser();
$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());
$this
->drupalGet('crm-core/individual');
$this
->assertLink('Nameless #' . $individual
->id());
$this
->assertLinkByHref('crm-core/individual/' . $individual
->id());
}
public function testIndividualTypeOperations() {
$user = $this
->drupalCreateUser([
'administer individual types',
]);
$this
->drupalLogin($user);
$this
->drupalGet('admin/structure/crm-core/individual-types');
$this
->assertIndividualTypeLink('customer', 'Edit link for customer.');
$this
->assertIndividualTypeLink('customer/delete', 'Delete link for customer.');
Individual::create([
'type' => 'customer',
])
->save();
$this
->drupalGet('admin/structure/crm-core/individual-types');
$this
->assertNoIndividualTypeLink('customer/delete', 'No delete link for individual.');
$this
->drupalGet('admin/structure/crm-core/individual-types/customer/delete');
$this
->assertResponse(403);
$this
->drupalGet('admin/structure/crm-core/individual-types/customer');
$this
->assertResponse(200);
$this
->assertRaw(t('Save individual type'), 'Save individual type button is present.');
$this
->assertNoIndividualTypeLink('customer/delete', 'No delete link on individual type form.');
}
public function testFieldsUi() {
$user = $this
->drupalCreateUser([
'administer crm_core_individual display',
'administer crm_core_individual form display',
'administer crm_core_individual fields',
]);
$this
->drupalLogin($user);
$this
->drupalGet('admin/structure/crm-core/individual-types/customer/fields');
$this
->assertText(t('Manage fields'), 'Manage fields local task in available.');
$this
->assertText(t('Manage form display'), 'Manage form display local task in available.');
$this
->assertText(t('Manage display'), 'Manage display local task in available.');
$this
->drupalGet('admin/structure/crm-core/individual-types/customer/form-display');
$this
->assertText(t('Name'), 'Name field is available on form display.');
$this
->drupalGet('admin/structure/crm-core/individual-types/customer/display');
$this
->assertText(t('Name'), 'Name field is available on manage display.');
}
public function testIndividualRevisions() {
$user = $this
->drupalCreateUser([
'administer crm_core_individual entities',
'view all crm_core_individual revisions',
]);
$this
->drupalLogin($user);
$individual = [
'name[0][given]' => 'rev',
'name[0][family]' => '1',
];
$this
->drupalPostForm('crm-core/individual/add/customer', $individual, 'Save Customer');
$individual_1 = [
'name[0][family]' => '2',
];
$this
->drupalPostForm('crm-core/individual/1/edit', $individual_1, 'Save Customer');
$individual_2 = [
'name[0][family]' => '3',
];
$this
->drupalPostForm('crm-core/individual/1/edit', $individual_2, 'Save Customer');
$this
->clickLink('Revisions');
$this
->assertLinkByHref('crm-core/individual/1');
$this
->assertLinkByHref('crm-core/individual/1/revisions/1/view');
$this
->assertLinkByHref('crm-core/individual/1/revisions/2/view');
$this
->drupalGet('crm-core/individual/1/revisions/1/view');
$this
->assertText('rev 1');
$this
->drupalGet('crm-core/individual/1/revisions/2/view');
$this
->assertText('rev 2');
}
public function testListBuilder() {
$user = $this
->drupalCreateUser([
'view any crm_core_individual entity',
'view any crm_core_organization entity',
'administer views',
]);
$this
->drupalLogin($user);
$this
->drupalGet('admin/structure/views/view/crm_core_organizaiton_overview/delete');
$this
->drupalPostForm(NULL, [], TRUE);
$this
->drupalGet('/crm-core/organization');
$this
->assertResponse(200);
$this
->drupalGet('admin/structure/views/view/crm_core_individual_overview/delete');
$this
->drupalPostForm(NULL, [], TRUE);
$this
->drupalGet('/crm-core/individual');
$this
->assertResponse(200);
}
public function assertIndividualTypeLink($href, $message = '') {
$this
->assertLinkByHref('admin/structure/crm-core/individual-types/' . $href, 0, $message);
}
public function assertNoIndividualTypeLink($href, $message = '') {
$this
->assertNoLinkByHref('admin/structure/crm-core/individual-types/' . $href, $message);
}
}