You are here

public function SimpleMergeTest::testsimpleMergeResolver in Conflict 8

Creates an entity and it's revisions then performs a simple algorithm to find common parent of two revisions.

File

tests/src/Kernel/SimpleMergeTest.php, line 34

Class

SimpleMergeTest
@group conflict

Namespace

Drupal\Tests\conflict\Kernel

Code

public function testsimpleMergeResolver() {

  // Creates a new entity
  $entity = EntityTestRev::create([
    'name' => 'revision 1',
  ]);
  $entity
    ->save();

  // Updates it multiple times to create new revisions.
  $entity
    ->setName('revision 2');
  $entity
    ->setNewRevision();
  $entity
    ->save();
  $entity
    ->setName('revision 3');
  $entity
    ->setNewRevision();
  $entity
    ->save();
  $entity
    ->setName('revision 4');
  $entity
    ->setNewRevision();
  $entity
    ->save();
  $entity
    ->setName('revision 5');
  $entity
    ->setNewRevision();
  $entity
    ->save();
  $entity
    ->setName('revision 6');
  $entity
    ->setNewRevision();
  $entity
    ->save();

  // Load the revisions from database.
  $revision2 = Drupal::entityTypeManager()
    ->getStorage('entity_test_rev')
    ->loadRevision(2);
  $revision3 = Drupal::entityTypeManager()
    ->getStorage('entity_test_rev')
    ->loadRevision(3);
  $revision4 = Drupal::entityTypeManager()
    ->getStorage('entity_test_rev')
    ->loadRevision(4);
  $revision5 = Drupal::entityTypeManager()
    ->getStorage('entity_test_rev')
    ->loadRevision(5);
  $revision6 = Drupal::entityTypeManager()
    ->getStorage('entity_test_rev')
    ->loadRevision(6);
  $manager = Drupal::service('conflict.merge_manager');
  $newest_revision1 = $manager
    ->resolveSimpleMerge($revision2, $revision3, $revision4);
  $revisionLca = Drupal::entityTypeManager()
    ->getStorage('entity_test_rev')
    ->loadRevision($newest_revision1);
  $this
    ->assertEquals($revisionLca
    ->label(), "revision 4");

  //This test will pass as it returns "revision 6"
  $newest_revision2 = $manager
    ->resolveSimpleMerge($revision2, $revision3, $revision6);
  $revisionLca = Drupal::entityTypeManager()
    ->getStorage('entity_test_rev')
    ->loadRevision($newest_revision2);
  $this
    ->assertEquals($revisionLca
    ->label(), "revision 6");
}