Search in sources :

Example 76 with Form

use of com.enonic.xp.form.Form in project xp by enonic.

the class MacroInstruction method createContext.

private MacroContext createContext(final Instruction macroInstruction, final MacroDescriptor macroDescriptor, final PortalRequest request) {
    final Form macroForm = macroDescriptor.getForm();
    final Map<String, String> paramCaseTranslator = new HashMap<>(macroForm.size());
    for (FormItem formItem : macroForm) {
        final String name = formItem.getName();
        paramCaseTranslator.put(name.toLowerCase(), name);
    }
    final MacroContext.Builder context = MacroContext.create().name(macroDescriptor.getName());
    for (String name : macroInstruction.attributeNames()) {
        if (name.equalsIgnoreCase(MACRO_BODY) || name.equalsIgnoreCase(MACRO_NAME) || name.equalsIgnoreCase(MACRO_DOCUMENT)) {
            continue;
        }
        String contextParamName = name;
        if (macroForm.getFormItems().getItemByName(name) == null) {
            final String normalizedName = paramCaseTranslator.get(name.toLowerCase());
            if (normalizedName != null) {
                contextParamName = normalizedName;
            }
        }
        for (String attribute : macroInstruction.attributes(name)) {
            context.param(contextParamName, attribute);
        }
    }
    context.body(macroInstruction.attribute(MACRO_BODY));
    context.request(request);
    final String documentRef = macroInstruction.attribute(MACRO_DOCUMENT);
    final String document = (String) ContextAccessor.current().getLocalScope().getAttribute(documentRef);
    context.document(document);
    return context.build();
}
Also used : Form(com.enonic.xp.form.Form) HashMap(java.util.HashMap) FormItem(com.enonic.xp.form.FormItem) MacroContext(com.enonic.xp.portal.macro.MacroContext)

Example 77 with Form

use of com.enonic.xp.form.Form in project xp by enonic.

the class MacroInstructionTest method testInstructionMissingMacro.

@Test
public void testInstructionMissingMacro() throws Exception {
    MacroKey key = MacroKey.from("myapp:somemacro");
    Form form = Form.create().build();
    MacroDescriptor macroDescriptor = MacroDescriptor.create().key(key).form(form).build();
    when(macroDescriptorService.getByKey(key)).thenReturn(macroDescriptor);
    when(macroDescriptorService.getByApplication(key.getApplicationKey())).thenReturn(MacroDescriptors.from(macroDescriptor));
    String outputHtml = macroInstruction.evaluate(portalRequest, "MACRO _name=\"mymacro\" param1=\"value1\" _body=\"body\"").getAsString();
    assertEquals("[mymacro param1=\"value1\"]body[/mymacro]", outputHtml);
}
Also used : MacroKey(com.enonic.xp.macro.MacroKey) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) Form(com.enonic.xp.form.Form) Test(org.junit.jupiter.api.Test)

Example 78 with Form

use of com.enonic.xp.form.Form in project xp by enonic.

the class MacroInstructionTest method testInstructionMacroParamsCaseInsensitive.

@Test
public void testInstructionMacroParamsCaseInsensitive() throws Exception {
    MacroKey key = MacroKey.from("myapp:mymacro");
    Form form = Form.create().addFormItem(createTextLineInput("param1", "Param 1").occurrences(1, 1).build()).addFormItem(createTextLineInput("param2", "Param 2").occurrences(1, 1).build()).build();
    MacroDescriptor macroDescriptor = MacroDescriptor.create().key(key).form(form).build();
    when(macroDescriptorService.getByKey(key)).thenReturn(macroDescriptor);
    when(macroDescriptorService.getByApplication(key.getApplicationKey())).thenReturn(MacroDescriptors.from(macroDescriptor));
    MacroProcessor macro = (ctx) -> PortalResponse.create().body(ctx.getName() + ": param1=" + ctx.getParam("param1") + ", body=" + ctx.getBody()).build();
    when(macroProcessorFactory.fromScript(any())).thenReturn(macro);
    String outputHtml = macroInstruction.evaluate(portalRequest, "MACRO _name=\"MYMACRO\" PARAM1=\"value1\" _body=\"body\"").getAsString();
    assertEquals("mymacro: param1=value1, body=body", outputHtml);
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) MacroProcessorFactory(com.enonic.xp.portal.macro.MacroProcessorFactory) SiteConfigsDataSerializer(com.enonic.xp.site.SiteConfigsDataSerializer) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) MacroProcessor(com.enonic.xp.portal.macro.MacroProcessor) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName) ContentId(com.enonic.xp.content.ContentId) PortalRequest(com.enonic.xp.portal.PortalRequest) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PropertyTree(com.enonic.xp.data.PropertyTree) ContentPath(com.enonic.xp.content.ContentPath) SiteConfig(com.enonic.xp.site.SiteConfig) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) Mockito.when(org.mockito.Mockito.when) ApplicationKey(com.enonic.xp.app.ApplicationKey) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) Test(org.junit.jupiter.api.Test) Form(com.enonic.xp.form.Form) MacroDescriptors(com.enonic.xp.macro.MacroDescriptors) Mockito(org.mockito.Mockito) MacroDescriptorService(com.enonic.xp.macro.MacroDescriptorService) InputTypeName(com.enonic.xp.inputtype.InputTypeName) Site(com.enonic.xp.site.Site) PrincipalKey(com.enonic.xp.security.PrincipalKey) SiteConfigs(com.enonic.xp.site.SiteConfigs) Input(com.enonic.xp.form.Input) MacroKey(com.enonic.xp.macro.MacroKey) MacroKey(com.enonic.xp.macro.MacroKey) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) Form(com.enonic.xp.form.Form) MacroProcessor(com.enonic.xp.portal.macro.MacroProcessor) Test(org.junit.jupiter.api.Test)

Example 79 with Form

use of com.enonic.xp.form.Form in project xp by enonic.

the class HtmlAreaContentProcessorTest method inner_component_data.

@Test
public void inner_component_data() throws IOException {
    final PropertyTree data1 = new PropertyTree();
    data1.addProperty("htmlData", ValueFactory.newString("<img data-src=\"image://image-id1\"/>"));
    final PropertyTree data2 = new PropertyTree();
    data2.addProperty("htmlData", ValueFactory.newString("<img data-src=\"image://image-id2\"/>"));
    final Form form = Form.create().addFormItem(Input.create().name("htmlData").label("htmlData").inputType(InputTypeName.HTML_AREA).build()).build();
    final PartDescriptor partDescriptor = PartDescriptor.create().key(DescriptorKey.from("app:part")).config(form).build();
    Mockito.when(partDescriptorService.getByKey(partDescriptor.getKey())).thenReturn(partDescriptor);
    final PartComponent partComponent = PartComponent.create().descriptor("myapp:partest").descriptor(partDescriptor.getKey()).config(data2).build();
    final LayoutDescriptor layoutDescriptor = LayoutDescriptor.create().key(DescriptorKey.from("app:layout")).config(form).regions(RegionDescriptors.create().add(RegionDescriptor.create().name("part").build()).build()).build();
    Mockito.when(layoutDescriptorService.getByKey(layoutDescriptor.getKey())).thenReturn(layoutDescriptor);
    final LayoutComponent layoutComponent = LayoutComponent.create().descriptor("myapp:layout").descriptor(layoutDescriptor.getKey()).regions(LayoutRegions.create().add(Region.create().name("part").add(partComponent).build()).build()).config(data1).build();
    final PageDescriptor pageDescriptor = PageDescriptor.create().regions(RegionDescriptors.create().add(RegionDescriptor.create().name("region").build()).build()).key(DescriptorKey.from("app:page")).config(Form.create().build()).build();
    Mockito.when(pageDescriptorService.getByKey(pageDescriptor.getKey())).thenReturn(pageDescriptor);
    final Page page = Page.create().config(new PropertyTree()).descriptor(pageDescriptor.getKey()).regions(PageRegions.create().add(Region.create().name("region").add(layoutComponent).build()).build()).build();
    final EditableContent editableContent = new EditableContent(Media.create().name("myContentName").type(contentTypeName).page(page).parentPath(ContentPath.ROOT).data(new PropertyTree()).build());
    result.getEditor().edit(editableContent);
    assertEquals(2, editableContent.processedReferences.build().getSize());
    assertTrue(editableContent.processedReferences.build().contains(ContentId.from("image-id1")));
    assertTrue(editableContent.processedReferences.build().contains(ContentId.from("image-id2")));
}
Also used : PageDescriptor(com.enonic.xp.page.PageDescriptor) LayoutDescriptor(com.enonic.xp.region.LayoutDescriptor) Form(com.enonic.xp.form.Form) PartDescriptor(com.enonic.xp.region.PartDescriptor) PropertyTree(com.enonic.xp.data.PropertyTree) EditableContent(com.enonic.xp.content.EditableContent) Page(com.enonic.xp.page.Page) PartComponent(com.enonic.xp.region.PartComponent) LayoutComponent(com.enonic.xp.region.LayoutComponent) Test(org.junit.jupiter.api.Test)

Aggregations

Form (com.enonic.xp.form.Form)79 Test (org.junit.jupiter.api.Test)59 PropertyTree (com.enonic.xp.data.PropertyTree)36 FormDefaultValuesProcessor (com.enonic.xp.form.FormDefaultValuesProcessor)17 Input (com.enonic.xp.form.Input)17 FormItemSet (com.enonic.xp.form.FormItemSet)13 FormOptionSet (com.enonic.xp.form.FormOptionSet)11 PatternIndexConfigDocument (com.enonic.xp.index.PatternIndexConfigDocument)10 ContentType (com.enonic.xp.schema.content.ContentType)8 FormOptionSetOption (com.enonic.xp.form.FormOptionSetOption)7 AbstractSchemaTest (com.enonic.xp.core.impl.schema.AbstractSchemaTest)5 PropertySet (com.enonic.xp.data.PropertySet)5 Content (com.enonic.xp.content.Content)4 CreateContentParams (com.enonic.xp.content.CreateContentParams)4 EditableContent (com.enonic.xp.content.EditableContent)4 MacroKey (com.enonic.xp.macro.MacroKey)4 XData (com.enonic.xp.schema.xdata.XData)4 SiteConfigs (com.enonic.xp.site.SiteConfigs)4 ContentPath (com.enonic.xp.content.ContentPath)3 Page (com.enonic.xp.page.Page)3