use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class TemplateMimeBodyPartFactoryTest method createWithAttachmentAndTemplateAttachments.
@Test
public void createWithAttachmentAndTemplateAttachments() throws Exception {
MimeBodyPartFactory<String> htmlMimeBodyPartFactory = this.mocker.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class), "text/html");
Attachment attachment1 = mock(Attachment.class, "attachment1");
Map<String, Object> bodyPartParameters = new HashMap<>();
bodyPartParameters.put("velocityVariables", new HashMap<String, String>());
bodyPartParameters.put("attachments", Collections.singletonList(attachment1));
bodyPartParameters.put("includeTemplateAttachments", true);
// Mock the retrieval and conversion of attachments from the Template document
DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
XWikiDocument xwikiDocument = mock(XWikiDocument.class);
when(dab.getDocumentInstance(this.documentReference)).thenReturn(xwikiDocument);
XWikiAttachment xwikiAttachment = mock(XWikiAttachment.class);
when(xwikiDocument.getAttachmentList()).thenReturn(Collections.singletonList(xwikiAttachment));
AttachmentConverter attachmentConverter = this.mocker.getInstance(AttachmentConverter.class);
Attachment attachment2 = mock(Attachment.class, "attachment2");
when(attachmentConverter.convert(Collections.singletonList(xwikiAttachment))).thenReturn(Collections.singletonList(attachment2));
this.mocker.getComponentUnderTest().create(this.documentReference, bodyPartParameters);
Map<String, Object> htmlParameters = new HashMap<>();
htmlParameters.put("alternate", "Hello John Doe, john@doe.com");
htmlParameters.put("attachments", Arrays.asList(attachment1, attachment2));
verify(htmlMimeBodyPartFactory).create("Hello <b>John Doe</b> <br />john@doe.com", htmlParameters);
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class ViewAttachRevAction method render.
@Override
public String render(XWikiContext context) throws XWikiException {
XWikiRequest request = context.getRequest();
XWikiDocument doc = context.getDoc();
String filename;
if (context.getMode() == XWikiContext.MODE_PORTLET) {
filename = request.getParameter("filename");
} else {
filename = getFileName();
}
XWikiAttachment attachment;
if (context.getWiki().hasAttachmentRecycleBin(context) && request.getParameter("rid") != null) {
int recycleId = Integer.parseInt(request.getParameter("rid"));
attachment = new XWikiAttachment(doc, filename);
attachment = context.getWiki().getAttachmentRecycleBinStore().restoreFromRecycleBin(attachment, recycleId, context, true);
} else if (request.getParameter("id") != null) {
int id = Integer.parseInt(request.getParameter("id"));
attachment = doc.getAttachmentList().get(id);
} else {
attachment = doc.getAttachment(filename);
if (attachment == null) {
context.put("message", "attachmentdoesnotexist");
return "exception";
}
}
ScriptContext scriptContext = getCurrentScriptContext();
scriptContext.setAttribute("attachment", new Attachment((Document) scriptContext.getAttribute("doc"), attachment, context), ScriptContext.ENGINE_SCOPE);
return "viewattachrev";
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class XWikiTest method testParseTemplateConsidersAttachment.
/**
* See XWIKI-2096
*/
public void testParseTemplateConsidersAttachment() throws XWikiException {
XWikiDocument skin = new XWikiDocument(new DocumentReference("Wiki", "XWiki", "Skin"));
XWikiAttachment attachment = new XWikiAttachment();
skin.getAttachmentList().add(attachment);
attachment.setContent("parsing an attachment".getBytes());
attachment.setFilename("template.vm");
attachment.setDoc(skin);
this.xwiki.saveDocument(skin, getContext());
getContext().put("skin", "XWiki.Skin");
assertEquals("XWiki.Skin", this.xwiki.getSkin(getContext()));
assertFalse(this.xwiki.getDocument("XWiki.Skin", getContext()).isNew());
assertEquals(skin, this.xwiki.getDocument("XWiki.Skin", getContext()));
assertEquals("parsing an attachment", this.xwiki.parseTemplate("template.vm", getContext()));
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class XWikiServletURLFactory method findDeletedAttachmentForDocRevision.
public long findDeletedAttachmentForDocRevision(XWikiDocument doc, String docRevision, String filename, XWikiContext context) throws XWikiException {
XWikiAttachment attachment = null;
XWikiDocument rdoc = context.getWiki().getDocument(doc, docRevision, context);
if (context.getWiki().hasAttachmentRecycleBin(context) && filename != null) {
attachment = rdoc.getAttachment(filename);
if (attachment != null) {
List<DeletedAttachment> deleted = context.getWiki().getAttachmentRecycleBinStore().getAllDeletedAttachments(attachment, context, true);
Collections.reverse(deleted);
for (DeletedAttachment entry : deleted) {
if (entry.getDate().after(rdoc.getDate())) {
return entry.getId();
}
}
}
}
return -1;
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class UsersClassPropertyValuesProviderTest method getValuesLocal.
@Test
public void getValuesLocal() throws Exception {
when(this.wikiUserManager.getUserScope(this.classReference.getWikiReference().getName())).thenReturn(UserScope.LOCAL_ONLY);
DocumentReference aliceReference = new DocumentReference("wiki", "Users", "Alice");
when(this.allowedValuesQuery.execute()).thenReturn(Collections.singletonList(new Object[] { aliceReference, " Alice One " }));
when(this.xcontext.getWiki().getDocument(aliceReference, this.xcontext)).thenReturn(mock(XWikiDocument.class, "alice"));
QueryFilter documentFilter = this.mocker.getInstance(QueryFilter.class, "document");
QueryFilter viewableFilter = this.mocker.getInstance(QueryFilter.class, "viewable");
List<QueryFilter> filters = mock(List.class);
DocumentReference bobReference = new DocumentReference("wiki", "Users", "Bob");
when(this.usedValuesQuery.getFilters()).thenReturn(filters);
when(this.usedValuesQuery.execute()).thenReturn(Arrays.asList(new Object[] { bobReference, 17L }, new Object[] { aliceReference, 3L }));
EntityReferenceSerializer<String> compactSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "compact");
when(compactSerializer.serialize(aliceReference, this.classReference.getWikiReference())).thenReturn("Users.Alice");
when(compactSerializer.serialize(bobReference, this.classReference.getWikiReference())).thenReturn("Users.Bob");
when(this.xcontext.getWiki().getURL(aliceReference, this.xcontext)).thenReturn("url/to/alice");
when(this.xcontext.getWiki().getURL(bobReference, this.xcontext)).thenReturn("url/to/bob");
XWikiDocument bobProfile = mock(XWikiDocument.class);
XWikiAttachment bobAvatar = mock(XWikiAttachment.class);
AttachmentReference bobAvatarReference = new AttachmentReference("somePhoto.png", bobReference);
when(this.xcontext.getWiki().getDocument(bobReference, this.xcontext)).thenReturn(bobProfile);
when(bobProfile.getStringValue("avatar")).thenReturn(bobAvatarReference.getName());
when(bobProfile.getAttachment("somePhoto.png")).thenReturn(bobAvatar);
when(bobAvatar.isImage(this.xcontext)).thenReturn(true);
when(bobAvatar.getReference()).thenReturn(bobAvatarReference);
when(this.xcontext.getWiki().getURL(bobAvatarReference, "download", "width=30&height=30&keepAspectRatio=true", null, this.xcontext)).thenReturn("url/to/bob/avatar");
PropertyValues values = this.mocker.getComponentUnderTest().getValues(this.propertyReference, 5, "foo");
assertEquals(2, values.getPropertyValues().size());
assertEquals("Users.Alice", values.getPropertyValues().get(0).getValue());
assertEquals("Alice One", values.getPropertyValues().get(0).getMetaData().get("label"));
assertEquals(3L, values.getPropertyValues().get(0).getMetaData().get("count"));
assertEquals("url/to/noavatar.png", values.getPropertyValues().get(0).getMetaData().get("icon"));
assertEquals("url/to/alice", values.getPropertyValues().get(0).getMetaData().get("url"));
assertEquals("Users.Bob", values.getPropertyValues().get(1).getValue());
assertEquals("Bob", values.getPropertyValues().get(1).getMetaData().get("label"));
assertEquals(17L, values.getPropertyValues().get(1).getMetaData().get("count"));
assertEquals("url/to/bob/avatar", values.getPropertyValues().get(1).getMetaData().get("icon"));
assertEquals("url/to/bob", values.getPropertyValues().get(1).getMetaData().get("url"));
verify(this.allowedValuesQuery, never()).setWiki(any(String.class));
verify(this.allowedValuesQuery, times(1)).execute();
verify(filters).clear();
verify(this.usedValuesQuery).addFilter(documentFilter);
verify(this.usedValuesQuery).addFilter(viewableFilter);
}
Aggregations