View source
<?php
namespace Drupal\Tests\entity_reference_revisions\Functional;
use Drupal\entity_composite_relationship_test\Entity\EntityTestCompositeRelationship;
use Drupal\entity_host_relationship_test\Entity\EntityTestHostRelationship;
class EntityReferenceRevisionsOrphanRemovalForBaseFieldDefinitionTest extends EntityReferenceRevisionsOrphanRemovalTest {
protected $adminUser;
public static $modules = [
'node',
'field',
'entity_reference_revisions',
'entity_composite_relationship_test',
'entity_host_relationship_test',
];
public function insertRevisionableData() {
$entity_host_storage = \Drupal::entityTypeManager()
->getStorage('entity_host_relationship_test');
$composite_entity_first = EntityTestCompositeRelationship::create([
'name' => 'first not used, second used',
'parent_id' => 1000,
'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 composite',
'entity' => $composite_entity_first,
]);
$entity_host
->save();
$composite_entity_second = EntityTestCompositeRelationship::create([
'name' => 'first used, second not used',
]);
$composite_entity_second
->save();
$entity_host = EntityTestHostRelationship::create([
'name' => 'Second composite',
'entity' => $composite_entity_second,
]);
$entity_host
->save();
$entity_host = $this
->getEntityHostByName('Second composite');
$entity_host = $entity_host_storage
->createRevision($entity_host);
$entity_host
->set('entity', NULL);
$entity_host
->save();
$composite_entity_second = EntityTestCompositeRelationship::load($composite_entity_second
->id());
$composite_entity_second
->setNewRevision(TRUE);
$composite_entity_second
->save();
$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();
$composite_entity_fourth = EntityTestCompositeRelationship::create([
'name' => '1st filled not, 2nd filled not',
'parent_id' => 1001,
'parent_type' => 'entity_host_relationship_test',
'parent_field_name' => '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();
$composite_entity_fifth = EntityTestCompositeRelationship::create([
'name' => '1st not, 2nd used, 3rd not, 4th',
'parent_id' => 1001,
'parent_type' => 'entity_host_relationship_test',
'parent_field_name' => 'entity',
]);
$composite_entity_fifth
->save();
$composite_entity_fifth = EntityTestCompositeRelationship::load($composite_entity_fifth
->id());
$composite_entity_fifth
->setNewRevision(TRUE);
$composite_entity_fifth
->save();
$entity_host = EntityTestHostRelationship::create([
'name' => 'Third composite',
'entity' => $composite_entity_fifth,
]);
$entity_host
->save();
$entity_host = $this
->getEntityHostByName('Second composite');
$entity_host = $entity_host_storage
->createRevision($entity_host);
$entity_host
->set('entity', NULL);
$entity_host
->save();
$composite_entity_fifth = EntityTestCompositeRelationship::load($composite_entity_fifth
->id());
$composite_entity_fifth
->setNewRevision(TRUE);
$composite_entity_fifth
->save();
$entity_host = $this
->getEntityHostByName('Third composite');
$entity_host = $entity_host_storage
->createRevision($entity_host);
$entity_host
->set('entity', $composite_entity_fifth);
$entity_host
->save();
$entity_host = EntityTestHostRelationship::create([
'name' => 'DELETED composite',
]);
$entity_host
->save();
$composite_entity_sixth = EntityTestCompositeRelationship::create([
'name' => 'wrong parent fields',
'parent_id' => $entity_host
->id(),
'parent_type' => 'entity_host_relationship_test',
'parent_field_name' => 'entity',
]);
$composite_entity_sixth
->save();
$entity_host
->delete();
$entity_host = EntityTestHostRelationship::create([
'name' => 'Fourth composite',
'entity' => $composite_entity_sixth,
]);
$entity_host
->save();
}
public function insertNonRevisionableData() {
$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();
$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();
$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();
}
protected function getEntityHostByName($name, $reset = FALSE) {
if ($reset) {
\Drupal::entityTypeManager()
->getStorage('entity_host_relationship_test')
->resetCache();
}
$name = (string) $name;
$entities = \Drupal::entityTypeManager()
->getStorage('entity_host_relationship_test')
->loadByProperties([
'name' => $name,
]);
return reset($entities);
}
}