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

File WikiMacrosTest.java

 

Code metrics

0
41
5
1
154
88
7
0.17
8.2
5
1.4

Classes

Class Line # Actions
WikiMacrosTest 47 41 0% 7 2
0.9565217595.7%
 

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 org.junit.Assert;
23    import org.junit.Before;
24    import org.junit.Rule;
25    import org.junit.Test;
26    import org.xwiki.component.manager.ComponentLookupException;
27    import org.xwiki.component.manager.ComponentManager;
28    import org.xwiki.model.reference.DocumentReference;
29    import org.xwiki.rendering.macro.Macro;
30    import org.xwiki.rendering.syntax.Syntax;
31    import org.xwiki.test.annotation.AllComponents;
32   
33    import com.xpn.xwiki.XWikiContext;
34    import com.xpn.xwiki.doc.XWikiDocument;
35    import com.xpn.xwiki.objects.BaseObject;
36    import com.xpn.xwiki.test.MockitoOldcoreRule;
37   
38    import static org.mockito.ArgumentMatchers.any;
39    import static org.mockito.Mockito.when;
40   
41    /**
42    * Various general tests on wiki macros.
43    *
44    * @version $Id: d7667e80fc9b98047caadf9203b59396c1776d64 $
45    */
46    @AllComponents
 
47    public class WikiMacrosTest
48    {
49    @Rule
50    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
51   
52    private XWikiDocument macroDocument;
53   
54    private BaseObject macroObject;
55   
 
56  2 toggle @Before
57    public void before() throws Exception
58    {
59  2 this.macroDocument = new XWikiDocument(new DocumentReference("wiki", "Space", "Page"));
60  2 this.macroDocument.setSyntax(Syntax.XWIKI_2_0);
61  2 this.macroObject = new BaseObject();
62  2 this.macroObject.setXClassReference(new DocumentReference("wiki", "XWiki", "WikiMacroClass"));
63  2 this.macroObject.setStringValue("id", "macroid");
64  2 this.macroObject.setLargeStringValue("code", "code");
65  2 this.macroDocument.addXObject(macroObject);
66   
67  2 this.oldcore.getXWikiContext().setWikiId("wiki");
68   
69    // We need component related events
70  2 this.oldcore.notifyComponentDescriptorEvent();
71  2 this.oldcore.notifyDocumentCreatedEvent(true);
72  2 this.oldcore.notifyDocumentUpdatedEvent(true);
73    }
74   
 
75  1 toggle private ComponentManager getWikiComponentManager() throws Exception
76    {
77  1 return this.oldcore.getMocker().getInstance(ComponentManager.class, "wiki");
78    }
79   
 
80  3 toggle private ComponentManager getUserComponentManager() throws Exception
81    {
82  3 return this.oldcore.getMocker().getInstance(ComponentManager.class, "user");
83    }
84   
 
85  1 toggle @Test
86    public void testSaveWikiMacro() throws Exception
87    {
88  1 when(this.oldcore.getMockRightService().hasAccessLevel(any(String.class), any(String.class), any(String.class), any(XWikiContext.class))).thenReturn(true);
89  1 when(this.oldcore.getMockRightService().hasWikiAdminRights(any(XWikiContext.class))).thenReturn(true);
90  1 when(this.oldcore.getMockRightService().hasProgrammingRights(any(XWikiContext.class))).thenReturn(true);
91   
92  1 this.macroObject.setStringValue("visibility", "Current Wiki");
93   
94    // Save wiki macro
95  1 this.oldcore.getSpyXWiki().saveDocument(this.macroDocument, this.oldcore.getXWikiContext());
96   
97  1 Macro testMacro = getWikiComponentManager().getInstance(Macro.class, "macroid");
98   
99  1 Assert.assertEquals("macroid", testMacro.getDescriptor().getId().getId());
100   
101  1 try {
102  1 testMacro = this.oldcore.getMocker().getInstance(Macro.class, "macroid");
103   
104  0 Assert.fail("Found macro with wiki visibility in global componenet manager");
105    } catch (ComponentLookupException expected) {
106    }
107    }
108   
 
109  1 toggle @Test
110    public void testUnRegisterWikiMacroWithDifferentVisibilityKeys() throws Exception
111    {
112  1 when(this.oldcore.getMockRightService().hasAccessLevel(any(String.class), any(String.class), any(String.class), any(XWikiContext.class))).thenReturn(true);
113   
114  1 this.macroObject.setStringValue("visibility", "Current User");
115   
116  1 DocumentReference user1 = new DocumentReference("wiki", "Wiki", "user1");
117   
118  1 this.macroDocument.setAuthorReference(user1);
119   
120    // Save wiki macro
121  1 this.oldcore.getSpyXWiki().saveDocument(this.macroDocument, this.oldcore.getXWikiContext());
122   
123    // Try to lookup the macro
124  1 this.oldcore.getXWikiContext().setUserReference(user1);
125  1 Macro testMacro = getUserComponentManager().getInstance(Macro.class, "macroid");
126   
127  1 Assert.assertEquals("macroid", testMacro.getDescriptor().getId().getId());
128   
129    // register with another user
130   
131  1 DocumentReference user2 = new DocumentReference("wiki", "Wiki", "user2");
132   
133  1 this.macroDocument.setAuthorReference(user2);
134   
135    // Save wiki macro
136  1 this.oldcore.getSpyXWiki().saveDocument(this.macroDocument, this.oldcore.getXWikiContext());
137   
138    // Try to lookup the macro
139  1 this.oldcore.getXWikiContext().setUserReference(user2);
140  1 testMacro = getUserComponentManager().getInstance(Macro.class, "macroid");
141   
142  1 Assert.assertEquals("macroid", testMacro.getDescriptor().getId().getId());
143   
144    // validate that the macro as been properly unregistered for former user
145  1 this.oldcore.getXWikiContext().setUserReference(user1);
146   
147  1 try {
148  1 testMacro = getUserComponentManager().getInstance(Macro.class, "macroid");
149   
150  0 Assert.fail("The macro has not been properly unregistered");
151    } catch (ComponentLookupException expected) {
152    }
153    }
154    }