use of org.xwiki.rendering.macro.Macro in project xwiki-platform by xwiki.
the class RenderingScriptServiceTest method getMacroDescriptors.
@Test
public void getMacroDescriptors() throws Exception {
MacroManager macroManager = this.mocker.registerMockComponent(MacroManager.class);
MacroId macroId = new MacroId("macroid");
when(macroManager.getMacroIds(Syntax.XWIKI_2_1)).thenReturn(Collections.singleton(macroId));
Macro macro = mock(Macro.class);
when(macroManager.getMacro(macroId)).thenReturn(macro);
MacroDescriptor descriptor = mock(MacroDescriptor.class);
when(macro.getDescriptor()).thenReturn(descriptor);
List<MacroDescriptor> descriptors = this.mocker.getComponentUnderTest().getMacroDescriptors(Syntax.XWIKI_2_1);
assertEquals(1, descriptors.size());
assertSame(descriptor, descriptors.get(0));
}
use of org.xwiki.rendering.macro.Macro in project xwiki-platform by xwiki.
the class ClassLoadingTest method registerComponents.
@Override
protected void registerComponents() throws Exception {
super.registerComponents();
this.mockSetup = new ScriptMockSetup(getComponentManager());
this.macro = getComponentManager().getInstance(Macro.class, "groovy");
this.context = new MacroTransformationContext();
// The script macro checks the current block (which is a macro block) to see what engine to use
this.context.setCurrentMacroBlock(new MacroBlock("groovy", Collections.<String, String>emptyMap(), false));
// Set the syntax since the script macro needs it to parse the script result using that syntax
this.context.setSyntax(Syntax.XWIKI_2_0);
}
use of org.xwiki.rendering.macro.Macro in project xwiki-platform by xwiki.
the class SecurityTest method executeGroovyMacro.
private void executeGroovyMacro(String script, boolean restricted) throws Exception {
Macro macro = getComponentManager().getInstance(Macro.class, "groovy");
JSR223ScriptMacroParameters parameters = new JSR223ScriptMacroParameters();
MacroTransformationContext context = new MacroTransformationContext();
context.getTransformationContext().setRestricted(restricted);
context.setSyntax(Syntax.XWIKI_2_1);
// The script macro checks the current block (which is a macro block) to see what engine to use.
context.setCurrentMacroBlock(new MacroBlock("groovy", Collections.<String, String>emptyMap(), false));
macro.execute(parameters, script, context);
}
use of org.xwiki.rendering.macro.Macro in project xwiki-platform by xwiki.
the class DefaultWikiMacroManager method registerWikiMacro.
@Override
public void registerWikiMacro(DocumentReference documentReference, WikiMacro wikiMacro) throws InsufficientPrivilegesException, WikiMacroException {
WikiMacroDescriptor macroDescriptor = (WikiMacroDescriptor) wikiMacro.getDescriptor();
// Verify that the user has the right to register this wiki macro the chosen visibility
if (this.wikiMacroFactory.isAllowed(documentReference, macroDescriptor.getVisibility())) {
DefaultComponentDescriptor<Macro> componentDescriptor = new DefaultComponentDescriptor<>();
componentDescriptor.setRoleType(Macro.class);
componentDescriptor.setRoleHint(wikiMacro.getDescriptor().getId().getId());
// Save current context informations
String currentUser = this.bridge.getCurrentUser();
EntityReference currentEntityReference = this.modelContext.getCurrentEntityReference();
try {
// Put the proper context information to let components manager use the proper keys to find
// components to unregister
this.bridge.setCurrentUser(this.serializer.serialize(wikiMacro.getAuthorReference() != null ? wikiMacro.getAuthorReference() : this.bridge.getCurrentUserReference()));
this.modelContext.setCurrentEntityReference(documentReference);
// Register the macro against the right Component Manager, depending on the defined macro visibility.
findComponentManager(macroDescriptor.getVisibility()).registerComponent(componentDescriptor, wikiMacro);
this.wikiMacroMap.put(documentReference, new WikiMacroData(componentDescriptor.getRoleHint(), wikiMacro));
} catch (Exception e) {
throw new WikiMacroException(String.format("Failed to register macro [%s] in [%s] for visibility [%s]", wikiMacro.getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility()), e);
} finally {
// Restore previous context informations
this.bridge.setCurrentUser(currentUser);
this.modelContext.setCurrentEntityReference(currentEntityReference);
}
} else {
throw new InsufficientPrivilegesException(String.format("Unable to register macro [%s] in [%s] for visibility [%s] due to insufficient privileges", wikiMacro.getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility()));
}
}
use of org.xwiki.rendering.macro.Macro in project xwiki-platform by xwiki.
the class IncludeMacroTest method registerComponents.
@Override
protected void registerComponents() throws Exception {
super.registerComponents();
this.mockSetup = new ScriptMockSetup(getMockery(), getComponentManager());
this.mockDocumentReferenceResolver = registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "macro", "macroDocumentReferenceResolver");
this.mockAuthorization = registerMockComponent(AuthorizationManager.class);
this.includeMacro = getComponentManager().getInstance(Macro.class, "include");
this.rendererFactory = getComponentManager().getInstance(PrintRendererFactory.class, "event/1.0");
}
Aggregations