Search in sources :

Example 1 with XWikiRecycleBinStoreInterface

use of com.xpn.xwiki.store.XWikiRecycleBinStoreInterface in project xwiki-platform by xwiki.

the class DocumentEventConverter method unserializeDeletdDocument.

private XWikiDocument unserializeDeletdDocument(Serializable remoteData, XWikiContext xcontext) throws XWikiException {
    Map<String, Serializable> remoteDataMap = (Map<String, Serializable>) remoteData;
    DocumentReference docReference = (DocumentReference) remoteDataMap.get(DOC_NAME);
    XWikiDocument doc = new XWikiDocument(docReference);
    XWikiDocument origDoc = new XWikiDocument(docReference);
    // We have to get deleted document from the trash (hoping it is in the trash...)
    XWiki xwiki = xcontext.getWiki();
    XWikiRecycleBinStoreInterface store = xwiki.getRecycleBinStore();
    XWikiDeletedDocument[] deletedDocuments = store.getAllDeletedDocuments(origDoc, xcontext, true);
    if (deletedDocuments != null && deletedDocuments.length > 0) {
        long index = deletedDocuments[0].getId();
        origDoc = store.restoreFromRecycleBin(index, xcontext, true);
    }
    doc.setOriginalDocument(origDoc);
    return doc;
}
Also used : Serializable(java.io.Serializable) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiRecycleBinStoreInterface(com.xpn.xwiki.store.XWikiRecycleBinStoreInterface) XWiki(com.xpn.xwiki.XWiki) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 2 with XWikiRecycleBinStoreInterface

use of com.xpn.xwiki.store.XWikiRecycleBinStoreInterface in project xwiki-platform by xwiki.

the class XWikiMockitoTest method deleteAllDocumentsAndWithoutSendingToTrash.

@Test
public void deleteAllDocumentsAndWithoutSendingToTrash() throws Exception {
    XWiki xwiki = new XWiki();
    XWikiDocument document = mock(XWikiDocument.class);
    DocumentReference reference = new DocumentReference("wiki", "space", "page");
    when(document.getDocumentReference()).thenReturn(reference);
    // Make sure we have a trash for the test.
    XWikiRecycleBinStoreInterface recycleBinStoreInterface = mock(XWikiRecycleBinStoreInterface.class);
    xwiki.setRecycleBinStore(recycleBinStoreInterface);
    when(xwikiCfgConfigurationSource.getProperty("xwiki.recyclebin", "1")).thenReturn("1");
    // Configure the mocked Store to later verify if it's called
    XWikiStoreInterface storeInterface = mock(XWikiStoreInterface.class);
    xwiki.setStore(storeInterface);
    XWikiContext xwikiContext = mock(XWikiContext.class);
    xwiki.deleteAllDocuments(document, false, xwikiContext);
    // Verify that saveToRecycleBin is never called since otherwise it would mean the doc has been saved in the
    // trash
    verify(recycleBinStoreInterface, never()).saveToRecycleBin(any(XWikiDocument.class), any(String.class), any(Date.class), any(XWikiContext.class), any(Boolean.class));
    // Verify that deleteXWikiDoc() is called
    verify(storeInterface).deleteXWikiDoc(document, xwikiContext);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiRecycleBinStoreInterface(com.xpn.xwiki.store.XWikiRecycleBinStoreInterface) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiStoreInterface(com.xpn.xwiki.store.XWikiStoreInterface) Date(java.util.Date) Test(org.junit.Test)

Example 3 with XWikiRecycleBinStoreInterface

use of com.xpn.xwiki.store.XWikiRecycleBinStoreInterface in project xwiki-platform by xwiki.

the class DefaultModelBridgeTest method canRestoreDeletedDocument.

@Test
public void canRestoreDeletedDocument() throws Exception {
    long deletedDocumentId = 42;
    String deletedDocumentFullName = "Space.DeletedDocument";
    DocumentReference userReferenceToCheck = new DocumentReference("wiki", "Space", "User");
    DocumentReference currentUserReference = new DocumentReference("wiki", "Space", "CurrentUser");
    when(xcontext.getUserReference()).thenReturn(currentUserReference);
    XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
    when(deletedDocument.getFullName()).thenReturn(deletedDocumentFullName);
    XWikiRecycleBinStoreInterface recycleBin = mock(XWikiRecycleBinStoreInterface.class);
    when(recycleBin.getDeletedDocument(deletedDocumentId, xcontext, true)).thenReturn(deletedDocument);
    when(xwiki.getRecycleBinStore()).thenReturn(recycleBin);
    XWikiRightService rightService = mock(XWikiRightService.class);
    when(xwiki.getRightService()).thenReturn(rightService);
    when(rightService.hasAccessLevel(any(), any(), any(), any())).thenReturn(true);
    assertTrue(mocker.getComponentUnderTest().canRestoreDeletedDocument(deletedDocumentId, userReferenceToCheck));
    // Verify that the rights were checked with the specified user as context user.
    verify(xcontext).setUserReference(userReferenceToCheck);
    // Verify that the context user was restored. Note: We don`t know the order here, but maybe we don`t care that
    // much.
    verify(xcontext).setUserReference(currentUserReference);
}
Also used : XWikiRecycleBinStoreInterface(com.xpn.xwiki.store.XWikiRecycleBinStoreInterface) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) XWikiRightService(com.xpn.xwiki.user.api.XWikiRightService) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 4 with XWikiRecycleBinStoreInterface

use of com.xpn.xwiki.store.XWikiRecycleBinStoreInterface in project xwiki-platform by xwiki.

the class DefaultModelBridgeTest method restoreDeletedDocumentNoRights.

@Test
public void restoreDeletedDocumentNoRights() throws Exception {
    long deletedDocumentId = 42;
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    String deletedDocumentFullName = "space.page";
    XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
    when(deletedDocument.getDocumentReference()).thenReturn(documentReference);
    when(deletedDocument.getId()).thenReturn(deletedDocumentId);
    when(deletedDocument.getFullName()).thenReturn(deletedDocumentFullName);
    when(xwiki.getDeletedDocument(deletedDocumentId, xcontext)).thenReturn(deletedDocument);
    when(xwiki.exists(documentReference, xcontext)).thenReturn(false);
    XWikiRecycleBinStoreInterface recycleBin = mock(XWikiRecycleBinStoreInterface.class);
    when(recycleBin.getDeletedDocument(deletedDocumentId, xcontext, true)).thenReturn(deletedDocument);
    when(xwiki.getRecycleBinStore()).thenReturn(recycleBin);
    // No rights.
    XWikiRightService rightService = mock(XWikiRightService.class);
    when(xwiki.getRightService()).thenReturn(rightService);
    when(rightService.hasAccessLevel(any(), any(), any(), any())).thenReturn(false);
    assertFalse(mocker.getComponentUnderTest().restoreDeletedDocument(deletedDocumentId, true));
    verify(mocker.getMockedLogger()).error("You are not allowed to restore document [{}] with ID [{}]", documentReference, deletedDocumentId);
    verify(xwiki, never()).restoreFromRecycleBin(any(), any(), any());
}
Also used : XWikiRecycleBinStoreInterface(com.xpn.xwiki.store.XWikiRecycleBinStoreInterface) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) XWikiRightService(com.xpn.xwiki.user.api.XWikiRightService) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 5 with XWikiRecycleBinStoreInterface

use of com.xpn.xwiki.store.XWikiRecycleBinStoreInterface in project xwiki-platform by xwiki.

the class DefaultModelBridgeTest method getDeletedDocumentIds.

@Test
public void getDeletedDocumentIds() throws Exception {
    String batchId = "abc123";
    long id1 = 1;
    long id2 = 2;
    XWikiDeletedDocument deletedDocument1 = mock(XWikiDeletedDocument.class);
    when(deletedDocument1.getId()).thenReturn(id1);
    XWikiDeletedDocument deletedDocument2 = mock(XWikiDeletedDocument.class);
    when(deletedDocument2.getId()).thenReturn(id2);
    XWikiDeletedDocument[] deletedDocuments = { deletedDocument1, deletedDocument2 };
    XWikiRecycleBinStoreInterface recycleBin = mock(XWikiRecycleBinStoreInterface.class);
    when(recycleBin.getAllDeletedDocuments(batchId, false, xcontext, true)).thenReturn(deletedDocuments);
    when(xwiki.getRecycleBinStore()).thenReturn(recycleBin);
    List<Long> result = mocker.getComponentUnderTest().getDeletedDocumentIds(batchId);
    assertNotNull(result);
    assertEquals(deletedDocuments.length, result.size());
    assertThat(result, containsInAnyOrder(id1, id2));
}
Also used : XWikiRecycleBinStoreInterface(com.xpn.xwiki.store.XWikiRecycleBinStoreInterface) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

XWikiRecycleBinStoreInterface (com.xpn.xwiki.store.XWikiRecycleBinStoreInterface)5 XWikiDeletedDocument (com.xpn.xwiki.doc.XWikiDeletedDocument)4 Test (org.junit.Test)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 DocumentReference (org.xwiki.model.reference.DocumentReference)4 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 XWikiRightService (com.xpn.xwiki.user.api.XWikiRightService)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiStoreInterface (com.xpn.xwiki.store.XWikiStoreInterface)1 Serializable (java.io.Serializable)1 Date (java.util.Date)1 Map (java.util.Map)1