Skip to content

Commit

Permalink
XWIKI-22571: Backlinks update changes an absolute reference to the mo…
Browse files Browse the repository at this point in the history
…ved page into one relative to the current wiki (#3654)

  * Use default serializer if the wiki is present in a link, not the
    compact serializer
  * Provide unit test and integration test to cover this
  * Fix DeletePageIT tests
  * simplify condition by using the relative resource reference resolver
  * fix tests
  • Loading branch information
surli authored Dec 10, 2024
1 parent 3192739 commit d2d9a57
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ void deleteWithUpdateLinksAndAutoRedirect(TestUtils testUtils, TestReference ref
// Verify that a redirect was added and the link was updated.
viewPage = testUtils.gotoPage(reference);
assertEquals("New target", this.viewPage.getDocumentTitle());
assertEquals("[[Link>>doc:NewTarget.WebHome]]",
assertEquals("[[Link>>doc:xwiki:NewTarget.WebHome]]",
testUtils.rest().<Page>get(backlinkDocumentReference).getContent());
}

Expand Down Expand Up @@ -826,7 +826,7 @@ void deleteWithAffectChildrenAndNewTarget(TestUtils testUtils, TestReference par
TestConfiguration testConfiguration) throws Exception
{
DocumentReference childReference = new DocumentReference("Child", parentReference.getLastSpaceReference());
String childFullName = testUtils.serializeReference(childReference).split(":")[1];
String childFullName = testUtils.serializeLocalReference(childReference);
DocumentReference backlinkDocReference = new DocumentReference("xwiki", "Backlink", "WebHome");
DocumentReference newTargetReference = new DocumentReference("xwiki", "NewTarget", "WebHome");

Expand All @@ -836,7 +836,7 @@ void deleteWithAffectChildrenAndNewTarget(TestUtils testUtils, TestReference par
// Create backlinks to the parent and the child page.
String format = "[[Parent>>doc:%s]] [[Child>>doc:%s]]";
testUtils.createPage(backlinkDocReference,
String.format(format, testUtils.serializeReference(parentReference), childFullName), "Backlink document");
String.format(format, testUtils.serializeLocalReference(parentReference), childFullName), "Backlink document");

// Wait for Solr indexing to complete as backlink information from Solr is needed
new SolrTestUtils(testUtils, testConfiguration.getServletEngine()).waitEmptyQueue();
Expand All @@ -855,7 +855,7 @@ void deleteWithAffectChildrenAndNewTarget(TestUtils testUtils, TestReference par
// Verify that there is no redirect on the child page and backlink was not altered.
assertEquals(DELETE_SUCCESSFUL, deletingPage.getInfoMessage());
String newContent =
String.format(format, testUtils.serializeReference(newTargetReference).split(":")[1], childFullName);
String.format(format, testUtils.serializeLocalReference(newTargetReference), childFullName);
assertEquals(newContent, testUtils.rest().<Page>get(backlinkDocReference).getContent());
parentPage = testUtils.gotoPage(parentReference);
assertEquals("New target", parentPage.getDocumentTitle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,4 +723,24 @@ void renameWithRelativeLinks(TestUtils testUtils, TestReference testReference, T
//wikiEditPage = WikiEditPage.gotoPage(new DocumentReference("WebHome", newBobSpace));
//assertEquals(String.format("[[%s]]", serializedAlice2Reference), wikiEditPage.getContent());
}

@Order(10)
@Test
void renameLinkContainingWiki(TestUtils testUtils, TestReference testReference, TestConfiguration testConfiguration)
throws Exception
{
DocumentReference documentReference = new DocumentReference("xwiki", "TestLinkWithWiki", "WebHome");
testUtils.rest().delete(documentReference);
testUtils.rest().savePage(documentReference, "Some content", "TestLinkWithWiki");
testUtils.rest().savePage(testReference, "[[MyPage>>xwiki:TestLinkWithWiki.WebHome]]",
"renameLinkContainingWiki");
new SolrTestUtils(testUtils, testConfiguration.getServletEngine()).waitEmptyQueue();
RenamePage renamePage = testUtils.gotoPage(documentReference).rename();
renamePage.getDocumentPicker().setName("TestLinkWithWikiNew");
CopyOrRenameOrDeleteStatusPage statusPage =
renamePage.clickRenameButton().waitUntilFinished();
assertEquals("Done.", statusPage.getInfoMessage());
WikiEditPage wikiEditPage = WikiEditPage.gotoPage(testReference);
assertEquals("[[MyPage>>xwiki:TestLinkWithWikiNew.WebHome]]", wikiEditPage.getContent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,17 @@ public class ResourceReferenceRenamer
@Inject
private EntityReferenceResolver<ResourceReference> entityReferenceResolver;

@Inject
@Named("relative")
private EntityReferenceResolver<ResourceReference> relativeEntityReferenceResolver;

@Inject
@Named("compact")
private EntityReferenceSerializer<String> compactEntityReferenceSerializer;

@Inject
private EntityReferenceSerializer<String> defaultEntityReferenceSerializer;

@Inject
private DocumentReferenceResolver<EntityReference> defaultReferenceDocumentReferenceResolver;

Expand Down Expand Up @@ -152,8 +159,6 @@ private boolean updateAbsoluteResourceReference(ResourceReference resourceRefere
DocumentReference absoluteResolvedDocumentReference =
this.defaultReferenceDocumentReferenceResolver.resolve(absoluteResolvedEntityReference);

boolean isRelativePageReferenceOutsideOfParent = false;

// If the link targets the old (renamed) document reference and it's an absolute reference
// (i.e. its resolution without any given parameter gives same result than its resolution with the
// currentDocument) then we must update it
Expand Down Expand Up @@ -193,16 +198,28 @@ private boolean updateAbsoluteResourceReference(ResourceReference resourceRefere
newTargetReference = new AttachmentReference(linkEntityReference.getName(), newReference);
}

String newReferenceString =
this.compactEntityReferenceSerializer.serialize(newTargetReference, currentDocumentReference);

String newReferenceString = getNewTargetReference(resourceReference, newTargetReference,
currentDocumentReference);
resourceReference.setReference(newReferenceString);
resourceReference.setType(newResourceType);
result = true;
}
return result;
}

private String getNewTargetReference(ResourceReference resourceReference, EntityReference newTargetReference,
EntityReference currentReference)
{
EntityReference entityReference =
this.relativeEntityReferenceResolver.resolve(resourceReference, null, (Object) null);
// If the reference contains the wiki name, then we should keep the absolute serialization.
if (entityReference.extractReference(EntityType.WIKI) != null) {
return this.defaultEntityReferenceSerializer.serialize(newTargetReference, currentReference);
} else {
return this.compactEntityReferenceSerializer.serialize(newTargetReference, currentReference);
}
}

private boolean isPageReferenceOutOfParent(ResourceReference resourceReference,
DocumentReference linkTargetDocumentReference, Map<EntityReference, EntityReference> updatedEntities)
{
Expand Down Expand Up @@ -266,8 +283,7 @@ private <T extends EntityReference> boolean updateRelativeResourceReference(Reso

if (shouldBeUpdated) {
// Serialize the old (original) link relative to the new document's location, in compact form.
String serializedLinkReference =
this.compactEntityReferenceSerializer.serialize(oldLinkReference, newReference);
String serializedLinkReference = getNewTargetReference(resourceReference, oldLinkReference, newReference);
resourceReference.setReference(serializedLinkReference);
result = true;
}
Expand Down
Loading

0 comments on commit d2d9a57

Please sign in to comment.