/** * Provides the node submission form. * * @param \Drupal\crm_core_activity\Entity\ActivityType $crm_core_activity_type * The activity type to add. * @param \Drupal\crm_core_contact\Entity\Contact $crm_core_contact * (optional) The contact the activity will be assigned. If left blank, the * Form will show a field to select a contact. * @return array * A node submission form. */ public function add(ActivityType $crm_core_activity_type, Contact $crm_core_contact = NULL) { $values = array('type' => $crm_core_activity_type->id()); if ($crm_core_contact) { $values['activity_participants'] = array(array('target_id' => $crm_core_contact->id())); } $activity = Activity::create($values); $form = $this->entityFormBuilder()->getForm($activity); return $form; }
/** * Tests the match method. */ public function testMatch() { $this->field->expects($this->any())->method('getType')->will($this->returnValue('crap')); $this->contact->expects($this->any())->method('getFieldDefinitions')->will($this->returnValue(array('foo' => $this->field))); $this->pluginManager->expects($this->any())->method('hasDefinition')->will($this->returnValue(TRUE)); $this->matchHandler->expects($this->any())->method('match')->will($this->returnValue(array('42' => array('value' => 100)))); // $this->pluginManager->expects($this->once()) // ->method('createInstance') // ->will($this->returnValue($this->matchHandler)); // $ids = $this->engine->match($this->contact); // $this->assertEquals([], $ids); }
/** * Test basic UI operations with Activities. * * Create a contact. * Add activity of every type to contact. * Assert activities listed on Activities tab listing page. * Edit every activity. Assert activities changed from listing page. * Delete every activity. Assert they disappeared from listing page. */ public function testActivityOperations() { // Create and login user. User should be able to create contacts and // activities. $user = $this->drupalCreateUser(array('administer crm_core_contact entities', 'view any crm_core_contact entity', 'administer crm_core_activity entities')); $this->drupalLogin($user); // Create Household contact. $household = Contact::create(array('name' => 'Fam. Smith', 'type' => 'household')); $household->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('Add Meeting')); $this->assertLink(t('Add Phone call')); // Create Meeting activity. Ensure it it listed. $meeting_activity = array('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]' => $household->label() . ' (' . $household->id() . ')'); $this->drupalPostForm('crm-core/activity/add/meeting', $meeting_activity, 'Save Activity'); $this->assertText('Activity Pellentesque created.', 'No errors after adding new activity.'); // Create Meeting activity. Ensure it it listed. $phonecall_activity = array('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_id]' => $household->label() . ' (' . $household->id() . ')'); $this->drupalPostForm('crm-core/activity/add/phone_call', $phonecall_activity, 'Save Activity'); $this->assertText('Activity Mollis created.', 'No errors after adding new activity.'); // Update activity and assert its title changed on the list. $meeting_activity = array('title[0][value]' => 'Vestibulum'); $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.'); // Update phone call activity and assert its title changed on the list. $phonecall_activity = array('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.'); // Delete Meeting activity. $this->drupalPostForm('crm-core/activity/1/delete', array(), '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.'); // Delete Phone call activity. $this->drupalPostForm('crm-core/activity/2/delete', array(), '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.'); // Assert there is no activities left. $this->drupalGet('crm-core/activity'); $this->assertText(t('There are no activities available.'), 'No activities listed.'); }
/** * Test the email field. */ public function testEmail() { FieldStorageConfig::create(array('entity_type' => 'crm_core_contact', 'type' => 'email', 'field_name' => 'contact_mail'))->save(); FieldConfig::create(array('field_name' => 'contact_mail', 'entity_type' => 'crm_core_contact', 'bundle' => 'individual', 'label' => t('Email'), 'required' => FALSE))->save(); $config = array('value' => array('operator' => '=', 'score' => 42)); $contact_needle = Contact::create(array('type' => 'individual')); $contact_needle->set('contact_mail', '*****@*****.**'); $contact_needle->save(); $contact_match = Contact::create(array('type' => 'individual')); $contact_match->set('contact_mail', '*****@*****.**'); $contact_match->save(); $config['field'] = $contact_needle->getFieldDefinition('contact_mail'); /* @var \Drupal\crm_core_match\Plugin\crm_core_match\field\FieldHandlerInterface $text */ $text = $this->pluginManager->createInstance('email', $config); $ids = $text->match($contact_needle); $this->assertTrue(array_key_exists($contact_match->id(), $ids), 'Text match returns expected match'); $this->assertEqual(42, $ids[$contact_match->id()]['contact_mail.value'], 'Got expected match score'); }
/** * Tests CRUD of contacts. * * @todo Check if working once https://drupal.org/node/2239969 got committed. */ public function testContact() { $this->installEntitySchema('user'); $type = ContactType::create(array('type' => 'test')); $type->primary_fields = []; $type->save(); // Create. $contact = Contact::create(array('type' => $type->type)); $this->assertEqual(SAVED_NEW, $contact->save(), 'Contact saved.'); // Load. $contact_load = Contact::load($contact->id()); $uuid = $contact_load->uuid(); $this->assertTrue(!empty($uuid), 'Loaded contact has uuid.'); // Delete. $contact->delete(); $contact_load = Contact::load($contact->id()); $this->assertNull($contact_load, 'Contact deleted.'); }
/** * Returns the label of the contact. * * @param \Drupal\crm_core_contact\Entity\Contact $entity * The Contact entity. * * @return string * Contact label. */ public static function labelCallback(Contact $entity) { // @todo Replace with the value of the contact_name field, when name module will be available. $label = $entity->get('name')->value; \Drupal::moduleHandler()->alter('crm_core_contact_label', $label, $entity); return $label; }
/** * Tests the contact type operations. * * User with permissions 'administer contact types' should be able to * create/edit/delete contact types. */ public function testContactTypeOperations() { // Given I am logged in as a user with permission 'administer contact types' $user = $this->drupalCreateUser(array('administer contact types')); $this->drupalLogin($user); // When I visit the contact type admin page. $this->drupalGet('admin/structure/crm-core/contact-types'); // Then I should see edit, enable, delete but no enable links for existing // contacts. $this->assertContactTypeLink('household', 'Edit link for household.'); $this->assertContactTypeLink('household/disable', 'Disable link for household.'); $this->assertNoContactTypeLink('household/enable', 'No enable link for household.'); $this->assertContactTypeLink('household/delete', 'Delete link for household.'); $this->assertcontacttypelink('individual', 'Edit link for individual.'); $this->assertcontacttypelink('individual/disable', 'Disable link for individual.'); $this->assertNoContacttypelink('individual/enable', 'No enable link for individual.'); $this->assertcontacttypelink('individual/delete', 'Delete link for individual.'); $this->assertcontacttypelink('organization', 'Edit link for organization.'); $this->assertcontacttypelink('organization/disable', 'Disable link for organization.'); $this->assertNoContacttypelink('organization/enable', 'No enable link for organization.'); $this->assertcontacttypelink('organization/delete', 'Delete link for organization.'); // Given the 'household' contact type is disabled. $this->drupalPostForm('admin/structure/crm-core/contact-types/household/disable', array(), 'Disable'); // When I visit the contact type admin page. $this->drupalGet('admin/structure/crm-core/contact-types'); // Then I should see an enable link. $this->assertContactTypeLink('household/enable', 'Enable link for household.'); // And I should not see a disable link. $this->assertNoContactTypeLink('household/disable', 'No disable link for household.'); // When I enable 'household' $this->drupalPostForm('admin/structure/crm-core/contact-types/household/enable', array(), 'Enable'); // Then I should see a disable link. $this->assertContactTypeLink('household/disable', 'Disable link for household.'); // Given there is a contact of type 'individual.'. Contact::create(array('type' => 'individual'))->save(); // When I visit the contact type admin page. $this->drupalGet('admin/structure/crm-core/contact-types'); // Then I should not see a delete link. $this->assertNoContactTypeLink('individual/delete', 'No delete link for individual.'); // When I edit the organization type. $this->drupalGet('admin/structure/crm-core/contact-types/organization'); // Then I should see a delete link. $this->assertContactTypeLink('organization/delete', 'Delete link on organization type form.'); // When I edit the individual type. $this->drupalGet('admin/structure/crm-core/contact-types/individual'); // @todo Assert for a positive fact to ensure being on the correct page. // Then I should not see a delete link. $this->assertNoContactTypeLink('individual/delete', 'No delete link on individual type form.'); }
/** * {@inheritdoc} * * @todo This does not scale. * * Deleting a contact type with thousands of contact records associated will * run into execution timeout. */ public static function preDelete(EntityStorageInterface $storage, array $entities) { parent::preDelete($storage, $entities); $ids = array_map(function (EntityInterface $entity) { return $entity->id(); }, $entities); // Delete all instances of the given type. $results = \Drupal::entityQuery('crm_core_contact')->condition('type', $ids, 'IN')->execute(); if (!empty($results)) { $contacts = Contact::loadMultiple($results); \Drupal::entityManager()->getStorage('crm_core_contact')->delete($contacts); watchdog('crm_core_contact', 'Delete !count contacts due to deletion of contact type.', array('!count' => count($results)), WATCHDOG_INFO); } }