You are here

public function EntityReferenceRevisionsOrphanRemovalTest::insertNonRevisionableData in Entity Reference Revisions 8

Inserts non revisionable entities needed for testing.

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

File

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

Class

EntityReferenceRevisionsOrphanRemovalTest
Tests orphan composite revisions are properly removed.

Namespace

Drupal\Tests\entity_reference_revisions\Functional

Code

public function insertNonRevisionableData() {

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

  // Add a translatable field and a not translatable field to both content
  // types.
  $field_storage = FieldStorageConfig::loadByName('node', 'field_composite_entity');
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'non_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' => 'NR first not used, second used',
    'parent_id' => 1001,
    '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' => 'non_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' => 'NR first used, second not used',
  ]);
  $composite_entity_second
    ->save();
  $node = $this
    ->drupalCreateNode([
    'type' => 'non_revisionable',
    'title' => 'Second composite',
    'field_composite_entity' => $composite_entity_second,
  ]);
  $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 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' => 'node',
    'parent_field_name' => 'field_composite_entity',
  ]);
  $composite_entity_third
    ->save();
  $composite_entity_third = EntityTestCompositeRelationship::load($composite_entity_third
    ->id());
  $composite_entity_third
    ->setNewRevision(TRUE);
  $composite_entity_third
    ->save();
  $node = $this
    ->drupalCreateNode([
    'type' => 'non_revisionable',
    'title' => 'Third composite',
    'field_composite_entity' => $composite_entity_third,
  ]);
  $node
    ->save();
  $node = $this
    ->getNodeByTitle('Third composite');
  $node
    ->set('field_composite_entity', NULL);
  $node
    ->save();
  $composite_entity_third = EntityTestCompositeRelationship::load($composite_entity_third
    ->id());
  $composite_entity_third
    ->setNewRevision(TRUE);
  $composite_entity_third
    ->save();
  $node = $this
    ->getNodeByTitle('Third composite');
  $node
    ->set('field_composite_entity', $composite_entity_third);
  $node
    ->save();
}