View source
<?php
namespace Drupal\Tests\crm_core_contact\Kernel;
use Drupal\crm_core_activity\Entity\Activity;
use Drupal\crm_core_activity\Entity\ActivityType;
use Drupal\crm_core_contact\Entity\Individual;
use Drupal\crm_core_contact\Entity\IndividualType;
use Drupal\crm_core_contact\Entity\Organization;
use Drupal\crm_core_contact\Entity\OrganizationType;
use Drupal\relation\Entity\Relation;
use Drupal\relation\Entity\RelationType;
use Drupal\KernelTests\KernelTestBase;
class IndividualCRUDTest extends KernelTestBase {
public static $modules = [
'field',
'text',
'user',
'crm_core',
'crm_core_contact',
'crm_core_activity',
'dynamic_entity_reference',
'datetime',
'relation',
'relation_endpoint',
'name',
'options',
];
protected function setUp() {
parent::setUp();
$this
->installConfig([
'field',
]);
$this
->installEntitySchema('crm_core_individual');
$this
->installEntitySchema('crm_core_organization');
$this
->installEntitySchema('crm_core_activity');
$this
->installEntitySchema('relation');
$this->pluginManager = $this->container
->get('plugin.manager.action');
}
public function testIndividualType() {
$type = 'dog';
$individual_type = IndividualType::create([
'type' => $type,
]);
$this
->assertTrue(isset($individual_type->type) && $individual_type->type == $type, 'New individual type exists.');
$individual_type->name = $this
->randomMachineName();
$individual_type->description = $this
->randomString();
$individual_type->primary_fields = [];
$this
->assertEquals(SAVED_NEW, $individual_type
->save(), 'Individual type saved.');
$individual_type_load = IndividualType::load($type);
$this
->assertEquals($individual_type->type, $individual_type_load->type, 'Loaded individual type has same type.');
$this
->assertEquals($individual_type->name, $individual_type_load->name, 'Loaded individual type has same name.');
$this
->assertEquals($individual_type->description, $individual_type_load->description, 'Loaded individual type has same description.');
$uuid = $individual_type_load
->uuid();
$this
->assertNotEmpty($uuid, 'Loaded individual type has uuid.');
$individual_type_labels = IndividualType::getNames();
$this
->assertSame($individual_type->name, $individual_type_labels[$individual_type->type]);
$individual_type_load
->delete();
$individual_type_load = IndividualType::load($type);
$this
->assertNull($individual_type_load, 'Individual type deleted.');
}
public function testIndividual() {
$this
->installEntitySchema('user');
$type = IndividualType::create([
'type' => 'test',
]);
$type->primary_fields = [];
$type
->save();
$individual = Individual::create([
'type' => $type->type,
]);
$this
->assertEquals(SAVED_NEW, $individual
->save(), 'Individual saved.');
$individual_one = Individual::create([
'type' => $type->type,
]);
$this
->assertEquals(SAVED_NEW, $individual_one
->save(), 'Individual saved.');
$this
->assertEquals('Nameless #' . $individual_one
->id(), $individual_one
->label());
$individual_one->name->given = 'First';
$individual_one->name->family = 'Last';
$individual_one
->save();
$this
->assertEquals('First Last', $individual_one
->label());
$individual_load = Individual::load($individual
->id());
$uuid = $individual_load
->uuid();
$this
->assertNotEmpty($uuid, 'Loaded individual has uuid.');
$activity_type = ActivityType::create([
'type' => 'activity_test',
]);
$activity_type
->save();
$activity = Activity::create([
'type' => $activity_type->type,
]);
$activity
->get('activity_participants')
->appendItem($individual);
$activity
->get('activity_participants')
->appendItem($individual_one);
$this
->assertEquals(SAVED_NEW, $activity
->save(), 'Activity saved.');
$activity_load = Activity::load($activity
->id());
$this
->assertNotEmpty($activity_load
->uuid(), 'Loaded activity has uuid.');
$individual
->delete();
$individual_load = Individual::load($individual
->id());
$this
->assertNull($individual_load, 'Individual deleted.');
$activity_load = Activity::load($activity
->id());
$this
->assertNotNull($activity_load, 'Activity not deleted.');
$individual_one
->delete();
$individual_load = Individual::load($individual_one
->id());
$this
->assertNull($individual_load, 'Contact deleted.');
$activity_load = Activity::load($activity
->id());
$this
->assertNull($activity_load, 'Activity deleted.');
}
public function testActionPlugins() {
$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();
$seller_individual = Individual::create([
'type' => 'seller',
'name' => [
'title' => 'Will',
'family' => 'Smith',
],
]);
$seller_individual
->save();
$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();
$organization = Organization::create([
'type' => $organization_type
->id(),
]);
$organization
->save();
$relation_type = RelationType::create([
'id' => 'crm_member',
'source_bundles' => [
'crm_core_individual:*',
'crm_core_organization:*',
],
'target_bundles' => [
'crm_core_individual:seller',
],
]);
$relation_type
->save();
$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.');
$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();
$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());
$data = [
'data' => [
'contact_id' => $individual_customer_1
->id(),
'name' => [
$individual_customer_3
->id() => $individual_customer_3
->get('name'),
],
],
];
$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,
]);
$individual_customer_3 = Individual::load($individual_customer_3
->id());
$this
->assertNull($individual_customer_3);
$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());
$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());
}
}