1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.wikimacro.internal

File DefaultWikiMacroTest.java

 

Code metrics

0
72
9
1
247
168
9
0.12
8
9
1

Classes

Class Line # Actions
DefaultWikiMacroTest 66 72 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 2 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.StringReader;
23    import java.util.Arrays;
24    import java.util.Collections;
25    import java.util.List;
26   
27    import org.jmock.Expectations;
28    import org.jmock.api.Invocation;
29    import org.jmock.lib.action.CustomAction;
30    import org.junit.Assert;
31    import org.junit.Before;
32    import org.junit.Test;
33    import org.xwiki.model.EntityType;
34    import org.xwiki.model.reference.DocumentReference;
35    import org.xwiki.model.reference.EntityReference;
36    import org.xwiki.rendering.converter.Converter;
37    import org.xwiki.rendering.internal.macro.wikibridge.DefaultWikiMacro;
38    import org.xwiki.rendering.macro.MacroId;
39    import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor;
40    import org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor;
41    import org.xwiki.rendering.macro.wikibridge.WikiMacroManager;
42    import org.xwiki.rendering.macro.wikibridge.WikiMacroParameterDescriptor;
43    import org.xwiki.rendering.macro.wikibridge.WikiMacroVisibility;
44    import org.xwiki.rendering.parser.Parser;
45    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
46    import org.xwiki.rendering.syntax.Syntax;
47    import org.xwiki.security.authorization.ContextualAuthorizationManager;
48    import org.xwiki.security.authorization.Right;
49    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
50   
51    import com.xpn.xwiki.XWiki;
52    import com.xpn.xwiki.XWikiContext;
53    import com.xpn.xwiki.doc.XWikiDocument;
54    import com.xpn.xwiki.objects.BaseObject;
55    import com.xpn.xwiki.test.AbstractBridgedComponentTestCase;
56    import com.xpn.xwiki.user.api.XWikiGroupService;
57    import com.xpn.xwiki.user.api.XWikiRightService;
58    import com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl;
59   
60    /**
61    * Unit tests for {@link DefaultWikiMacro} in the context of XWiki core.
62    *
63    * @version $Id: 787c3147f376d93d4ec8f7aeca69c587069cbed0 $
64    * @since 2.5M1
65    */
 
66    public class DefaultWikiMacroTest extends AbstractBridgedComponentTestCase
67    {
68    private static final EntityReference XWIKIPREFERENCES_REFERENCE = new EntityReference("XWikiPreferences",
69    EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE));
70   
71    /**
72    * Dummy document reference of the document which contains the wiki macro.
73    */
74    private DocumentReference wikiMacroDocumentReference;
75   
76    /**
77    * The {@link org.xwiki.rendering.macro.wikibridge.WikiMacroManager} component.
78    */
79    private WikiMacroManager wikiMacroManager;
80   
81    private Parser xwiki20Parser;
82   
83    private XWikiDocument wikiMacroDocument;
84   
85    private XWikiDocument user;
86   
87    private WikiDescriptorManager mockWikiDescriptorManager;
88   
 
89  2 toggle @Override
90    protected void registerComponents() throws Exception
91    {
92  2 super.registerComponents();
93   
94  2 this.mockWikiDescriptorManager = registerMockComponent(WikiDescriptorManager.class);
95    }
96   
 
97  2 toggle @Override
98    @Before
99    public void setUp() throws Exception
100    {
101  2 super.setUp();
102   
103  2 getMockery().checking(new Expectations()
104    {
 
105  2 toggle {
106  2 allowing(mockWikiDescriptorManager).getCurrentWikiId();
107  2 will(new CustomAction("WikiDescriptorManager#getCurrentWikiId")
108    {
 
109  57 toggle @Override
110    public Object invoke(Invocation invocation) throws Throwable
111    {
112  57 return getContext().getWikiId();
113    }
114    });
115    }
116    });
117   
118  2 final ContextualAuthorizationManager mockCam = getContextualAuthorizationManager();
119   
120  2 final XWiki mockXWiki = getMockery().mock(XWiki.class);
121  2 final XWikiGroupService mockXWikiGroupService = getMockery().mock(XWikiGroupService.class);
122   
123  2 getContext().setWiki(mockXWiki);
124   
125  2 this.xwiki20Parser = getComponentManager().getInstance(Parser.class, "xwiki/2.0");
126   
127  2 this.wikiMacroDocumentReference = new DocumentReference(getContext().getWikiId(), "space", "macroPage");
128  2 this.wikiMacroManager = getComponentManager().getInstance(WikiMacroManager.class);
129   
130  2 this.wikiMacroDocument = new XWikiDocument(wikiMacroDocumentReference);
131   
132  2 final XWikiRightService rightService = new XWikiRightServiceImpl();
133   
134  2 this.user = new XWikiDocument(new DocumentReference(getContext().getWikiId(), "XWiki", "user"));
135  2 this.user.setNew(false);
136  2 BaseObject userObject = new BaseObject();
137  2 userObject.setXClassReference(new DocumentReference(getContext().getWikiId(), "XWiki", "XWikiusers"));
138  2 this.user.addXObject(userObject);
139   
140  2 this.wikiMacroDocument.setCreatorReference(this.user.getAuthorReference());
141  2 this.wikiMacroDocument.setAuthorReference(this.user.getAuthorReference());
142  2 this.wikiMacroDocument.setContentAuthorReference(this.user.getAuthorReference());
143   
144    // Setup an XWikiPreferences document granting programming rights to user
145  2 final XWikiDocument prefs =
146    new XWikiDocument(new DocumentReference(getContext().getWikiId(), "XWiki", "XWikiPreferences"));
147  2 final BaseObject mockGlobalRightObj = getMockery().mock(BaseObject.class);
148   
149  2 getMockery().checking(new Expectations()
150    {
 
151  2 toggle {
152  2 allowing(mockCam).hasAccess(Right.PROGRAM);
153  2 will(returnValue(true));
154   
155  2 allowing(mockXWiki).getDocument(with(equal(wikiMacroDocumentReference)), with(any(XWikiContext.class)));
156  2 will(returnValue(wikiMacroDocument));
157   
158  2 allowing(mockXWiki).isReadOnly();
159  2 will(returnValue(false));
160  2 allowing(mockXWiki).getLanguagePreference(with(any(XWikiContext.class)));
161  2 will(returnValue(null));
162  2 allowing(mockXWiki).getRightService();
163  2 will(returnValue(rightService));
164  2 allowing(mockXWiki).getGroupService(with(any(XWikiContext.class)));
165  2 will(returnValue(mockXWikiGroupService));
166   
167  2 allowing(mockXWikiGroupService).getAllGroupsReferencesForMember(with(any(DocumentReference.class)),
168    with(any(int.class)), with(any(int.class)), with(any(XWikiContext.class)));
169  2 will(returnValue(Collections.EMPTY_LIST));
170   
171  2 allowing(mockXWiki).getDocument(with(equal(XWIKIPREFERENCES_REFERENCE)), with(any(XWikiContext.class)));
172  2 will(returnValue(prefs));
173  2 allowing(mockGlobalRightObj).getStringValue("levels");
174  2 will(returnValue("programming"));
175  2 allowing(mockGlobalRightObj).getStringValue("users");
176  2 will(returnValue(user.getFullName()));
177  2 allowing(mockGlobalRightObj).getIntValue("allow");
178  2 will(returnValue(1));
179  2 allowing(mockGlobalRightObj).setNumber(with(any(int.class)));
180  2 allowing(mockGlobalRightObj).setDocumentReference(with(any(DocumentReference.class)));
181  2 allowing(mockGlobalRightObj).setOwnerDocument(with(any(XWikiDocument.class)));
182    }
183    });
184   
185  2 prefs.addObject("XWiki.XWikiGlobalRights", mockGlobalRightObj);
186   
187  2 getContext().setUserReference(this.user.getDocumentReference());
188    }
189   
 
190  1 toggle @Test
191    public void testExecuteWhenWikiMacroBinding() throws Exception
192    {
193  1 registerWikiMacro("wikimacrobindings", "{{groovy}}" + "print xcontext.macro.doc" + "{{/groovy}}");
194   
195  1 Converter converter = getComponentManager().getInstance(Converter.class);
196   
197  1 DefaultWikiPrinter printer = new DefaultWikiPrinter();
198  1 converter.convert(new StringReader("{{wikimacrobindings param1=\"value2\" param2=\"value2\"/}}"),
199    Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
200   
201    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
202  1 Assert.assertEquals("<p>" + this.wikiMacroDocument.toString() + "</p>", printer.toString());
203    }
204   
 
205  1 toggle @Test
206    public void testExecuteWhenWikiRequiringPRAfterDropPermission() throws Exception
207    {
208  1 registerWikiMacro("wikimacrobindings", "{{groovy}}" + "print xcontext.macro.doc" + "{{/groovy}}");
209   
210  1 Converter converter = getComponentManager().getInstance(Converter.class);
211   
212  1 DefaultWikiPrinter printer = new DefaultWikiPrinter();
213   
214  1 getContext().dropPermissions();
215  1 this.wikiMacroDocument.newDocument(getContext()).dropPermissions();
216   
217  1 converter.convert(new StringReader("{{wikimacrobindings param1=\"value2\" param2=\"value2\"/}}"),
218    Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
219   
220    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
221  1 Assert.assertEquals("<p>" + this.wikiMacroDocument.toString() + "</p>", printer.toString());
222  1 Assert.assertTrue("Wiki macro did not properly restord persmission dropping", getContext()
223    .hasDroppedPermissions());
224    }
225   
 
226  2 toggle private void registerWikiMacro(String macroId, String macroContent) throws Exception
227    {
228  2 List<WikiMacroParameterDescriptor> parameterDescriptors =
229    Arrays.asList(new WikiMacroParameterDescriptor("param1", "This is param1", true),
230    new WikiMacroParameterDescriptor("param2", "This is param2", true));
231  2 registerWikiMacro(macroId, macroContent, parameterDescriptors);
232    }
233   
 
234  2 toggle private void registerWikiMacro(String macroId, String macroContent,
235    List<WikiMacroParameterDescriptor> parameterDescriptors) throws Exception
236    {
237  2 WikiMacroDescriptor descriptor =
238    new WikiMacroDescriptor(new MacroId(macroId), "Wiki Macro", "Description", "Test",
239    WikiMacroVisibility.GLOBAL, new DefaultContentDescriptor(false), parameterDescriptors);
240   
241  2 DefaultWikiMacro wikiMacro =
242    new DefaultWikiMacro(wikiMacroDocumentReference, this.user.getDocumentReference(), true, descriptor,
243    this.xwiki20Parser.parse(new StringReader(macroContent)), Syntax.XWIKI_2_0, getComponentManager());
244   
245  2 this.wikiMacroManager.registerWikiMacro(wikiMacroDocumentReference, wikiMacro);
246    }
247    }