You are here

public function EntityReferenceRevisionsOrphanRemovalForBaseFieldDefinitionTest::insertNonRevisionableData in Entity Reference Revisions 8

Inserts non revisionable entities needed for testing.

Overrides EntityReferenceRevisionsOrphanRemovalTest::insertNonRevisionableData

File

tests/src/Functional/EntityReferenceRevisionsOrphanRemovalForBaseFieldDefinitionTest.php, line 158

Class

EntityReferenceRevisionsOrphanRemovalForBaseFieldDefinitionTest
Tests orphan composite revisions are properly removed.

Namespace

Drupal\Tests\entity_reference_revisions\Functional

Code

public function insertNonRevisionableData() {

  // Scenario 1: A composite with a default revision that is referenced and an
  // old revision that is not. Result: Only the old revision is deleted.
  $composite_entity_first = EntityTestCompositeRelationship::create([
    'name' => 'NR first not used, second used',
    'parent_id' => 1001,
    'parent_type' => 'entity_host_relationship_test',
    'parent_field_name' => 'entity',
  ]);
  $composite_entity_first
    ->save();
  $composite_entity_first = EntityTestCompositeRelationship::load($composite_entity_first
    ->id());
  $composite_entity_first
    ->setNewRevision(TRUE);
  $composite_entity_first
    ->save();
  $entity_host = EntityTestHostRelationship::create([
    'name' => 'First NR composite',
    'entity' => $composite_entity_first,
  ]);
  $entity_host
    ->save();

  // Scenario 2: A composite with an old revision that is used and a default
  // revision that is not. Result: Nothing should be deleted.
  $composite_entity_second = EntityTestCompositeRelationship::create([
    'name' => 'NR first used, second not used',
  ]);
  $composite_entity_second
    ->save();
  $entity_host = EntityTestHostRelationship::create([
    'name' => 'Second NR composite',
    'entity' => $composite_entity_second,
  ]);
  $entity_host
    ->save();
  $composite_entity_second = EntityTestCompositeRelationship::load($composite_entity_second
    ->id());
  $composite_entity_second
    ->setNewRevision(TRUE);
  $composite_entity_second
    ->save();

  // Scenario 3: A composite with many revisions and 2 at least used. Result:
  // Delete all unused revisions.
  $composite_entity_third = EntityTestCompositeRelationship::create([
    'name' => 'NR 1st not, 2nd, 3rd not, 4th',
    'parent_id' => 1001,
    'parent_type' => 'entity_host_relationship_test',
    'parent_field_name' => 'entity',
  ]);
  $composite_entity_third
    ->save();
  $composite_entity_third = EntityTestCompositeRelationship::load($composite_entity_third
    ->id());
  $composite_entity_third
    ->setNewRevision(TRUE);
  $composite_entity_third
    ->save();
  $entity_host = EntityTestHostRelationship::create([
    'name' => 'Third NR composite',
    'entity' => $composite_entity_third,
  ]);
  $entity_host
    ->save();
  $entity_host = $this
    ->getEntityHostByName('Third NR composite');
  $entity_host
    ->set('entity', NULL);
  $entity_host
    ->save();
  $composite_entity_third = EntityTestCompositeRelationship::load($composite_entity_third
    ->id());
  $composite_entity_third
    ->setNewRevision(TRUE);
  $composite_entity_third
    ->save();
  $entity_host = $this
    ->getEntityHostByName('Third NR composite');
  $entity_host
    ->set('entity', $composite_entity_third);
  $entity_host
    ->save();
}