Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XWIKI-22571: Backlinks update changes an absolute reference to the moved page into one relative to the current wiki #3654

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -22,6 +22,7 @@
import org.junit.jupiter.api.Test;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.WikiReference;
import org.xwiki.test.annotation.ComponentList;
import org.xwiki.test.junit5.mockito.ComponentTest;
import org.xwiki.test.junit5.mockito.InjectMockComponents;
Expand Down Expand Up @@ -70,5 +71,19 @@ public void resolveDocumentReferenceWithBaseReference()
assertNull(reference.extractReference(EntityType.WIKI));
assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
assertNull(reference.extractReference(EntityType.DOCUMENT));

reference =
this.resolver.resolve("", EntityType.DOCUMENT, new EntityReference("wikiFoo", EntityType.WIKI));

assertEquals("wikiFoo", reference.extractReference(EntityType.WIKI).getName());
assertNull(reference.extractReference(EntityType.SPACE));
assertNull(reference.extractReference(EntityType.DOCUMENT));

reference =
this.resolver.resolve("", EntityType.DOCUMENT, new WikiReference("wikiFoo"));

assertEquals("wikiFoo", reference.extractReference(EntityType.WIKI).getName());
assertNull(reference.extractReference(EntityType.SPACE));
assertNull(reference.extractReference(EntityType.DOCUMENT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Unit tests for {@link WikiReference}.
Expand All @@ -49,4 +50,11 @@ public void testInvalidParent()
() -> new WikiReference(new EntityReference("wiki", EntityType.WIKI, badParent)));
assertEquals("Unexpected parent [" + badParent + "] in a wiki reference", expected.getMessage());
}

@Test
void instanceOf()
{
assertTrue(new WikiReference("foo") instanceof WikiReference);
assertTrue(new WikiReference("foo") instanceof EntityReference);
}
}
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