use of org.xwiki.rendering.transformation.MacroTransformationContext in project xwiki-platform by xwiki.
the class IncludeMacroTest method createMacroTransformationContext.
private MacroTransformationContext createMacroTransformationContext(String documentName, boolean isInline) {
MacroTransformationContext context = new MacroTransformationContext();
MacroBlock includeMacro = new MacroBlock("include", Collections.singletonMap("reference", documentName), isInline);
context.setCurrentMacroBlock(includeMacro);
return context;
}
use of org.xwiki.rendering.transformation.MacroTransformationContext 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);
}
use of org.xwiki.rendering.transformation.MacroTransformationContext 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());
}
}
use of org.xwiki.rendering.transformation.MacroTransformationContext 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);
}
use of org.xwiki.rendering.transformation.MacroTransformationContext in project xwiki-platform by xwiki.
the class CacheMacroTest method executeWhenNoIdAndSameContent.
@Test
public void executeWhenNoIdAndSameContent() throws Exception {
String expected = "beginDocument\n" + "beginMacroMarkerStandalone [velocity] [] [$var]\n" + "beginParagraph\n" + "onWord [content]\n" + "endParagraph\n" + "endMacroMarkerStandalone [velocity] [] [$var]\n" + "endDocument";
CacheMacroParameters params = new CacheMacroParameters();
MacroTransformationContext context = createMacroTransformationContext();
VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class);
StringWriter writer = new StringWriter();
velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template", "#set ($var = 'content')");
List<Block> result = this.cacheMacro.execute(params, "{{velocity}}$var{{/velocity}}", context);
assertBlocks(expected, result, this.rendererFactory);
// Execute a second time with a different value for the velocity $var variable to ensure the returned result
// is the cached one.
velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template", "#set ($var = 'newcontent')");
result = this.cacheMacro.execute(params, "{{velocity}}$var{{/velocity}}", context);
assertBlocks(expected, result, this.rendererFactory);
}
Aggregations