Search in sources :

Example 1 with Macro

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));
}
Also used : MacroDescriptor(org.xwiki.rendering.macro.descriptor.MacroDescriptor) Macro(org.xwiki.rendering.macro.Macro) MacroId(org.xwiki.rendering.macro.MacroId) MacroManager(org.xwiki.rendering.macro.MacroManager) Test(org.junit.Test)

Example 2 with Macro

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);
}
Also used : ScriptMockSetup(org.xwiki.rendering.macro.script.ScriptMockSetup) Macro(org.xwiki.rendering.macro.Macro) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 3 with Macro

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);
}
Also used : Macro(org.xwiki.rendering.macro.Macro) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) JSR223ScriptMacroParameters(org.xwiki.rendering.macro.script.JSR223ScriptMacroParameters) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 4 with Macro

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()));
    }
}
Also used : InsufficientPrivilegesException(org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) WikiMacro(org.xwiki.rendering.macro.wikibridge.WikiMacro) Macro(org.xwiki.rendering.macro.Macro) WikiMacroException(org.xwiki.rendering.macro.wikibridge.WikiMacroException) EntityReference(org.xwiki.model.reference.EntityReference) WikiMacroDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) InsufficientPrivilegesException(org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException) WikiMacroException(org.xwiki.rendering.macro.wikibridge.WikiMacroException)

Example 5 with Macro

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");
}
Also used : ScriptMockSetup(org.xwiki.rendering.macro.script.ScriptMockSetup) IncludeMacro(org.xwiki.rendering.internal.macro.include.IncludeMacro) Macro(org.xwiki.rendering.macro.Macro) PrintRendererFactory(org.xwiki.rendering.renderer.PrintRendererFactory) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager)

Aggregations

Macro (org.xwiki.rendering.macro.Macro)9 Test (org.junit.Test)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 MacroBlock (org.xwiki.rendering.block.MacroBlock)2 MacroId (org.xwiki.rendering.macro.MacroId)2 MacroManager (org.xwiki.rendering.macro.MacroManager)2 ScriptMockSetup (org.xwiki.rendering.macro.script.ScriptMockSetup)2 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)2 Before (org.junit.Before)1 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)1 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)1 ModelContext (org.xwiki.model.ModelContext)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 EntityReference (org.xwiki.model.reference.EntityReference)1 BeanDescriptor (org.xwiki.properties.BeanDescriptor)1 BeanManager (org.xwiki.properties.BeanManager)1 IncludeMacro (org.xwiki.rendering.internal.macro.include.IncludeMacro)1 VelocityMacro (org.xwiki.rendering.internal.macro.velocity.VelocityMacro)1 MacroDescriptor (org.xwiki.rendering.macro.descriptor.MacroDescriptor)1