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\RelationType;
use Drupal\relation\Entity\Relation;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
class IndividualCRUDTest extends KernelTestBase {
public static $modules = array(
'field',
'text',
'user',
'crm_core',
'crm_core_contact',
'crm_core_activity',
'dynamic_entity_reference',
'datetime',
'relation',
'name',
'options',
);
protected function setUp() {
parent::setUp();
$this
->installConfig(array(
'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(array(
'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
->assertTrue(!empty($uuid), 'Loaded individual type has uuid.');
$individual_type_labels = IndividualType::getNames();
$this
->assertTrue($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
->assertTrue(!empty($uuid), 'Loaded individual has uuid.');
$activity_type = ActivityType::create(array(
'type' => 'activity_test',
));
$activity_type
->save();
$activity = Activity::create(array(
'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
->assertTrue(!empty($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',
],
'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();
$organization = Organization::create([
'type' => $organization_type
->id(),
]);
$organization
->save();
$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();
$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();
$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,
]);
$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);
}
}