@Test void restoreDeletedDocument() throws Exception { long deletedDocumentId = 42; DocumentReference documentReference = new DocumentReference("wiki", "space", "page"); XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class); when(deletedDocument.getDocumentReference()).thenReturn(documentReference); when(deletedDocument.getId()).thenReturn(deletedDocumentId); when(xwiki.getDeletedDocument(deletedDocumentId, xcontext)).thenReturn(deletedDocument); when(xwiki.exists(documentReference, xcontext)).thenReturn(false); when(request.isCheckAuthorRights()).thenReturn(false); when(request.isCheckRights()).thenReturn(false); assertTrue(this.modelBridge.restoreDeletedDocument(deletedDocumentId, request)); verify(xwiki).restoreFromRecycleBin(deletedDocumentId, "Restored from recycle bin", xcontext); assertLog(Level.INFO, "Document [{}] has been restored", documentReference); }
@Override public boolean restoreDeletedDocument(long deletedDocumentId, AbstractCheckRightsRequest request) { XWikiContext context = this.xcontextProvider.get(); XWiki xwiki = context.getWiki(); DocumentReference deletedDocumentReference = null; try { XWikiDeletedDocument deletedDocument = this.getDeletedDocument(context, deletedDocumentId); if (deletedDocument == null) { return false; } deletedDocumentReference = deletedDocument.getDocumentReference(); if (xwiki.exists(deletedDocumentReference, context)) { logger.error("Document [{}] with ID [{}] can not be restored. Document already exists", deletedDocument.getFullName(), deletedDocumentId); } else if (request.isCheckAuthorRights() && !canRestoreDeletedDocument(deletedDocument, context.getAuthorReference())) { logger.error("The author [{}] of this script is not allowed to restore document [{}] with ID [{}]", context.getAuthorReference(), deletedDocumentReference, deletedDocumentId); } else if (request.isCheckRights() && !canRestoreDeletedDocument(deletedDocument, context.getUserReference())) { logger.error("You are not allowed to restore document [{}] with ID [{}]", deletedDocumentReference, deletedDocumentId); } else { xwiki.restoreFromRecycleBin(deletedDocument.getId(), "Restored from recycle bin", context); logger.info("Document [{}] has been restored", deletedDocumentReference); return true; } } catch (Exception e) { if (deletedDocumentReference != null) { logger.error("Failed to restore document [{}] with ID [{}]", deletedDocumentReference, deletedDocumentId, e); } else { logger.error("Failed to restore deleted document with ID [{}]", deletedDocumentId, e); } } return false; }
DefaultModelBridge implements ModelBridge { @Override public boolean restoreDeletedDocument(long deletedDocumentId, AbstractCheckRightsRequest request) { XWikiContext context = this.xcontextProvider.get(); XWiki xwiki = context.getWiki(); DocumentReference deletedDocumentReference = null; try { XWikiDeletedDocument deletedDocument = this.getDeletedDocument(context, deletedDocumentId); if (deletedDocument == null) { return false; } deletedDocumentReference = deletedDocument.getDocumentReference(); if (xwiki.exists(deletedDocumentReference, context)) { logger.error("Document [{}] with ID [{}] can not be restored. Document already exists", deletedDocument.getFullName(), deletedDocumentId); } else if (request.isCheckAuthorRights() && !canRestoreDeletedDocument(deletedDocument, context.getAuthorReference())) { logger.error("The author [{}] of this script is not allowed to restore document [{}] with ID [{}]", context.getAuthorReference(), deletedDocumentReference, deletedDocumentId); } else if (request.isCheckRights() && !canRestoreDeletedDocument(deletedDocument, context.getUserReference())) { logger.error("You are not allowed to restore document [{}] with ID [{}]", deletedDocumentReference, deletedDocumentId); } else { xwiki.restoreFromRecycleBin(deletedDocument.getId(), "Restored from recycle bin", context); logger.info("Document [{}] has been restored", deletedDocumentReference); return true; } } catch (Exception e) { if (deletedDocumentReference != null) { logger.error("Failed to restore document [{}] with ID [{}]", deletedDocumentReference, deletedDocumentId, e); } else { logger.error("Failed to restore deleted document with ID [{}]", deletedDocumentId, e); } } return false; } }
DefaultModelBridge implements ModelBridge { @Override public boolean restoreDeletedDocument(long deletedDocumentId, AbstractCheckRightsRequest request) { XWikiContext context = this.xcontextProvider.get(); XWiki xwiki = context.getWiki(); DocumentReference deletedDocumentReference = null; try { XWikiDeletedDocument deletedDocument = this.getDeletedDocument(context, deletedDocumentId); if (deletedDocument == null) { return false; } deletedDocumentReference = deletedDocument.getDocumentReference(); if (xwiki.exists(deletedDocumentReference, context)) { logger.error("Document [{}] with ID [{}] can not be restored. Document already exists", deletedDocument.getFullName(), deletedDocumentId); } else if (request.isCheckAuthorRights() && !canRestoreDeletedDocument(deletedDocument, context.getAuthorReference())) { logger.error("The author [{}] of this script is not allowed to restore document [{}] with ID [{}]", context.getAuthorReference(), deletedDocumentReference, deletedDocumentId); } else if (request.isCheckRights() && !canRestoreDeletedDocument(deletedDocument, context.getUserReference())) { logger.error("You are not allowed to restore document [{}] with ID [{}]", deletedDocumentReference, deletedDocumentId); } else { xwiki.restoreFromRecycleBin(deletedDocument.getId(), "Restored from recycle bin", context); logger.info("Document [{}] has been restored", deletedDocumentReference); return true; } } catch (Exception e) { if (deletedDocumentReference != null) { logger.error("Failed to restore document [{}] with ID [{}]", deletedDocumentReference, deletedDocumentId, e); } else { logger.error("Failed to restore deleted document with ID [{}]", deletedDocumentId, e); } } return false; }  }
DefaultModelBridge implements ModelBridge { @Override public boolean restoreDeletedDocument(long deletedDocumentId, AbstractCheckRightsRequest request) { XWikiContext context = this.xcontextProvider.get(); XWiki xwiki = context.getWiki(); DocumentReference deletedDocumentReference = null; try { XWikiDeletedDocument deletedDocument = this.getDeletedDocument(context, deletedDocumentId); if (deletedDocument == null) { return false; } deletedDocumentReference = deletedDocument.getDocumentReference(); if (xwiki.exists(deletedDocumentReference, context)) { logger.error("Document [{}] with ID [{}] can not be restored. Document already exists", deletedDocument.getFullName(), deletedDocumentId); } else if (request.isCheckAuthorRights() && !canRestoreDeletedDocument(deletedDocument, context.getAuthorReference())) { logger.error("The author [{}] of this script is not allowed to restore document [{}] with ID [{}]", context.getAuthorReference(), deletedDocumentReference, deletedDocumentId); } else if (request.isCheckRights() && !canRestoreDeletedDocument(deletedDocument, context.getUserReference())) { logger.error("You are not allowed to restore document [{}] with ID [{}]", deletedDocumentReference, deletedDocumentId); } else { xwiki.restoreFromRecycleBin(deletedDocument.getId(), "Restored from recycle bin", context); logger.info("Document [{}] has been restored", deletedDocumentReference); return true; } } catch (Exception e) { if (deletedDocumentReference != null) { logger.error("Failed to restore document [{}] with ID [{}]", deletedDocumentReference, deletedDocumentId, e); } else { logger.error("Failed to restore deleted document with ID [{}]", deletedDocumentId, e); } } return false; }  @Override boolean create(DocumentReference documentReference); @Override boolean copy(DocumentReference source, DocumentReference destination); @Override boolean delete(DocumentReference reference); @Override boolean delete(DocumentReference reference, boolean skipRecycleBin); @Override boolean removeLock(DocumentReference reference); @Override void createRedirect(DocumentReference oldReference, DocumentReference newReference); @Override boolean canOverwriteSilently(DocumentReference documentReference); @Override boolean exists(DocumentReference reference); @Override List<DocumentReference> getBackLinkedReferences(DocumentReference documentReference, String wikiId); @Override List<DocumentReference> getDocumentReferences(SpaceReference spaceReference); @Override boolean updateParentField(final DocumentReference oldParentReference,
        final DocumentReference newParentReference); @Override DocumentReference setContextUserReference(DocumentReference userReference); @Override void update(DocumentReference documentReference, Map<String, String> parameters); @Override boolean restoreDeletedDocument(long deletedDocumentId, AbstractCheckRightsRequest request); @Override List<Long> getDeletedDocumentIds(String batchId); @Override boolean permanentlyDeleteDocument(long deletedDocumentId, AbstractCheckRightsRequest request); @Override boolean permanentlyDeleteAllDocuments(PermanentlyDeleteJob deleteJob, AbstractCheckRightsRequest request); @Override boolean rename(DocumentReference source, DocumentReference destination); }
DefaultModelBridge implements ModelBridge { @Override public boolean restoreDeletedDocument(long deletedDocumentId, AbstractCheckRightsRequest request) { XWikiContext context = this.xcontextProvider.get(); XWiki xwiki = context.getWiki(); DocumentReference deletedDocumentReference = null; try { XWikiDeletedDocument deletedDocument = this.getDeletedDocument(context, deletedDocumentId); if (deletedDocument == null) { return false; } deletedDocumentReference = deletedDocument.getDocumentReference(); if (xwiki.exists(deletedDocumentReference, context)) { logger.error("Document [{}] with ID [{}] can not be restored. Document already exists", deletedDocument.getFullName(), deletedDocumentId); } else if (request.isCheckAuthorRights() && !canRestoreDeletedDocument(deletedDocument, context.getAuthorReference())) { logger.error("The author [{}] of this script is not allowed to restore document [{}] with ID [{}]", context.getAuthorReference(), deletedDocumentReference, deletedDocumentId); } else if (request.isCheckRights() && !canRestoreDeletedDocument(deletedDocument, context.getUserReference())) { logger.error("You are not allowed to restore document [{}] with ID [{}]", deletedDocumentReference, deletedDocumentId); } else { xwiki.restoreFromRecycleBin(deletedDocument.getId(), "Restored from recycle bin", context); logger.info("Document [{}] has been restored", deletedDocumentReference); return true; } } catch (Exception e) { if (deletedDocumentReference != null) { logger.error("Failed to restore document [{}] with ID [{}]", deletedDocumentReference, deletedDocumentId, e); } else { logger.error("Failed to restore deleted document with ID [{}]", deletedDocumentId, e); } } return false; }  @Override boolean create(DocumentReference documentReference); @Override boolean copy(DocumentReference source, DocumentReference destination); @Override boolean delete(DocumentReference reference); @Override boolean delete(DocumentReference reference, boolean skipRecycleBin); @Override boolean removeLock(DocumentReference reference); @Override void createRedirect(DocumentReference oldReference, DocumentReference newReference); @Override boolean canOverwriteSilently(DocumentReference documentReference); @Override boolean exists(DocumentReference reference); @Override List<DocumentReference> getBackLinkedReferences(DocumentReference documentReference, String wikiId); @Override List<DocumentReference> getDocumentReferences(SpaceReference spaceReference); @Override boolean updateParentField(final DocumentReference oldParentReference,
        final DocumentReference newParentReference); @Override DocumentReference setContextUserReference(DocumentReference userReference); @Override void update(DocumentReference documentReference, Map<String, String> parameters); @Override boolean restoreDeletedDocument(long deletedDocumentId, AbstractCheckRightsRequest request); @Override List<Long> getDeletedDocumentIds(String batchId); @Override boolean permanentlyDeleteDocument(long deletedDocumentId, AbstractCheckRightsRequest request); @Override boolean permanentlyDeleteAllDocuments(PermanentlyDeleteJob deleteJob, AbstractCheckRightsRequest request); @Override boolean rename(DocumentReference source, DocumentReference destination);  }