| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 58 |
|
|
| 59 |
|
@version |
| 60 |
|
@since |
| 61 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (63) |
Complexity: 7 |
Complexity Density: 0.12 |
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
| 71 |
1 |
@Test... |
| 72 |
|
public void registerAndUnregisterWikiMacroWhenGlobalVisibilityAndAllowed() throws Exception |
| 73 |
|
{ |
| 74 |
1 |
DefaultWikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.GLOBAL); |
| 75 |
|
|
| 76 |
|
|
| 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 |
|
|
| 87 |
1 |
wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro); |
| 88 |
1 |
assertTrue(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference())); |
| 89 |
|
|
| 90 |
|
|
| 91 |
1 |
assertTrue(this.mocker.hasComponent(Macro.class, "testwikimacro")); |
| 92 |
|
|
| 93 |
|
|
| 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 |
|
|
| 100 |
1 |
wikiMacroManager.unregisterWikiMacro(wikiMacro.getDocumentReference()); |
| 101 |
1 |
assertFalse(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference())); |
| 102 |
|
|
| 103 |
|
|
| 104 |
1 |
assertFalse(this.mocker.hasComponent(Macro.class, "testwikimacro")); |
| 105 |
|
} |
| 106 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
| 107 |
1 |
@Test... |
| 108 |
|
public void registerWikiMacroWhenWikiVisibilityAndAllowed() throws Exception |
| 109 |
|
{ |
| 110 |
1 |
DefaultWikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.WIKI); |
| 111 |
|
|
| 112 |
|
|
| 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 |
|
|
| 119 |
1 |
WikiMacroManager wikiMacroManager = this.mocker.getComponentUnderTest(); |
| 120 |
1 |
wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro); |
| 121 |
1 |
assertTrue(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference())); |
| 122 |
|
|
| 123 |
|
|
| 124 |
1 |
verify(wikiComponentManager).registerComponent(any(DefaultComponentDescriptor.class), eq(wikiMacro)); |
| 125 |
|
|
| 126 |
|
|
| 127 |
1 |
wikiMacroManager.unregisterWikiMacro(wikiMacro.getDocumentReference()); |
| 128 |
1 |
assertFalse(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference())); |
| 129 |
|
|
| 130 |
|
|
| 131 |
1 |
verify(wikiComponentManager).unregisterComponent(Macro.class, "testwikimacro"); |
| 132 |
|
} |
| 133 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
| 134 |
1 |
@Test... |
| 135 |
|
public void registerWikiMacroWhenUserVisibilityAndAllowed() throws Exception |
| 136 |
|
{ |
| 137 |
1 |
DefaultWikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.USER); |
| 138 |
|
|
| 139 |
|
|
| 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 |
|
|
| 146 |
1 |
WikiMacroManager wikiMacroManager = this.mocker.getComponentUnderTest(); |
| 147 |
1 |
wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro); |
| 148 |
1 |
assertTrue(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference())); |
| 149 |
|
|
| 150 |
|
|
| 151 |
1 |
verify(userComponentManager).registerComponent(any(DefaultComponentDescriptor.class), eq(wikiMacro)); |
| 152 |
|
|
| 153 |
|
|
| 154 |
1 |
wikiMacroManager.unregisterWikiMacro(wikiMacro.getDocumentReference()); |
| 155 |
1 |
assertFalse(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference())); |
| 156 |
|
|
| 157 |
|
|
| 158 |
1 |
verify(userComponentManager).unregisterComponent(Macro.class, "testwikimacro"); |
| 159 |
|
} |
| 160 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 161 |
1 |
@Test(expected = InsufficientPrivilegesException.class)... |
| 162 |
|
public void registerWikiMacroWhenGlobalVisibilityAndNotAllowed() throws Exception |
| 163 |
|
{ |
| 164 |
1 |
DefaultWikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.GLOBAL); |
| 165 |
|
|
| 166 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 174 |
1 |
@Test(expected = InsufficientPrivilegesException.class)... |
| 175 |
|
public void registerWikiMacroWhenWikiVisibilityAndNotAllowed() throws Exception |
| 176 |
|
{ |
| 177 |
1 |
DefaultWikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.WIKI); |
| 178 |
|
|
| 179 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 187 |
1 |
@Test(expected = InsufficientPrivilegesException.class)... |
| 188 |
|
public void tegisterWikiMacroWhenUserVisibilityAndNotAllowed() throws Exception |
| 189 |
|
{ |
| 190 |
1 |
DefaultWikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.USER); |
| 191 |
|
|
| 192 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 200 |
6 |
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 |
|
} |