View source
<?php
namespace Drupal\Tests\crm_core_activity\Functional;
use Drupal\crm_core_activity\Entity\ActivityType;
use Drupal\crm_core_contact\Entity\Individual;
use Drupal\crm_core_contact\Entity\IndividualType;
use Drupal\Tests\BrowserTestBase;
class ActivityUiTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
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
->assertSession()
->pageTextContains('There are no activities available.');
$this
->assertSession()
->linkExists('Add an activity');
$this
->drupalGet('crm-core/activity/add');
$this
->assertSession()
->linkExists('Meeting');
$this
->assertSession()
->linkExists('Phone call');
$this
->drupalGet('crm-core/activity/add/meeting');
$this
->assertSession()
->pageTextContains('Format: ' . date('Y-m-d'));
$this
->assertSession()
->pageTextContains('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
->assertSession()
->linkExists('Home');
$this
->assertSession()
->linkExists('CRM');
$this
->assertSession()
->linkExists('Activities');
$this
->drupalPostForm(NULL, $meeting_activity, 'Save Activity');
$this
->assertSession()
->pageTextContains('Activity Pellentesque created.');
$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), 'Meeting activity has participant ' . $individual
->label());
$new_individual = Individual::create([
'name' => [
'given' => 'John',
'family' => 'Doe',
],
'type' => 'customer',
]);
$new_individual
->save();
$this
->assertFalse($meeting_activity
->hasParticipant($new_individual), 'Meeting activity does not have ' . $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
->assertSession()
->pageTextContains('Activity Mollis created.');
$phonecall_activity_db = \Drupal::entityTypeManager()
->getStorage('crm_core_activity')
->loadByProperties([
'title' => 'Mollis',
]);
$phonecall_activity_db = reset($phonecall_activity_db);
$this
->assertTrue($phonecall_activity_db
->hasParticipant($individual));
$this
->drupalGet('crm-core/activity/' . $phonecall_activity_db
->id() . '/edit');
$meeting_activity = [
'title[0][value]' => 'Vestibulum',
'activity_notes[0][value]' => 'Pellentesque egestas neque sit',
];
$this
->drupalPostForm(NULL, $meeting_activity, 'Save Activity');
$this
->assertSession()
->pageTextContains('Vestibulum');
$this
->drupalGet('crm-core/activity');
$this
->assertSession()
->linkExists('Vestibulum');
$this
->assertSession()
->linkExists('Activity Date');
$this
->assertSession()
->linkExists('Title');
$this
->assertSession()
->linkExists('Activity Type');
$this
->assertSession()
->pageTextContains('Operations');
$this
->assertSession()
->pageTextContains('Activity preview');
$elements = $this
->xpath('//form[@class="views-exposed-form"]/div/label[text()="Title"]');
$this
->assertCount(1, $elements);
$elements = $this
->xpath('//form[@class="views-exposed-form"]/div/label[text()="Type"]');
$this
->assertCount(1, $elements);
$activities = \Drupal::entityTypeManager()
->getStorage('crm_core_activity')
->loadByProperties([
'title' => 'Vestibulum',
]);
$activity = current($activities);
$this
->assertRaw('crm-core/activity/' . $activity
->id() . '/edit');
$this
->assertRaw('crm-core/activity/' . $activity
->id() . '/delete');
$date = $activity
->get('activity_date')->date;
$this->container
->get('date.formatter')
->format($date
->getTimeStamp(), 'medium');
$this
->assertSession()
->pageTextContains($this->container
->get('date.formatter')
->format($date
->getTimeStamp(), 'medium'));
$this
->drupalGet('activity-view-data');
$this
->assertSession()
->pageTextContains('Vestibulum');
$this
->assertSession()
->pageTextContains('Pellentesque egestas neque sit');
$empty_participant = [
'activity_participants[0][target_id]' => '',
];
$this
->drupalPostForm('crm-core/activity/1/edit', $empty_participant, 'Save Activity');
$this
->assertSession()
->pageTextContains('Label field is required.');
$phonecall_activity = [
'title[0][value]' => 'Commodo',
];
$this
->drupalPostForm('crm-core/activity/2/edit', $phonecall_activity, 'Save Activity');
$this
->assertSession()
->pageTextContains('Commodo');
$this
->drupalGet('crm-core/activity');
$this
->assertSession()
->linkExists('Commodo', 0);
$this
->drupalPostForm('crm-core/activity/1/delete', [], 'Delete');
$this
->assertSession()
->pageTextContains('Meeting Pellentesque has been deleted.');
$this
->drupalGet('crm-core/activity');
$this
->assertSession()
->linkNotExists('Pellentesque');
$this
->drupalPostForm('crm-core/activity/2/delete', [], 'Delete');
$this
->assertSession()
->pageTextContains('Phone call Commodo has been deleted.');
$this
->drupalGet('crm-core/activity');
$this
->assertSession()
->linkNotExists('Commodo');
$this
->drupalGet('crm-core/activity');
$this
->assertSession()
->pageTextContains('There are no activities available.');
$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
->assertSession()
->pageTextContains($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
->assertSession()
->pageTextContains($edit['name']);
$this
->drupalGet('admin/structure/crm-core/activity-types');
$this
->clickLink('Delete');
$this
->drupalPostForm(NULL, [], 'Delete');
$this
->assertSession()
->pageTextContains('The activity type ' . $edit['name'] . ' has been deleted.');
$this
->drupalGet('admin/structure/crm-core/activity-types');
$this
->assertSession()
->pageTextNotContains($edit['name']);
}
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, [], 'Delete');
$this
->drupalGet('/crm-core/activity');
$this
->assertSession()
->statusCodeEquals(200);
}
protected function randomDate() {
$request_time = \Drupal::time()
->getRequestTime();
return \Drupal::service('date.formatter')
->format($request_time + rand(0, 100000), 'custom', 'Y-m-d');
}
protected function randomTime() {
$request_time = \Drupal::time()
->getRequestTime();
return \Drupal::service('date.formatter')
->format($request_time + rand(0, 100000), 'custom', 'H:m:s');
}
}