| 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 java.io.StringReader; |
| 23 |
|
import java.util.Arrays; |
| 24 |
|
import java.util.Collections; |
| 25 |
|
import java.util.List; |
| 26 |
|
|
| 27 |
|
import org.jmock.Expectations; |
| 28 |
|
import org.jmock.api.Invocation; |
| 29 |
|
import org.jmock.lib.action.CustomAction; |
| 30 |
|
import org.junit.Assert; |
| 31 |
|
import org.junit.Before; |
| 32 |
|
import org.junit.Test; |
| 33 |
|
import org.xwiki.model.EntityType; |
| 34 |
|
import org.xwiki.model.reference.DocumentReference; |
| 35 |
|
import org.xwiki.model.reference.EntityReference; |
| 36 |
|
import org.xwiki.rendering.converter.Converter; |
| 37 |
|
import org.xwiki.rendering.internal.macro.wikibridge.DefaultWikiMacro; |
| 38 |
|
import org.xwiki.rendering.macro.MacroId; |
| 39 |
|
import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor; |
| 40 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor; |
| 41 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroManager; |
| 42 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroParameterDescriptor; |
| 43 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroVisibility; |
| 44 |
|
import org.xwiki.rendering.parser.Parser; |
| 45 |
|
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; |
| 46 |
|
import org.xwiki.rendering.syntax.Syntax; |
| 47 |
|
import org.xwiki.security.authorization.ContextualAuthorizationManager; |
| 48 |
|
import org.xwiki.security.authorization.Right; |
| 49 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
| 50 |
|
|
| 51 |
|
import com.xpn.xwiki.XWiki; |
| 52 |
|
import com.xpn.xwiki.XWikiContext; |
| 53 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 54 |
|
import com.xpn.xwiki.objects.BaseObject; |
| 55 |
|
import com.xpn.xwiki.test.AbstractBridgedComponentTestCase; |
| 56 |
|
import com.xpn.xwiki.user.api.XWikiGroupService; |
| 57 |
|
import com.xpn.xwiki.user.api.XWikiRightService; |
| 58 |
|
import com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl; |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
@link |
| 62 |
|
|
| 63 |
|
@version |
| 64 |
|
@since |
| 65 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (81) |
Complexity: 9 |
Complexity Density: 0.12 |
|
| 66 |
|
public class DefaultWikiMacroTest extends AbstractBridgedComponentTestCase |
| 67 |
|
{ |
| 68 |
|
private static final EntityReference XWIKIPREFERENCES_REFERENCE = new EntityReference("XWikiPreferences", |
| 69 |
|
EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE)); |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
private DocumentReference wikiMacroDocumentReference; |
| 75 |
|
|
| 76 |
|
|
| 77 |
|
@link |
| 78 |
|
|
| 79 |
|
private WikiMacroManager wikiMacroManager; |
| 80 |
|
|
| 81 |
|
private Parser xwiki20Parser; |
| 82 |
|
|
| 83 |
|
private XWikiDocument wikiMacroDocument; |
| 84 |
|
|
| 85 |
|
private XWikiDocument user; |
| 86 |
|
|
| 87 |
|
private WikiDescriptorManager mockWikiDescriptorManager; |
| 88 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 89 |
2 |
@Override... |
| 90 |
|
protected void registerComponents() throws Exception |
| 91 |
|
{ |
| 92 |
2 |
super.registerComponents(); |
| 93 |
|
|
| 94 |
2 |
this.mockWikiDescriptorManager = registerMockComponent(WikiDescriptorManager.class); |
| 95 |
|
} |
| 96 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (24) |
Complexity: 1 |
Complexity Density: 0.04 |
|
| 97 |
2 |
@Override... |
| 98 |
|
@Before |
| 99 |
|
public void setUp() throws Exception |
| 100 |
|
{ |
| 101 |
2 |
super.setUp(); |
| 102 |
|
|
| 103 |
2 |
getMockery().checking(new Expectations() |
| 104 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 105 |
2 |
{... |
| 106 |
2 |
allowing(mockWikiDescriptorManager).getCurrentWikiId(); |
| 107 |
2 |
will(new CustomAction("WikiDescriptorManager#getCurrentWikiId") |
| 108 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 109 |
57 |
@Override... |
| 110 |
|
public Object invoke(Invocation invocation) throws Throwable |
| 111 |
|
{ |
| 112 |
57 |
return getContext().getWikiId(); |
| 113 |
|
} |
| 114 |
|
}); |
| 115 |
|
} |
| 116 |
|
}); |
| 117 |
|
|
| 118 |
2 |
final ContextualAuthorizationManager mockCam = getContextualAuthorizationManager(); |
| 119 |
|
|
| 120 |
2 |
final XWiki mockXWiki = getMockery().mock(XWiki.class); |
| 121 |
2 |
final XWikiGroupService mockXWikiGroupService = getMockery().mock(XWikiGroupService.class); |
| 122 |
|
|
| 123 |
2 |
getContext().setWiki(mockXWiki); |
| 124 |
|
|
| 125 |
2 |
this.xwiki20Parser = getComponentManager().getInstance(Parser.class, "xwiki/2.0"); |
| 126 |
|
|
| 127 |
2 |
this.wikiMacroDocumentReference = new DocumentReference(getContext().getWikiId(), "space", "macroPage"); |
| 128 |
2 |
this.wikiMacroManager = getComponentManager().getInstance(WikiMacroManager.class); |
| 129 |
|
|
| 130 |
2 |
this.wikiMacroDocument = new XWikiDocument(wikiMacroDocumentReference); |
| 131 |
|
|
| 132 |
2 |
final XWikiRightService rightService = new XWikiRightServiceImpl(); |
| 133 |
|
|
| 134 |
2 |
this.user = new XWikiDocument(new DocumentReference(getContext().getWikiId(), "XWiki", "user")); |
| 135 |
2 |
this.user.setNew(false); |
| 136 |
2 |
BaseObject userObject = new BaseObject(); |
| 137 |
2 |
userObject.setXClassReference(new DocumentReference(getContext().getWikiId(), "XWiki", "XWikiusers")); |
| 138 |
2 |
this.user.addXObject(userObject); |
| 139 |
|
|
| 140 |
2 |
this.wikiMacroDocument.setCreatorReference(this.user.getAuthorReference()); |
| 141 |
2 |
this.wikiMacroDocument.setAuthorReference(this.user.getAuthorReference()); |
| 142 |
2 |
this.wikiMacroDocument.setContentAuthorReference(this.user.getAuthorReference()); |
| 143 |
|
|
| 144 |
|
|
| 145 |
2 |
final XWikiDocument prefs = |
| 146 |
|
new XWikiDocument(new DocumentReference(getContext().getWikiId(), "XWiki", "XWikiPreferences")); |
| 147 |
2 |
final BaseObject mockGlobalRightObj = getMockery().mock(BaseObject.class); |
| 148 |
|
|
| 149 |
2 |
getMockery().checking(new Expectations() |
| 150 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (25) |
Complexity: 1 |
Complexity Density: 0.04 |
|
| 151 |
2 |
{... |
| 152 |
2 |
allowing(mockCam).hasAccess(Right.PROGRAM); |
| 153 |
2 |
will(returnValue(true)); |
| 154 |
|
|
| 155 |
2 |
allowing(mockXWiki).getDocument(with(equal(wikiMacroDocumentReference)), with(any(XWikiContext.class))); |
| 156 |
2 |
will(returnValue(wikiMacroDocument)); |
| 157 |
|
|
| 158 |
2 |
allowing(mockXWiki).isReadOnly(); |
| 159 |
2 |
will(returnValue(false)); |
| 160 |
2 |
allowing(mockXWiki).getLanguagePreference(with(any(XWikiContext.class))); |
| 161 |
2 |
will(returnValue(null)); |
| 162 |
2 |
allowing(mockXWiki).getRightService(); |
| 163 |
2 |
will(returnValue(rightService)); |
| 164 |
2 |
allowing(mockXWiki).getGroupService(with(any(XWikiContext.class))); |
| 165 |
2 |
will(returnValue(mockXWikiGroupService)); |
| 166 |
|
|
| 167 |
2 |
allowing(mockXWikiGroupService).getAllGroupsReferencesForMember(with(any(DocumentReference.class)), |
| 168 |
|
with(any(int.class)), with(any(int.class)), with(any(XWikiContext.class))); |
| 169 |
2 |
will(returnValue(Collections.EMPTY_LIST)); |
| 170 |
|
|
| 171 |
2 |
allowing(mockXWiki).getDocument(with(equal(XWIKIPREFERENCES_REFERENCE)), with(any(XWikiContext.class))); |
| 172 |
2 |
will(returnValue(prefs)); |
| 173 |
2 |
allowing(mockGlobalRightObj).getStringValue("levels"); |
| 174 |
2 |
will(returnValue("programming")); |
| 175 |
2 |
allowing(mockGlobalRightObj).getStringValue("users"); |
| 176 |
2 |
will(returnValue(user.getFullName())); |
| 177 |
2 |
allowing(mockGlobalRightObj).getIntValue("allow"); |
| 178 |
2 |
will(returnValue(1)); |
| 179 |
2 |
allowing(mockGlobalRightObj).setNumber(with(any(int.class))); |
| 180 |
2 |
allowing(mockGlobalRightObj).setDocumentReference(with(any(DocumentReference.class))); |
| 181 |
2 |
allowing(mockGlobalRightObj).setOwnerDocument(with(any(XWikiDocument.class))); |
| 182 |
|
} |
| 183 |
|
}); |
| 184 |
|
|
| 185 |
2 |
prefs.addObject("XWiki.XWikiGlobalRights", mockGlobalRightObj); |
| 186 |
|
|
| 187 |
2 |
getContext().setUserReference(this.user.getDocumentReference()); |
| 188 |
|
} |
| 189 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 190 |
1 |
@Test... |
| 191 |
|
public void testExecuteWhenWikiMacroBinding() throws Exception |
| 192 |
|
{ |
| 193 |
1 |
registerWikiMacro("wikimacrobindings", "{{groovy}}" + "print xcontext.macro.doc" + "{{/groovy}}"); |
| 194 |
|
|
| 195 |
1 |
Converter converter = getComponentManager().getInstance(Converter.class); |
| 196 |
|
|
| 197 |
1 |
DefaultWikiPrinter printer = new DefaultWikiPrinter(); |
| 198 |
1 |
converter.convert(new StringReader("{{wikimacrobindings param1=\"value2\" param2=\"value2\"/}}"), |
| 199 |
|
Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer); |
| 200 |
|
|
| 201 |
|
|
| 202 |
1 |
Assert.assertEquals("<p>" + this.wikiMacroDocument.toString() + "</p>", printer.toString()); |
| 203 |
|
} |
| 204 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
| 205 |
1 |
@Test... |
| 206 |
|
public void testExecuteWhenWikiRequiringPRAfterDropPermission() throws Exception |
| 207 |
|
{ |
| 208 |
1 |
registerWikiMacro("wikimacrobindings", "{{groovy}}" + "print xcontext.macro.doc" + "{{/groovy}}"); |
| 209 |
|
|
| 210 |
1 |
Converter converter = getComponentManager().getInstance(Converter.class); |
| 211 |
|
|
| 212 |
1 |
DefaultWikiPrinter printer = new DefaultWikiPrinter(); |
| 213 |
|
|
| 214 |
1 |
getContext().dropPermissions(); |
| 215 |
1 |
this.wikiMacroDocument.newDocument(getContext()).dropPermissions(); |
| 216 |
|
|
| 217 |
1 |
converter.convert(new StringReader("{{wikimacrobindings param1=\"value2\" param2=\"value2\"/}}"), |
| 218 |
|
Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer); |
| 219 |
|
|
| 220 |
|
|
| 221 |
1 |
Assert.assertEquals("<p>" + this.wikiMacroDocument.toString() + "</p>", printer.toString()); |
| 222 |
1 |
Assert.assertTrue("Wiki macro did not properly restord persmission dropping", getContext() |
| 223 |
|
.hasDroppedPermissions()); |
| 224 |
|
} |
| 225 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 226 |
2 |
private void registerWikiMacro(String macroId, String macroContent) throws Exception... |
| 227 |
|
{ |
| 228 |
2 |
List<WikiMacroParameterDescriptor> parameterDescriptors = |
| 229 |
|
Arrays.asList(new WikiMacroParameterDescriptor("param1", "This is param1", true), |
| 230 |
|
new WikiMacroParameterDescriptor("param2", "This is param2", true)); |
| 231 |
2 |
registerWikiMacro(macroId, macroContent, parameterDescriptors); |
| 232 |
|
} |
| 233 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 234 |
2 |
private void registerWikiMacro(String macroId, String macroContent,... |
| 235 |
|
List<WikiMacroParameterDescriptor> parameterDescriptors) throws Exception |
| 236 |
|
{ |
| 237 |
2 |
WikiMacroDescriptor descriptor = |
| 238 |
|
new WikiMacroDescriptor(new MacroId(macroId), "Wiki Macro", "Description", "Test", |
| 239 |
|
WikiMacroVisibility.GLOBAL, new DefaultContentDescriptor(false), parameterDescriptors); |
| 240 |
|
|
| 241 |
2 |
DefaultWikiMacro wikiMacro = |
| 242 |
|
new DefaultWikiMacro(wikiMacroDocumentReference, this.user.getDocumentReference(), true, descriptor, |
| 243 |
|
this.xwiki20Parser.parse(new StringReader(macroContent)), Syntax.XWIKI_2_0, getComponentManager()); |
| 244 |
|
|
| 245 |
2 |
this.wikiMacroManager.registerWikiMacro(wikiMacroDocumentReference, wikiMacro); |
| 246 |
|
} |
| 247 |
|
} |