Search in sources :

Example 51 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge 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)

Example 52 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class SheetDocumentDisplayerTest method testPreserveSheetPRWhenDocumentIsOnContext.

/**
 * Tests if the programming rights of the sheet are preserved when the document is already on the context.
 *
 * @throws Exception if something wrong happens
 */
@Test
public void testPreserveSheetPRWhenDocumentIsOnContext() throws Exception {
    DocumentModelBridge document = mockDocument(DOCUMENT_REFERENCE);
    DocumentModelBridge sheet = mockDocument(SHEET_REFERENCE);
    setCurrentDocument(document);
    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);
}
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) SheetManager(org.xwiki.sheet.SheetManager) Test(org.junit.Test)

Example 53 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class SheetDocumentDisplayerTest method mockDocument.

/**
 * Creates a mock {@link DocumentModelBridge} that has the specified reference.
 *
 * @param documentReference the document reference
 * @return the mock {@link DocumentModelBridge}
 * @throws Exception if creating the mock fails
 */
private DocumentModelBridge mockDocument(DocumentReference documentReference) throws Exception {
    StringBuilder id = new StringBuilder(documentReference.getLastSpaceReference().getName());
    // Allow different instances of the same document to exist.
    id.append('.').append(documentReference.getName()).append(RandomStringUtils.randomAlphanumeric(3));
    DocumentModelBridge document = mock(DocumentModelBridge.class, id.toString());
    when(document.getDocumentReference()).thenReturn(documentReference);
    when(this.documentAccessBridge.getTranslatedDocumentInstance(documentReference)).thenReturn(document);
    when(this.modelBridge.getDefaultEditMode(document)).thenReturn("edit");
    when(this.modelBridge.getDefaultTranslation(document)).thenReturn(document);
    return document;
}
Also used : DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge)

Example 54 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class DefaultSheetManager method getClassSheets.

/**
 * @param classReference a reference to a XWiki class
 * @param action the action for which to retrieve the class sheets
 * @return the list of sheets bound to the specified class and matching the specified action; these are sheets
 *         designed to be displayed when the specified action is performed on a document holding an object of the
 *         specified class
 */
private List<DocumentReference> getClassSheets(DocumentReference classReference, String action) {
    DocumentModelBridge classDocument;
    try {
        classDocument = documentAccessBridge.getTranslatedDocumentInstance(classReference);
    } catch (Exception e) {
        String classStringReference = defaultEntityReferenceSerializer.serialize(classReference);
        logger.warn("Failed to get class sheets for [{}]. Reason: [{}]", classStringReference, e.getMessage());
        return Collections.emptyList();
    }
    List<DocumentReference> sheetReferences = new ArrayList<DocumentReference>();
    for (DocumentReference sheetReference : classSheetBinder.getSheets(classDocument)) {
        if (matchSheet(sheetReference, action)) {
            sheetReferences.add(sheetReference);
        }
    }
    return sheetReferences;
}
Also used : DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) ArrayList(java.util.ArrayList) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 55 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class DefaultWikiComponentManagerEventListenerTest method onEventWhenSourceDocumentButNoMatchingEvent.

@Test
public void onEventWhenSourceDocumentButNoMatchingEvent() throws Exception {
    DocumentModelBridge componentDocument = mock(DocumentModelBridge.class);
    when(componentDocument.getDocumentReference()).thenReturn(DOC_REFERENCE);
    mocker.getComponentUnderTest().onEvent(null, componentDocument, null);
    verify(this.wikiComponentManagerEventListenerHelper, never()).registerComponentList(any());
    verify(this.wikiComponentManagerEventListenerHelper, never()).unregisterComponents(any(EntityReference.class));
}
Also used : DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) EntityReference(org.xwiki.model.reference.EntityReference) Test(org.junit.Test)

Aggregations

DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)58 DocumentReference (org.xwiki.model.reference.DocumentReference)43 Test (org.junit.Test)39 Expectations (org.jmock.Expectations)18 XDOM (org.xwiki.rendering.block.XDOM)18 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)14 Syntax (org.xwiki.rendering.syntax.Syntax)11 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)10 MacroBlock (org.xwiki.rendering.block.MacroBlock)9 DocumentDisplayerParameters (org.xwiki.display.internal.DocumentDisplayerParameters)8 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)7 Block (org.xwiki.rendering.block.Block)6 HashMap (java.util.HashMap)5 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)5 DocumentDisplayer (org.xwiki.display.internal.DocumentDisplayer)5 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)5 MetaData (org.xwiki.rendering.listener.MetaData)5 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)5 StringReader (java.io.StringReader)4