Search in sources :

Example 11 with MacroBlock

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

the class ContextMacroTest method executeWithReferencedDocumentHavingProgrammingRightsButNotTheCallingDocument.

@Test
public void executeWithReferencedDocumentHavingProgrammingRightsButNotTheCallingDocument() throws Exception {
    MacroTransformationContext macroContext = new MacroTransformationContext();
    MacroBlock macroBlock = new MacroBlock("context", Collections.emptyMap(), false);
    macroContext.setCurrentMacroBlock(macroBlock);
    DocumentReferenceResolver<String> resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "macro");
    when(resolver.resolve("wiki:space.page", macroBlock)).thenReturn(new DocumentReference("wiki", "space", "page"));
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    when(dab.hasProgrammingRights()).thenReturn(false).thenReturn(true);
    ContextMacroParameters parameters = new ContextMacroParameters();
    parameters.setDocument("wiki:space.page");
    try {
        this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
        fail("Should have thrown an exception");
    } catch (MacroExecutionException expected) {
        assertEquals("Current document must have programming rights since the context document provided [" + "wiki:space.page] has programming rights.", expected.getMessage());
    }
}
Also used : MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) ContextMacroParameters(org.xwiki.rendering.macro.context.ContextMacroParameters) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 12 with MacroBlock

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

the class ContextMacroTest method executeWithRelativeDocumentReferenceParameter.

@Test
public void executeWithRelativeDocumentReferenceParameter() throws Exception {
    MacroBlock macroBlock = new MacroBlock("context", Collections.<String, String>emptyMap(), false);
    MacroTransformationContext macroContext = new MacroTransformationContext();
    macroContext.setSyntax(Syntax.XWIKI_2_0);
    macroContext.setCurrentMacroBlock(macroBlock);
    DocumentReferenceResolver<String> resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "macro");
    DocumentReference referencedDocumentReference = new DocumentReference("basewiki", "basespace", "page");
    when(resolver.resolve("page", macroBlock)).thenReturn(referencedDocumentReference);
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    DocumentModelBridge dmb = mock(DocumentModelBridge.class);
    when(dab.getTranslatedDocumentInstance(referencedDocumentReference)).thenReturn(dmb);
    MacroContentParser parser = this.mocker.getInstance(MacroContentParser.class);
    when(parser.parse(eq(""), same(macroContext), eq(false), any(MetaData.class), eq(false))).thenReturn(new XDOM(Collections.emptyList()));
    ContextMacroParameters parameters = new ContextMacroParameters();
    parameters.setDocument("page");
    this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MacroContentParser(org.xwiki.rendering.macro.MacroContentParser) MetaData(org.xwiki.rendering.listener.MetaData) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ContextMacroParameters(org.xwiki.rendering.macro.context.ContextMacroParameters) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 13 with MacroBlock

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

the class DocumentTableBlockDataSourceTest method isDefinedChartSourceTheCurrentDocumentWhenReferenceNotNullAndMatching.

@Test
public void isDefinedChartSourceTheCurrentDocumentWhenReferenceNotNullAndMatching() throws Exception {
    DocumentAccessBridge dab = this.componentManager.getInstance(DocumentAccessBridge.class);
    DocumentReference currentReference = new DocumentReference("currentwiki", "currentspace", "currentpage");
    when(dab.getCurrentDocumentReference()).thenReturn(currentReference);
    DocumentReferenceResolver<String> resolver = this.componentManager.getInstance(DocumentReferenceResolver.TYPE_STRING);
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    when(resolver.resolve("wiki:space.page", currentReference)).thenReturn(documentReference);
    MacroBlock currentMacroBlock = mock(MacroBlock.class);
    MetaDataBlock metaDataBlock = new MetaDataBlock(Collections.EMPTY_LIST, new MetaData(Collections.singletonMap(MetaData.SOURCE, (Object) "wiki:space.page")));
    when(currentMacroBlock.getFirstBlock(any(BlockMatcher.class), any(Block.Axes.class))).thenReturn(metaDataBlock);
    DocumentTableBlockDataSource source = this.componentManager.getComponentUnderTest();
    source.setParameter("document", "wiki:space.page");
    Assert.assertTrue(source.isDefinedChartSourceTheCurrentDocument(currentMacroBlock));
}
Also used : MetaData(org.xwiki.rendering.listener.MetaData) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) BlockMatcher(org.xwiki.rendering.block.match.BlockMatcher) MacroBlock(org.xwiki.rendering.block.MacroBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) Test(org.junit.Test)

Example 14 with MacroBlock

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

the class MacroBlockDumper method dump.

/**
 * {@inheritDoc}
 *
 * The dump contains all the macro parameters and the content of the macro.
 * The source MetaData is also included if available.
 */
@Override
public void dump(OutputStream out, Block block) throws IOException {
    if (block instanceof MacroBlock) {
        MacroBlock b = (MacroBlock) block;
        dump(out, b.getId(), b.getParameters(), b.getContent());
    } else if (block instanceof MacroMarkerBlock) {
        MacroMarkerBlock b = (MacroMarkerBlock) block;
        dump(out, b.getId(), b.getParameters(), b.getContent());
    } else {
        throw new IllegalArgumentException("Unsupported block [" + block.getClass().getName() + "].");
    }
    String source = getSourceReference(block);
    if (source != null) {
        out.write(toBytes(source));
    }
    out.write(0x00);
}
Also used : MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 15 with MacroBlock

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

the class MacroBlockDumperTest method testDumpMacroBlockWithSource.

@Test
public void testDumpMacroBlockWithSource() throws Exception {
    Block macro1 = new MacroBlock("macro", params, "content", false);
    Block macro2 = new MacroBlock("macro", params, "content", false);
    XDOM xdom1 = new XDOM(Collections.singletonList(macro1));
    XDOM xdom2 = new XDOM(Collections.singletonList(macro2));
    xdom1.getMetaData().addMetaData(MetaData.SOURCE, "source");
    xdom2.getMetaData().addMetaData(MetaData.SOURCE, "source");
    byte[] dump1 = dumper.dump(macro1);
    byte[] dump2 = dumper.dump(macro2);
    assertThat(dump1, equalTo(dump2));
    dump2 = dumper.dump(new MacroBlock("macro", params, "content", false));
    assertThat(dump1, not(equalTo(dump2)));
    xdom2.getMetaData().addMetaData(MetaData.SOURCE, "other");
    dump2 = dumper.dump(macro2);
    assertThat(dump1, not(equalTo(dump2)));
    xdom2.getMetaData().addMetaData(MetaData.SOURCE, null);
    dump1 = dumper.dump(new MacroBlock("macro", params, "content", false));
    dump2 = dumper.dump(macro2);
    assertThat(dump1, equalTo(dump2));
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) MacroBlock(org.xwiki.rendering.block.MacroBlock) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Aggregations

MacroBlock (org.xwiki.rendering.block.MacroBlock)30 Test (org.junit.Test)14 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)13 XDOM (org.xwiki.rendering.block.XDOM)12 DocumentReference (org.xwiki.model.reference.DocumentReference)9 Block (org.xwiki.rendering.block.Block)8 LinkBlock (org.xwiki.rendering.block.LinkBlock)7 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)7 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 HashMap (java.util.HashMap)5 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)5 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)4 MetaData (org.xwiki.rendering.listener.MetaData)4 ContextMacroParameters (org.xwiki.rendering.macro.context.ContextMacroParameters)4 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)3 WordBlock (org.xwiki.rendering.block.WordBlock)3 MacroContentParser (org.xwiki.rendering.macro.MacroContentParser)3 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)3 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 ToString (org.suigeneris.jrcs.util.ToString)2