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

File WikiMacrosTest.java

 

Code metrics

0
39
5
1
154
88
7
0.18
7.8
5
1.4

Classes

Class Line # Actions
WikiMacrosTest 49 39 0% 7 2
0.9545454495.5%
 

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.security.authorization.Right;
32    import org.xwiki.test.annotation.AllComponents;
33   
34    import com.xpn.xwiki.XWikiContext;
35    import com.xpn.xwiki.doc.XWikiDocument;
36    import com.xpn.xwiki.objects.BaseObject;
37    import com.xpn.xwiki.test.MockitoOldcoreRule;
38   
39    import static org.mockito.ArgumentMatchers.any;
40    import static org.mockito.ArgumentMatchers.same;
41    import static org.mockito.Mockito.when;
42   
43    /**
44    * Various general tests on wiki macros.
45    *
46    * @version $Id: e8a2dbf1994d7da80a09fe246908b5b8c84896b1 $
47    */
48    @AllComponents
 
49    public class WikiMacrosTest
50    {
51    @Rule
52    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
53   
54    private XWikiDocument macroDocument;
55   
56    private BaseObject macroObject;
57   
 
58  2 toggle @Before
59    public void before() throws Exception
60    {
61  2 this.macroDocument = new XWikiDocument(new DocumentReference("wiki", "Space", "Page"));
62  2 this.macroDocument.setSyntax(Syntax.XWIKI_2_0);
63  2 this.macroObject = new BaseObject();
64  2 this.macroObject.setXClassReference(new DocumentReference("wiki", "XWiki", "WikiMacroClass"));
65  2 this.macroObject.setStringValue("id", "macroid");
66  2 this.macroObject.setLargeStringValue("code", "code");
67  2 this.macroDocument.addXObject(macroObject);
68   
69  2 this.oldcore.getXWikiContext().setWikiId("wiki");
70   
71    // We need component related events
72  2 this.oldcore.notifyComponentDescriptorEvent();
73  2 this.oldcore.notifyDocumentCreatedEvent(true);
74  2 this.oldcore.notifyDocumentUpdatedEvent(true);
75    }
76   
 
77  1 toggle private ComponentManager getWikiComponentManager() throws Exception
78    {
79  1 return this.oldcore.getMocker().getInstance(ComponentManager.class, "wiki");
80    }
81   
 
82  3 toggle private ComponentManager getUserComponentManager() throws Exception
83    {
84  3 return this.oldcore.getMocker().getInstance(ComponentManager.class, "user");
85    }
86   
 
87  1 toggle @Test
88    public void testSaveWikiMacro() throws Exception
89    {
90  1 when(this.oldcore.getMockAuthorizationManager().hasAccess(any(), any(), any())).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    }