You are here

public function OrganizationUiTest::testOrganizationOperations in CRM Core 8

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

Tests the organization operations.

User with permissions 'administer crm_core_organization entities' should be able to create/edit/delete organizations of any organization type.

File

modules/crm_core_contact/src/Tests/OrganizationUiTest.php, line 61

Class

OrganizationUiTest
Tests the UI for Organization CRUD operations.

Namespace

Drupal\crm_core_contact\Tests

Code

public function testOrganizationOperations() {

  // Create user and login.
  $user = $this
    ->drupalCreateUser([
    'administer crm_core_organization entities',
    'administer organization types',
    'create crm_core_organization entities of bundle supplier',
    'view any crm_core_organization entity',
    'view any crm_core_activity entity',
  ]);
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('crm-core');
  $this
    ->assertLink(t('CRM Organizations'));
  $this
    ->clickLink(t('CRM Organizations'));

  // There should be no organizations available after fresh installation and
  // there is a link to create new organizations.
  $this
    ->assertText(t('There are no organizations available.'), 'No organizations available after fresh installation.');
  $this
    ->assertLink(t('Add an organization'));
  $household_node = [
    'name[0][value]' => 'Fam. Johnson',
  ];
  $this
    ->drupalPostForm('crm-core/organization/add/household', $household_node, 'Save Household');

  // Assert we were redirected back to the list of contacts.
  $this
    ->assertUrl('crm-core/organization');
  $this
    ->assertText('Fam. Johnson', 0, 'Newly created organization title listed.');
  $this
    ->assertText(t('Household'), 'Newly created organization type listed.');
  $household_node = [
    'name[0][value]' => 'Fam. Bane',
  ];
  $households = \Drupal::entityTypeManager()
    ->getStorage('crm_core_organization')
    ->loadByProperties([
    'name' => 'Fam. Johnson',
  ]);
  $household = current($households);
  $this
    ->drupalPostForm('crm-core/organization/' . $household
    ->id() . '/edit', $household_node, 'Save Household');

  // Assert we are viewing the entity.
  $this
    ->assertUrl('crm-core/organization/' . $household
    ->id());
  $this
    ->assertText('Fam. Bane', 0, 'Organization updated.');

  // Assert organization template has been used.
  $this
    ->assertRaw('Fam. Bane</div>');

  // Check listing page.
  $this
    ->drupalGet('crm-core/organization');
  $this
    ->assertText('Fam. Bane', 0, 'Updated organization title listed.');

  // Create Supplier organization.
  $supplier_node = [
    'name[0][value]' => 'Example ltd',
  ];
  $this
    ->drupalPostForm('crm-core/organization/add/supplier', $supplier_node, 'Save Supplier');

  // Create supplier with no name.
  $this
    ->drupalPostForm('crm-core/organization/add/supplier', [], 'Save Supplier');

  // Assert we were redirected back to the list of organizations.
  $this
    ->assertUrl('crm-core/organization');
  $this
    ->assertLink('Example ltd', 0, 'Newly created organization title listed.');
  $this
    ->assertLink('Nameless #3', 0, 'Nameless organization title listed.');
  $this
    ->assertText(t('Supplier'), 'Newly created organization type listed.');

  // Assert all view headers are available.
  $this
    ->assertLink('Name');
  $this
    ->assertLink('Organization type');
  $this
    ->assertLink('Updated');
  $this
    ->assertText('Operations');
  $count = $this
    ->xpath('//form[@class="views-exposed-form"]/div/div/label[text()="Type"]');
  $this
    ->assertTrue($count, 1, 'Individual type is an exposed filter.');
  $count = $this
    ->xpath('//form[@class="views-exposed-form"]/div/div/label[text()="Name"]');
  $this
    ->assertTrue($count, 1, 'Name is an exposed filter.');
  $organizations = \Drupal::entityTypeManager()
    ->getStorage('crm_core_organization')
    ->loadByProperties([
    'name' => 'Example ltd',
  ]);
  $organization = current($organizations);

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

  // Test EntityOwnerTrait functions on organization.
  $this
    ->assertEqual($organization
    ->getOwnerId(), $user
    ->id());
  $this
    ->assertEqual($organization
    ->getOwner()
    ->id(), $user
    ->id());
  $organization
    ->setOwner($new_user);
  $this
    ->assertEqual($organization
    ->getOwnerId(), $new_user
    ->id());
  $this
    ->assertEqual($organization
    ->getOwner()
    ->id(), $new_user
    ->id());
  $organization
    ->setOwnerId($user
    ->id());
  $this
    ->assertEqual($organization
    ->getOwnerId(), $user
    ->id());
  $this
    ->assertEqual($organization
    ->getOwner()
    ->id(), $user
    ->id());
  $this
    ->assertRaw('crm-core/organization/' . $organization
    ->id() . '/edit', 'Edit link is available.');
  $this
    ->assertRaw('crm-core/organization/' . $organization
    ->id() . '/delete', 'Delete link is available.');
  $this
    ->assertText($this->container
    ->get('date.formatter')
    ->format($organization
    ->get('changed')->value, 'short'), 'Organization updated date is available.');

  // Edit operation.
  $supplier_node = [
    'name[0][value]' => 'Another Example ltd',
  ];
  $this
    ->drupalPostForm('crm-core/organization/' . $organization
    ->id() . '/edit', $supplier_node, 'Save Supplier');

  // Assert we are viewing the entity.
  $this
    ->assertUrl('crm-core/organization/' . $organization
    ->id());
  $this
    ->assertText('Another Example ltd', 0, 'Organization updated.');
  $this
    ->drupalGet('crm-core/organization/1/edit');
  $this
    ->assertRaw('data-drupal-link-system-path="crm-core/organization/1/delete"', 'Local task "Delete" is available.');
  $this
    ->assertRaw('crm-core/organization/1/delete" class="button button--danger" data-drupal-selector="edit-delete" id="edit-delete"', 'Delete link is available.');

  // Check listing page.
  $this
    ->drupalGet('crm-core/organization');
  $this
    ->assertLink('Another Example ltd', 0, 'Updated organization title listed.');

  // Delete organizations.
  $this
    ->drupalPostForm('crm-core/organization/1/delete', [], t('Delete'));
  $this
    ->drupalPostForm('crm-core/organization/2/delete', [], t('Delete'));
  $this
    ->drupalPostForm('crm-core/organization/3/delete', [], t('Delete'));
  $this
    ->assertUrl('crm-core/organization');
  $this
    ->assertNoLink('Another Example ltd', 0, 'Deleted organization title no more listed.');

  // Assert that there are no organizations.
  $this
    ->assertText(t('There are no organizations available.'), 'No organizations available.');
}