use of org.xwiki.rendering.transformation.MacroTransformationContext in project xwiki-platform by xwiki.
the class DisplayMacroTest method createMacroTransformationContext.
private MacroTransformationContext createMacroTransformationContext(String documentName, boolean isInline) {
MacroTransformationContext context = new MacroTransformationContext();
MacroBlock displayMacro = new MacroBlock("display", Collections.singletonMap("reference", documentName), isInline);
context.setCurrentMacroBlock(displayMacro);
return context;
}
use of org.xwiki.rendering.transformation.MacroTransformationContext in project xwiki-platform by xwiki.
the class DisplayMacroTest method testDisplayMacroWithRecursiveDisplay.
@Test
public void testDisplayMacroWithRecursiveDisplay() throws Exception {
final DocumentDisplayer mockDocumentDisplayer = getMockery().mock(DocumentDisplayer.class);
this.displayMacro.setDocumentAccessBridge(mockSetup.bridge);
FieldUtils.writeField(this.displayMacro, "documentDisplayer", mockDocumentDisplayer, true);
final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);
final DisplayMacroParameters parameters = new DisplayMacroParameters();
parameters.setReference("wiki:space.page");
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 {
displayMacro.execute(parameters, null, macroContext);
} catch (Exception expected) {
if (expected.getMessage().contains("Found recursive display")) {
throw new ExpectedRecursiveInclusionException();
}
}
return true;
}
});
}
});
try {
this.displayMacro.execute(parameters, null, macroContext);
Assert.fail("The display macro hasn't checked the recursive display");
} catch (MacroExecutionException expected) {
if (!(expected.getCause() instanceof ExpectedRecursiveInclusionException)) {
throw expected;
}
}
}
use of org.xwiki.rendering.transformation.MacroTransformationContext in project xwiki-platform by xwiki.
the class DisplayMacroTest method testDisplayMacroInsideBaseMetaDataBlockAndWithRelativeDocumentReferencePassed.
@Test
public void testDisplayMacroInsideBaseMetaDataBlockAndWithRelativeDocumentReferencePassed() throws Exception {
String expected = "beginDocument\n" + "beginMetaData [[base]=[wiki:space.relativePage][source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "beginParagraph\n" + "onWord [content]\n" + "endParagraph\n" + "endMetaData [[base]=[wiki:space.relativePage][source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "endDocument";
DisplayMacroParameters parameters = new DisplayMacroParameters();
parameters.setReference("relativePage");
MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
// Add a Source MetaData Block as a parent of the display 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");
setUpDocumentMock("relativePage", resolvedReference, "content");
getMockery().checking(new Expectations() {
{
allowing(mockDocumentReferenceResolver).resolve(with("wiki:space.page"), with(IsArray.array(any(MacroBlock.class))));
will(returnValue(sourceReference));
allowing(mockDocumentReferenceResolver).resolve(with("relativePage"), with(IsArray.array(any(MacroBlock.class))));
will(returnValue(resolvedReference));
oneOf(mockSetup.bridge).isDocumentViewable(resolvedReference);
will(returnValue(true));
oneOf(mockSetup.bridge).pushDocumentInContext(with(any(Map.class)), with(same(mockDocument)));
oneOf(mockSetup.bridge).getCurrentDocumentReference();
will(returnValue(resolvedReference));
oneOf(mockSetup.bridge).popDocumentFromContext(with(any(Map.class)));
}
});
List<Block> blocks = this.displayMacro.execute(parameters, null, macroContext);
assertBlocks(expected, blocks, this.rendererFactory);
}
use of org.xwiki.rendering.transformation.MacroTransformationContext in project xwiki-platform by xwiki.
the class DashboardMacroTest method executeWhenInsideDashboardMacro.
@Test
public void executeWhenInsideDashboardMacro() throws Exception {
BeanManager beanManager = this.mocker.getInstance(BeanManager.class);
BeanDescriptor descriptor = mock(BeanDescriptor.class);
when(beanManager.getBeanDescriptor(any())).thenReturn(descriptor);
when(descriptor.getProperties()).thenReturn(Collections.emptyList());
Execution execution = this.mocker.getInstance(Execution.class);
ExecutionContext ec = new ExecutionContext();
when(execution.getContext()).thenReturn(ec);
ec.setProperty("dashboardMacroCalls", 1);
DashboardMacroParameters parameters = new DashboardMacroParameters();
MacroTransformationContext macroContext = new MacroTransformationContext();
try {
this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
fail("Exception should have been raised here");
} catch (MacroExecutionException expected) {
assertEquals("Dashboard macro recursion detected. Don't call the Dashboard macro inside of itself...", expected.getMessage());
}
}
use of org.xwiki.rendering.transformation.MacroTransformationContext in project xwiki-platform by xwiki.
the class DashboardMacroTest method executeWhenNotInsideDashboardMacro.
@Test
public void executeWhenNotInsideDashboardMacro() throws Exception {
BeanManager beanManager = this.mocker.getInstance(BeanManager.class);
BeanDescriptor descriptor = mock(BeanDescriptor.class);
when(beanManager.getBeanDescriptor(any())).thenReturn(descriptor);
when(descriptor.getProperties()).thenReturn(Collections.emptyList());
DashboardRenderer renderer = this.mocker.registerMockComponent(DashboardRenderer.class, "columns");
GadgetRenderer gadgetRenderer = this.mocker.registerMockComponent(GadgetRenderer.class);
Execution execution = this.mocker.getInstance(Execution.class);
ExecutionContext ec = new ExecutionContext();
when(execution.getContext()).thenReturn(ec);
DashboardMacroParameters parameters = new DashboardMacroParameters();
MacroTransformationContext macroContext = new MacroTransformationContext();
this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
// We verify that the counter ends up at 0 so that calls to subsequent dashboard macros can succeed.
assertEquals(0, ec.getProperty("dashboardMacroCalls"));
}
Aggregations