Skip to content

Commit d2d9a57

Browse files
authored
XWIKI-22571: Backlinks update changes an absolute reference to the moved 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
1 parent 3192739 commit d2d9a57

File tree

5 files changed

+209
-47
lines changed

5 files changed

+209
-47
lines changed

xwiki-platform-core/xwiki-platform-flamingo/xwiki-platform-flamingo-skin/xwiki-platform-flamingo-skin-test/xwiki-platform-flamingo-skin-test-docker/src/test/it/org/xwiki/flamingo/test/docker/DeletePageIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ void deleteWithUpdateLinksAndAutoRedirect(TestUtils testUtils, TestReference ref
773773
// Verify that a redirect was added and the link was updated.
774774
viewPage = testUtils.gotoPage(reference);
775775
assertEquals("New target", this.viewPage.getDocumentTitle());
776-
assertEquals("[[Link>>doc:NewTarget.WebHome]]",
776+
assertEquals("[[Link>>doc:xwiki:NewTarget.WebHome]]",
777777
testUtils.rest().<Page>get(backlinkDocumentReference).getContent());
778778
}
779779

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

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

841841
// Wait for Solr indexing to complete as backlink information from Solr is needed
842842
new SolrTestUtils(testUtils, testConfiguration.getServletEngine()).waitEmptyQueue();
@@ -855,7 +855,7 @@ void deleteWithAffectChildrenAndNewTarget(TestUtils testUtils, TestReference par
855855
// Verify that there is no redirect on the child page and backlink was not altered.
856856
assertEquals(DELETE_SUCCESSFUL, deletingPage.getInfoMessage());
857857
String newContent =
858-
String.format(format, testUtils.serializeReference(newTargetReference).split(":")[1], childFullName);
858+
String.format(format, testUtils.serializeLocalReference(newTargetReference), childFullName);
859859
assertEquals(newContent, testUtils.rest().<Page>get(backlinkDocReference).getContent());
860860
parentPage = testUtils.gotoPage(parentReference);
861861
assertEquals("New target", parentPage.getDocumentTitle());

xwiki-platform-core/xwiki-platform-flamingo/xwiki-platform-flamingo-skin/xwiki-platform-flamingo-skin-test/xwiki-platform-flamingo-skin-test-docker/src/test/it/org/xwiki/flamingo/test/docker/RenamePageIT.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,4 +723,24 @@ void renameWithRelativeLinks(TestUtils testUtils, TestReference testReference, T
723723
//wikiEditPage = WikiEditPage.gotoPage(new DocumentReference("WebHome", newBobSpace));
724724
//assertEquals(String.format("[[%s]]", serializedAlice2Reference), wikiEditPage.getContent());
725725
}
726+
727+
@Order(10)
728+
@Test
729+
void renameLinkContainingWiki(TestUtils testUtils, TestReference testReference, TestConfiguration testConfiguration)
730+
throws Exception
731+
{
732+
DocumentReference documentReference = new DocumentReference("xwiki", "TestLinkWithWiki", "WebHome");
733+
testUtils.rest().delete(documentReference);
734+
testUtils.rest().savePage(documentReference, "Some content", "TestLinkWithWiki");
735+
testUtils.rest().savePage(testReference, "[[MyPage>>xwiki:TestLinkWithWiki.WebHome]]",
736+
"renameLinkContainingWiki");
737+
new SolrTestUtils(testUtils, testConfiguration.getServletEngine()).waitEmptyQueue();
738+
RenamePage renamePage = testUtils.gotoPage(documentReference).rename();
739+
renamePage.getDocumentPicker().setName("TestLinkWithWikiNew");
740+
CopyOrRenameOrDeleteStatusPage statusPage =
741+
renamePage.clickRenameButton().waitUntilFinished();
742+
assertEquals("Done.", statusPage.getInfoMessage());
743+
WikiEditPage wikiEditPage = WikiEditPage.gotoPage(testReference);
744+
assertEquals("[[MyPage>>xwiki:TestLinkWithWikiNew.WebHome]]", wikiEditPage.getContent());
745+
}
726746
}

xwiki-platform-core/xwiki-platform-refactoring/xwiki-platform-refactoring-default/src/main/java/org/xwiki/refactoring/internal/ResourceReferenceRenamer.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,17 @@ public class ResourceReferenceRenamer
5959
@Inject
6060
private EntityReferenceResolver<ResourceReference> entityReferenceResolver;
6161

62+
@Inject
63+
@Named("relative")
64+
private EntityReferenceResolver<ResourceReference> relativeEntityReferenceResolver;
65+
6266
@Inject
6367
@Named("compact")
6468
private EntityReferenceSerializer<String> compactEntityReferenceSerializer;
6569

70+
@Inject
71+
private EntityReferenceSerializer<String> defaultEntityReferenceSerializer;
72+
6673
@Inject
6774
private DocumentReferenceResolver<EntityReference> defaultReferenceDocumentReferenceResolver;
6875

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

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

196-
String newReferenceString =
197-
this.compactEntityReferenceSerializer.serialize(newTargetReference, currentDocumentReference);
198-
201+
String newReferenceString = getNewTargetReference(resourceReference, newTargetReference,
202+
currentDocumentReference);
199203
resourceReference.setReference(newReferenceString);
200204
resourceReference.setType(newResourceType);
201205
result = true;
202206
}
203207
return result;
204208
}
205209

210+
private String getNewTargetReference(ResourceReference resourceReference, EntityReference newTargetReference,
211+
EntityReference currentReference)
212+
{
213+
EntityReference entityReference =
214+
this.relativeEntityReferenceResolver.resolve(resourceReference, null, (Object) null);
215+
// If the reference contains the wiki name, then we should keep the absolute serialization.
216+
if (entityReference.extractReference(EntityType.WIKI) != null) {
217+
return this.defaultEntityReferenceSerializer.serialize(newTargetReference, currentReference);
218+
} else {
219+
return this.compactEntityReferenceSerializer.serialize(newTargetReference, currentReference);
220+
}
221+
}
222+
206223
private boolean isPageReferenceOutOfParent(ResourceReference resourceReference,
207224
DocumentReference linkTargetDocumentReference, Map<EntityReference, EntityReference> updatedEntities)
208225
{
@@ -266,8 +283,7 @@ private <T extends EntityReference> boolean updateRelativeResourceReference(Reso
266283

267284
if (shouldBeUpdated) {
268285
// Serialize the old (original) link relative to the new document's location, in compact form.
269-
String serializedLinkReference =
270-
this.compactEntityReferenceSerializer.serialize(oldLinkReference, newReference);
286+
String serializedLinkReference = getNewTargetReference(resourceReference, oldLinkReference, newReference);
271287
resourceReference.setReference(serializedLinkReference);
272288
result = true;
273289
}

0 commit comments

Comments
 (0)