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\Organization;
use Drupal\crm_core_contact\Entity\OrganizationType;
use Drupal\KernelTests\KernelTestBase;
class OrganizationCRUDTest extends KernelTestBase {
public static $modules = [
'field',
'text',
'user',
'crm_core',
'crm_core_contact',
'crm_core_activity',
'dynamic_entity_reference',
'datetime',
'options',
];
protected function setUp() {
parent::setUp();
$this
->installConfig([
'field',
]);
$this
->installEntitySchema('crm_core_organization');
$this
->installEntitySchema('crm_core_activity');
}
public function testOrganizationType() {
$id = 'new_organization_type';
$organization_type = OrganizationType::create([
'id' => $id,
'label' => $this
->randomMachineName(),
'description' => $this
->randomString(),
'primary_fields' => [],
]);
$organization_type_id = $organization_type
->id();
$this
->assertSame($organization_type_id, $id, "New organization type {$id} exists.");
$this
->assertEquals(SAVED_NEW, $organization_type
->save(), 'Organization type saved.');
$organization_type_load = OrganizationType::load($id);
$this
->assertEquals($organization_type
->id(), $organization_type_load
->id(), 'Loaded organization type has same id.');
$this
->assertEquals($organization_type
->label(), $organization_type_load
->label(), 'Loaded organization type has same label.');
$this
->assertEquals($organization_type
->getDescription(), $organization_type_load
->getDescription(), 'Loaded organization type has same description.');
$uuid = $organization_type_load
->uuid();
$this
->assertNotEmpty($uuid, 'Loaded organization type has uuid.');
$organization_type_labels = OrganizationType::getNames();
$this
->assertSame($organization_type
->label(), $organization_type_labels[$organization_type
->id()]);
$organization_type_load
->delete();
$organization_type_load = OrganizationType::load($id);
$this
->assertNull($organization_type_load, 'Organization type deleted.');
}
public function testOrganization() {
$this
->installEntitySchema('user');
$type = OrganizationType::create([
'id' => 'test',
'primary_fields' => [],
]);
$type
->save();
$organization = Organization::create([
'type' => $type
->id(),
]);
$this
->assertEquals(SAVED_NEW, $organization
->save(), 'Organization saved.');
$organization_one = Organization::create([
'type' => $type
->id(),
]);
$this
->assertEquals(SAVED_NEW, $organization_one
->save(), 'Organization saved.');
$organization_load = Organization::load($organization
->id());
$uuid = $organization_load
->uuid();
$this
->assertNotEmpty($uuid, 'Loaded organization has uuid.');
$activity_type = ActivityType::create([
'type' => 'activity_test',
]);
$activity_type
->save();
$activity = Activity::create([
'type' => $activity_type->type,
]);
$activity
->get('activity_participants')
->appendItem($organization);
$activity
->get('activity_participants')
->appendItem($organization_one);
$this
->assertEquals(SAVED_NEW, $activity
->save(), 'Activity saved.');
$activity_load = Activity::load($activity
->id());
$this
->assertNotEmpty($activity_load
->uuid(), 'Loaded activity has uuid.');
$organization
->delete();
$organization_load = Organization::load($organization
->id());
$this
->assertNull($organization_load, 'Organization deleted.');
$activity_load = Activity::load($activity
->id());
$this
->assertNotNull($activity_load, 'Activity not deleted.');
$organization_one
->delete();
$organization_load = Organization::load($organization_one
->id());
$this
->assertNull($organization_load, 'Organization deleted.');
$activity_load = Activity::load($activity
->id());
$this
->assertNull($activity_load, 'Activity deleted.');
}
}