Skip to content

Commit

Permalink
#16420 Fixing issue when updating self related fields
Browse files Browse the repository at this point in the history
  • Loading branch information
nollymar committed Apr 22, 2019
1 parent 190aad4 commit 2912b5c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
Expand Up @@ -699,6 +699,51 @@ public void testUpdateParentFieldInARelationship_shouldSucceed()
}
}

@Test
public void testUpdateSelfRelatedFields_shouldNotChangeRelationNames()
throws DotDataException, DotSecurityException {

ContentType parentContentType = null;

final long time = System.currentTimeMillis();
final String newCardinality = String.valueOf(RELATIONSHIP_CARDINALITY.ONE_TO_ONE.ordinal());

try {
parentContentType = createAndSaveSimpleContentType("parentContentType" + time);

final Field childField = createAndSaveManyToManyRelationshipField("newRel",
parentContentType.id(), parentContentType.variable(), CARDINALITY);

final String fullFieldVar =
parentContentType.variable() + StringPool.PERIOD + childField.variable();

//Adding the other side of the relationship
Field parentField = FieldBuilder.builder(RelationshipField.class).name("otherSideRel")
.contentTypeId(parentContentType.id()).values(CARDINALITY)
.relationType(fullFieldVar).required(true).build();

parentField = fieldAPI.save(parentField, user);

//Update parent field
fieldAPI
.save(FieldBuilder.builder(parentField).values(newCardinality).build(), user);

//Update child field
fieldAPI
.save(FieldBuilder.builder(childField).values(newCardinality).build(), user);

final Relationship relationship = relationshipAPI.byTypeValue(fullFieldVar);

assertEquals(childField.variable(), relationship.getChildRelationName());
assertEquals(parentField.variable(), relationship.getParentRelationName());

} finally {
if (UtilMethods.isSet(parentContentType) && UtilMethods.isSet(parentContentType.id())) {
contentTypeAPI.delete(parentContentType);
}
}
}

@Test(expected = DotDataException.class)
@UseDataProvider("testCases")
public void testValidateRelationshipField_shouldThrowAnException(TestCase testCase)
Expand Down
Expand Up @@ -48,7 +48,6 @@
import com.dotcms.repackage.com.google.common.annotations.VisibleForTesting;
import com.dotcms.repackage.com.google.common.collect.ImmutableList;
import com.dotcms.system.event.local.business.LocalSystemEventsAPI;
import com.dotcms.util.DotPreconditions;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.CacheLocator;
import com.dotmarketing.business.DotStateException;
Expand Down Expand Up @@ -338,10 +337,23 @@ Optional<Relationship> getRelationshipForField(final Field field, final ContentT
private void updateRelationshipObject(final Field field, final ContentType type, final ContentType relatedContentType,
final Relationship relationship, final int cardinality, final User user)
throws DotDataException {

final boolean isChildField;
final String relationName = field.variable();
FieldBuilder builder;

if (relationshipAPI.sameParentAndChild(relationship)){
isChildField = relationship.getParentRelationName() != null && relationship
.getParentRelationName().equals(field.variable()) || (
relationship.getParentRelationName() == null && !relationship
.getChildRelationName().equals(field.variable()));

} else{
isChildField = relationship.getChildStructureInode().equals(type.id());
}

//check which side of the relationship is being updated (parent or child)
if (relationship.getChildStructureInode().equals(type.id())) {
if (isChildField) {
//parent is updated
relationship.setParentRelationName(relationName);
relationship.setParentRequired(field.required());
Expand Down

0 comments on commit 2912b5c

Please sign in to comment.