You are here

public function IndividualCRUDTest::testIndividual 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::testIndividual()
  2. 8 modules/crm_core_contact/tests/src/Kernel/IndividualCRUDTest.php \Drupal\Tests\crm_core_contact\Kernel\IndividualCRUDTest::testIndividual()

Tests CRUD of individuals.

@todo Check if working once https://drupal.org/node/2239969 got committed.

File

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

Class

IndividualCRUDTest
Tests CRUD operations for the CRM Core Individual entity.

Namespace

Drupal\Tests\crm_core_contact\Kernel

Code

public function testIndividual() {
  $this
    ->installEntitySchema('user');
  $type = IndividualType::create([
    'type' => 'test',
  ]);
  $type->primary_fields = [];
  $type
    ->save();

  // Create.
  $individual = Individual::create([
    'type' => $type->type,
  ]);
  $this
    ->assertEquals(SAVED_NEW, $individual
    ->save(), 'Individual saved.');

  // Create second individual.
  $individual_one = Individual::create([
    'type' => $type->type,
  ]);
  $this
    ->assertEquals(SAVED_NEW, $individual_one
    ->save(), 'Individual saved.');

  // Assert default labels.
  $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());

  // Load.
  $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();

  // Create activity and add participants individual.
  $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.');

  // Load activity.
  $activity_load = Activity::load($activity
    ->id());
  $this
    ->assertTrue(!empty($activity_load
    ->uuid()), 'Loaded activity has uuid.');

  // Delete first individual, activity should'n be deleted
  // because it's related to second individual.
  $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.');

  // Delete second individual and now activity should be deleted too.
  $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.');
}