/
AttachmentSelectorPageTest.java
402 lines (352 loc) · 17.2 KB
/
AttachmentSelectorPageTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.attachment;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.apache.commons.io.IOUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.xwiki.bridge.event.DocumentCreatedEvent;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.component.wiki.internal.bridge.DefaultContentParser;
import org.xwiki.icon.IconManagerScriptService;
import org.xwiki.icon.internal.DefaultIconManagerComponentList;
import org.xwiki.model.internal.reference.converter.EntityReferenceConverter;
import org.xwiki.model.reference.AttachmentReference;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.script.ModelScriptService;
import org.xwiki.observation.EventListener;
import org.xwiki.rendering.RenderingScriptServiceComponentList;
import org.xwiki.rendering.internal.configuration.DefaultExtendedRenderingConfiguration;
import org.xwiki.rendering.internal.configuration.RenderingConfigClassDocumentConfigurationSource;
import org.xwiki.rendering.internal.macro.wikibridge.DefaultWikiMacroManager;
import org.xwiki.rendering.internal.macro.wikibridge.WikiMacroEventListener;
import org.xwiki.rendering.syntax.Syntax;
import org.xwiki.rendering.wikimacro.internal.DefaultWikiMacroFactory;
import org.xwiki.rendering.wikimacro.internal.DefaultWikiMacroRenderer;
import org.xwiki.security.authorization.AuthorizationManager;
import org.xwiki.security.authorization.Right;
import org.xwiki.store.TemporaryAttachmentSessionsManager;
import org.xwiki.store.script.TemporaryAttachmentsScriptService;
import org.xwiki.test.annotation.ComponentList;
import org.xwiki.test.junit5.mockito.MockComponent;
import org.xwiki.test.page.HTML50ComponentList;
import org.xwiki.test.page.IconSetup;
import org.xwiki.test.page.PageTest;
import org.xwiki.test.page.TestNoScriptMacro;
import org.xwiki.test.page.XWikiSyntax21ComponentList;
import org.xwiki.xml.internal.html.filter.ControlCharactersFilter;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiAttachment;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.internal.model.reference.DocumentReferenceConverter;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.classes.BaseClass;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Test of {@code XWiki.AttachmentSelector}.
*
* @version $Id$
* @since 14.5
* @since 14.4.2
* @since 13.10.7
*/
@HTML50ComponentList
@XWikiSyntax21ComponentList
@RenderingScriptServiceComponentList
@DefaultIconManagerComponentList
@ComponentList({
// Start - Required in addition of RenderingScriptServiceComponentList
DefaultExtendedRenderingConfiguration.class,
RenderingConfigClassDocumentConfigurationSource.class,
// Start - Required in addition of RenderingScriptServiceComponentList
ControlCharactersFilter.class,
ModelScriptService.class,
TestNoScriptMacro.class,
// Start WikiMacroEventListener
WikiMacroEventListener.class,
DefaultWikiMacroFactory.class,
DefaultWikiMacroManager.class,
DefaultContentParser.class,
org.xwiki.rendering.internal.parser.DefaultContentParser.class,
DefaultWikiMacroRenderer.class,
// End WikiMacroEventListener
TemporaryAttachmentsScriptService.class,
IconManagerScriptService.class,
DocumentReferenceConverter.class,
EntityReferenceConverter.class,
ModelScriptService.class,
})
class AttachmentSelectorPageTest extends PageTest
{
/**
* Mocked because we don't want to deal with actual sessions for this test.
*/
@MockComponent
private TemporaryAttachmentSessionsManager temporaryAttachmentSessionsManager;
@BeforeEach
void setUp() throws Exception
{
when(this.temporaryAttachmentSessionsManager.getUploadedAttachments(any(DocumentReference.class)))
.thenReturn(List.of());
// Initializes then environment for the icon extension.
IconSetup.setUp(this, "/icons.properties");
}
@ParameterizedTest
@ValueSource(strings = {
"{{noscript}}println(\"Hello from noscript!\"){{/noscript}}.png",
"]] {{noscript}}println(\"Hello from noscript!\"){{/noscript}}.png"
})
void renderDisplayImageFalse(String fileName) throws Exception
{
commonFixup(fileName);
this.request.put("docname", "xwiki:Space.Test");
this.request.put("classname", "xwiki:Space.Test");
this.request.put("property", "avatar");
this.request.put("object", "0");
this.request.put("filter", "png");
this.request.put("displayImage", "false");
this.request.put("xpage", "plain");
Document document = renderHTMLPage(new DocumentReference("xwiki", "XWiki", "AttachmentSelector"));
assertNotNull(document);
Element galleryAttachmentTitle = document.select(".current .gallery_attachmenttitle").get(0);
assertEquals(fileName, galleryAttachmentTitle.attr("title"));
assertEquals(fileName, galleryAttachmentTitle.text());
assertEquals(fileName, document.select(".gallery_attachmentframe .filename").text());
assertEquals(String.format("attach:xwiki:Space.Test@%s", fileName),
document.select(".gallery_attachmentframe a").attr("href"));
}
@ParameterizedTest
@ValueSource(strings = {
"{{noscript}}println(\"Hello from noscript!\"){{/noscript}}.png",
"]] {{noscript}}println(\"Hello from noscript!\"){{/noscript}}.png"
})
void renderDisplayImageTrue(String fileName) throws Exception
{
commonFixup(fileName);
this.request.put("docname", "xwiki:Space.Test");
this.request.put("classname", "xwiki:Space.Test");
this.request.put("property", "avatar");
this.request.put("object", "0");
this.request.put("filter", "png");
this.request.put("displayImage", "true");
this.request.put("xpage", "plain");
Document document = renderHTMLPage(new DocumentReference("xwiki", "XWiki", "AttachmentSelector"));
assertNotNull(document);
Element galleryAttachmentTitle = document.select(".current .gallery_attachmenttitle").get(0);
assertEquals(fileName, galleryAttachmentTitle.attr("title"));
assertEquals(fileName, galleryAttachmentTitle.text());
assertEquals(String.format("attach:xwiki:Space.Test@%s", fileName),
document.select(".gallery_attachmentframe a").attr("href"));
Elements img = document.select(".gallery_attachmentframe img");
assertEquals(String.format("xwiki:Space.Test@%s", fileName), img.attr("src"));
assertEquals(String.format("xwiki:Space.Test@%s", fileName), img.attr("alt"));
}
@ParameterizedTest
@MethodSource("attachmentSelectorMacroSource")
void attachmentSelectorMacroWidth(String widthValue, String expectedWidth) throws Exception
{
attachmentSelectorMacroFixup();
XWikiDocument xwikiDocument = commonFixup("test.png");
xwikiDocument.setContent(String.format("{{attachmentSelector "
+ "classname=\"Space.Test\" "
+ "property=\"avatar\" "
+ "savemode=\"direct\" "
+ "width=\"%s\" "
+ "displayImage=\"true\"/}}", widthValue));
Document document = renderHTMLPage(xwikiDocument);
assertEquals(expectedWidth, document.select(".displayed img").attr("width"));
}
@ParameterizedTest
@MethodSource("attachmentSelectorMacroSource")
void attachmentSelectorMacroHeight(String widthValue, String expectedWidth) throws Exception
{
attachmentSelectorMacroFixup();
XWikiDocument xwikiDocument = commonFixup("test.png");
xwikiDocument.setContent(String.format("{{attachmentSelector "
+ "classname=\"Space.Test\" "
+ "property=\"avatar\" "
+ "savemode=\"direct\" "
+ "height=\"%s\" "
+ "displayImage=\"true\"/}}", widthValue));
Document document = renderHTMLPage(xwikiDocument);
assertEquals(expectedWidth, document.select(".displayed img").attr("height"));
}
@ParameterizedTest
@MethodSource("attachmentSelectorMacroSource")
void attachmentSelectorMacroAlternateText(String widthValue, String expectedWidth) throws Exception
{
attachmentSelectorMacroFixup();
XWikiDocument xwikiDocument = commonFixup("test.png");
xwikiDocument.setContent(String.format("{{attachmentSelector "
+ "classname=\"Space.Test\" "
+ "property=\"avatar\" "
+ "savemode=\"direct\" "
+ "alternateText=\"%s\" "
+ "displayImage=\"true\"/}}", widthValue));
xwikiDocument.setSyntax(Syntax.XWIKI_2_1);
Document document = renderHTMLPage(xwikiDocument);
assertEquals(expectedWidth, document.select(".displayed img").attr("alt"));
}
@ParameterizedTest
@MethodSource("attachmentSelectorMacroSource")
void attachmentSelectorMacroWidthWithLink(String widthValue, String expectedWidth) throws Exception
{
attachmentSelectorMacroFixup();
XWikiDocument xwikiDocument = commonFixup("test.png");
xwikiDocument.setContent(String.format("{{attachmentSelector "
+ "classname=\"Space.Test\" "
+ "property=\"avatar\" "
+ "savemode=\"direct\" "
+ "width=\"%s\" "
+ "link=\"true\" "
+ "displayImage=\"true\"/}}", widthValue));
Document document = renderHTMLPage(xwikiDocument);
assertEquals(expectedWidth, document.select(".displayed img").attr("width"));
}
@ParameterizedTest
@MethodSource("attachmentSelectorMacroSource")
void attachmentSelectorMacroHeightWithLink(String widthValue, String expectedWidth) throws Exception
{
attachmentSelectorMacroFixup();
XWikiDocument xwikiDocument = commonFixup("test.png");
xwikiDocument.setContent(String.format("{{attachmentSelector "
+ "classname=\"Space.Test\" "
+ "property=\"avatar\" "
+ "savemode=\"direct\" "
+ "height=\"%s\" "
+ "link=\"true\" "
+ "displayImage=\"true\"/}}", widthValue));
Document document = renderHTMLPage(xwikiDocument);
assertEquals(expectedWidth, document.select(".displayed img").attr("height"));
}
@ParameterizedTest
@MethodSource("attachmentSelectorMacroSource")
void attachmentSelectorMacroAlternateTextWithLink(String widthValue, String expectedWidth) throws Exception
{
attachmentSelectorMacroFixup();
XWikiDocument xwikiDocument = commonFixup("test.png");
xwikiDocument.setContent(String.format("{{attachmentSelector "
+ "classname=\"Space.Test\" "
+ "property=\"avatar\" "
+ "savemode=\"direct\" "
+ "alternateText=\"%s\" "
+ "link=\"true\" "
+ "displayImage=\"true\"/}}", widthValue));
xwikiDocument.setSyntax(Syntax.XWIKI_2_1);
Document document = renderHTMLPage(xwikiDocument);
assertEquals(expectedWidth, document.select(".displayed img").attr("alt"));
}
@Test
void withTemporaryAttachment() throws Exception
{
String fileName = "test.png";
XWikiDocument xWikiDocument = commonFixup(fileName);
XWikiAttachment xWikiAttachment = mock(XWikiAttachment.class);
when(xWikiAttachment.getFilename()).thenReturn(fileName);
AttachmentReference attachmentReference =
new AttachmentReference(fileName, xWikiDocument.getDocumentReference());
when(xWikiAttachment.getReference()).thenReturn(attachmentReference);
when(this.temporaryAttachmentSessionsManager.getUploadedAttachments(xWikiDocument.getDocumentReference()))
.thenReturn(List.of(xWikiAttachment));
when(this.temporaryAttachmentSessionsManager.getUploadedAttachment(attachmentReference))
.thenReturn(Optional.of(xWikiAttachment));
this.request.put("docname", "xwiki:Space.Test");
this.request.put("classname", "xwiki:Space.Test");
this.request.put("property", "avatar");
this.request.put("object", "0");
this.request.put("filter", "png");
this.request.put("displayImage", "true");
this.request.put("xpage", "plain");
Document document = renderHTMLPage(new DocumentReference("xwiki", "XWiki", "AttachmentSelector"));
Element element = document.selectFirst(".gallery_attachmentbox.current");
assertTrue(element.classNames().contains("temporary_attachment"),
String.format("temporary_attachment class not found in %s", element.classNames()));
assertNotNull(element.selectFirst(".fake-attach"), "An icon with class fake-attach is expected to "
+ "be found");
}
@Test
void cancelButton() throws Exception
{
commonFixup("test.png");
this.request.put("docname", "xwiki:Space.]] {{noscript/}}");
Document document = renderHTMLPage(new DocumentReference("xwiki", "XWiki", "AttachmentSelector"));
assertEquals("Space.]] {{noscript/}}", document.getElementById("attachment-picker-close").attr("href"));
}
private void attachmentSelectorMacroFixup() throws Exception
{
DocumentReference attachmentSelectorDocumentReference =
new DocumentReference("xwiki", "XWiki", "AttachmentSelector");
XWikiDocument xWikiDocument = loadPage(attachmentSelectorDocumentReference);
// TODO: The code below is more generic than this test and should be moved to be reusable.
// Make the wiki component manager point to the default component manager.
this.componentManager.registerComponent(ComponentManager.class, "wiki",
this.componentManager.getInstance(ComponentManager.class));
when(this.componentManager.<AuthorizationManager>getInstance(AuthorizationManager.class)
.hasAccess(Right.ADMIN, new DocumentReference("xwiki", "XWiki", "Admin"),
xWikiDocument.getDocumentReference().getWikiReference())).thenReturn(true);
// Simulate the event not thrown by page test for now.
this.componentManager.<EventListener>getInstance(EventListener.class, "wikimacrolistener")
.onEvent(new DocumentCreatedEvent(), xWikiDocument, null);
}
private XWikiDocument commonFixup(String fileName) throws XWikiException, IOException
{
// Initialize a document containing an XClass definition and an XObject of this XClass.
// The interesting property is the avatar string field, which references the name of an attachment contained in
// the document itself too.
DocumentReference documentReference = new DocumentReference("xwiki", "Space", "Test");
XWikiDocument xwikiDocument = this.xwiki.getDocument(documentReference, this.context);
BaseClass xClass = xwikiDocument.getXClass();
xClass.addTextField("avatar", "Avatar", 10);
BaseObject baseObject = xwikiDocument.newXObject(xwikiDocument.getDocumentReference(), this.context);
baseObject.setStringValue("avatar", fileName);
// Simulates the file rename action.
xwikiDocument.setAttachment("tmp.png", IOUtils.toInputStream("", Charset.defaultCharset()), this.context);
xwikiDocument.getAttachment("tmp.png").setFilename(fileName);
xwikiDocument.setSyntax(Syntax.XWIKI_2_1);
this.xwiki.saveDocument(xwikiDocument, this.context);
this.context.setDoc(xwikiDocument);
return xwikiDocument;
}
public static Stream<Arguments> attachmentSelectorMacroSource()
{
return Stream.of(
arguments("{{noscript /~}~}", "{{noscript /}}"),
arguments("]] {{noscript /~}~}", "]] {{noscript /}}"),
arguments("]] {{noscript /~}~} [[", "]] {{noscript /}} [[")
);
}
}