You are here

public function EntityReferenceRevisionsOrphanRemovalTest::insertRevisionableData in Entity Reference Revisions 8

Inserts revisionable entities needed for testing.

1 call to EntityReferenceRevisionsOrphanRemovalTest::insertRevisionableData()
EntityReferenceRevisionsOrphanRemovalTest::setUp in tests/src/Functional/EntityReferenceRevisionsOrphanRemovalTest.php
1 method overrides EntityReferenceRevisionsOrphanRemovalTest::insertRevisionableData()
EntityReferenceRevisionsOrphanRemovalForBaseFieldDefinitionTest::insertRevisionableData in tests/src/Functional/EntityReferenceRevisionsOrphanRemovalForBaseFieldDefinitionTest.php
Inserts revisionable entities needed for testing.

File

tests/src/Functional/EntityReferenceRevisionsOrphanRemovalTest.php, line 151

Class

EntityReferenceRevisionsOrphanRemovalTest
Tests orphan composite revisions are properly removed.

Namespace

Drupal\Tests\entity_reference_revisions\Functional

Code

public function insertRevisionableData() {

  /** @var \Drupal\node\NodeStorageInterface $node_storage */
  $node_storage = \Drupal::entityTypeManager()
    ->getStorage('node');
  NodeType::create([
    'type' => 'revisionable',
    'new_revision' => TRUE,
  ])
    ->save();

  // Add a translatable field and a not translatable field to both content
  // types.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_composite_entity',
    'entity_type' => 'node',
    'type' => 'entity_reference_revisions',
    'settings' => [
      'target_type' => 'entity_test_composite',
    ],
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'revisionable',
    'translatable' => FALSE,
  ]);
  $field
    ->save();

  // 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' => 'first not used, second used',
    'parent_id' => 1000,
    'parent_type' => 'node',
    'parent_field_name' => 'field_composite_entity',
  ]);
  $composite_entity_first
    ->save();
  $composite_entity_first = EntityTestCompositeRelationship::load($composite_entity_first
    ->id());
  $composite_entity_first
    ->setNewRevision(TRUE);
  $composite_entity_first
    ->save();
  $node = $this
    ->drupalCreateNode([
    'type' => 'revisionable',
    'title' => 'First composite',
    'field_composite_entity' => $composite_entity_first,
  ]);
  $node
    ->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' => 'first used, second not used',
  ]);
  $composite_entity_second
    ->save();
  $node = $this
    ->drupalCreateNode([
    'type' => 'revisionable',
    'title' => 'Second composite',
    'field_composite_entity' => $composite_entity_second,
  ]);
  $node
    ->save();
  $node = $this
    ->getNodeByTitle('Second composite');
  $node = $node_storage
    ->createRevision($node);
  $node
    ->set('field_composite_entity', NULL);
  $node
    ->save();
  $composite_entity_second = EntityTestCompositeRelationship::load($composite_entity_second
    ->id());
  $composite_entity_second
    ->setNewRevision(TRUE);
  $composite_entity_second
    ->save();

  // Scenario 3: A composite with an old revision and a default revision both
  // that are not used with empty parent fields. Result: Nothing should be
  // deleted since we do not know if it is still used.
  $composite_entity_third = EntityTestCompositeRelationship::create([
    'name' => 'first not used, second not used',
  ]);
  $composite_entity_third
    ->save();
  $composite_entity_third = EntityTestCompositeRelationship::load($composite_entity_third
    ->id());
  $composite_entity_third
    ->setNewRevision(TRUE);
  $composite_entity_third
    ->save();

  // Scenario 4: A composite with an old revision and a default revision both
  // that are not used with filled parent fields. Result: Should first delete
  // the old revision and then the default revision. Delete the entity too.
  $composite_entity_fourth = EntityTestCompositeRelationship::create([
    'name' => '1st filled not, 2nd filled not',
    'parent_id' => 1001,
    'parent_type' => 'node',
    'parent_field_name' => 'field_composite_entity',
  ]);
  $composite_entity_fourth
    ->save();
  $composite_entity_fourth = EntityTestCompositeRelationship::load($composite_entity_fourth
    ->id());
  $composite_entity_fourth
    ->setNewRevision(TRUE);
  $composite_entity_fourth
    ->set('parent_id', 1001);
  $composite_entity_fourth
    ->save();

  // Scenario 5: A composite with many revisions and 2 at least used. Result:
  // Delete all unused revisions.
  $composite_entity_fifth = EntityTestCompositeRelationship::create([
    'name' => '1st not, 2nd used, 3rd not, 4th',
    'parent_id' => 1001,
    'parent_type' => 'node',
    'parent_field_name' => 'field_composite_entity',
  ]);
  $composite_entity_fifth
    ->save();
  $composite_entity_fifth = EntityTestCompositeRelationship::load($composite_entity_fifth
    ->id());
  $composite_entity_fifth
    ->setNewRevision(TRUE);
  $composite_entity_fifth
    ->save();
  $node = $this
    ->drupalCreateNode([
    'type' => 'revisionable',
    'title' => 'Third composite',
    'field_composite_entity' => $composite_entity_fifth,
  ]);
  $node
    ->save();
  $node = $this
    ->getNodeByTitle('Third composite');
  $node = $node_storage
    ->createRevision($node);
  $node
    ->set('field_composite_entity', NULL);
  $node
    ->save();
  $composite_entity_fifth = EntityTestCompositeRelationship::load($composite_entity_fifth
    ->id());
  $composite_entity_fifth
    ->setNewRevision(TRUE);
  $composite_entity_fifth
    ->save();
  $node = $this
    ->getNodeByTitle('Third composite');
  $node = $node_storage
    ->createRevision($node);
  $node
    ->set('field_composite_entity', $composite_entity_fifth);
  $node
    ->save();

  // Scenario 6: A composite with wrong parent fields filled pointing to a non
  // existent parent (Parent 1). However, Parent 2 references it. Result: Must
  // not be deleted.
  $node = $this
    ->drupalCreateNode([
    'type' => 'revisionable',
    'title' => 'DELETED composite',
  ]);
  $node
    ->save();
  $composite_entity_sixth = EntityTestCompositeRelationship::create([
    'name' => 'wrong parent fields',
    'parent_id' => $node
      ->id(),
    'parent_type' => 'node',
    'parent_field_name' => 'field_composite_entity',
  ]);
  $composite_entity_sixth
    ->save();
  $node
    ->delete();
  $node = $this
    ->drupalCreateNode([
    'type' => 'revisionable',
    'title' => 'Fourth composite',
    'field_composite_entity' => $composite_entity_sixth,
  ]);
  $node
    ->save();
}