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