| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
|
| 43 |
|
|
| 44 |
|
@version |
| 45 |
|
|
| 46 |
|
@AllComponents |
| |
|
| 95.7% |
Uncovered Elements: 2 (46) |
Complexity: 7 |
Complexity Density: 0.17 |
|
| 47 |
|
public class WikiMacrosTest |
| 48 |
|
{ |
| 49 |
|
@Rule |
| 50 |
|
public MockitoOldcoreRule oldcore = new MockitoOldcoreRule(); |
| 51 |
|
|
| 52 |
|
private XWikiDocument macroDocument; |
| 53 |
|
|
| 54 |
|
private BaseObject macroObject; |
| 55 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
| 56 |
2 |
@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 |
|
|
| 70 |
2 |
this.oldcore.notifyComponentDescriptorEvent(); |
| 71 |
2 |
this.oldcore.notifyDocumentCreatedEvent(true); |
| 72 |
2 |
this.oldcore.notifyDocumentUpdatedEvent(true); |
| 73 |
|
} |
| 74 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 75 |
1 |
private ComponentManager getWikiComponentManager() throws Exception... |
| 76 |
|
{ |
| 77 |
1 |
return this.oldcore.getMocker().getInstance(ComponentManager.class, "wiki"); |
| 78 |
|
} |
| 79 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 80 |
3 |
private ComponentManager getUserComponentManager() throws Exception... |
| 81 |
|
{ |
| 82 |
3 |
return this.oldcore.getMocker().getInstance(ComponentManager.class, "user"); |
| 83 |
|
} |
| 84 |
|
|
| |
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 2 |
Complexity Density: 0.2 |
1PASS
|
|
| 85 |
1 |
@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 |
|
|
| 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 |
|
|
| |
|
| 94.4% |
Uncovered Elements: 1 (18) |
Complexity: 2 |
Complexity Density: 0.11 |
1PASS
|
|
| 109 |
1 |
@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 |
|
|
| 121 |
1 |
this.oldcore.getSpyXWiki().saveDocument(this.macroDocument, this.oldcore.getXWikiContext()); |
| 122 |
|
|
| 123 |
|
|
| 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 |
|
|
| 130 |
|
|
| 131 |
1 |
DocumentReference user2 = new DocumentReference("wiki", "Wiki", "user2"); |
| 132 |
|
|
| 133 |
1 |
this.macroDocument.setAuthorReference(user2); |
| 134 |
|
|
| 135 |
|
|
| 136 |
1 |
this.oldcore.getSpyXWiki().saveDocument(this.macroDocument, this.oldcore.getXWikiContext()); |
| 137 |
|
|
| 138 |
|
|
| 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 |
|
|
| 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 |
|
} |