use of org.xwiki.rendering.transformation.Transformation in project xwiki-platform by xwiki.
the class DefaultHTMLConverterTest method parseAndRender.
/**
* Unit test for {@link DefaultHTMLConverter#parseAndRender(String, String)}.
*/
@Test
public void parseAndRender() throws Exception {
String html = "some HTML";
String syntaxId = "syntax/x.y";
// Verify the HTML is cleaned.
HTMLCleaner cleaner = mocker.getInstance(HTMLCleaner.class);
when(cleaner.clean(html)).thenReturn(html);
// Verify the HTML is parsed into XDOM.
XDOM xdom = new XDOM(Collections.<Block>emptyList());
Parser xhtmlParser = mocker.getInstance(Parser.class, "xhtml/1.0");
when(xhtmlParser.parse(any(StringReader.class))).thenReturn(xdom);
Assert.assertEquals("", mocker.getComponentUnderTest().parseAndRender(html, syntaxId));
// Verify that the macro transformations have been executed.
Transformation macroTransformation = mocker.getInstance(Transformation.class, "macro");
RenderingContext renderingContext = mocker.getInstance(RenderingContext.class);
// It's very important to verify that a transformation context id is set as otherwise if the content being
// edited has different velocity macros executing, they'll be executed in isolation and thus what's defined in
// one won't be visible from the other ones (For example see https://jira.xwiki.org/browse/XWIKI-11695).
ArgumentCaptor<TransformationContext> txContextArgument = ArgumentCaptor.forClass(TransformationContext.class);
verify((MutableRenderingContext) renderingContext).transformInContext(same(macroTransformation), txContextArgument.capture(), same(xdom));
assertEquals("wysiwygtxid", txContextArgument.getValue().getId());
// Verify the XDOM is rendered to Annotated XHTML.
BlockRenderer xhtmlRenderer = mocker.getInstance(BlockRenderer.class, "annotatedxhtml/1.0");
verify(xhtmlRenderer).render(same(xdom), any(WikiPrinter.class));
// Verify that the syntax meta data has been set.
Assert.assertEquals(Syntax.valueOf("syntax/x.y"), xdom.getMetaData().getMetaData(MetaData.SYNTAX));
}
use of org.xwiki.rendering.transformation.Transformation in project xwiki-platform by xwiki.
the class MacroContentExecutorTest method executeWhenTransformationException.
@Test
public void executeWhenTransformationException() throws Exception {
XDOM parsedBlocks = new XDOM(Collections.emptyList());
ContentParser contentParser = this.mocker.getInstance(ContentParser.class);
when(contentParser.parse("", Syntax.PLAIN_1_0)).thenReturn(parsedBlocks);
TransformationContext transformationContext = new TransformationContext();
MacroTransformationContext context = new MacroTransformationContext(transformationContext);
Transformation macroTransformation = this.mocker.getInstance(Transformation.class, "macro");
doThrow(new TransformationException("error")).when(macroTransformation).transform(parsedBlocks, transformationContext);
try {
this.mocker.getComponentUnderTest().execute("", Syntax.PLAIN_1_0, context);
fail("Should have raised a ContentExecutorException");
} catch (ContentExecutorException expected) {
assertEquals("Failed to execute content", expected.getMessage());
}
}
use of org.xwiki.rendering.transformation.Transformation in project xwiki-platform by xwiki.
the class MacroContentExecutorTest method executeWithSource.
@Test
public void executeWithSource() throws Exception {
XDOM parsedBlocks = new XDOM(Collections.emptyList());
ContentParser contentParser = this.mocker.getInstance(ContentParser.class);
when(contentParser.parse("", Syntax.PLAIN_1_0, DOCUMENT_REFERENCE)).thenReturn(parsedBlocks);
TransformationContext transformationContext = new TransformationContext();
MacroTransformationContext context = new MacroTransformationContext(transformationContext);
this.mocker.getComponentUnderTest().execute("", Syntax.PLAIN_1_0, DOCUMENT_REFERENCE, context);
// The test is here: Verify that the Macro Transformation has been called
Transformation macroTransformation = this.mocker.getInstance(Transformation.class, "macro");
verify(macroTransformation).transform(parsedBlocks, transformationContext);
}
use of org.xwiki.rendering.transformation.Transformation in project xwiki-platform by xwiki.
the class MacroContentExecutorTest method executeWithNoSource.
@Test
public void executeWithNoSource() throws Exception {
XDOM parsedBlocks = new XDOM(Collections.emptyList());
ContentParser contentParser = this.mocker.getInstance(ContentParser.class);
when(contentParser.parse("", Syntax.PLAIN_1_0)).thenReturn(parsedBlocks);
TransformationContext transformationContext = new TransformationContext();
MacroTransformationContext context = new MacroTransformationContext(transformationContext);
this.mocker.getComponentUnderTest().execute("", Syntax.PLAIN_1_0, context);
// The test is here: Verify that the Macro Transformation has been called
Transformation macroTransformation = this.mocker.getInstance(Transformation.class, "macro");
verify(macroTransformation).transform(parsedBlocks, transformationContext);
}
use of org.xwiki.rendering.transformation.Transformation in project xwiki-platform by xwiki.
the class DisplayMacroTest method runDisplayMacro.
private List<Block> runDisplayMacro(DisplayMacroParameters parameters, String displayedContent) throws Exception {
final DocumentReference displayedDocumentReference = new DocumentReference("wiki", "Space", "DisplayedPage");
String displayedDocStringRef = "wiki:space.page";
setUpDocumentMock(displayedDocStringRef, displayedDocumentReference, displayedContent);
getMockery().checking(new Expectations() {
{
allowing(mockSetup.bridge).isDocumentViewable(with(same(displayedDocumentReference)));
will(returnValue(true));
oneOf(mockSetup.bridge).pushDocumentInContext(with(any(Map.class)), with(same(mockDocument)));
atMost(1).of(mockSetup.bridge).getCurrentDocumentReference();
will(returnValue(displayedDocumentReference));
oneOf(mockSetup.bridge).popDocumentFromContext(with(any(Map.class)));
}
});
this.displayMacro.setDocumentAccessBridge(this.mockSetup.bridge);
parameters.setReference(displayedDocStringRef);
// Create a Macro transformation context with the Macro transformation object defined so that the display
// macro can transform displayed page which is using a new context.
MacroTransformation macroTransformation = (MacroTransformation) getComponentManager().getInstance(Transformation.class, "macro");
MacroTransformationContext macroContext = createMacroTransformationContext(displayedDocStringRef, false);
macroContext.setId("wiki:Space.DisplayingPage");
macroContext.setTransformation(macroTransformation);
return this.displayMacro.execute(parameters, null, macroContext);
}
Aggregations