You are here

public function IndividualCRUDTest::testActionPlugins in CRM Core 8.2

Same name and namespace in other branches
  1. 8.3 modules/crm_core_contact/tests/src/Kernel/IndividualCRUDTest.php \Drupal\Tests\crm_core_contact\Kernel\IndividualCRUDTest::testActionPlugins()
  2. 8 modules/crm_core_contact/tests/src/Kernel/IndividualCRUDTest.php \Drupal\Tests\crm_core_contact\Kernel\IndividualCRUDTest::testActionPlugins()

Tests action plugins.

File

modules/crm_core_contact/tests/src/Kernel/IndividualCRUDTest.php, line 156

Class

IndividualCRUDTest
Tests CRUD operations for the CRM Core Individual entity.

Namespace

Drupal\Tests\crm_core_contact\Kernel

Code

public function testActionPlugins() {

  // Create individual types.
  $individual_type_1 = IndividualType::create([
    'type' => 'seller',
  ]);
  $individual_type_1->primary_fields = [];
  $individual_type_1
    ->save();
  $individual_type_2 = IndividualType::create([
    'type' => 'customer',
  ]);
  $individual_type_2->primary_fields = [];
  $individual_type_2
    ->save();
  $organization_type = OrganizationType::create([
    'id' => 'supplier',
    'label' => $this
      ->randomMachineName(),
    'description' => $this
      ->randomString(),
    'primary_fields' => [],
  ]);
  $organization_type
    ->save();

  // Create seller individual.
  $seller_individual = Individual::create([
    'type' => 'seller',
    'name' => [
      'title' => 'Will',
      'family' => 'Smith',
    ],
  ]);
  $seller_individual
    ->save();

  // Create 3 individual customers.
  $individual_customer_1 = Individual::create([
    'type' => 'customer',
    'name' => [
      'given' => 'John',
      'family' => 'Smith',
    ],
    'email_value' => 'test1@example.com',
    'email_type' => 'private',
  ]);
  $individual_customer_1
    ->save();
  $individual_customer_2 = Individual::create([
    'type' => 'customer',
    'name' => [
      'given' => 'Mark',
      'family' => 'Jones',
    ],
    'email_value' => 'test2@example.com',
    'email_type' => 'private',
  ]);
  $individual_customer_2
    ->save();
  $individual_customer_3 = Individual::create([
    'type' => 'customer',
    'name' => [
      'given' => 'Joan',
      'family' => 'Johnson',
    ],
  ]);
  $individual_customer_3
    ->save();

  // Create one organization.
  $organization = Organization::create([
    'type' => $organization_type
      ->id(),
  ]);
  $organization
    ->save();

  // Add primary email to individual type.
  $individual_type_id = $individual_customer_1->type->target_id;
  $individual_type = IndividualType::load($individual_type_id);
  $individual_type->primary_fields['email'] = $individual_customer_1
    ->get('email')
    ->getName();
  $individual_type
    ->save();

  // Create crm_member relation type.
  $relation_type = RelationType::create([
    'id' => 'crm_member',
    'source_bundles' => [
      'crm_core_individual:*',
      'crm_core_organization:*',
    ],
    'target_bundles' => [
      'crm_core_individual:seller',
    ],
  ]);
  $relation_type
    ->save();

  // Create meeting activity.
  $meeting_activity = Activity::create([
    'type' => 'meeting',
    'title' => $this
      ->randomString(),
    'activity_participants' => [
      $individual_customer_2,
      $individual_customer_3,
    ],
  ]);
  $meeting_activity
    ->save();

  // Test send_email_action.
  $send_email_action_plugin = $this->pluginManager
    ->createInstance('send_email_action', [
    'subject' => 'Testing subject',
    'message' => 'Testing message',
  ]);
  $send_email_action_plugin
    ->executeMultiple([
    $individual_customer_1,
    $individual_customer_2,
  ]);

  // Get all emails sent during this test and test it's values.
  $captured_emails = $this->container
    ->get('state')
    ->get('system.test_mail_collector', []);
  $this
    ->assertEquals($captured_emails[0]['to'], $individual_customer_1->email->value);
  $this
    ->assertEquals($captured_emails[1]['to'], $individual_customer_2->email->value);

  // Test join_into_household_action.
  // @todo there is no more household bundle after we rename contact to individual.

  //$join_into_household_action_plugin = $this->pluginManager->createInstance('join_into_household_action', ['household' => $household_contact]);

  //$join_into_household_action_plugin->executeMultiple([$individual_contact_1, $individual_contact_2, $organization]);

  //$relations = Relation::loadMultiple();

  // Test that there are two new relations with correct endpoints and types.

  //$this->assertEquals(count($relations), 3, 'Three new relations were created during this test.');

  //$this->assertEquals($relations[1]->relation_type->target_id, 'crm_member');

  //$this->assertEquals($relations[2]->relation_type->target_id, 'crm_member');

  //$this->assertEquals($relations[3]->relation_type->target_id, 'crm_member');

  //$this->assertEquals($relations[1]->endpoints[0]->entity_id, $individual_contact_1->id());

  //$this->assertEquals($relations[1]->endpoints[1]->entity_id, $household_contact->id());

  //$this->assertEquals($relations[2]->endpoints[0]->entity_id, $individual_contact_2->id());

  //$this->assertEquals($relations[2]->endpoints[1]->entity_id, $household_contact->id());

  //$this->assertEquals($relations[3]->endpoints[0]->entity_id, $organization->id());

  //$this->assertEquals($relations[3]->endpoints[1]->entity_id, $household_contact->id());

  // Test merge_contacts_action.
  // @todo contacts are now individuals

  //$data = array(

  //  'data' => array(
  //    'contact_id' => $individual_contact_1->id(),
  //    'name' => array($individual_contact_3->id() => $individual_contact_3->get('name')),
  //  ),

  //);

  // Create relation between two individuals.

  //$endpoints = [

  //  [
  //    'entity_type' => 'crm_core_individual',
  //    'entity_id' => $household_contact->id(),
  //  ],
  //  [
  //    'entity_type' => 'crm_core_individual',
  //    'entity_id' => $individual_contact_3->id(),
  //  ],

  //];

  //$relation = Relation::create(array('relation_type' => 'crm_member'));

  //$relation->endpoints = $endpoints;

  //$relation->save();

  //$merge_contacts_action_plugin = $this->pluginManager->createInstance('merge_contacts_action', $data);

  //$merge_contacts_action_plugin->executeMultiple([$individual_contact_1, $individual_contact_3]);

  // Test that there is no individual_contact_3.

  //$individual_contact_3 = Individual::load($individual_contact_3->id());

  //$this->assertNull($individual_contact_3);

  // Test that values are updated in meeting_activity.

  //$meeting_activity = Activity::load($meeting_activity->id());

  //$this->assertEquals($meeting_activity->activity_participants[0]->target_id, $individual_contact_2->id());

  //$this->assertEquals($meeting_activity->activity_participants[1]->target_id, $individual_contact_1->id());

  // Test that relation has been created with correct values.

  //$relations = Relation::loadMultiple();

  //$this->assertEquals($relations[5]->relation_type->target_id, 'crm_member');

  //$this->assertEquals($relations[5]->endpoints[0]->entity_id, $individual_contact_1->id());

  //$this->assertEquals($relations[5]->endpoints[1]->entity_id, $household_contact->id());
}