View source
<?php
namespace Drupal\crm_core_activity\Tests;
use Drupal\crm_core_activity\Entity\ActivityType;
use Drupal\crm_core_contact\Entity\Individual;
use Drupal\crm_core_contact\Entity\IndividualType;
use Drupal\simpletest\WebTestBase;
class ActivityUiTest extends WebTestBase {
public static $modules = [
'crm_core_contact',
'crm_core_activity',
'crm_core_tests',
'block',
'entity',
'views_ui',
'datetime',
'options',
];
public function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('local_actions_block');
$this
->drupalPlaceBlock('local_tasks_block');
IndividualType::create([
'name' => 'Customer',
'type' => 'customer',
'description' => 'A single customer.',
'primary_fields' => [],
])
->save();
ActivityType::create([
'type' => 'meeting',
'name' => 'Meeting',
'description' => 'A meeting between 2 or more contacts.',
])
->save();
ActivityType::create([
'type' => 'phone_call',
'name' => 'Phone call',
'description' => 'A phone call between 2 or more contacts.',
])
->save();
$this
->drupalPlaceBlock('system_breadcrumb_block');
}
public function testActivityOperations() {
$user = $this
->drupalCreateUser([
'administer crm_core_individual entities',
'view any crm_core_individual entity',
'administer crm_core_activity entities',
'administer activity types',
'view any crm_core_activity entity',
]);
$this
->drupalLogin($user);
$individual = Individual::create([
'name' => [
'given' => 'John',
'family' => 'Smith',
],
'type' => 'customer',
]);
$individual
->save();
$this
->drupalGet('crm-core/activity');
$this
->assertText(t('There are no activities available.'), 'No activities available.');
$this
->assertLink(t('Add an activity'));
$this
->drupalGet('crm-core/activity/add');
$this
->assertLink(t('Meeting'));
$this
->assertLink(t('Phone call'));
$this
->drupalGet('crm-core/activity/add/meeting');
$this
->assertText(t('Format: @date', [
'@date' => date('Y-m-d'),
]));
$this
->assertText('Entity type');
$meeting_activity = [
'title[0][value]' => 'Pellentesque',
'activity_date[0][value][date]' => $this
->randomDate(),
'activity_date[0][value][time]' => $this
->randomTime(),
'activity_notes[0][value]' => $this
->randomString(),
'activity_participants[0][target_type]' => $individual
->getEntityTypeId(),
'activity_participants[0][target_id]' => $individual
->label() . ' (' . $individual
->id() . ')',
];
$this
->assertLink(t('Home'));
$this
->assertLink(t('CRM Core'));
$this
->assertLink(t('Activities'));
$this
->drupalPostForm(NULL, $meeting_activity, 'Save Activity');
$this
->assertText('Activity Pellentesque created.', 'No errors after adding new activity.');
$activities = \Drupal::entityTypeManager()
->getStorage('crm_core_activity')
->loadByProperties([
'title' => 'Pellentesque',
]);
$meeting_activity = current($activities);
$new_user = $this
->drupalCreateUser();
$this
->assertEqual($meeting_activity
->getOwnerId(), $user
->id());
$this
->assertEqual($meeting_activity
->getOwner()
->id(), $user
->id());
$meeting_activity
->setOwner($new_user);
$this
->assertEqual($meeting_activity
->getOwnerId(), $new_user
->id());
$this
->assertEqual($meeting_activity
->getOwner()
->id(), $new_user
->id());
$meeting_activity
->setOwnerId($user
->id());
$this
->assertEqual($meeting_activity
->getOwnerId(), $user
->id());
$this
->assertEqual($meeting_activity
->getOwner()
->id(), $user
->id());
$this
->assertTrue($meeting_activity
->hasParticipant($individual), t('Meeting activity has participant @name.', [
'@name' => $individual
->label(),
]));
$new_individual = Individual::create([
'name' => [
'given' => 'John',
'family' => 'Doe',
],
'type' => 'customer',
]);
$new_individual
->save();
$this
->assertFalse($meeting_activity
->hasParticipant($new_individual), t('Meeting activity does not have participant @name.', [
'@name' => $new_individual
->label(),
]));
$phonecall_activity = [
'title[0][value]' => 'Mollis',
'activity_date[0][value][date]' => $this
->randomDate(),
'activity_date[0][value][time]' => $this
->randomTime(),
'activity_notes[0][value]' => $this
->randomString(),
'activity_participants[0][target_type]' => $individual
->getEntityTypeId(),
'activity_participants[0][target_id]' => $individual
->label() . ' (' . $individual
->id() . ')',
];
$this
->drupalPostForm('crm-core/activity/add/phone_call', $phonecall_activity, 'Save Activity');
$this
->assertText('Activity Mollis created.', 'No errors after adding new activity.');
$meeting_activity = [
'title[0][value]' => 'Vestibulum',
'activity_notes[0][value]' => 'Pellentesque egestas neque sit',
];
$this
->drupalPostForm('crm-core/activity/1/edit', $meeting_activity, 'Save Activity');
$this
->assertText('Vestibulum', 'Activity updated.');
$this
->drupalGet('crm-core/activity');
$this
->assertLink('Vestibulum', 0, 'Updated activity listed properly.');
$this
->assertLink(t('Activity Date'));
$this
->assertLink(t('Title'));
$this
->assertLink(t('Activity Type'));
$this
->assertText(t('Operations'));
$this
->assertText(t('Activity preview'));
$count = $this
->xpath('//form[@class="views-exposed-form"]/div/div/label[text()="Title"]');
$this
->assertTrue($count, 1, 'Title is an exposed filter.');
$count = $this
->xpath('//form[@class="views-exposed-form"]/div/div/label[text()="Type"]');
$this
->assertTrue($count, 1, 'Activity type is an exposed filter.');
$activities = \Drupal::entityTypeManager()
->getStorage('crm_core_activity')
->loadByProperties([
'title' => 'Vestibulum',
]);
$activity = current($activities);
$this
->assertRaw('crm-core/activity/' . $activity
->id() . '/edit', 'Edit link is available.');
$this
->assertRaw('crm-core/activity/' . $activity
->id() . '/delete', 'Delete link is available.');
$date = $activity
->get('activity_date')->date;
$this->container
->get('date.formatter')
->format($date
->getTimeStamp(), 'medium');
$this
->assertText($this->container
->get('date.formatter')
->format($date
->getTimeStamp(), 'medium'), 'Activity date is available.');
$this
->drupalGet('activity-view-data');
$this
->assertText('Vestibulum');
$this
->assertText('Pellentesque egestas neque sit');
$empty_participant = [
'activity_participants[0][target_id]' => '',
];
$this
->drupalPostForm('crm-core/activity/1/edit', $empty_participant, 'Save Activity');
$this
->assertText('Label field is required.', 'Empty activity participant not allowed.');
$phonecall_activity = [
'title[0][value]' => 'Commodo',
];
$this
->drupalPostForm('crm-core/activity/2/edit', $phonecall_activity, 'Save Activity');
$this
->assertText('Commodo', 'Activity updated.');
$this
->drupalGet('crm-core/activity');
$this
->assertLink('Commodo', 0, 'Updated activity listed properly.');
$this
->drupalPostForm('crm-core/activity/1/delete', [], 'Delete');
$this
->assertText('Meeting Vestibulum has been deleted.', 'No errors after deleting activity.');
$this
->drupalGet('crm-core/activity');
$this
->assertNoLink('Vestibulum', 'Deleted activity is no more listed.');
$this
->drupalPostForm('crm-core/activity/2/delete', [], 'Delete');
$this
->assertText('Phone call Commodo has been deleted.', 'No errors after deleting activity.');
$this
->drupalGet('crm-core/activity');
$this
->assertNoLink('Commodo', 'Deleted activity is no more listed.');
$this
->drupalGet('crm-core/activity');
$this
->assertText(t('There are no activities available.'), 'No activities listed.');
$this
->drupalGet('admin/structure/crm-core/activity-types');
$this
->clickLink('Add activity type');
$new_activity_type = [
'name' => 'New activity type',
'type' => 'new_activity_type',
'description' => 'New activity type description',
];
$this
->drupalPostForm(NULL, $new_activity_type, 'Save activity type');
$this
->drupalGet('admin/structure/crm-core/activity-types');
$this
->assertText($new_activity_type['name']);
$this
->clickLink('Edit', 1);
$edit = [
'name' => 'Edited activity type',
];
$this
->drupalPostForm(NULL, $edit, 'Save activity type');
$this
->drupalGet('admin/structure/crm-core/activity-types');
$this
->assertText($edit['name']);
$this
->drupalGet('admin/structure/crm-core/activity-types');
$this
->clickLink('Delete');
$this
->drupalPostForm(NULL, [], 'Delete');
$this
->assertText(t('The crm core activity type @type has been deleted.', [
'@type' => $edit['name'],
]));
$this
->drupalGet('admin/structure/crm-core/activity-types');
$this
->assertNoText($edit['name']);
}
public function testActivityRevisions() {
$user = $this
->drupalCreateUser([
'administer crm_core_activity entities',
'view all crm_core_activity revisions',
]);
$this
->drupalLogin($user);
$customer = Individual::create([
'name' => [
'given' => 'John',
'family' => 'Smith',
],
'type' => 'customer',
]);
$customer
->save();
$meeting_activity = [
'title[0][value]' => 'Pellentesque',
'activity_date[0][value][date]' => $this
->randomDate(),
'activity_date[0][value][time]' => $this
->randomTime(),
'activity_notes[0][value]' => $this
->randomString(),
'activity_participants[0][target_id]' => $customer
->label() . ' (' . $customer
->id() . ')',
];
$this
->drupalPostForm('crm-core/activity/add/meeting', $meeting_activity, 'Save Activity');
$meeting_activity_1 = [
'title[0][value]' => 'Pellentesque rev1',
];
$this
->drupalPostForm('crm-core/activity/1/edit', $meeting_activity_1, 'Save Activity');
$meeting_activity_2 = [
'title[0][value]' => 'Pellentesque rev2',
];
$this
->drupalPostForm('crm-core/activity/1/edit', $meeting_activity_2, 'Save Activity');
$this
->drupalGet('crm-core/activity/1/revisions');
$this
->assertLinkByHref('crm-core/activity/1');
$this
->assertLinkByHref('crm-core/activity/1/revisions/2/view');
$this
->drupalGet('crm-core/activity/1/revisions/2/view');
$this
->assertText('Pellentesque rev2');
}
public function testListBuilder() {
$user = $this
->drupalCreateUser([
'view any crm_core_activity entity',
'view any crm_core_activity entity',
'administer views',
]);
$this
->drupalLogin($user);
$this
->drupalGet('admin/structure/views/view/crm_core_activity_overview/delete');
$this
->drupalPostForm(NULL, [], TRUE);
$this
->drupalGet('/crm-core/activity');
$this
->assertResponse(200);
}
protected function randomDate() {
return \Drupal::service('date.formatter')
->format(REQUEST_TIME + rand(0, 100000), 'custom', 'Y-m-d');
}
protected function randomTime() {
return \Drupal::service('date.formatter')
->format(REQUEST_TIME + rand(0, 100000), 'custom', 'H:m:s');
}
}