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

File DefaultWikiMacroTest.java

 

Code metrics

0
99
19
1
416
278
19
0.19
5.21
19
1

Classes

Class Line # Actions
DefaultWikiMacroTest 77 99 0% 19 0
1.0100%
 

Contributing tests

This file is covered by 10 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.wikimacro.internal;
21   
22    import java.io.Reader;
23    import java.io.StringReader;
24    import java.io.Writer;
25    import java.util.Arrays;
26    import java.util.Collections;
27    import java.util.List;
28    import java.util.Properties;
29   
30    import org.apache.velocity.VelocityContext;
31    import org.junit.jupiter.api.BeforeEach;
32    import org.junit.jupiter.api.Test;
33    import org.mockito.invocation.InvocationOnMock;
34    import org.mockito.stubbing.Answer;
35    import org.xwiki.component.manager.ComponentLookupException;
36    import org.xwiki.model.reference.DocumentReference;
37    import org.xwiki.rendering.converter.ConversionException;
38    import org.xwiki.rendering.converter.Converter;
39    import org.xwiki.rendering.listener.reference.DocumentResourceReference;
40    import org.xwiki.rendering.macro.MacroId;
41    import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor;
42    import org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor;
43    import org.xwiki.rendering.macro.wikibridge.WikiMacroManager;
44    import org.xwiki.rendering.macro.wikibridge.WikiMacroParameterDescriptor;
45    import org.xwiki.rendering.macro.wikibridge.WikiMacroVisibility;
46    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
47    import org.xwiki.rendering.syntax.Syntax;
48    import org.xwiki.rendering.wiki.WikiModel;
49    import org.xwiki.security.authorization.Right;
50    import org.xwiki.test.annotation.AllComponents;
51    import org.xwiki.test.junit5.mockito.InjectComponentManager;
52    import org.xwiki.test.junit5.mockito.MockComponent;
53    import org.xwiki.test.mockito.MockitoComponentManager;
54    import org.xwiki.velocity.VelocityEngine;
55    import org.xwiki.velocity.VelocityManager;
56   
57    import com.xpn.xwiki.doc.XWikiDocument;
58    import com.xpn.xwiki.objects.BaseObject;
59    import com.xpn.xwiki.test.MockitoOldcore;
60    import com.xpn.xwiki.test.junit5.mockito.InjectMockitoOldcore;
61    import com.xpn.xwiki.test.junit5.mockito.OldcoreTest;
62   
63    import static org.junit.jupiter.api.Assertions.assertEquals;
64    import static org.junit.jupiter.api.Assertions.assertFalse;
65    import static org.junit.jupiter.api.Assertions.assertTrue;
66    import static org.mockito.ArgumentMatchers.any;
67    import static org.mockito.ArgumentMatchers.same;
68    import static org.mockito.Mockito.when;
69   
70    /**
71    * Unit tests for {@link DefaultWikiMacro}.
72    *
73    * @version $Id: 953275365647fe64f3eb01c1e35ec857dfd812ed $
74    */
75    @OldcoreTest
76    @AllComponents
 
77    public class DefaultWikiMacroTest
78    {
79    @MockComponent
80    private WikiModel mockWikiModel;
81   
82    @InjectMockitoOldcore
83    private MockitoOldcore oldcore;
84   
85    @InjectComponentManager
86    private MockitoComponentManager componentManager;
87   
88    /**
89    * Dummy document reference of the document which contains the wiki macro.
90    */
91    private DocumentReference wikiMacroDocumentReference;
92   
93    private XWikiDocument wikiMacroDocument;
94   
95    private BaseObject wikiMacroObject;
96   
97    private WikiMacroManager wikiMacroManager;
98   
99    private XWikiDocument user;
100   
 
101  10 toggle @BeforeEach
102    public void beforeEach() throws Exception
103    {
104  10 this.wikiMacroDocumentReference =
105    new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "space", "macroPage");
106  10 this.wikiMacroManager = this.componentManager.getInstance(WikiMacroManager.class);
107   
108  10 this.wikiMacroDocument = new XWikiDocument(wikiMacroDocumentReference);
109   
110  10 this.user =
111    new XWikiDocument(new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki", "user"));
112  10 this.user.setNew(false);
113  10 BaseObject userObject = new BaseObject();
114  10 userObject.setXClassReference(
115    new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki", "XWikiusers"));
116  10 this.user.addXObject(userObject);
117   
118  10 this.oldcore.getSpyXWiki().saveDocument(this.user, this.oldcore.getXWikiContext());
119   
120  10 this.wikiMacroDocument.setCreatorReference(this.user.getAuthorReference());
121  10 this.wikiMacroDocument.setAuthorReference(this.user.getAuthorReference());
122  10 this.wikiMacroDocument.setContentAuthorReference(this.user.getAuthorReference());
123   
124  10 this.wikiMacroObject = new BaseObject();
125  10 this.wikiMacroObject.setXClassReference(new DocumentReference(this.oldcore.getXWikiContext().getWikiId(),
126    WikiMacroConstants.WIKI_MACRO_PARAMETER_CLASS_SPACE, WikiMacroConstants.WIKI_MACRO_PARAMETER_CLASS_PAGE));
127  10 this.wikiMacroDocument.addXObject(this.wikiMacroObject);
128   
129  10 this.oldcore.getSpyXWiki().saveDocument(this.wikiMacroDocument, this.oldcore.getXWikiContext());
130   
131  10 when(this.oldcore.getMockAuthorizationManager().hasAccess(same(Right.PROGRAM), any(), any())).thenReturn(true);
132  10 when(this.oldcore.getMockContextualAuthorizationManager().hasAccess(same(Right.PROGRAM))).thenReturn(true);
133  10 when(this.oldcore.getMockContextualAuthorizationManager().hasAccess(same(Right.SCRIPT))).thenReturn(true);
134   
135  10 this.oldcore.getXWikiContext().setUserReference(this.user.getDocumentReference());
136    }
137   
 
138  9 toggle private void registerWikiMacro(String macroId, String macroContent, Syntax syntax) throws Exception
139    {
140  9 List<WikiMacroParameterDescriptor> parameterDescriptors =
141    Arrays.asList(new WikiMacroParameterDescriptor("param1", "This is param1", true),
142    new WikiMacroParameterDescriptor("param2", "This is param2", true));
143  9 registerWikiMacro(macroId, macroContent, syntax, parameterDescriptors);
144    }
145   
 
146  11 toggle private void registerWikiMacro(String macroId, String macroContent, Syntax syntax,
147    List<WikiMacroParameterDescriptor> parameterDescriptors) throws Exception
148    {
149  11 WikiMacroDescriptor descriptor = new WikiMacroDescriptor.Builder().id(new MacroId(macroId)).name("Wiki Macro")
150    .description("Description").defaultCategory("Test").visibility(WikiMacroVisibility.GLOBAL)
151    .supportsInlineMode(true).contentDescriptor(new DefaultContentDescriptor(false))
152    .parameterDescriptors(parameterDescriptors).build();
153   
154  11 this.wikiMacroDocument.setSyntax(syntax);
155  11 this.wikiMacroObject.setLargeStringValue(WikiMacroConstants.MACRO_CODE_PROPERTY, macroContent);
156   
157  11 DefaultWikiMacro wikiMacro = new DefaultWikiMacro(this.wikiMacroObject, descriptor, this.componentManager);
158   
159  11 this.wikiMacroManager.registerWikiMacro(wikiMacroDocumentReference, wikiMacro);
160    }
161   
 
162  2 toggle private void registerWikiMacro(String macroId, String macroContent) throws Exception
163    {
164  2 registerWikiMacro(macroId, macroContent, Syntax.XWIKI_2_0);
165    }
166   
 
167  1 toggle private void assertXDOM(String expectEvents, String inputXWiki) throws ComponentLookupException, ConversionException
168    {
169  1 assertMacro(expectEvents, Syntax.XDOMXML_CURRENT, inputXWiki);
170    }
171   
 
172  5 toggle private void assertXHTML(String expectXHTML, String inputXWiki) throws ComponentLookupException, ConversionException
173    {
174  5 assertMacro(expectXHTML, Syntax.XHTML_1_0, inputXWiki);
175    }
176   
 
177  6 toggle private void assertMacro(String expect, Syntax expectSyntax, String inputXWiki)
178    throws ComponentLookupException, ConversionException
179    {
180  6 Converter converter = this.componentManager.getInstance(Converter.class);
181   
182  6 DefaultWikiPrinter printer = new DefaultWikiPrinter();
183  6 converter.convert(new StringReader(inputXWiki), Syntax.XWIKI_2_0, expectSyntax, printer);
184   
185    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
186  6 assertEquals(expect, printer.toString());
187   
188  6 assertFalse(this.oldcore.getXWikiContext().containsKey("macro"));
189    }
190   
191    // Tests
192   
193    /**
194    * Test normal wiki macro execution.
195    */
 
196  1 toggle @Test
197    public void testExecute() throws Exception
198    {
199  1 registerWikiMacro("wikimacro1", "This is **bold**", Syntax.XWIKI_2_0);
200   
201  1 assertXHTML("<p>This is <strong>bold</strong></p>", "{{wikimacro1 param1=\"value1\" param2=\"value2\"/}}");
202  1 assertXDOM("<document><p><metadata><metadata><entry><string>syntax</string>"
203    + "<org.xwiki.rendering.syntax.Syntax><type><name>XWiki</name><id>xwiki</id></type><version>2.0</version></org.xwiki.rendering.syntax.Syntax></entry></metadata></metadata></p>"
204    + "<macroMarker name=\"wikimacro1\"><p><parameters><m><entry><string>param1</string><string>value1</string></entry><entry><string>param2</string><string>value2</string></entry></m></parameters></p>"
205    + "<paragraph><word>This</word><space></space><word>is</word><space></space><format format=\"BOLD\"><word>bold</word></format></paragraph>"
206    + "</macroMarker></document>", "{{wikimacro1 param1=\"value1\" param2=\"value2\"/}}");
207    }
208   
209    /**
210    * When a wiki macro is used in inline mode and its code starts with a macro, that nested macro is made inline. In
211    * other words, the nested macro should not generate extra paragraph elements.
212    */
 
213  1 toggle @Test
214    public void testExecuteWhenInlineAndWithMacro() throws Exception
215    {
216  1 registerWikiMacro("wikimacro1", "This is **bold**", Syntax.XWIKI_2_0);
217  1 registerWikiMacro("wikimacro2", "{{wikimacro1 param1=\"v1\" param2=\"v2\"/}}", Syntax.XWIKI_2_0);
218   
219    // Note: We're putting the macro after the "Hello" text to force it as an inline macro.
220  1 assertXHTML("<p>Hello This is <strong>bold</strong></p>",
221    "Hello {{wikimacro2 param1=\"value1\" param2=\"value2\"/}}");
222    }
223   
224    /**
225    * Check that macro used inside wiki macro are executed as part of the document.
226    */
 
227  1 toggle @Test
228    public void testExecuteWhenInnerMacro() throws Exception
229    {
230  1 registerWikiMacro("wikimacro1", "{{toc/}}", Syntax.XWIKI_2_0);
231   
232  1 DocumentResourceReference reference = new DocumentResourceReference(null);
233  1 reference.setAnchor("Hheading");
234  1 when(this.mockWikiModel.getDocumentViewURL(reference)).thenReturn("url");
235   
236  1 assertXHTML(
237    "<h1 id=\"Hheading\" class=\"wikigeneratedid\"><span>heading</span></h1>"
238    + "<ul><li><span class=\"wikilink\"><a href=\"#Hheading\">heading</a></span></li></ul>",
239    "= heading\n\n{{wikimacro1 param1=\"value1\" param2=\"value2\"/}}");
240    }
241   
242    /**
243    * Check that macro used inside wiki macro are executed with the right syntax.
244    */
 
245  1 toggle @Test
246    public void testExecuteWhenWithDifferentMacroSyntax() throws Exception
247    {
248  1 registerWikiMacro("wikimacro", "{{groovy}}println \"[[path:/some/path]]\"{{/groovy}}", Syntax.XWIKI_2_1);
249   
250  1 assertXHTML(
251    "<p><span class=\"wikiinternallink\"><a href=\"/some/path\">"
252    + "<span class=\"wikigeneratedlinkcontent\">/some/path</span></a></span></p>",
253    "{{wikimacro param1=\"value1\" param2=\"value2\"/}}");
254    }
255   
256    /**
257    * A wiki macro can directly provide the list of blocks instead of having to render them to let
258    * {@link DefaultWikiMacro} re-parse it.
259    */
 
260  1 toggle @Test
261    public void testExecuteWhenWikiMacroDirectlyProvideTheResult() throws Exception
262    {
263  1 registerWikiMacro("wikimacrowithresult", "{{groovy}}"
264    + "xcontext.macro.result = java.util.Collections.singletonList(new org.xwiki.rendering.block.WordBlock(xcontext.macro.params.param1));"
265    + "{{/groovy}}", Syntax.XWIKI_2_0);
266   
267  1 assertXHTML("<p>Hello World</p>", "Hello {{wikimacrowithresult param1=\"World\" param2=\"param2\"/}}");
268    }
269   
270    /**
271    * Test default parameter value injection.
272    */
 
273  1 toggle @Test
274    public void testDefaultParameterValues() throws Exception
275    {
276    // Override default velocity manager with a mock
277  1 VelocityManager mockVelocityManager = this.componentManager.registerMockComponent(VelocityManager.class);
278   
279    // Initialize velocity engine.
280  1 final VelocityEngine vEngine = this.componentManager.getInstance(VelocityEngine.class);
281  1 Properties properties = new Properties();
282  1 properties.setProperty("resource.loader", "file");
283  1 vEngine.initialize(properties);
284   
285    // Hack into velocity context.
286  1 final VelocityContext vContext = new VelocityContext();
287  1 vContext.put("xcontext", this.oldcore.getXWikiContext());
288   
289  1 when(mockVelocityManager.getCurrentVelocityContext()).thenReturn(vContext);
290  1 when(mockVelocityManager.evaluate(any(Writer.class), any(String.class), any(Reader.class)))
291    .thenAnswer(new Answer<Boolean>()
292    {
 
293  1 toggle @Override
294    public Boolean answer(InvocationOnMock invocation) throws Throwable
295    {
296  1 return vEngine.evaluate(vContext, (Writer) invocation.getArgument(0),
297    (String) invocation.getArgument(1), (Reader) invocation.getArgument(2));
298    }
299    });
300   
301  1 List<WikiMacroParameterDescriptor> parameterDescriptors =
302    Arrays.asList(new WikiMacroParameterDescriptor("param1", "This is param1", false, "default_value"));
303   
304  1 registerWikiMacro("wikimacro1",
305    "{{velocity}}$xcontext.macro.params.param1 $xcontext.macro.params.paraM1{{/velocity}}", Syntax.XWIKI_2_0,
306    parameterDescriptors);
307   
308  1 Converter converter = this.componentManager.getInstance(Converter.class);
309   
310  1 DefaultWikiPrinter printer = new DefaultWikiPrinter();
311  1 converter.convert(new StringReader("{{wikimacro1/}}"), Syntax.XWIKI_2_0, Syntax.PLAIN_1_0, printer);
312   
313  1 assertEquals("default_value default_value", printer.toString());
314    }
315   
316    /**
317    * Makes sure getParameterNames return parameters with source case.
318    */
 
319  1 toggle @Test
320    public void testDefaultParameterNames() throws Exception
321    {
322    // Override default velocity manager with a mock
323  1 VelocityManager mockVelocityManager = this.componentManager.registerMockComponent(VelocityManager.class);
324   
325    // Initialize velocity engine.
326  1 final VelocityEngine vEngine = this.componentManager.getInstance(VelocityEngine.class);
327  1 Properties properties = new Properties();
328  1 properties.setProperty("resource.loader", "file");
329  1 vEngine.initialize(properties);
330   
331    // Hack into velocity context.
332  1 final VelocityContext vContext = new VelocityContext();
333  1 vContext.put("xcontext", this.oldcore.getXWikiContext());
334   
335  1 when(mockVelocityManager.getCurrentVelocityContext()).thenReturn(vContext);
336  1 when(mockVelocityManager.evaluate(any(Writer.class), any(String.class), any(Reader.class)))
337    .thenAnswer(new Answer<Boolean>()
338    {
 
339  1 toggle @Override
340    public Boolean answer(InvocationOnMock invocation) throws Throwable
341    {
342  1 return vEngine.evaluate(vContext, (Writer) invocation.getArgument(0),
343    (String) invocation.getArgument(1), (Reader) invocation.getArgument(2));
344    }
345    });
346   
347  1 registerWikiMacro("wikimacro1", "{{velocity}}$xcontext.macro.params.parameterNames{{/velocity}}",
348    Syntax.XWIKI_2_0);
349   
350  1 Converter converter = this.componentManager.getInstance(Converter.class);
351   
352  1 DefaultWikiPrinter printer = new DefaultWikiPrinter();
353  1 converter.convert(new StringReader("{{wikimacro1 paRam1=\"value1\" paraM2=\"value2\"/}}"), Syntax.XWIKI_2_0,
354    Syntax.PLAIN_1_0, printer);
355   
356  1 assertEquals("[paRam1, paraM2]", printer.toString());
357    }
358   
 
359  1 toggle @Test
360    public void testGetCurrentMacroBlock() throws Exception
361    {
362  1 registerWikiMacro("wikimacro",
363    "{{groovy}}" + "println xcontext.macro.context.getCurrentMacroBlock().id\n"
364    + "println xcontext.macro.context.getCurrentMacroBlock().parent.class\n"
365    + "println xcontext.macro.context.getCurrentMacroBlock().nextSibling.children[0].word\n"
366    + "println xcontext.macro.context.getCurrentMacroBlock().previousSibling.children[0].word\n"
367    + "{{/groovy}}",
368    Syntax.XWIKI_2_0, Collections.<WikiMacroParameterDescriptor>emptyList());
369   
370  1 Converter converter = this.componentManager.getInstance(Converter.class);
371   
372  1 DefaultWikiPrinter printer = new DefaultWikiPrinter();
373  1 converter.convert(new StringReader("before\n\n{{wikimacro/}}\n\nafter"), Syntax.XWIKI_2_0, Syntax.PLAIN_1_0,
374    printer);
375   
376  1 assertEquals("before" + "\n\n" + "wikimacro\n" + "class org.xwiki.rendering.block.XDOM\n" + "after\n" + "before"
377    + "\n\n" + "after", printer.toString());
378    }
379   
 
380  1 toggle @Test
381    public void testExecuteWhenWikiMacroBinding() throws Exception
382    {
383  1 registerWikiMacro("wikimacrobindings", "{{groovy}}" + "print xcontext.macro.doc" + "{{/groovy}}");
384   
385  1 Converter converter = this.componentManager.getInstance(Converter.class);
386   
387  1 DefaultWikiPrinter printer = new DefaultWikiPrinter();
388  1 converter.convert(new StringReader("{{wikimacrobindings param1=\"value2\" param2=\"value2\"/}}"),
389    Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
390   
391    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
392  1 assertEquals("<p>" + this.wikiMacroDocument.toString() + "</p>", printer.toString());
393    }
394   
 
395  1 toggle @Test
396    public void testExecuteWhenWikiRequiringPRAfterDropPermission() throws Exception
397    {
398  1 registerWikiMacro("wikimacrobindings", "{{groovy}}" + "print xcontext.macro.doc" + "{{/groovy}}");
399   
400  1 Converter converter = this.componentManager.getInstance(Converter.class);
401   
402  1 DefaultWikiPrinter printer = new DefaultWikiPrinter();
403   
404  1 this.oldcore.getXWikiContext().dropPermissions();
405  1 this.wikiMacroDocument.newDocument(this.oldcore.getXWikiContext()).dropPermissions();
406   
407  1 converter.convert(new StringReader("{{wikimacrobindings param1=\"value2\" param2=\"value2\"/}}"),
408    Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
409   
410    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
411  1 assertEquals("<p>" + this.wikiMacroDocument.toString() + "</p>", printer.toString());
412  1 assertTrue(this.oldcore.getXWikiContext().hasDroppedPermissions(),
413    "Wiki macro did not properly restord persmission dropping");
414    }
415   
416    }