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;
}
}
}
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;
}
}
}
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);
}
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);
}
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());
}
}
Aggregations