Search in sources :

Example 1 with IncludeMacroParameters

use of org.xwiki.rendering.macro.include.IncludeMacroParameters in project xwiki-platform by xwiki.

the class IncludeMacroTest method testIncludeMacroWithRecursiveIncludeContextNew.

@Test
public void testIncludeMacroWithRecursiveIncludeContextNew() throws Exception {
    final DocumentDisplayer mockDocumentDisplayer = getMockery().mock(DocumentDisplayer.class);
    this.includeMacro.setDocumentAccessBridge(mockSetup.bridge);
    this.includeMacro.setDocumentDisplayer(mockDocumentDisplayer);
    final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);
    final IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference("wiki:space.page");
    parameters.setContext(Context.NEW);
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentReferenceResolver).resolve("wiki:space.page", macroContext.getCurrentMacroBlock());
            will(returnValue(new DocumentReference("wiki", "space", "page")));
            allowing(mockSetup.bridge).isDocumentViewable(with(any(DocumentReference.class)));
            will(returnValue(true));
            allowing(mockSetup.bridge).getDocumentInstance(with(any(DocumentReference.class)));
            will(returnValue(null));
            allowing(mockDocumentDisplayer).display(with(same((DocumentModelBridge) null)), with(any(DocumentDisplayerParameters.class)));
            will(new CustomAction("recursively call the include macro again") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    try {
                        includeMacro.execute(parameters, null, macroContext);
                    } catch (Exception expected) {
                        if (expected.getMessage().contains("Found recursive inclusion")) {
                            throw new ExpectedRecursiveInclusionException();
                        }
                    }
                    return true;
                }
            });
        }
    });
    try {
        this.includeMacro.execute(parameters, null, macroContext);
        Assert.fail("The include macro hasn't checked the recursive inclusion");
    } catch (MacroExecutionException expected) {
        if (!(expected.getCause() instanceof ExpectedRecursiveInclusionException)) {
            throw expected;
        }
    }
}
Also used : Expectations(org.jmock.Expectations) DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) DocumentDisplayer(org.xwiki.display.internal.DocumentDisplayer) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) 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) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Test(org.junit.Test)

Example 2 with IncludeMacroParameters

use of org.xwiki.rendering.macro.include.IncludeMacroParameters in project xwiki-platform by xwiki.

the class IncludeMacroTest method testIncludeMacroWithRecursiveIncludeContextCurrent.

@Test
public void testIncludeMacroWithRecursiveIncludeContextCurrent() throws Exception {
    this.includeMacro.setDocumentAccessBridge(mockSetup.bridge);
    final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);
    // Add an Include Macro MarkerBlock as a parent of the include Macro block since this is what would have
    // happened if an Include macro is included in another Include macro.
    final MacroMarkerBlock includeMacroMarker = new MacroMarkerBlock("include", Collections.singletonMap("reference", "space.page"), Collections.<Block>singletonList(macroContext.getCurrentMacroBlock()), false);
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentReferenceResolver).resolve("wiki:space.page", macroContext.getCurrentMacroBlock());
            will(returnValue(new DocumentReference("wiki", "space", "page")));
            allowing(mockDocumentReferenceResolver).resolve("space.page", includeMacroMarker);
            will(returnValue(new DocumentReference("wiki", "space", "page")));
        }
    });
    IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference("wiki:space.page");
    parameters.setContext(Context.CURRENT);
    try {
        this.includeMacro.execute(parameters, null, macroContext);
        Assert.fail("The include macro hasn't checked the recursive inclusion");
    } catch (MacroExecutionException expected) {
        if (!expected.getMessage().startsWith("Found recursive inclusion")) {
            throw expected;
        }
    }
}
Also used : Expectations(org.jmock.Expectations) IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 3 with IncludeMacroParameters

use of org.xwiki.rendering.macro.include.IncludeMacroParameters in project xwiki-platform by xwiki.

the class IncludeMacroTest method runIncludeMacro.

private List<Block> runIncludeMacro(final Context context, String includedContent, boolean restricted) throws Exception {
    final DocumentReference includedDocumentReference = new DocumentReference("wiki", "Space", "IncludedPage");
    String includedDocStringRef = "wiki:space.page";
    setUpDocumentMock(includedDocStringRef, includedDocumentReference, includedContent);
    getMockery().checking(new Expectations() {

        {
            allowing(mockSetup.bridge).isDocumentViewable(with(same(includedDocumentReference)));
            will(returnValue(true));
            // Verify that push/pop are called when context is NEW
            if (context == Context.NEW) {
                oneOf(mockSetup.bridge).pushDocumentInContext(with(any(Map.class)), with(same(mockDocument)));
                oneOf(mockSetup.bridge).getCurrentDocumentReference();
                will(returnValue(includedDocumentReference));
                oneOf(mockSetup.bridge).popDocumentFromContext(with(any(Map.class)));
            } else {
                oneOf(mockSetup.bridge).getCurrentDocumentReference();
                will(returnValue(new DocumentReference("wiki", "Space", "IncludingPage")));
            }
        }
    });
    this.includeMacro.setDocumentAccessBridge(this.mockSetup.bridge);
    IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference(includedDocStringRef);
    parameters.setContext(context);
    // Create a Macro transformation context with the Macro transformation object defined so that the include
    // macro can transform included page which is using a new context.
    MacroTransformation macroTransformation = getComponentManager().getInstance(Transformation.class, "macro");
    MacroTransformationContext macroContext = createMacroTransformationContext(includedDocStringRef, false);
    macroContext.setId("wiki:Space.IncludingPage");
    macroContext.setTransformation(macroTransformation);
    macroContext.getTransformationContext().setRestricted(restricted);
    return this.includeMacro.execute(parameters, null, macroContext);
}
Also used : Expectations(org.jmock.Expectations) IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroTransformation(org.xwiki.rendering.internal.transformation.macro.MacroTransformation)

Example 4 with IncludeMacroParameters

use of org.xwiki.rendering.macro.include.IncludeMacroParameters 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 5 with IncludeMacroParameters

use of org.xwiki.rendering.macro.include.IncludeMacroParameters 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)

Aggregations

IncludeMacroParameters (org.xwiki.rendering.macro.include.IncludeMacroParameters)8 Expectations (org.jmock.Expectations)7 Test (org.junit.Test)7 DocumentReference (org.xwiki.model.reference.DocumentReference)7 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)6 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)5 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)4 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)4 Block (org.xwiki.rendering.block.Block)3 MacroBlock (org.xwiki.rendering.block.MacroBlock)3 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Invocation (org.jmock.api.Invocation)1 CustomAction (org.jmock.lib.action.CustomAction)1 DocumentDisplayer (org.xwiki.display.internal.DocumentDisplayer)1 DocumentDisplayerParameters (org.xwiki.display.internal.DocumentDisplayerParameters)1 MacroTransformation (org.xwiki.rendering.internal.transformation.macro.MacroTransformation)1 MetaData (org.xwiki.rendering.listener.MetaData)1