Search in sources :

Example 46 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class DefaultModelBridge method getHierarchicalParent.

private DocumentReference getHierarchicalParent(DocumentReference documentReference) {
    final String spaceHomePage = entityReferenceProvider.getDefaultReference(EntityType.DOCUMENT).getName();
    EntityReference parentOfTheSpace = documentReference.getLastSpaceReference().getParent();
    boolean pageIsNotTerminal = documentReference.getName().equals(spaceHomePage);
    // The parent should be A.B.WebHome
    if (pageIsNotTerminal && parentOfTheSpace.getType() == EntityType.SPACE) {
        return new DocumentReference(spaceHomePage, new SpaceReference(parentOfTheSpace));
    }
    // The parent should be Main.WebHome
    if (pageIsNotTerminal && parentOfTheSpace.getType() == EntityType.WIKI) {
        return new DocumentReference(spaceHomePage, new SpaceReference(entityReferenceProvider.getDefaultReference(EntityType.SPACE).getName(), documentReference.getWikiReference()));
    }
    // The parent should be A.WebHome
    return new DocumentReference(spaceHomePage, documentReference.getLastSpaceReference());
}
Also used : SpaceReference(org.xwiki.model.reference.SpaceReference) EntityReference(org.xwiki.model.reference.EntityReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 47 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class DefaultLinkRefactoringTest method updateRelativeLinksAcrossWikis.

@Test
public void updateRelativeLinksAcrossWikis() throws Exception {
    DocumentReference oldReference = new DocumentReference("wiki1", "A", "B");
    DocumentReference newReference = new DocumentReference("wiki2", "X", "Y");
    XWikiDocument newDocument = mock(XWikiDocument.class);
    when(this.xcontext.getWiki().getDocument(newReference, this.xcontext)).thenReturn(newDocument);
    when(newDocument.getDocumentReference()).thenReturn(newReference);
    when(newDocument.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
    this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
    XDOM xdom = mock(XDOM.class);
    when(newDocument.getXDOM()).thenReturn(xdom);
    ResourceReference docLinkReference = new ResourceReference("C", ResourceType.DOCUMENT);
    LinkBlock docLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false);
    ResourceReference spaceLinkReference = new ResourceReference("Z", ResourceType.SPACE);
    LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false);
    when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(docLinkBlock, spaceLinkBlock));
    DocumentReference originalDocLinkReference = new DocumentReference("C", oldReference.getLastSpaceReference());
    when(this.resourceReferenceResolver.resolve(docLinkReference, null, oldReference)).thenReturn(originalDocLinkReference);
    DocumentReference newDocLinkReference = new DocumentReference("C", newReference.getLastSpaceReference());
    when(this.resourceReferenceResolver.resolve(docLinkReference, null, newReference)).thenReturn(newDocLinkReference);
    SpaceReference originalSpaceReference = new SpaceReference("wiki1", "Z");
    when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, oldReference)).thenReturn(originalSpaceReference);
    SpaceReference newSpaceReference = new SpaceReference("wiki2", "Z");
    when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, newReference)).thenReturn(newSpaceReference);
    when(this.compactEntityReferenceSerializer.serialize(originalDocLinkReference, newReference)).thenReturn("wiki1:A.C");
    when(this.compactEntityReferenceSerializer.serialize(originalSpaceReference, newReference)).thenReturn("wiki1:Z");
    this.mocker.getComponentUnderTest().updateRelativeLinks(oldReference, newReference);
    // Document link block is updated.
    assertEquals("wiki1:A.C", docLinkBlock.getReference().getReference());
    assertEquals(ResourceType.DOCUMENT, docLinkBlock.getReference().getType());
    // Space link is also updated, since they were referring entities on a different wiki.
    assertEquals("wiki1:Z", spaceLinkBlock.getReference().getReference());
    assertEquals(ResourceType.SPACE, spaceLinkBlock.getReference().getType());
    verifyDocumentSave(newDocument, "Updated the relative links.", true);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XDOM(org.xwiki.rendering.block.XDOM) LinkBlock(org.xwiki.rendering.block.LinkBlock) SpaceReference(org.xwiki.model.reference.SpaceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 48 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class DefaultLinkRefactoringTest method updateRelativeLinks.

@Test
public void updateRelativeLinks() throws Exception {
    DocumentReference oldReference = new DocumentReference("wiki", "A", "B");
    DocumentReference newReference = new DocumentReference("wiki", "X", "Y");
    XWikiDocument newDocument = mock(XWikiDocument.class);
    when(this.xcontext.getWiki().getDocument(newReference, this.xcontext)).thenReturn(newDocument);
    when(newDocument.getDocumentReference()).thenReturn(newReference);
    when(newDocument.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
    this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
    XDOM xdom = mock(XDOM.class);
    when(newDocument.getXDOM()).thenReturn(xdom);
    ResourceReference docLinkReference = new ResourceReference("C", ResourceType.DOCUMENT);
    LinkBlock docLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false);
    ResourceReference spaceLinkReference = new ResourceReference("Z", ResourceType.SPACE);
    LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false);
    when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(docLinkBlock, spaceLinkBlock));
    DocumentReference originalDocLinkReference = new DocumentReference("C", oldReference.getLastSpaceReference());
    when(this.resourceReferenceResolver.resolve(docLinkReference, null, oldReference)).thenReturn(originalDocLinkReference);
    DocumentReference newDocLinkReference = new DocumentReference("C", newReference.getLastSpaceReference());
    when(this.resourceReferenceResolver.resolve(docLinkReference, null, newReference)).thenReturn(newDocLinkReference);
    SpaceReference originalSpaceReference = new SpaceReference("wiki", "Z");
    when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, oldReference)).thenReturn(originalSpaceReference);
    when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, newReference)).thenReturn(originalSpaceReference);
    when(this.compactEntityReferenceSerializer.serialize(originalDocLinkReference, newReference)).thenReturn("A.C");
    this.mocker.getComponentUnderTest().updateRelativeLinks(oldReference, newReference);
    // Document link block is updated.
    assertEquals("A.C", docLinkBlock.getReference().getReference());
    assertEquals(ResourceType.DOCUMENT, docLinkBlock.getReference().getType());
    // Space link block stays the same, since they were on the same wiki.
    assertEquals("Z", spaceLinkBlock.getReference().getReference());
    assertEquals(ResourceType.SPACE, spaceLinkBlock.getReference().getType());
    verifyDocumentSave(newDocument, "Updated the relative links.", true);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XDOM(org.xwiki.rendering.block.XDOM) LinkBlock(org.xwiki.rendering.block.LinkBlock) SpaceReference(org.xwiki.model.reference.SpaceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 49 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class DefaultLinkRefactoringTest method renameNonTerminalToTerminalDocumentLinks.

@Test
public void renameNonTerminalToTerminalDocumentLinks() throws Exception {
    DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
    XWikiDocument document = mock(XWikiDocument.class);
    when(this.xcontext.getWiki().getDocument(documentReference, this.xcontext)).thenReturn(document);
    when(document.getDocumentReference()).thenReturn(documentReference);
    when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
    this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
    // From a non-terminal document to a terminal document.
    DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "WebHome");
    DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y");
    XDOM xdom = mock(XDOM.class);
    when(document.getXDOM()).thenReturn(xdom);
    ResourceReference docLinkReference = new ResourceReference("A.WebHome", ResourceType.DOCUMENT);
    LinkBlock documentLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false);
    ResourceReference spaceLinkReference = new ResourceReference("A", ResourceType.SPACE);
    LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false);
    when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(documentLinkBlock, spaceLinkBlock));
    // Doc link
    when(this.resourceReferenceResolver.resolve(docLinkReference, null, documentReference)).thenReturn(oldLinkTarget);
    when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget);
    when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y");
    // Space link
    SpaceReference spaceReference = oldLinkTarget.getLastSpaceReference();
    when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, documentReference)).thenReturn(spaceReference);
    when(this.defaultReferenceDocumentReferenceResolver.resolve(spaceReference)).thenReturn(oldLinkTarget);
    when(this.compactEntityReferenceSerializer.serialize(newLinkTarget.getLastSpaceReference(), documentReference)).thenReturn("X");
    this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget);
    // Note that both resulting renamed back-links are of type document. (i.e. the space link was converted to a doc
    // link)
    assertEquals("X.Y", documentLinkBlock.getReference().getReference());
    assertEquals(ResourceType.DOCUMENT, documentLinkBlock.getReference().getType());
    assertEquals("X.Y", spaceLinkBlock.getReference().getReference());
    assertEquals(ResourceType.DOCUMENT, spaceLinkBlock.getReference().getType());
    verifyDocumentSave(document, "Renamed back-links.", false);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XDOM(org.xwiki.rendering.block.XDOM) LinkBlock(org.xwiki.rendering.block.LinkBlock) SpaceReference(org.xwiki.model.reference.SpaceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 50 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class DefaultModelBridgeTest method configure.

@Before
public void configure() throws Exception {
    when(this.xcontext.getWiki()).thenReturn(xwiki);
    Provider<XWikiContext> xcontextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER);
    when(xcontextProvider.get()).thenReturn(this.xcontext);
    EntityReferenceProvider entityReferenceProvider = this.mocker.getInstance(EntityReferenceProvider.class);
    when(entityReferenceProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(new DocumentReference("what", "ever", "WebHome"));
    when(entityReferenceProvider.getDefaultReference(EntityType.SPACE)).thenReturn(new SpaceReference("whatever", "Main"));
}
Also used : EntityReferenceProvider(org.xwiki.model.reference.EntityReferenceProvider) SpaceReference(org.xwiki.model.reference.SpaceReference) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Aggregations

SpaceReference (org.xwiki.model.reference.SpaceReference)142 DocumentReference (org.xwiki.model.reference.DocumentReference)96 Test (org.junit.Test)83 WikiReference (org.xwiki.model.reference.WikiReference)58 EntityReference (org.xwiki.model.reference.EntityReference)24 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)21 ArrayList (java.util.ArrayList)11 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)11 XWikiException (com.xpn.xwiki.XWikiException)9 QueryRestrictionGroup (com.celements.search.lucene.query.QueryRestrictionGroup)8 Expectations (org.jmock.Expectations)8 XWikiContext (com.xpn.xwiki.XWikiContext)7 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)7 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)7 ComponentManager (org.xwiki.component.manager.ComponentManager)7 NamespacedComponentManager (org.xwiki.component.manager.NamespacedComponentManager)7 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)7 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)7 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)7 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)7