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

File DefaultWikiMacroManagerTest.java

 

Code metrics

0
56
7
1
214
130
7
0.12
8
7
1

Classes

Class Line # Actions
DefaultWikiMacroManagerTest 62 56 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 6 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.block.Block;
34    import org.xwiki.rendering.block.ParagraphBlock;
35    import org.xwiki.rendering.block.WordBlock;
36    import org.xwiki.rendering.block.XDOM;
37    import org.xwiki.rendering.macro.Macro;
38    import org.xwiki.rendering.macro.MacroId;
39    import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor;
40    import org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException;
41    import org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor;
42    import org.xwiki.rendering.macro.wikibridge.WikiMacroFactory;
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.syntax.Syntax;
47    import org.xwiki.test.mockito.MockitoComponentMockingRule;
48   
49    import static org.junit.Assert.assertFalse;
50    import static org.junit.Assert.assertTrue;
51    import static org.mockito.ArgumentMatchers.any;
52    import static org.mockito.ArgumentMatchers.eq;
53    import static org.mockito.Mockito.verify;
54    import static org.mockito.Mockito.when;
55   
56    /**
57    * Unit tests for {@link org.xwiki.rendering.internal.macro.wikibridge.DefaultWikiMacroManager}.
58    *
59    * @version $Id: a3a5671be10f57f67f6e01a1e798714489416ee2 $
60    * @since 2.0M2
61    */
 
62    public class DefaultWikiMacroManagerTest
63    {
64    @Rule
65    public MockitoComponentMockingRule<DefaultWikiMacroManager> mocker =
66    new MockitoComponentMockingRule<>(DefaultWikiMacroManager.class);
67   
68    private DocumentReference authorReference = new DocumentReference("authorwiki", Arrays.asList("authorspace"),
69    "authorpage");
70   
 
71  1 toggle @Test
72    public void registerAndUnregisterWikiMacroWhenGlobalVisibilityAndAllowed() throws Exception
73    {
74  1 DefaultWikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.GLOBAL);
75   
76    // Simulate a user who's allowed for the GLOBAL visibility
77  1 WikiMacroFactory wikiMacroFactory = this.mocker.getInstance(WikiMacroFactory.class);
78  1 when(wikiMacroFactory.isAllowed(wikiMacro.getDocumentReference(), WikiMacroVisibility.GLOBAL)).thenReturn(true);
79   
80  1 WikiMacroManager wikiMacroManager = this.mocker.getComponentUnderTest();
81  1 assertFalse(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
82   
83  1 EntityReferenceSerializer<String> serializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
84  1 when(serializer.serialize(this.authorReference)).thenReturn("authorwiki:authorspace.authorpage");
85   
86    // Test registration
87  1 wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro);
88  1 assertTrue(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
89   
90    // Verify that the WikiMacroManager has registered the macro against the root CM
91  1 assertTrue(this.mocker.hasComponent(Macro.class, "testwikimacro"));
92   
93    // Verify that the user and wiki where the macro is located have been set in the context
94  1 DocumentAccessBridge bridge = this.mocker.getInstance(DocumentAccessBridge.class);
95  1 verify(bridge).setCurrentUser("authorwiki:authorspace.authorpage");
96  1 ModelContext modelContext = this.mocker.getInstance(ModelContext.class);
97  1 verify(modelContext).setCurrentEntityReference(wikiMacro.getDocumentReference());
98   
99    // Test unregistration
100  1 wikiMacroManager.unregisterWikiMacro(wikiMacro.getDocumentReference());
101  1 assertFalse(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
102   
103    // Verify that the WikiMacroManager has unregistered the macro from the root CM
104  1 assertFalse(this.mocker.hasComponent(Macro.class, "testwikimacro"));
105    }
106   
 
107  1 toggle @Test
108    public void registerWikiMacroWhenWikiVisibilityAndAllowed() throws Exception
109    {
110  1 DefaultWikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.WIKI);
111   
112    // Simulate a user who's allowed for the WIKI visibility
113  1 WikiMacroFactory wikiMacroFactory = this.mocker.getInstance(WikiMacroFactory.class);
114  1 when(wikiMacroFactory.isAllowed(wikiMacro.getDocumentReference(), WikiMacroVisibility.WIKI)).thenReturn(true);
115   
116  1 ComponentManager wikiComponentManager = this.mocker.registerMockComponent(ComponentManager.class, "wiki");
117   
118    // Test registration
119  1 WikiMacroManager wikiMacroManager = this.mocker.getComponentUnderTest();
120  1 wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro);
121  1 assertTrue(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
122   
123    // Verify that the WikiMacroManager has registered the macro against the wiki CM
124  1 verify(wikiComponentManager).registerComponent(any(DefaultComponentDescriptor.class), eq(wikiMacro));
125   
126    // Test unregistration
127  1 wikiMacroManager.unregisterWikiMacro(wikiMacro.getDocumentReference());
128  1 assertFalse(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
129   
130    // Verify that the WikiMacroManager has unregistered the macro against the wiki CM
131  1 verify(wikiComponentManager).unregisterComponent(Macro.class, "testwikimacro");
132    }
133   
 
134  1 toggle @Test
135    public void registerWikiMacroWhenUserVisibilityAndAllowed() throws Exception
136    {
137  1 DefaultWikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.USER);
138   
139    // Simulate a user who's allowed for the USER visibility
140  1 WikiMacroFactory wikiMacroFactory = this.mocker.getInstance(WikiMacroFactory.class);
141  1 when(wikiMacroFactory.isAllowed(wikiMacro.getDocumentReference(), WikiMacroVisibility.USER)).thenReturn(true);
142   
143  1 ComponentManager userComponentManager = this.mocker.registerMockComponent(ComponentManager.class, "user");
144   
145    // Test registration
146  1 WikiMacroManager wikiMacroManager = this.mocker.getComponentUnderTest();
147  1 wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro);
148  1 assertTrue(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
149   
150    // Verify that the WikiMacroManager has registered the macro against the user CM
151  1 verify(userComponentManager).registerComponent(any(DefaultComponentDescriptor.class), eq(wikiMacro));
152   
153    // Test unregistration
154  1 wikiMacroManager.unregisterWikiMacro(wikiMacro.getDocumentReference());
155  1 assertFalse(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
156   
157    // Verify that the WikiMacroManager has unregistered the macro against the user CM
158  1 verify(userComponentManager).unregisterComponent(Macro.class, "testwikimacro");
159    }
160   
 
161  1 toggle @Test(expected = InsufficientPrivilegesException.class)
162    public void registerWikiMacroWhenGlobalVisibilityAndNotAllowed() throws Exception
163    {
164  1 DefaultWikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.GLOBAL);
165   
166    // Simulate a user who's not allowed for the GLOBAL visibility
167  1 WikiMacroFactory wikiMacroFactory = this.mocker.getInstance(WikiMacroFactory.class);
168  1 when(wikiMacroFactory.isAllowed(wikiMacro.getDocumentReference(), WikiMacroVisibility.GLOBAL)).thenReturn(
169    false);
170   
171  1 this.mocker.getComponentUnderTest().registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro);
172    }
173   
 
174  1 toggle @Test(expected = InsufficientPrivilegesException.class)
175    public void registerWikiMacroWhenWikiVisibilityAndNotAllowed() throws Exception
176    {
177  1 DefaultWikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.WIKI);
178   
179    // Simulate a user who's not allowed for the WIKI visibility
180  1 WikiMacroFactory wikiMacroFactory = this.mocker.getInstance(WikiMacroFactory.class);
181  1 when(wikiMacroFactory.isAllowed(wikiMacro.getDocumentReference(), WikiMacroVisibility.WIKI)).thenReturn(
182    false);
183   
184  1 this.mocker.getComponentUnderTest().registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro);
185    }
186   
 
187  1 toggle @Test(expected = InsufficientPrivilegesException.class)
188    public void tegisterWikiMacroWhenUserVisibilityAndNotAllowed() throws Exception
189    {
190  1 DefaultWikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.USER);
191   
192    // Simulate a user who's not allowed for the USER visibility
193  1 WikiMacroFactory wikiMacroFactory = this.mocker.getInstance(WikiMacroFactory.class);
194  1 when(wikiMacroFactory.isAllowed(wikiMacro.getDocumentReference(), WikiMacroVisibility.USER)).thenReturn(
195    false);
196   
197  1 this.mocker.getComponentUnderTest().registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro);
198    }
199   
 
200  6 toggle private DefaultWikiMacro generateWikiMacro(WikiMacroVisibility visibility) throws Exception
201    {
202  6 DocumentReference wikiMacroDocReference = new DocumentReference("wiki", Arrays.asList("space"),
203    "space");
204  6 WikiMacroDescriptor descriptor =
205    new WikiMacroDescriptor(new MacroId("testwikimacro"), "Test Wiki Macro", "Description", "Test", visibility,
206    new DefaultContentDescriptor(), Collections.<WikiMacroParameterDescriptor>emptyList());
207  6 XDOM xdom = new XDOM(Arrays.asList(new ParagraphBlock(Arrays.<Block>asList(new WordBlock("test")))));
208   
209  6 DefaultWikiMacro wikiMacro = new DefaultWikiMacro(wikiMacroDocReference, authorReference, true, descriptor,
210    xdom, Syntax.XWIKI_2_0, this.mocker);
211   
212  6 return wikiMacro;
213    }
214    }