Search in sources :

Example 31 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class DisplayMacro method execute.

@Override
public List<Block> execute(DisplayMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    // Step 1: Perform checks.
    if (parameters.getReference() == null) {
        throw new MacroExecutionException("You must specify a 'reference' parameter pointing to the entity to display.");
    }
    DocumentReference includedReference = resolve(context.getCurrentMacroBlock(), parameters);
    checkRecursiveDisplay(context.getCurrentMacroBlock(), includedReference);
    if (!this.documentAccessBridge.isDocumentViewable(includedReference)) {
        throw new MacroExecutionException("Current user [" + this.documentAccessBridge.getCurrentUserReference() + "] doesn't have view rights on document [" + this.defaultEntityReferenceSerializer.serialize(includedReference) + "]");
    }
    // Step 2: Retrieve the included document.
    DocumentModelBridge documentBridge;
    try {
        documentBridge = this.documentAccessBridge.getDocumentInstance(includedReference);
    } catch (Exception e) {
        throw new MacroExecutionException("Failed to load Document [" + this.defaultEntityReferenceSerializer.serialize(includedReference) + "]", e);
    }
    // Step 3: Display the content of the included document.
    // Display the content in an isolated execution and transformation context.
    DocumentDisplayerParameters displayParameters = new DocumentDisplayerParameters();
    displayParameters.setContentTransformed(true);
    displayParameters.setExecutionContextIsolated(displayParameters.isContentTransformed());
    displayParameters.setSectionId(parameters.getSection());
    displayParameters.setTransformationContextIsolated(displayParameters.isContentTransformed());
    displayParameters.setTargetSyntax(context.getTransformationContext().getTargetSyntax());
    displayParameters.setContentTranslated(true);
    Stack<Object> references = this.displaysBeingExecuted.get();
    if (references == null) {
        references = new Stack<Object>();
        this.displaysBeingExecuted.set(references);
    }
    references.push(includedReference);
    XDOM result;
    try {
        result = this.documentDisplayer.display(documentBridge, displayParameters);
    } catch (Exception e) {
        throw new MacroExecutionException(e.getMessage(), e);
    } finally {
        references.pop();
    }
    // Step 4: Wrap Blocks in a MetaDataBlock with the "source" meta data specified so that we know from where the
    // content comes and "base" meta data so that reference are properly resolved
    MetaDataBlock metadata = new MetaDataBlock(result.getChildren(), result.getMetaData());
    String source = this.defaultEntityReferenceSerializer.serialize(includedReference);
    metadata.getMetaData().addMetaData(MetaData.SOURCE, source);
    metadata.getMetaData().addMetaData(MetaData.BASE, source);
    return Arrays.<Block>asList(metadata);
}
Also used : DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MacroBlock(org.xwiki.rendering.block.MacroBlock) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 32 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class DocumentContentDisplayerTest method testBaseMetaDataIsSetBeforeExecutingTransformations.

@Test
public void testBaseMetaDataIsSetBeforeExecutingTransformations() throws Exception {
    // The execution context is expected to have the "xwikicontext" property set.
    Execution mockExecution = mocker.getInstance(Execution.class);
    ExecutionContext executionContext = new ExecutionContext();
    executionContext.setProperty("xwikicontext", new HashMap<String, Object>());
    Mockito.when(mockExecution.getContext()).thenReturn(executionContext);
    // The document being displayed.
    DocumentModelBridge mockDocument = Mockito.mock(DocumentModelBridge.class);
    XDOM content = new XDOM(Collections.<Block>emptyList());
    Mockito.when(mockDocument.getXDOM()).thenReturn(content);
    // The reference of the current document musts be set as the value of the BASE meta data.
    DocumentReference currentDocRef = new DocumentReference("wiki", "Space", "Page");
    DocumentAccessBridge mockDocumentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
    Mockito.when(mockDocumentAccessBridge.getCurrentDocumentReference()).thenReturn(currentDocRef);
    EntityReferenceSerializer<String> serializer = mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
    Mockito.when(serializer.serialize(currentDocRef)).thenReturn("foo");
    // We can't verify the meta data after the display method is called because we want to make sure the BASE meta
    // data is correctly set before XDOM transformations are executed, not after.
    TransformationManager mockTransformationManager = mocker.getInstance(TransformationManager.class);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            XDOM xdom = (XDOM) invocation.getArguments()[0];
            // We have to assert the meta data before the transformations are executed not at the end!
            Assert.assertEquals("foo", xdom.getMetaData().getMetaData(MetaData.BASE));
            return null;
        }
    }).when(mockTransformationManager).performTransformations(Mockito.any(XDOM.class), Mockito.any(TransformationContext.class));
    // Execute the display.
    Assert.assertSame(content, mocker.getComponentUnderTest().display(mockDocument, new DocumentDisplayerParameters()));
    // Make sure the transformations are executed exactly once, and on the right content.
    Mockito.verify(mockTransformationManager, Mockito.times(1)).performTransformations(Mockito.same(content), Mockito.any(TransformationContext.class));
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TransformationManager(org.xwiki.rendering.transformation.TransformationManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 33 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class IncludeMacroTest method testIncludeMacroInsideSourceMetaDataBlockAndWithRelativeDocumentReferencePassed.

@Test
public void testIncludeMacroInsideSourceMetaDataBlockAndWithRelativeDocumentReferencePassed() throws Exception {
    String expected = "beginDocument\n" + "beginMetaData [[source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "beginParagraph\n" + "onWord [content]\n" + "endParagraph\n" + "endMetaData [[source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "endDocument";
    IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference("relativePage");
    final MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
    // Add a Source MetaData Block as a parent of the include Macro block.
    new MetaDataBlock(Collections.<Block>singletonList(macroContext.getCurrentMacroBlock()), new MetaData(Collections.<String, Object>singletonMap(MetaData.BASE, "wiki:space.page")));
    final DocumentReference sourceReference = new DocumentReference("wiki", "space", "page");
    final DocumentReference resolvedReference = new DocumentReference("wiki", "space", "relativePage");
    final DocumentModelBridge mockDocument = getMockery().mock(DocumentModelBridge.class);
    getMockery().checking(new Expectations() {

        {
            oneOf(mockDocumentReferenceResolver).resolve("relativePage", macroContext.getCurrentMacroBlock());
            will(returnValue(resolvedReference));
            oneOf(mockSetup.bridge).isDocumentViewable(resolvedReference);
            will(returnValue(true));
            oneOf(mockSetup.bridge).getDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            oneOf(mockSetup.bridge).getTranslatedDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            oneOf(mockSetup.bridge).getCurrentDocumentReference();
            will(returnValue(sourceReference));
            oneOf(mockDocument).getXDOM();
            will(returnValue(getXDOM("content")));
            oneOf(mockDocument).getSyntax();
            will(returnValue(Syntax.XWIKI_2_0));
            oneOf(mockDocument).getDocumentReference();
            will(returnValue(resolvedReference));
            allowing(mockDocument).getRealLanguage();
            will(returnValue(""));
        }
    });
    List<Block> blocks = this.includeMacro.execute(parameters, null, macroContext);
    assertBlocks(expected, blocks, this.rendererFactory);
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MetaData(org.xwiki.rendering.listener.MetaData) IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) Test(org.junit.Test)

Example 34 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class IncludeMacroTest method testIncludeMacroWhenInvalidSectionSpecified.

@Test
public void testIncludeMacroWhenInvalidSectionSpecified() throws Exception {
    IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference("document");
    parameters.setSection("unknown");
    final MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
    final DocumentReference resolvedReference = new DocumentReference("wiki", "space", "document");
    final DocumentModelBridge mockDocument = getMockery().mock(DocumentModelBridge.class);
    getMockery().checking(new Expectations() {

        {
            oneOf(mockDocumentReferenceResolver).resolve("document", macroContext.getCurrentMacroBlock());
            will(returnValue(resolvedReference));
            oneOf(mockSetup.bridge).isDocumentViewable(resolvedReference);
            will(returnValue(true));
            oneOf(mockSetup.bridge).getDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            allowing(mockSetup.bridge).getTranslatedDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            oneOf(mockSetup.bridge).getCurrentDocumentReference();
            will(returnValue(new DocumentReference("wiki", "Space", "IncludingPage")));
            oneOf(mockDocument).getSyntax();
            will(returnValue(Syntax.XWIKI_2_0));
            oneOf(mockDocument).getXDOM();
            will(returnValue(getXDOM("content")));
            allowing(mockDocument).getDocumentReference();
            will(returnValue(resolvedReference));
            allowing(mockDocument).getRealLanguage();
            will(returnValue(""));
        }
    });
    try {
        this.includeMacro.execute(parameters, null, macroContext);
        Assert.fail("Should have raised an exception");
    } catch (MacroExecutionException expected) {
        Assert.assertEquals("Cannot find section [unknown] in document [wiki:space.document]", expected.getMessage());
    }
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 35 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class IncludeMacroTest method testIncludeMacroWhenSectionSpecified.

@Test
public void testIncludeMacroWhenSectionSpecified() throws Exception {
    String expected = "beginDocument\n" + "beginMetaData [[source]=[wiki:space.document][syntax]=[XWiki 2.0]]\n" + "beginHeader [1, Hsection]\n" + "onWord [section]\n" + "endHeader [1, Hsection]\n" + "beginParagraph\n" + "onWord [content2]\n" + "endParagraph\n" + "endMetaData [[source]=[wiki:space.document][syntax]=[XWiki 2.0]]\n" + "endDocument";
    IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference("document");
    parameters.setSection("Hsection");
    final MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
    final DocumentReference resolvedReference = new DocumentReference("wiki", "space", "document");
    final DocumentModelBridge mockDocument = getMockery().mock(DocumentModelBridge.class);
    getMockery().checking(new Expectations() {

        {
            oneOf(mockDocumentReferenceResolver).resolve("document", macroContext.getCurrentMacroBlock());
            will(returnValue(resolvedReference));
            oneOf(mockSetup.bridge).isDocumentViewable(resolvedReference);
            will(returnValue(true));
            oneOf(mockSetup.bridge).getDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            oneOf(mockSetup.bridge).getTranslatedDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            oneOf(mockSetup.bridge).getCurrentDocumentReference();
            will(returnValue(new DocumentReference("wiki", "Space", "IncludingPage")));
            oneOf(mockDocument).getDocumentReference();
            will(returnValue(resolvedReference));
            oneOf(mockDocument).getSyntax();
            will(returnValue(Syntax.XWIKI_2_0));
            oneOf(mockDocument).getXDOM();
            will(returnValue(getXDOM("content1\n\n= section =\ncontent2")));
            allowing(mockDocument).getRealLanguage();
            will(returnValue(""));
        }
    });
    List<Block> blocks = this.includeMacro.execute(parameters, null, macroContext);
    assertBlocks(expected, blocks, this.rendererFactory);
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)58 DocumentReference (org.xwiki.model.reference.DocumentReference)43 Test (org.junit.Test)39 Expectations (org.jmock.Expectations)18 XDOM (org.xwiki.rendering.block.XDOM)18 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)14 Syntax (org.xwiki.rendering.syntax.Syntax)11 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)10 MacroBlock (org.xwiki.rendering.block.MacroBlock)9 DocumentDisplayerParameters (org.xwiki.display.internal.DocumentDisplayerParameters)8 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)7 Block (org.xwiki.rendering.block.Block)6 HashMap (java.util.HashMap)5 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)5 DocumentDisplayer (org.xwiki.display.internal.DocumentDisplayer)5 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)5 MetaData (org.xwiki.rendering.listener.MetaData)5 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)5 StringReader (java.io.StringReader)4