You are here

public function IndividualUiTest::testIndividualOperations in CRM Core 8.3

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/tests/src/Functional/IndividualUiTest.php, line 60

Class

IndividualUiTest
Tests the UI for Individual CRUD operations.

Namespace

Drupal\Tests\crm_core_contact\Functional

Code

public function testIndividualOperations() : void {
  $this
    ->drupalGet('crm-core');
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $user = $this
    ->drupalCreateUser([
    'view any crm_core_individual entity',
  ]);
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('crm-core');
  $this
    ->assertSession()
    ->linkExists('CRM Individuals');
  $this
    ->assertSession()
    ->linkNotExists('CRM Activities');
  $user = $this
    ->drupalCreateUser([
    'view any crm_core_activity entity',
  ]);
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('crm-core');
  $this
    ->assertSession()
    ->linkNotExists('CRM Individuals');
  $this
    ->assertSession()
    ->linkExists('CRM Activities');

  // User has no permission to create Customer individuals.
  $this
    ->assertSession()
    ->linkByHrefNotExists('crm-core/individual/add/customer');
  $this
    ->drupalGet('crm-core/individual/add/customer');
  $this
    ->assertSession()
    ->statusCodeEquals(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('CRM | Drupal');
  $this
    ->assertSession()
    ->linkExists('CRM Activities');
  $this
    ->assertSession()
    ->linkExists('CRM Individuals');
  $this
    ->clickLink('CRM Individuals');

  // There should be no individuals available after fresh installation and
  // there is a link to create new individuals.
  $this
    ->assertSession()
    ->pageTextContains('There are no individuals available.');
  $this
    ->assertSession()
    ->linkExists('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
    ->assertSession()
    ->linkExists('John Smith', 0, 'Newly created individual title listed.');
  $this
    ->assertSession()
    ->pageTextContains('Customer');

  // Assert all view headers are available.
  $this
    ->assertSession()
    ->linkExists('Name');
  $this
    ->assertSession()
    ->linkExists('Individual Type');
  $this
    ->assertSession()
    ->linkExists('Updated');
  $this
    ->assertSession()
    ->pageTextContains('Operations links');
  $elements = $this
    ->xpath('//form[@class="views-exposed-form"]/div/label[text()="Name (given)"]');
  $this
    ->assertCount(1, $elements);
  $elements = $this
    ->xpath('//form[@class="views-exposed-form"]/div/label[text()="Name (family)"]');
  $this
    ->assertCount(1, $elements);
  $elements = $this
    ->xpath('//form[@class="views-exposed-form"]/div/label[text()="Type"]');
  $this
    ->assertCount(1, $elements);
  $individuals = \Drupal::entityTypeManager()
    ->getStorage('crm_core_individual')
    ->loadByProperties([
    'name__given' => 'John',
    'name__family' => 'Smith',
  ]);
  $individual = current($individuals);
  $this
    ->assertSession()
    ->linkByHrefExists('crm-core/individual/' . $individual
    ->id());

  // Edit link is available.
  $this
    ->assertRaw('crm-core/individual/' . $individual
    ->id() . '/edit');

  // Delete link is available'.
  $this
    ->assertRaw('crm-core/individual/' . $individual
    ->id() . '/delete');

  // Individual updated date is available.
  $this
    ->assertSession()
    ->pageTextContains($this->container
    ->get('date.formatter')
    ->format($individual
    ->get('changed')->value, 'medium'));
  $this
    ->drupalGet('crm-core/individual/1/edit');

  // Delete link is available.
  $this
    ->assertRaw('crm-core/individual/1/delete" class="button button--danger" data-drupal-selector="edit-delete" id="edit-delete"');
  $individual
    ->save();

  // Get test view data page.
  $this
    ->drupalGet('individual-view-data');
  $this
    ->assertSession()
    ->pageTextContains('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());

  // Local task "Delete" is available.
  $this
    ->assertRaw('data-drupal-link-system-path="crm-core/individual/' . $individual
    ->id() . '/delete"');

  // Check listing page.
  $this
    ->drupalGet('crm-core/individual');

  // Updated customer individual title listed.
  $this
    ->assertSession()
    ->pageTextContains('Maynard Keenan');

  // Delete individual contact.
  $this
    ->drupalPostForm('crm-core/individual/' . $individual
    ->id() . '/delete', [], 'Delete');
  $this
    ->assertUrl('crm-core/individual');

  // Deleted individual customer title no more listed.
  $this
    ->assertSession()
    ->linkNotExists('Maynard Keenan');

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

  // 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());

  // Test whether owner is loaded from database correctly.
  // No need to save individual here, since nothing has changed.
  $id = $individual
    ->id();
  $individual = Individual::load($id);
  $this
    ->assertEqual($individual
    ->getOwnerId(), $user
    ->id());
  $this
    ->assertEqual($individual
    ->getOwner()
    ->id(), $user
    ->id());

  // And then make sure the result is correct even with owner being someone
  // else than the user who was logged in while creating entity.
  $individual
    ->setOwnerId($new_user
    ->id());
  $individual
    ->save();
  $id = $individual
    ->id();
  $individual = Individual::load($id);
  $this
    ->assertEqual($individual
    ->getOwnerId(), $new_user
    ->id());
  $this
    ->assertEqual($individual
    ->getOwner()
    ->id(), $new_user
    ->id());

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