Skip to content
Snippets Groups Projects
Verified Commit 52f85e5c authored by Nguyen Tan's avatar Nguyen Tan Committed by Lee Rowlands
Browse files

Issue #3333675 by tannguyenhn, jibran: Validate in...

Issue #3333675 by tannguyenhn, jibran: Validate in ValidHierarchyReferenceConstraintValidator should bypass non default entity revisions
parent d966ada9
Branches
Tags
No related merge requests found
......@@ -112,9 +112,22 @@ class ValidHierarchyReferenceConstraintValidator extends ConstraintValidator imp
$target_type = $this_entity->getEntityTypeId();
/** @var \PNX\NestedSet\Storage\DbalNestedSet $storage */
$storage = $this->nestedSetStorageFactory->get($value->getFieldDefinition()->getFieldStorageDefinition()->getName(), $target_type);
$children = array_map(function (Node $node) {
$descendant_nested_set_nodes = $storage->findDescendants($thisNode);
$descendant_nested_set_node_ids = array_map(function (Node $node) {
return $node->getId();
}, $storage->findDescendants($thisNode));
}, $descendant_nested_set_nodes);
$descendant_entities = $this->entityTypeManager->getStorage($this_entity->getEntityTypeId())->loadMultiple($descendant_nested_set_node_ids);
$children = [];
foreach ($descendant_nested_set_nodes as $descendant_nested_set_node) {
$node_id = $descendant_nested_set_node->getId();
$entity = $descendant_entities[$node_id] ?? FALSE;
if (!$entity || ($entity->getEntityType()->hasKey('revision') && $descendant_nested_set_node->getRevisionId() != $entity->getRevisionId())) {
// Bypass non default revisions and deleted items.
continue;
}
$children[] = $node_id;
}
$children = array_unique($children);
// Cannot reference self either.
$children[] = $this_entity->id();
......
......@@ -80,6 +80,19 @@ class ForwardRevisionNodeValidationTest extends BrowserTestBase {
], 'Save');
$assert = $this->assertSession();
$assert->pageTextContains(sprintf('This entity (node: %s) cannot be referenced as it is either a child or the same entity.', $first_child->id()));
// Remove parent in first child.
$this->drupalGet($first_child->toUrl('edit-form'));
$this->submitForm([
sprintf('%s[0][target_id][target_id]', static::FIELD_NAME) => '',
], 'Save');
// Try to submit form with first child as parent.
$this->drupalGet($this->parent->toUrl('edit-form'));
$this->submitForm([
sprintf('%s[0][target_id][target_id]', static::FIELD_NAME) => sprintf('%s (%s)', $first_child->label(), $first_child->id()),
], 'Save');
$assert = $this->assertSession();
$assert->pageTextNotContains(sprintf('This entity (node: %s) cannot be referenced as it is either a child or the same entity.', $first_child->id()));
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment