ConflictTrackerTest.php in Multiversion 8.2
File
src/Tests/ConflictTrackerTest.php
View source
<?php
namespace Drupal\multiversion\Tests;
class ConflictTrackerTest extends MultiversionWebTestBase {
protected $conflictTracker;
protected $storage;
protected function setUp() {
parent::setUp();
$this->conflictTracker = \Drupal::service('multiversion.conflict_tracker');
$this->storage = $this->entityTypeManager
->getStorage('entity_test_rev');
}
public function testConflictTracker() {
$entity_1_conflicts = $this
->checkEntityConflicts();
$entity_2_conflicts = $this
->checkEntityConflicts(6);
$all_entities_conflicts = $entity_1_conflicts + $entity_2_conflicts;
$all_tracker_conflicts = $this->conflictTracker
->getAll();
$this
->assertEqual($all_entities_conflicts, $all_tracker_conflicts, 'Both entities conflicts are tracked in workspace conflict tracker.');
$entity_1_uuid = array_keys($entity_1_conflicts)[0];
$this
->resolveConflicts($entity_1_uuid);
$all_tracker_conflicts = $this->conflictTracker
->getAll();
$this
->assertEqual($entity_2_conflicts, $all_tracker_conflicts, 'Workspace conflicts only contain Entity 1\'s conflicts after Entity 2\' conflicts resolved.');
}
protected function checkEntityConflicts($revision_start = 0) {
$entity = $this->storage
->create();
$uuid = $entity
->uuid();
$entity
->save();
$revs[] = $entity->_rev->value;
$entity
->save();
$revs[] = $entity->_rev->value;
$entity
->save();
$revs[] = $leaf_one = $entity->_rev->value;
$entity = $this->storage
->loadRevision($revision_start + 2);
$entity
->save();
$revs[] = $entity->_rev->value;
$entity = $this->storage
->loadRevision($revision_start + 4);
$entity
->save();
$revs[] = $entity->_rev->value;
$entity = $this->storage
->loadRevision($revision_start + 1);
$entity
->save();
$revs[] = $entity->_rev->value;
$expected_conflicts = [
$revs[2] => 'available',
$revs[5] => 'available',
];
$tracker_conflicts = $this->conflictTracker
->get($uuid);
$this
->assertEqual($tracker_conflicts, $expected_conflicts, 'Tracker conflicts are correct');
return [
$uuid => $tracker_conflicts,
];
}
protected function resolveConflicts($uuid) {
$expected_conflicts = $this->conflictTracker
->get($uuid);
$revs = array_keys($expected_conflicts);
foreach ($revs as $rev) {
$record = \Drupal::service('multiversion.entity_index.rev')
->get("{$uuid}:{$rev}");
$revision = $this->storage
->loadRevision($record['revision_id']);
$revision
->delete();
unset($expected_conflicts[$rev]);
$tracker_conflicts = $this->conflictTracker
->get($uuid);
$this
->assertEqual($tracker_conflicts, $expected_conflicts, 'Resolved conflict removed correctly.');
}
$this
->assertEqual($tracker_conflicts, [], 'All conflicts resolved for entity.');
}
}