use of org.xwiki.model.reference.AttachmentReference 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);
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class SpacesResourceTest method testAttachments.
@Test
public void testAttachments() throws Exception {
DocumentReference reference = new DocumentReference(getWiki(), getTestClassName(), getTestMethodName());
this.testUtils.rest().delete(reference);
this.testUtils.rest().savePage(reference);
this.testUtils.rest().attachFile(new AttachmentReference("attachment.txt", reference), new ReaderInputStream(new StringReader("content")), true);
// Matches Sandbox.WebHome@XWikLogo.png
GetMethod getMethod = executeGet(String.format("%s", buildURI(SpaceAttachmentsResource.class, getWiki(), Arrays.asList(getTestClassName()))));
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
Attachments attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
Assert.assertEquals(getAttachmentsInfo(attachments), 1, attachments.getAttachments().size());
for (Attachment attachment : attachments.getAttachments()) {
checkLinks(attachment);
}
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class WikisResourceTest method testAttachments.
@Test
public void testAttachments() throws Exception {
this.testUtils.rest().delete(reference);
this.testUtils.rest().attachFile(new AttachmentReference(getTestClassName() + ".txt", reference), new ReaderInputStream(new StringReader("attachment content")), true);
// Verify there are attachments in the whole wiki
GetMethod getMethod = executeGet(buildURI(WikiAttachmentsResource.class, getWiki()).toString());
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
Attachments attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
Assert.assertTrue(attachments.getAttachments().size() > 0);
for (Attachment attachment : attachments.getAttachments()) {
checkLinks(attachment);
}
// Verify we can search for a specific attachment name in the whole wiki
getMethod = executeGet(String.format("%s?name=" + getTestClassName(), buildURI(WikiAttachmentsResource.class, getWiki())));
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
Assert.assertEquals(getAttachmentsInfo(attachments), 1, attachments.getAttachments().size());
for (Attachment attachment : attachments.getAttachments()) {
checkLinks(attachment);
}
// Verify we can search for all attachments in a given space (sandbox)
// Also verify that a space can be looked up independtly of its case ("sandbox" will match the "Sandbox" space)
getMethod = executeGet(String.format("%s?space=" + getTestClassName(), buildURI(WikiAttachmentsResource.class, getWiki())));
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
Assert.assertEquals(getAttachmentsInfo(attachments), 1, attachments.getAttachments().size());
for (Attachment attachment : attachments.getAttachments()) {
checkLinks(attachment);
}
// Verify we can search for an attachment in a given space (sandbox)
getMethod = executeGet(String.format("%s?name=" + getTestClassName() + "&space=" + getTestClassName(), buildURI(WikiAttachmentsResource.class, getWiki())));
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
Assert.assertEquals(getAttachmentsInfo(attachments), 1, attachments.getAttachments().size());
for (Attachment attachment : attachments.getAttachments()) {
checkLinks(attachment);
}
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class AttachmentSolrMetadataExtractor method setFieldsInternal.
@Override
public boolean setFieldsInternal(LengthSolrInputDocument solrDocument, EntityReference entityReference) throws Exception {
AttachmentReference attachmentReference = new AttachmentReference(entityReference);
XWikiDocument originalDocument = getDocument(attachmentReference.getDocumentReference());
XWikiAttachment attachment = originalDocument.getAttachment(attachmentReference.getName());
if (attachment == null) {
return false;
}
XWikiContext xcontext = xcontextProvider.get();
solrDocument.setField(FieldUtils.FILENAME, attachment.getFilename());
solrDocument.setField(FieldUtils.FILENAME_SORT, attachment.getFilename());
solrDocument.setField(FieldUtils.MIME_TYPE, attachment.getMimeType(xcontext));
solrDocument.setField(FieldUtils.ATTACHMENT_DATE, attachment.getDate());
// We need to add a dedicated sort field because the corresponding field is multiValued and thus cannot be used
// for sorting (the reason it is multiValued is because it is 'reused' on document rows and documents can have
// multiple attachments).
solrDocument.setField(FieldUtils.ATTACHMENT_DATE_SORT, attachment.getDate());
solrDocument.setField(FieldUtils.ATTACHMENT_SIZE, attachment.getLongSize());
solrDocument.setField(FieldUtils.ATTACHMENT_SIZE_SORT, attachment.getLongSize());
// We need to index the attachment version (revision) to be able to detect when the search index is out of date
// (not in sync with the database).
solrDocument.setField(FieldUtils.ATTACHMENT_VERSION, attachment.getVersion());
// Index the full author reference for exact matching (faceting).
String authorStringReference = entityReferenceSerializer.serialize(attachment.getAuthorReference());
solrDocument.setField(FieldUtils.ATTACHMENT_AUTHOR, authorStringReference);
try {
// Index the author display name for free text search and results sorting.
String authorDisplayName = xcontext.getWiki().getPlainUserName(attachment.getAuthorReference(), xcontext);
solrDocument.setField(FieldUtils.ATTACHMENT_AUTHOR_DISPLAY, authorDisplayName);
solrDocument.setField(FieldUtils.ATTACHMENT_AUTHOR_DISPLAY_SORT, authorDisplayName);
} catch (Exception e) {
this.logger.error("Failed to get author display name for attachment [{}]", attachment.getReference(), e);
}
setLocaleAndContentFields(attachment, solrDocument);
return true;
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class TestUtils method attachFile.
/**
* @since 7.2M2
*/
public void attachFile(List<String> spaces, String page, String name, InputStream is, boolean failIfExists) throws Exception {
AttachmentReference reference = new AttachmentReference(name, new DocumentReference(getCurrentWiki(), spaces, page));
attachFile(reference, is, failIfExists);
}
Aggregations