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

File DefaultWikiMacroManagerTest.java

 

Code metrics

0
88
8
1
269
166
8
0.09
11
8
1

Classes

Class Line # Actions
DefaultWikiMacroManagerTest 60 88 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 7 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.macro.wikibridge;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24   
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.bridge.DocumentAccessBridge;
28    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
29    import org.xwiki.component.manager.ComponentManager;
30    import org.xwiki.model.ModelContext;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.model.reference.EntityReferenceSerializer;
33    import org.xwiki.rendering.macro.Macro;
34    import org.xwiki.rendering.macro.MacroId;
35    import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor;
36    import org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException;
37    import org.xwiki.rendering.macro.wikibridge.WikiMacro;
38    import org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor;
39    import org.xwiki.rendering.macro.wikibridge.WikiMacroFactory;
40    import org.xwiki.rendering.macro.wikibridge.WikiMacroManager;
41    import org.xwiki.rendering.macro.wikibridge.WikiMacroParameterDescriptor;
42    import org.xwiki.rendering.macro.wikibridge.WikiMacroVisibility;
43    import org.xwiki.test.mockito.MockitoComponentMockingRule;
44    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
45   
46    import static org.junit.Assert.assertFalse;
47    import static org.junit.Assert.assertTrue;
48    import static org.mockito.ArgumentMatchers.any;
49    import static org.mockito.ArgumentMatchers.eq;
50    import static org.mockito.Mockito.mock;
51    import static org.mockito.Mockito.verify;
52    import static org.mockito.Mockito.when;
53   
54    /**
55    * Unit tests for {@link org.xwiki.rendering.internal.macro.wikibridge.DefaultWikiMacroManager}.
56    *
57    * @version $Id: 074f814da55f1f10bacffb04bb0d2b44db579aac $
58    * @since 2.0M2
59    */
 
60    public class DefaultWikiMacroManagerTest
61    {
62    @Rule
63    public MockitoComponentMockingRule<DefaultWikiMacroManager> mocker =
64    new MockitoComponentMockingRule<>(DefaultWikiMacroManager.class);
65   
66    private DocumentReference authorReference =
67    new DocumentReference("authorwiki", Arrays.asList("authorspace"), "authorpage");
68   
 
69  1 toggle @Test
70    public void registerAndUnregisterWikiMacroWhenGlobalVisibilityAndAllowed() throws Exception
71    {
72  1 WikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.GLOBAL);
73   
74    // Simulate a user who's allowed for the GLOBAL visibility
75  1 WikiMacroFactory wikiMacroFactory = this.mocker.getInstance(WikiMacroFactory.class);
76  1 when(wikiMacroFactory.isAllowed(wikiMacro.getDocumentReference(), WikiMacroVisibility.GLOBAL)).thenReturn(true);
77   
78  1 WikiMacroManager wikiMacroManager = this.mocker.getComponentUnderTest();
79  1 assertFalse(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
80   
81  1 EntityReferenceSerializer<String> serializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
82  1 when(serializer.serialize(this.authorReference)).thenReturn("authorwiki:authorspace.authorpage");
83   
84    // Indicate current wiki is the main one (otherwise it won't be registered at root level)
85  1 WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
86  1 when(wikiDescriptorManager.isMainWiki(wikiMacro.getDocumentReference().getWikiReference().getName()))
87    .thenReturn(true);
88   
89    // Test registration
90  1 wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro);
91  1 assertTrue(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
92   
93    // Verify that the WikiMacroManager has registered the macro against the root CM
94  1 assertTrue(this.mocker.hasComponent(Macro.class, "testwikimacro"));
95   
96    // Verify that the user and wiki where the macro is located have been set in the context
97  1 DocumentAccessBridge bridge = this.mocker.getInstance(DocumentAccessBridge.class);
98  1 verify(bridge).setCurrentUser("authorwiki:authorspace.authorpage");
99  1 ModelContext modelContext = this.mocker.getInstance(ModelContext.class);
100  1 verify(modelContext).setCurrentEntityReference(wikiMacro.getDocumentReference());
101   
102    // Test unregistration
103  1 wikiMacroManager.unregisterWikiMacro(wikiMacro.getDocumentReference());
104  1 assertFalse(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
105   
106    // Verify that the WikiMacroManager has unregistered the macro from the root CM
107  1 assertFalse(this.mocker.hasComponent(Macro.class, "testwikimacro"));
108    }
109   
 
110  1 toggle @Test
111    public void registerWikiMacroWhenWikiVisibilityAndAllowed() throws Exception
112    {
113  1 WikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.WIKI);
114   
115    // Simulate a user who's allowed for the WIKI visibility
116  1 WikiMacroFactory wikiMacroFactory = this.mocker.getInstance(WikiMacroFactory.class);
117  1 when(wikiMacroFactory.isAllowed(wikiMacro.getDocumentReference(), WikiMacroVisibility.WIKI)).thenReturn(true);
118   
119  1 ComponentManager wikiComponentManager = this.mocker.registerMockComponent(ComponentManager.class, "wiki");
120  1 DefaultComponentDescriptor<Macro> componentDescriptor = new DefaultComponentDescriptor<>();
121  1 componentDescriptor.setRoleType(Macro.class);
122  1 componentDescriptor.setRoleHint("testwikimacro");
123   
124    // Test registration
125  1 WikiMacroManager wikiMacroManager = this.mocker.getComponentUnderTest();
126  1 wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro);
127  1 assertTrue(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
128   
129    // Verify that the WikiMacroManager has registered the macro against the wiki CM
130  1 verify(wikiComponentManager).registerComponent(any(DefaultComponentDescriptor.class), eq(wikiMacro));
131   
132  1 when(wikiComponentManager.<Macro>getComponentDescriptor(Macro.class, "testwikimacro"))
133    .thenReturn(componentDescriptor);
134  1 when(wikiComponentManager.getInstance(Macro.class, "testwikimacro")).thenReturn(wikiMacro);
135   
136    // Test unregistration
137  1 wikiMacroManager.unregisterWikiMacro(wikiMacro.getDocumentReference());
138  1 assertFalse(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
139   
140    // Verify that the WikiMacroManager has unregistered the macro against the wiki CM
141  1 verify(wikiComponentManager).unregisterComponent(Macro.class, "testwikimacro");
142    }
143   
 
144  1 toggle @Test
145    public void registerWikiMacroWhenGlobalVisibilityOnSubWiki() throws Exception
146    {
147  1 WikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.GLOBAL);
148   
149    // Simulate a user who's allowed for the WIKI visibility
150  1 WikiMacroFactory wikiMacroFactory = this.mocker.getInstance(WikiMacroFactory.class);
151  1 when(wikiMacroFactory.isAllowed(wikiMacro.getDocumentReference(), WikiMacroVisibility.WIKI)).thenReturn(true);
152   
153    // Indicate current wiki is a subwiki (so that it's not registered at root level)
154  1 WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
155  1 when(wikiDescriptorManager.isMainWiki(wikiMacro.getDocumentReference().getWikiReference().getName()))
156    .thenReturn(false);
157   
158  1 ComponentManager wikiComponentManager = this.mocker.registerMockComponent(ComponentManager.class, "wiki");
159  1 DefaultComponentDescriptor<Macro> componentDescriptor = new DefaultComponentDescriptor<>();
160  1 componentDescriptor.setRoleType(Macro.class);
161  1 componentDescriptor.setRoleHint("testwikimacro");
162   
163    // Test registration
164  1 WikiMacroManager wikiMacroManager = this.mocker.getComponentUnderTest();
165  1 wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro);
166  1 assertTrue(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
167   
168    // Verify that the WikiMacroManager has registered the macro against the wiki CM
169  1 verify(wikiComponentManager).registerComponent(componentDescriptor, wikiMacro);
170   
171  1 when(wikiComponentManager.<Macro>getComponentDescriptor(Macro.class, "testwikimacro"))
172    .thenReturn(componentDescriptor);
173  1 when(wikiComponentManager.getInstance(Macro.class, "testwikimacro")).thenReturn(wikiMacro);
174   
175    // Test unregistration
176  1 wikiMacroManager.unregisterWikiMacro(wikiMacro.getDocumentReference());
177  1 assertFalse(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
178   
179    // Verify that the WikiMacroManager has unregistered the macro against the wiki CM
180  1 verify(wikiComponentManager).unregisterComponent(Macro.class, "testwikimacro");
181    }
182   
 
183  1 toggle @Test
184    public void registerWikiMacroWhenUserVisibilityAndAllowed() throws Exception
185    {
186  1 WikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.USER);
187   
188    // Simulate a user who's allowed for the USER visibility
189  1 WikiMacroFactory wikiMacroFactory = this.mocker.getInstance(WikiMacroFactory.class);
190  1 when(wikiMacroFactory.isAllowed(wikiMacro.getDocumentReference(), WikiMacroVisibility.USER)).thenReturn(true);
191   
192  1 ComponentManager userComponentManager = this.mocker.registerMockComponent(ComponentManager.class, "user");
193  1 DefaultComponentDescriptor<Macro> componentDescriptor = new DefaultComponentDescriptor<>();
194  1 componentDescriptor.setRoleType(Macro.class);
195  1 componentDescriptor.setRoleHint("testwikimacro");
196   
197    // Test registration
198  1 WikiMacroManager wikiMacroManager = this.mocker.getComponentUnderTest();
199  1 wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro);
200  1 assertTrue(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
201   
202    // Verify that the WikiMacroManager has registered the macro against the user CM
203  1 verify(userComponentManager).registerComponent(any(DefaultComponentDescriptor.class), eq(wikiMacro));
204   
205  1 when(userComponentManager.<Macro>getComponentDescriptor(Macro.class, "testwikimacro"))
206    .thenReturn(componentDescriptor);
207  1 when(userComponentManager.getInstance(Macro.class, "testwikimacro")).thenReturn(wikiMacro);
208   
209    // Test unregistration
210  1 wikiMacroManager.unregisterWikiMacro(wikiMacro.getDocumentReference());
211  1 assertFalse(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
212   
213    // Verify that the WikiMacroManager has unregistered the macro against the user CM
214  1 verify(userComponentManager).unregisterComponent(Macro.class, "testwikimacro");
215    }
216   
 
217  1 toggle @Test(expected = InsufficientPrivilegesException.class)
218    public void registerWikiMacroWhenGlobalVisibilityAndNotAllowed() throws Exception
219    {
220  1 WikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.GLOBAL);
221   
222    // Simulate a user who's not allowed for the GLOBAL visibility
223  1 WikiMacroFactory wikiMacroFactory = this.mocker.getInstance(WikiMacroFactory.class);
224  1 when(wikiMacroFactory.isAllowed(wikiMacro.getDocumentReference(), WikiMacroVisibility.GLOBAL))
225    .thenReturn(false);
226   
227  1 this.mocker.getComponentUnderTest().registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro);
228    }
229   
 
230  1 toggle @Test(expected = InsufficientPrivilegesException.class)
231    public void registerWikiMacroWhenWikiVisibilityAndNotAllowed() throws Exception
232    {
233  1 WikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.WIKI);
234   
235    // Simulate a user who's not allowed for the WIKI visibility
236  1 WikiMacroFactory wikiMacroFactory = this.mocker.getInstance(WikiMacroFactory.class);
237  1 when(wikiMacroFactory.isAllowed(wikiMacro.getDocumentReference(), WikiMacroVisibility.WIKI)).thenReturn(false);
238   
239  1 this.mocker.getComponentUnderTest().registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro);
240    }
241   
 
242  1 toggle @Test(expected = InsufficientPrivilegesException.class)
243    public void registerWikiMacroWhenUserVisibilityAndNotAllowed() throws Exception
244    {
245  1 WikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.USER);
246   
247    // Simulate a user who's not allowed for the USER visibility
248  1 WikiMacroFactory wikiMacroFactory = this.mocker.getInstance(WikiMacroFactory.class);
249  1 when(wikiMacroFactory.isAllowed(wikiMacro.getDocumentReference(), WikiMacroVisibility.USER)).thenReturn(false);
250   
251  1 this.mocker.getComponentUnderTest().registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro);
252    }
253   
 
254  7 toggle private WikiMacro generateWikiMacro(WikiMacroVisibility visibility) throws Exception
255    {
256  7 DocumentReference wikiMacroDocReference = new DocumentReference("wiki", Arrays.asList("space"), "space");
257  7 WikiMacroDescriptor descriptor = new WikiMacroDescriptor.Builder().id(new MacroId("testwikimacro"))
258    .name("Test Wiki Macro").description("Description").defaultCategory("Test").visibility(visibility)
259    .contentDescriptor(new DefaultContentDescriptor())
260    .parameterDescriptors(Collections.<WikiMacroParameterDescriptor>emptyList()).build();
261   
262  7 WikiMacro wikiMacro = mock(WikiMacro.class);
263  7 when(wikiMacro.getDocumentReference()).thenReturn(wikiMacroDocReference);
264  7 when(wikiMacro.getAuthorReference()).thenReturn(authorReference);
265  7 when(wikiMacro.getDescriptor()).thenReturn(descriptor);
266   
267  7 return wikiMacro;
268    }
269    }