Search in sources :

Example 1 with MacroMarkerBlock

use of org.xwiki.rendering.block.MacroMarkerBlock in project xwiki-platform by xwiki.

the class EmptyXDOMCheckerTest method checkWhenTwoMacroMarkerBlock.

@Test
public void checkWhenTwoMacroMarkerBlock() throws Exception {
    List<Block> blocks = Arrays.asList(new MacroMarkerBlock("macro", Collections.emptyMap(), Collections.emptyList(), false), new MacroMarkerBlock("macro2", Collections.emptyMap(), Collections.emptyList(), false));
    assertTrue(this.mocker.getComponentUnderTest().check(blocks));
}
Also used : MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) Test(org.junit.Test)

Example 2 with MacroMarkerBlock

use of org.xwiki.rendering.block.MacroMarkerBlock in project xwiki-platform by xwiki.

the class EmptyXDOMCheckerTest method checkWhenSingleMacroMarkerBlock.

@Test
public void checkWhenSingleMacroMarkerBlock() throws Exception {
    List<Block> blocks = Collections.singletonList(new MacroMarkerBlock("macro", Collections.emptyMap(), Collections.emptyList(), false));
    assertTrue(this.mocker.getComponentUnderTest().check(blocks));
}
Also used : MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) Test(org.junit.Test)

Example 3 with MacroMarkerBlock

use of org.xwiki.rendering.block.MacroMarkerBlock in project xwiki-platform by xwiki.

the class IncludeMacro method checkRecursiveInclusion.

/**
 * Protect form recursive inclusion.
 *
 * @param currrentBlock the child block to check
 * @param documentReference the reference of the document being included
 * @throws MacroExecutionException recursive inclusion has been found
 */
private void checkRecursiveInclusion(Block currrentBlock, DocumentReference documentReference) throws MacroExecutionException {
    // Check for parent context=new macros
    Stack<Object> references = this.inclusionsBeingExecuted.get();
    if (references != null && references.contains(documentReference)) {
        throw new MacroExecutionException("Found recursive inclusion of document [" + documentReference + "]");
    }
    // Check for parent context=current macros
    Block parentBlock = currrentBlock.getParent();
    if (parentBlock != null) {
        if (parentBlock instanceof MacroMarkerBlock) {
            MacroMarkerBlock parentMacro = (MacroMarkerBlock) parentBlock;
            if (isRecursive(parentMacro, documentReference)) {
                throw new MacroExecutionException("Found recursive inclusion of document [" + documentReference + "]");
            }
        }
        checkRecursiveInclusion(parentBlock, documentReference);
    }
}
Also used : MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 4 with MacroMarkerBlock

use of org.xwiki.rendering.block.MacroMarkerBlock in project xwiki-platform by xwiki.

the class NestedScriptMacroValidatorTest method buildContext.

/**
 * Build a chain of nested macros ({@link MacroMarkerBlock} blocks) and put them into a macro transformation
 * context. The chain will be the only child of a top level XDOM, the last child will be the current
 * {@link MacroBlock}.
 *
 * @param chain list of nested macro names (starting with parent)
 * @return an initialized macro transformation context
 */
private MacroTransformationContext buildContext(String... chain) {
    MacroTransformationContext context = new MacroTransformationContext();
    if (chain == null || chain.length < 1) {
        context.setXDOM(new XDOM(new LinkedList<Block>()));
        return context;
    }
    Map<String, String> parameters = new HashMap<String, String>();
    MacroBlock child = new MacroBlock(chain[chain.length - 1], parameters, false);
    Block current = child;
    for (int i = chain.length - 2; i >= 0; i--) {
        Block parent = new MacroMarkerBlock(chain[i], parameters, Collections.singletonList(current), false);
        current = parent;
    }
    XDOM root = new XDOM(Collections.singletonList(current));
    context.setXDOM(root);
    context.setCurrentMacroBlock(child);
    return context;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) LinkedList(java.util.LinkedList) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 5 with MacroMarkerBlock

use of org.xwiki.rendering.block.MacroMarkerBlock 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)

Aggregations

MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)12 Block (org.xwiki.rendering.block.Block)6 MacroBlock (org.xwiki.rendering.block.MacroBlock)6 Test (org.junit.Test)5 WordBlock (org.xwiki.rendering.block.WordBlock)4 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)4 XDOM (org.xwiki.rendering.block.XDOM)3 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)3 HashMap (java.util.HashMap)2 Expectations (org.jmock.Expectations)2 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)2 Reader (java.io.Reader)1 Writer (java.io.Writer)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Properties (java.util.Properties)1 VelocityContext (org.apache.velocity.VelocityContext)1 Mockery (org.jmock.Mockery)1 Invocation (org.jmock.api.Invocation)1 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)1