/** * Verify that {@link XWiki#rollback(XWikiDocument, String, XWikiContext)} fires the right events. */ @Test public void rollbackFiresEvents() throws Exception { ObservationManager observationManager = mocker.getInstance(ObservationManager.class); DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page"); XWikiDocument document = mock(XWikiDocument.class); when(document.getDocumentReference()).thenReturn(documentReference); XWikiDocument originalDocument = mock(XWikiDocument.class); // Mark the document as existing so that the roll-back method will fire an update event. when(originalDocument.isNew()).thenReturn(false); XWikiDocument result = mock(XWikiDocument.class); when(result.clone()).thenReturn(result); when(result.getDocumentReference()).thenReturn(documentReference); when(result.getOriginalDocument()).thenReturn(originalDocument); String revision = "3.5"; when(xwiki.getVersioningStore().loadXWikiDoc(document, revision, context)).thenReturn(result); this.mocker.registerMockComponent(ContextualLocalizationManager.class); xwiki.rollback(document, revision, context); verify(observationManager) .notify(new DocumentRollingBackEvent(documentReference, revision), result, context); verify(observationManager) .notify(new DocumentUpdatingEvent(documentReference), result, context); verify(observationManager).notify(new DocumentUpdatedEvent(documentReference), result, context); verify(observationManager) .notify(new DocumentRolledBackEvent(documentReference, revision), result, context); }
/** * @see "XWIKI-9399: Attachment version is incremented when a document is rolled back even if the * attachment did not change" */ @Test public void rollbackDoesNotSaveUnchangedAttachment() throws Exception { String version = "1.1"; String fileName = "logo.png"; Date date = new Date(); XWikiAttachment currentAttachment = mock(XWikiAttachment.class); when(currentAttachment.getAttachmentRevision(version, context)).thenReturn(currentAttachment); when(currentAttachment.getDate()).thenReturn(new Timestamp(date.getTime())); when(currentAttachment.getVersion()).thenReturn(version); when(currentAttachment.getFilename()).thenReturn(fileName); XWikiAttachment oldAttachment = mock(XWikiAttachment.class); when(oldAttachment.getFilename()).thenReturn(fileName); when(oldAttachment.getVersion()).thenReturn(version); when(oldAttachment.getDate()).thenReturn(date); DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page"); XWikiDocument document = mock(XWikiDocument.class); when(document.getDocumentReference()).thenReturn(documentReference); when(document.getAttachmentList()).thenReturn(Arrays.asList(currentAttachment)); when(document.getAttachment(fileName)).thenReturn(currentAttachment); XWikiDocument result = mock(XWikiDocument.class); when(result.clone()).thenReturn(result); when(result.getDocumentReference()).thenReturn(documentReference); when(result.getAttachmentList()).thenReturn(Arrays.asList(oldAttachment)); when(result.getAttachment(fileName)).thenReturn(oldAttachment); String revision = "3.5"; when(xwiki.getVersioningStore().loadXWikiDoc(document, revision, context)).thenReturn(result); AttachmentRecycleBinStore attachmentRecycleBinStore = mock(AttachmentRecycleBinStore.class); xwiki.setAttachmentRecycleBinStore(attachmentRecycleBinStore); DocumentReference reference = document.getDocumentReference(); this.mocker.registerMockComponent(ContextualLocalizationManager.class); when(xwiki.getStore().loadXWikiDoc(any(XWikiDocument.class), same(context))) .thenReturn(new XWikiDocument(reference)); xwiki.rollback(document, revision, context); verify(attachmentRecycleBinStore, never()) .saveToRecycleBin( same(currentAttachment), any(String.class), any(Date.class), same(context), eq(true)); verify(oldAttachment, never()).setMetaDataDirty(true); }
public boolean add(XWikiDocument doc, int defaultAction, XWikiContext context) throws XWikiException { if (!context.getWiki().checkAccess("edit", doc, context)) { return false; } for (int i = 0; i < this.files.size(); i++) { DocumentInfo di = this.files.get(i); if (di.getFullName().equals(doc.getFullName()) && (di.getLanguage().equals(doc.getLanguage()))) { if (defaultAction != DocumentInfo.ACTION_NOT_DEFINED) { di.setAction(defaultAction); } if (!doc.isNew()) { di.setDoc(doc); } return true; } } doc = doc.clone(); try { filter(doc, context); DocumentInfo docinfo = new DocumentInfo(doc); docinfo.setAction(defaultAction); this.files.add(docinfo); BaseClass bclass = doc.getXClass(); if (bclass.getFieldList().size() > 0) { this.classFiles.add(docinfo); } if (bclass.getCustomMapping() != null) { this.customMappingFiles.add(docinfo); } return true; } catch (ExcludeDocumentException e) { LOGGER.info("Skip the document " + doc.getDocumentReference()); return false; } }