Search in sources :

Example 46 with XWikiAttachment

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);
}
Also used : HashMap(java.util.HashMap) MimeBodyPartFactory(org.xwiki.mail.MimeBodyPartFactory) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Test(org.junit.Test)

Example 47 with XWikiAttachment

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";
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ScriptContext(javax.script.ScriptContext) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Document(com.xpn.xwiki.api.Document)

Example 48 with XWikiAttachment

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()));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 49 with XWikiAttachment

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;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) DeletedAttachment(com.xpn.xwiki.doc.DeletedAttachment)

Example 50 with XWikiAttachment

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);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) QueryFilter(org.xwiki.query.QueryFilter) PropertyValues(org.xwiki.rest.model.jaxb.PropertyValues) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)133 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)71 DocumentReference (org.xwiki.model.reference.DocumentReference)51 Test (org.junit.Test)40 XWikiContext (com.xpn.xwiki.XWikiContext)35 XWikiException (com.xpn.xwiki.XWikiException)25 ByteArrayInputStream (java.io.ByteArrayInputStream)20 Attachment (com.xpn.xwiki.api.Attachment)18 Date (java.util.Date)17 IOException (java.io.IOException)15 ArrayList (java.util.ArrayList)15 Document (com.xpn.xwiki.api.Document)14 XWiki (com.xpn.xwiki.XWiki)13 BaseObject (com.xpn.xwiki.objects.BaseObject)13 AttachmentReference (org.xwiki.model.reference.AttachmentReference)13 InputStream (java.io.InputStream)11 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)10 File (java.io.File)10 URL (java.net.URL)7 List (java.util.List)7