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();
}
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);
}
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);
}
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")));
}
Aggregations