1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.internal.wiki

File XWikiWikiModelTest.java

 

Code metrics

0
85
17
1
338
205
17
0.2
5
17
1

Classes

Class Line # Actions
XWikiWikiModelTest 62 85 0% 17 0
1.0100%
 

Contributing tests

This file is covered by 16 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.internal.wiki;
21   
22    import java.util.Collections;
23    import java.util.HashMap;
24    import java.util.Map;
25   
26    import javax.inject.Named;
27   
28    import org.junit.jupiter.api.Test;
29    import org.xwiki.bridge.DocumentAccessBridge;
30    import org.xwiki.bridge.DocumentModelBridge;
31    import org.xwiki.bridge.SkinAccessBridge;
32    import org.xwiki.model.EntityType;
33    import org.xwiki.model.reference.AttachmentReference;
34    import org.xwiki.model.reference.DocumentReference;
35    import org.xwiki.model.reference.EntityReference;
36    import org.xwiki.model.reference.EntityReferenceResolver;
37    import org.xwiki.model.reference.EntityReferenceSerializer;
38    import org.xwiki.model.reference.PageReference;
39    import org.xwiki.rendering.block.XDOM;
40    import org.xwiki.rendering.configuration.ExtendedRenderingConfiguration;
41    import org.xwiki.rendering.listener.reference.AttachmentResourceReference;
42    import org.xwiki.rendering.listener.reference.DocumentResourceReference;
43    import org.xwiki.rendering.listener.reference.PageResourceReference;
44    import org.xwiki.rendering.listener.reference.ResourceReference;
45    import org.xwiki.rendering.listener.reference.ResourceType;
46    import org.xwiki.test.junit5.mockito.ComponentTest;
47    import org.xwiki.test.junit5.mockito.InjectMockComponents;
48    import org.xwiki.test.junit5.mockito.MockComponent;
49   
50    import static org.junit.Assert.assertEquals;
51    import static org.mockito.Mockito.mock;
52    import static org.mockito.Mockito.verify;
53    import static org.mockito.Mockito.when;
54   
55    /**
56    * Unit tests for {@link XWikiWikiModel}.
57    *
58    * @version $Id: ba46fd10109c87c0f98978a572f354677aa8ea67 $
59    * @since 2.0M1
60    */
61    @ComponentTest
 
62    public class XWikiWikiModelTest
63    {
64    @InjectMockComponents
65    private XWikiWikiModel wikiModel;
66   
67    @MockComponent
68    private EntityReferenceResolver<ResourceReference> referenceResolver;
69   
70    @MockComponent
71    private DocumentAccessBridge documentAccessBridge;
72   
73    @MockComponent
74    @Named("compactwiki")
75    private EntityReferenceSerializer<String> compactEntityReferenceSerializer;
76   
77    @MockComponent
78    private ExtendedRenderingConfiguration configuration;
79   
80    @MockComponent
81    private SkinAccessBridge skinAccessBridge;
82   
 
83  1 toggle @Test
84    public void getDocumentEditURLWhenNoQueryStringSpecified() throws Exception
85    {
86  1 DocumentResourceReference reference = new DocumentResourceReference("TargetSpace.TargetPage");
87  1 reference.setAnchor("anchor");
88   
89    // Note: we use a character that needs to be encoded in the current document's page name to make sure the
90    // generate query string is encoded.
91  1 DocumentReference currentDocumentReference = new DocumentReference("Wiki", "Space", "Page\u20AC");
92  1 EntityReference documentReference = new DocumentReference("TargetWiki", "TargetSpace", "TargetPage");
93   
94  1 when(this.documentAccessBridge.getCurrentDocumentReference()).thenReturn(currentDocumentReference);
95  1 when(compactEntityReferenceSerializer.serialize(currentDocumentReference)).thenReturn("Wiki:Space.Page\u20AC");
96  1 when(this.referenceResolver.resolve(reference, EntityType.DOCUMENT)).thenReturn(documentReference);
97   
98  1 this.wikiModel.getDocumentEditURL(reference);
99   
100    // Verify that getDocumentURL is called with the query string already encoded since getDocumentURL() doesn't
101    // encode it.
102  1 verify(this.documentAccessBridge).getDocumentURL(documentReference, "create",
103    "parent=Wiki%3ASpace.Page%E2%82%AC", "anchor");
104    }
105   
106    /**
107    * Tests that the proper image URL is generated when both the width and the height image parameters are specified.
108    *
109    * @throws Exception if an exception occurs while running the test
110    */
 
111  1 toggle @Test
112    public void getImageURLWhenBothWidthAndHeightAttributesAreSpecified() throws Exception
113    {
114  1 Map<String, String> parameters = new HashMap<String, String>();
115  1 parameters.put("width", "100px");
116  1 parameters.put("height", "50");
117   
118  1 testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, "width=100&height=50");
119    }
120   
121    /**
122    * Tests that the proper image URL is generated when both the width and the height image parameters are specified
123    * but including them in the image URL is forbidden from the rendering configuration.
124    *
125    * @throws Exception if an exception occurs while running the test
126    */
 
127  1 toggle @Test
128    public void getImageURLWhenIncludingImageDimensionsIsForbidden() throws Exception
129    {
130  1 Map<String, String> parameters = new HashMap<String, String>();
131  1 parameters.put("width", "101px");
132  1 parameters.put("height", "55px");
133   
134  1 testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, false, null);
135    }
136   
137    /**
138    * Tests that the proper image URL is generated when both the width and the height CSS properties are specified.
139    *
140    * @throws Exception if an exception occurs while running the test
141    */
 
142  1 toggle @Test
143    public void getImageURLWhenBothWidthAndHeightCSSPropertiesAreSpecified() throws Exception
144    {
145  1 Map<String, String> parameters = new HashMap<String, String>();
146  1 parameters.put("style", "border: 1px; height: 30px; margin-top: 2em; width: 70px");
147   
148  1 testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, "width=70&height=30");
149    }
150   
151    /**
152    * Tests that the proper image URL is generated when only the width image parameter is specified.
153    *
154    * @throws Exception if an exception occurs while running the test
155    */
 
156  1 toggle @Test
157    public void getImageURLWhenOnlyWidthAttributeIsSpecified() throws Exception
158    {
159  1 Map<String, String> parameters = new HashMap<String, String>();
160  1 parameters.put("width", "150");
161  1 parameters.put("height", "30%");
162   
163  1 testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, "width=150");
164    }
165   
166    /**
167    * Tests that the proper image URL is generated when only the height CSS property is specified.
168    *
169    * @throws Exception if an exception occurs while running the test
170    */
 
171  1 toggle @Test
172    public void getImageURLWhenOnlyHeightCSSPropertyIsSpecified() throws Exception
173    {
174  1 Map<String, String> parameters = new HashMap<String, String>();
175  1 parameters.put("style", "width: 5cm; height: 80px");
176   
177  1 testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, "height=80");
178    }
179   
180    /**
181    * Tests that the proper image URL is generated when both the width and the height are unspecified and image size is
182    * limited in the configuration.
183    *
184    * @throws Exception if an exception occurs while running the test
185    */
 
186  1 toggle @Test
187    public void getImageURLWhenBothWidthAndHeightAreUnspecifiedAndImageSizeIsLimited() throws Exception
188    {
189  1 when(configuration.getImageWidthLimit()).thenReturn(200);
190  1 when(configuration.getImageHeightLimit()).thenReturn(170);
191  1 Map<String, String> parameters = Collections.emptyMap();
192   
193  1 testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true,
194    "width=200&height=170&keepAspectRatio=true");
195    }
196   
197    /**
198    * Tests that the proper image URL is generated when both the width and the height are unspecified and only the
199    * image width is limited in the configuration.
200    *
201    * @throws Exception if an exception occurs while running the test
202    */
 
203  1 toggle @Test
204    public void getImageURLWhenBothWidthAndHeightAreUnspecifiedAndOnlyImageWidthIsLimited() throws Exception
205    {
206  1 when(configuration.getImageWidthLimit()).thenReturn(25);
207  1 when(configuration.getImageHeightLimit()).thenReturn(-1);
208  1 Map<String, String> parameters = new HashMap<String, String>();
209  1 parameters.put("width", "45%");
210  1 parameters.put("style", "height:10em");
211   
212  1 testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, "width=25");
213    }
214   
215    /**
216    * Tests that the proper image URL is generated when both the width and the height are unspecified and the image
217    * size is not limited in the configuration.
218    *
219    * @throws Exception if an exception occurs while running the test
220    */
 
221  1 toggle @Test
222    public void getImageURLWhenBothWidthAndHeightAreUnspecifiedAndImageSizeIsNotLimited() throws Exception
223    {
224  1 when(configuration.getImageWidthLimit()).thenReturn(-1);
225  1 when(configuration.getImageHeightLimit()).thenReturn(-1);
226  1 Map<String, String> parameters = new HashMap<String, String>();
227  1 parameters.put("style", "bad CSS declaration");
228   
229  1 testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, null);
230    }
231   
232    /**
233    * Tests that the proper image URL is generated when the attachment URL has a query string and a fragment
234    * identifier.
235    *
236    * @throws Exception if an exception occurs while running the test
237    */
 
238  1 toggle @Test
239    public void getImageURLWhenImageReferenceHasQueryString() throws Exception
240    {
241  1 Map<String, String> parameters = new HashMap<String, String>();
242  1 parameters.put("height", "17");
243  1 parameters.put("width", "23");
244   
245  1 AttachmentResourceReference reference = new AttachmentResourceReference("attachmentReference");
246  1 reference.setParameter(AttachmentResourceReference.QUERY_STRING, "width=41&param=value");
247  1 testImageURL(reference, parameters, true, "width=41&param=value&height=17");
248    }
249   
250    /**
251    * Tests that the proper image URL is generated when both the style and the dimension parameters are specified.
252    *
253    * @throws Exception if an exception occurs while running the test
254    */
 
255  1 toggle @Test
256    public void getImageURLWhenBothStyleAndDimensionParametersAreSpecified() throws Exception
257    {
258  1 Map<String, String> parameters = new HashMap<String, String>();
259  1 parameters.put("height", "46");
260  1 parameters.put("width", "101px");
261  1 parameters.put("style", "width: 20%; height:75px");
262   
263    // Note that the style parameter take precedence over the dimension parameters and the width is actually 20% but
264    // we can't use it for resizing the image on the server side so it's omitted from the query string.
265  1 testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, "height=75");
266    }
267   
 
268  1 toggle @Test
269    public void getImageURLWhenIcon() throws Exception
270    {
271  1 ResourceReference reference = new ResourceReference("iconname", ResourceType.ICON);
272  1 when(skinAccessBridge.getIconURL("iconname")).thenReturn("/path/to/icon");
273   
274  1 assertEquals("/path/to/icon", this.wikiModel.getImageURL(reference, Collections.<String, String>emptyMap()));
275    }
276   
 
277  1 toggle @Test
278    public void getDocumentViewURLWhenNoBaseReference() throws Exception
279    {
280  1 ResourceReference reference = new ResourceReference("reference", ResourceType.DOCUMENT);
281  1 EntityReference documentReference = new DocumentReference("wiki", "space", "page");
282  1 when(this.referenceResolver.resolve(reference, EntityType.DOCUMENT)).thenReturn(documentReference);
283  1 when(this.documentAccessBridge.getDocumentURL(documentReference, "view", null, null)).thenReturn("viewurl");
284   
285  1 assertEquals("viewurl", this.wikiModel.getDocumentViewURL(reference));
286    }
287   
 
288  1 toggle @Test
289    public void getDocumentViewURLWhenPage() throws Exception
290    {
291  1 ResourceReference reference = new PageResourceReference("page");
292  1 PageReference pageReference = new PageReference("wiki", "page");
293  1 when(this.referenceResolver.resolve(reference, EntityType.PAGE)).thenReturn(pageReference);
294  1 when(this.documentAccessBridge.getDocumentURL(pageReference, "view", null, null)).thenReturn("viewurl");
295   
296  1 assertEquals("viewurl", this.wikiModel.getDocumentViewURL(reference));
297    }
298   
 
299  1 toggle @Test
300    public void getDocumentViewURLWhenBaseReferenceSpecified() throws Exception
301    {
302  1 ResourceReference reference = new ResourceReference("reference", ResourceType.DOCUMENT);
303  1 reference.addBaseReference("base");
304  1 EntityReference documentReference = new DocumentReference("wiki", "space", "page");
305  1 when(this.referenceResolver.resolve(reference, EntityType.DOCUMENT)).thenReturn(documentReference);
306  1 when(this.documentAccessBridge.getDocumentURL(documentReference, "view", null, null)).thenReturn("viewurl");
307   
308  1 assertEquals("viewurl", this.wikiModel.getDocumentViewURL(reference));
309    }
310   
 
311  1 toggle @Test
312    public void getXDOM() throws Exception
313    {
314  1 ResourceReference reference = new ResourceReference("reference", ResourceType.DOCUMENT);
315  1 EntityReference documentReference = new DocumentReference("wiki", "space", "page");
316  1 when(this.referenceResolver.resolve(reference, EntityType.DOCUMENT)).thenReturn(documentReference);
317  1 DocumentModelBridge dmb = mock(DocumentModelBridge.class);
318  1 when(this.documentAccessBridge.getTranslatedDocumentInstance(documentReference)).thenReturn(dmb);
319  1 XDOM xdom = new XDOM(Collections.emptyList());
320  1 when(dmb.getXDOM()).thenReturn(xdom);
321   
322  1 assertEquals(xdom, this.wikiModel.getXDOM(reference));
323    }
324   
 
325  10 toggle private void testImageURL(final ResourceReference imageReference, Map<String, String> parameters,
326    final boolean expectedIsImageDimensionsIncludedInImageURL, final String expectedQueryString) throws Exception
327    {
328  10 AttachmentReference attachmentReference =
329    new AttachmentReference("image", new DocumentReference("wiki", "space", "page"));
330  10 when(configuration.isImageDimensionsIncludedInImageURL())
331    .thenReturn(expectedIsImageDimensionsIncludedInImageURL);
332  10 when(this.referenceResolver.resolve(imageReference, EntityType.ATTACHMENT)).thenReturn(attachmentReference);
333  10 when(this.documentAccessBridge.getAttachmentURL(attachmentReference, expectedQueryString, false))
334    .thenReturn("?" + expectedQueryString);
335   
336  10 assertEquals("?" + expectedQueryString, this.wikiModel.getImageURL(imageReference, parameters));
337    }
338    }