Search in sources :

Example 1 with DocumentDisplayer

use of org.xwiki.display.internal.DocumentDisplayer 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;
        }
    }
}
Also used : Expectations(org.jmock.Expectations) DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) DocumentDisplayer(org.xwiki.display.internal.DocumentDisplayer) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DisplayMacroParameters(org.xwiki.rendering.macro.display.DisplayMacroParameters) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Test(org.junit.Test)

Example 2 with DocumentDisplayer

use of org.xwiki.display.internal.DocumentDisplayer 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;
        }
    }
}
Also used : Expectations(org.jmock.Expectations) DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) DocumentDisplayer(org.xwiki.display.internal.DocumentDisplayer) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Test(org.junit.Test)

Example 3 with DocumentDisplayer

use of org.xwiki.display.internal.DocumentDisplayer in project xwiki-platform by xwiki.

the class IntegrationTests method initialize.

@RenderingTestSuite.Initialized
public void initialize(MockitoComponentManager componentManager) throws Exception {
    ModelContext modelContext = componentManager.registerMockComponent(ModelContext.class);
    when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("currentWiki"));
    // Document Access Bridge mock
    DocumentAccessBridge dab = componentManager.registerMockComponent(DocumentAccessBridge.class);
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    DocumentReference currentDocumentReference = new DocumentReference("currentwiki", "currentspace", "currentpage");
    DocumentModelBridge document = Mockito.mock(DocumentModelBridge.class);
    when(dab.getDocumentURL(new DocumentReference("currentWiki", "space", "page"), "temp", null, null)).thenReturn("temppath");
    when(dab.getCurrentDocumentReference()).thenReturn(currentDocumentReference);
    when(dab.exists(documentReference)).thenReturn(true);
    when(dab.getDocumentInstance(documentReference)).thenReturn(document);
    when(dab.getCurrentUserReference()).thenReturn(null);
    DocumentDisplayer displayer = componentManager.registerMockComponent(DocumentDisplayer.class);
    Parser parser = componentManager.getInstance(Parser.class, Syntax.XWIKI_2_0.toIdString());
    final XDOM xdom = parser.parse(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(WIKI_CONTENT_FILE)));
    when(displayer.display(eq(document), any(DocumentDisplayerParameters.class))).thenReturn(xdom);
    AuthorizationManager authorizationManager = componentManager.registerMockComponent(AuthorizationManager.class);
    when(authorizationManager.hasAccess(Right.VIEW, null, documentReference)).thenReturn(true);
    componentManager.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "current");
    componentManager.registerMockComponent(SpaceReferenceResolver.TYPE_STRING, "current");
}
Also used : ModelContext(org.xwiki.model.ModelContext) DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) DocumentDisplayer(org.xwiki.display.internal.DocumentDisplayer) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) InputStreamReader(java.io.InputStreamReader) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Parser(org.xwiki.rendering.parser.Parser)

Example 4 with DocumentDisplayer

use of org.xwiki.display.internal.DocumentDisplayer in project xwiki-platform by xwiki.

the class DisplayScriptService method document.

/**
 * Displays a document.
 *
 * @param document the document to display
 * @param parameters the display parameters
 * @return the result of displaying the given document
 */
private String document(Document document, Map<String, Object> parameters, Syntax outputSyntax) {
    DocumentDisplayerParameters displayerParameters = (DocumentDisplayerParameters) parameters.get(DISPLAYER_PARAMETERS_KEY);
    if (displayerParameters == null) {
        displayerParameters = new DocumentDisplayerParameters();
        displayerParameters.setTargetSyntax(outputSyntax);
    }
    String displayerHint = (String) parameters.get("displayerHint");
    if (displayerHint == null) {
        displayerHint = "configured";
    }
    try {
        DocumentDisplayer displayer = this.componentManager.getInstance(DocumentDisplayer.class, displayerHint);
        return renderXDOM(displayer.display(getDocument(document), displayerParameters), outputSyntax);
    } catch (Exception e) {
        this.logger.error("Failed to display document [{}].", document.getPrefixedFullName(), e);
        return null;
    }
}
Also used : DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) DocumentDisplayer(org.xwiki.display.internal.DocumentDisplayer) XWikiException(com.xpn.xwiki.XWikiException)

Example 5 with DocumentDisplayer

use of org.xwiki.display.internal.DocumentDisplayer in project xwiki-platform by xwiki.

the class SheetDocumentDisplayerTest method testPreserveSheetPRWhenDocumentIsNotOnContext.

/**
 * Tests if the programming rights of the sheet are preserved when the document is not on the context.
 *
 * @throws Exception if something wrong happens
 */
@Test
public void testPreserveSheetPRWhenDocumentIsNotOnContext() throws Exception {
    DocumentModelBridge document = mockDocument(DOCUMENT_REFERENCE);
    DocumentModelBridge sheet = mockDocument(SHEET_REFERENCE);
    // We test that the displayed document is put on the context even if the current document is just a different
    // instance of the displayed document. This is needed because the displayed document can have unsaved changes.
    setCurrentDocument(mockDocument(DOCUMENT_REFERENCE));
    // The sheet must be determined and displayed in a new execution context that has the target document as
    // the current document.
    Map<String, Object> backupObjects = new HashMap<String, Object>();
    when(this.modelBridge.pushDocumentInContext(document)).thenReturn(backupObjects);
    SheetManager sheetManager = this.mocker.getInstance(SheetManager.class);
    when(sheetManager.getSheets(document, "view")).thenReturn(Collections.singletonList(SHEET_REFERENCE));
    DocumentModelBridge originalSecurityDoc = mock(DocumentModelBridge.class, "sdoc");
    // Required in order to preserve the programming rights of the sheet.
    when(this.modelBridge.setSecurityDocument(sheet)).thenReturn(originalSecurityDoc);
    XDOM output = new XDOM(Collections.<Block>emptyList());
    DocumentDisplayer documentDisplayer = this.mocker.getInstance(DocumentDisplayer.class);
    when(documentDisplayer.display(eq(sheet), any(DocumentDisplayerParameters.class))).thenReturn(output);
    assertSame(output, this.mocker.getComponentUnderTest().display(document, new DocumentDisplayerParameters()));
    // The security document must be reverted.
    verify(this.modelBridge).setSecurityDocument(originalSecurityDoc);
    // The previous execution context must be restored.
    verify(this.documentAccessBridge).popDocumentFromContext(backupObjects);
}
Also used : DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) DocumentDisplayer(org.xwiki.display.internal.DocumentDisplayer) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) HashMap(java.util.HashMap) SheetManager(org.xwiki.sheet.SheetManager) Test(org.junit.Test)

Aggregations

DocumentDisplayer (org.xwiki.display.internal.DocumentDisplayer)6 DocumentDisplayerParameters (org.xwiki.display.internal.DocumentDisplayerParameters)6 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)5 Test (org.junit.Test)4 DocumentReference (org.xwiki.model.reference.DocumentReference)3 XDOM (org.xwiki.rendering.block.XDOM)3 Expectations (org.jmock.Expectations)2 Invocation (org.jmock.api.Invocation)2 CustomAction (org.jmock.lib.action.CustomAction)2 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)2 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)2 SheetManager (org.xwiki.sheet.SheetManager)2 XWikiException (com.xpn.xwiki.XWikiException)1 InputStreamReader (java.io.InputStreamReader)1 HashMap (java.util.HashMap)1 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)1 ModelContext (org.xwiki.model.ModelContext)1 WikiReference (org.xwiki.model.reference.WikiReference)1 DisplayMacroParameters (org.xwiki.rendering.macro.display.DisplayMacroParameters)1 IncludeMacroParameters (org.xwiki.rendering.macro.include.IncludeMacroParameters)1