Search in sources :

Example 1 with Transformation

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));
}
Also used : RenderingContext(org.xwiki.rendering.transformation.RenderingContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) Transformation(org.xwiki.rendering.transformation.Transformation) XDOM(org.xwiki.rendering.block.XDOM) StringReader(java.io.StringReader) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) HTMLCleaner(org.xwiki.wysiwyg.cleaner.HTMLCleaner) Parser(org.xwiki.rendering.parser.Parser) StreamParser(org.xwiki.rendering.parser.StreamParser) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) Test(org.junit.Test)

Example 2 with Transformation

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());
    }
}
Also used : Transformation(org.xwiki.rendering.transformation.Transformation) TransformationException(org.xwiki.rendering.transformation.TransformationException) XDOM(org.xwiki.rendering.block.XDOM) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) ContentParser(org.xwiki.rendering.parser.ContentParser) ContentExecutorException(org.xwiki.rendering.executor.ContentExecutorException) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) Test(org.junit.Test)

Example 3 with Transformation

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);
}
Also used : Transformation(org.xwiki.rendering.transformation.Transformation) XDOM(org.xwiki.rendering.block.XDOM) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) ContentParser(org.xwiki.rendering.parser.ContentParser) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) Test(org.junit.Test)

Example 4 with Transformation

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);
}
Also used : Transformation(org.xwiki.rendering.transformation.Transformation) XDOM(org.xwiki.rendering.block.XDOM) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) ContentParser(org.xwiki.rendering.parser.ContentParser) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) Test(org.junit.Test)

Example 5 with Transformation

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);
}
Also used : Expectations(org.jmock.Expectations) MacroTransformation(org.xwiki.rendering.internal.transformation.macro.MacroTransformation) Transformation(org.xwiki.rendering.transformation.Transformation) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroTransformation(org.xwiki.rendering.internal.transformation.macro.MacroTransformation)

Aggregations

Transformation (org.xwiki.rendering.transformation.Transformation)7 XDOM (org.xwiki.rendering.block.XDOM)6 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)6 Test (org.junit.Test)5 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)5 MutableRenderingContext (org.xwiki.rendering.internal.transformation.MutableRenderingContext)3 ContentParser (org.xwiki.rendering.parser.ContentParser)3 RenderingContext (org.xwiki.rendering.transformation.RenderingContext)3 StringReader (java.io.StringReader)2 HashMap (java.util.HashMap)2 Parser (org.xwiki.rendering.parser.Parser)2 StreamParser (org.xwiki.rendering.parser.StreamParser)2 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)2 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)2 Map (java.util.Map)1 Expectations (org.jmock.Expectations)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 Execution (org.xwiki.context.Execution)1 ExecutionContext (org.xwiki.context.ExecutionContext)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1