You are here

public function IndividualCRUDTest::testActionPlugins in CRM Core 8.3

Same name and namespace in other branches
  1. 8 modules/crm_core_contact/tests/src/Kernel/IndividualCRUDTest.php \Drupal\Tests\crm_core_contact\Kernel\IndividualCRUDTest::testActionPlugins()
  2. 8.2 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 180

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',
    ],
  ]);
  $individual_customer_1
    ->save();
  $individual_customer_2 = Individual::create([
    'type' => 'customer',
    'name' => [
      'given' => 'Mark',
      'family' => 'Jones',
    ],
  ]);
  $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();

  // 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();
  $this
    ->markTestIncomplete('Incomplete port follows.');

  // 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_customer_1,
    $individual_customer_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_customer_1
    ->id());
  $this
    ->assertEquals($relations[1]->endpoints[1]->entity_id, $household_contact
    ->id());
  $this
    ->assertEquals($relations[2]->endpoints[0]->entity_id, $individual_customer_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 = [
    'data' => [
      'contact_id' => $individual_customer_1
        ->id(),
      'name' => [
        $individual_customer_3
          ->id() => $individual_customer_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_customer_3
        ->id(),
    ],
  ];
  $relation = Relation::create([
    '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_customer_1,
    $individual_customer_3,
  ]);

  // Test that there is no individual_contact_3.
  $individual_customer_3 = Individual::load($individual_customer_3
    ->id());
  $this
    ->assertNull($individual_customer_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_customer_2
    ->id());
  $this
    ->assertEquals($meeting_activity->activity_participants[1]->target_id, $individual_customer_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_customer_1
    ->id());
  $this
    ->assertEquals($relations[5]->endpoints[1]->entity_id, $household_contact
    ->id());
}