View source
<?php
namespace Drupal\Tests\crm_core_contact\Functional;
use Drupal\crm_core_contact\Entity\Organization;
use Drupal\crm_core_contact\Entity\OrganizationType;
use Drupal\Tests\BrowserTestBase;
class OrganizationUiTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'crm_core_contact',
'crm_core_activity',
'crm_core_tests',
'block',
'datetime',
'options',
];
public function setUp() {
parent::setUp();
OrganizationType::create([
'label' => 'Supplier',
'id' => 'supplier',
'description' => 'A person or company that supplies goods or services.',
'primary_fields' => [],
])
->save();
OrganizationType::create([
'label' => 'Household',
'id' => 'household',
'description' => 'A collection of individuals generally located at the same residence.',
'primary_fields' => [],
])
->save();
$this
->drupalPlaceBlock('local_actions_block');
$this
->drupalPlaceBlock('local_tasks_block');
}
public function testOrganizationOperations() {
$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
->assertSession()
->linkExists('CRM Organizations');
$this
->clickLink('CRM Organizations');
$this
->assertText('There are no organizations available.');
$this
->assertSession()
->linkExists('Add an organization');
$household_values = [
'name[0][value]' => 'Fam. Johnson',
];
$this
->drupalPostForm('crm-core/organization/add/household', $household_values, 'Save Household');
$this
->assertUrl('crm-core/organization');
$this
->assertSession()
->pageTextContains('Fam. Johnson');
$this
->assertSession()
->pageTextContains('Household');
$households = \Drupal::entityTypeManager()
->getStorage('crm_core_organization')
->loadByProperties([
'name' => 'Fam. Johnson',
]);
$household = current($households);
$household_values = [
'name[0][value]' => 'Fam. Bane',
];
$this
->drupalPostForm('crm-core/organization/' . $household
->id() . '/edit', $household_values, 'Save Household');
$this
->assertUrl('crm-core/organization/' . $household
->id());
$this
->assertSession()
->pageTextContains('Fam. Bane');
$this
->drupalGet('crm-core/organization');
$this
->assertSession()
->pageTextContains('Fam. Bane');
$supplier_values = [
'name[0][value]' => 'Example ltd',
];
$this
->drupalPostForm('crm-core/organization/add/supplier', $supplier_values, 'Save Supplier');
$this
->drupalPostForm('crm-core/organization/add/supplier', [], 'Save Supplier');
$this
->assertUrl('crm-core/organization');
$this
->assertSession()
->linkExists('Example ltd');
$this
->assertSession()
->linkExists('Example ltd', 0, 'Newly created organization title listed.');
$this
->assertSession()
->linkExists('Nameless #3', 0, 'Nameless organization title listed.');
$this
->assertSession()
->pageTextContains('Supplier');
$this
->assertSession()
->linkExists('Name');
$this
->assertSession()
->linkExists('Organization type');
$this
->assertSession()
->linkExists('Updated');
$this
->assertSession()
->pageTextContains('Operations');
$labels = $this
->xpath('//form[@class="views-exposed-form"]/div/label[text()="Type"]');
$this
->assertCount(1, $labels, 'Organization type is an exposed filter.');
$labels = $this
->xpath('//form[@class="views-exposed-form"]/div/label[text()="Name"]');
$this
->assertCount(1, $labels, 'Name is an exposed filter.');
$organizations = \Drupal::entityTypeManager()
->getStorage('crm_core_organization')
->loadByProperties([
'name' => 'Example ltd',
]);
$organization = current($organizations);
$new_user = $this
->drupalCreateUser();
$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());
$organizations = \Drupal::entityTypeManager()
->getStorage('crm_core_organization')
->loadByProperties([
'name' => 'Example ltd',
]);
$organization = current($organizations);
$this
->assertEqual($organization
->getOwnerId(), $user
->id());
$this
->assertEqual($organization
->getOwner()
->id(), $user
->id());
$organization
->setOwnerId($new_user
->id());
$organization
->save();
$organizations = \Drupal::entityTypeManager()
->getStorage('crm_core_organization')
->loadByProperties([
'name' => 'Example ltd',
]);
$organization = current($organizations);
$this
->drupalGet('crm-core/organization');
$this
->assertEqual($organization
->getOwnerId(), $new_user
->id());
$this
->assertEqual($organization
->getOwner()
->id(), $new_user
->id());
$this
->assertSession()
->responseContains('crm-core/organization/' . $organization
->id() . '/edit');
$this
->assertSession()
->responseContains('crm-core/organization/' . $organization
->id() . '/delete');
$this
->assertSession()
->pageTextContains($this->container
->get('date.formatter')
->format($organization
->get('changed')->value, 'short'));
$supplier_values = [
'name[0][value]' => 'Another Example ltd',
];
$this
->drupalPostForm('crm-core/organization/' . $organization
->id() . '/edit', $supplier_values, 'Save Supplier');
$this
->assertUrl('crm-core/organization/' . $organization
->id());
$this
->assertSession()
->pageTextContains('Another Example ltd');
$this
->drupalGet('crm-core/organization/1/edit');
$this
->assertSession()
->responseContains('data-drupal-link-system-path="crm-core/organization/1/delete"');
$this
->assertSession()
->responseContains('crm-core/organization/1/delete" class="button button--danger" data-drupal-selector="edit-delete" id="edit-delete"');
$this
->drupalGet('crm-core/organization');
$this
->assertSession()
->linkExists('Another Example ltd', 0, 'Updated organization title listed.');
$this
->drupalPostForm('crm-core/organization/1/delete', [], 'Delete');
$this
->drupalPostForm('crm-core/organization/2/delete', [], 'Delete');
$this
->drupalPostForm('crm-core/organization/3/delete', [], 'Delete');
$this
->assertUrl('crm-core/organization');
$this
->assertSession()
->linkNotExists('Another Example ltd', 'Deleted organization title no more listed.');
$this
->assertSession()
->pageTextContains('There are no organizations available.');
}
public function testOrganizationTypeOperations() {
$user = $this
->drupalCreateUser([
'administer organization types',
]);
$this
->drupalLogin($user);
$this
->drupalGet('admin/structure/crm-core/organization-types');
$this
->assertOrganizationTypeLink('supplier', 'Edit link for supplier.');
$this
->assertOrganizationTypeLink('supplier/delete', 'Delete link for supplier.');
$this
->assertOrganizationTypeLink('household', 'Edit link for household.');
$this
->assertOrganizationTypeLink('household/delete', 'Delete link for household.');
$second_organization_type = OrganizationType::create([
'id' => 'new_organization_type',
'label' => 'New organization type',
'primary_fields' => [],
]);
$second_organization_type
->save();
$this
->drupalGet('admin/structure/crm-core/organization-types');
Organization::create([
'type' => 'supplier',
])
->save();
$this
->drupalGet('admin/structure/crm-core/organization-types');
$this
->assertNoOrganizationTypeLink('supplier/delete', 'No delete link for supplier.');
$this
->drupalGet('admin/structure/crm-core/organization-types/supplier/delete');
$this
->assertResponse(403);
$this
->drupalGet('admin/structure/crm-core/organization-types/supplier');
$this
->assertNoOrganizationTypeLink('supplier/delete', 'No delete link on supplier type form.');
}
public function testFieldsUi() {
$user = $this
->drupalCreateUser([
'administer crm_core_organization display',
'administer crm_core_organization form display',
'administer crm_core_organization fields',
'administer organization types',
]);
$this
->drupalLogin($user);
$this
->drupalGet('admin/structure/crm-core/organization-types');
$this
->assertSession()
->linkExists('Edit');
$this
->assertSession()
->linkExists('Manage fields');
$this
->assertSession()
->linkExists('Manage form display');
$this
->assertSession()
->linkExists('Manage display');
$this
->drupalGet('admin/structure/crm-core/organization-types/supplier');
$this
->assertSession()
->linkExists('Edit');
$this
->assertSession()
->linkExists('Manage fields');
$this
->assertSession()
->linkExists('Manage form display');
$this
->assertSession()
->linkExists('Manage display');
$this
->drupalGet('admin/structure/crm-core/organization-types/supplier/fields');
$this
->assertSession()
->linkExists('Edit');
$this
->assertSession()
->linkExists('Manage fields');
$this
->assertSession()
->linkExists('Manage form display');
$this
->assertSession()
->linkExists('Manage display');
$this
->drupalGet('admin/structure/crm-core/organization-types/supplier/form-display');
$this
->assertSession()
->pageTextContains('Name');
$this
->drupalGet('admin/structure/crm-core/organization-types/supplier/display');
$this
->assertSession()
->pageTextContains('Name');
}
public function testOrganizationRevisions() {
$user = $this
->drupalCreateUser([
'administer crm_core_organization entities',
'view all crm_core_organization revisions',
]);
$this
->drupalLogin($user);
$organization = [
'name[0][value]' => 'rev',
];
$this
->drupalPostForm('crm-core/organization/add/supplier', $organization, 'Save Supplier');
$organization_1 = [
'name[0][value]' => 'rev1',
];
$this
->drupalPostForm('crm-core/organization/1/edit', $organization_1, 'Save Supplier');
$organization_2 = [
'name[0][value]' => 'rev2',
];
$this
->drupalPostForm('crm-core/organization/1/edit', $organization_2, 'Save Supplier');
$this
->clickLink('Revisions');
$this
->assertLinkByHref('crm-core/organization/1');
$this
->assertLinkByHref('crm-core/organization/1/revisions/1/view');
$this
->assertLinkByHref('crm-core/organization/1/revisions/2/view');
$this
->drupalGet('crm-core/organization/1/revisions/1/view');
$this
->assertSession()
->pageTextContains('rev');
$this
->drupalGet('crm-core/organization/1/revisions/2/view');
$this
->assertSession()
->pageTextContains('rev1');
}
public function assertOrganizationTypeLink($href, $message = '') {
$this
->assertSession()
->linkByHrefExists('admin/structure/crm-core/organization-types/' . $href, 0, $message);
}
public function assertNoOrganizationTypeLink($href, $message = '') {
$this
->assertSession()
->linkByHrefNotExists('admin/structure/crm-core/organization-types/' . $href, $message);
}
}