use of org.structr.web.common.RenderContext in project structr by structr.
the class CustomHtmlAttributeTest method testCustomHtmlAttribute.
@Test
public void testCustomHtmlAttribute() {
try (final Tx tx = app.tx()) {
// create a page
final Page newPage = Page.createNewPage(securityContext, "customAttributeTestPage");
final Html html = createElement(newPage, newPage, "html");
final Head head = createElement(newPage, html, "head");
final Title title = createElement(newPage, head, "title", "Test Page for custom html attributes");
final Body body = createElement(newPage, html, "body");
final Div div1 = createElement(newPage, body, "div", "DIV with old-style data attribute");
div1.setProperty(new GenericProperty<String>("data-test-attribute-old-style"), "old-style data attribute");
final Div div2 = createElement(newPage, body, "div", "DIV with new-style custom html attribute");
div2.setProperty(new GenericProperty<String>("_custom_html_test-attribute-new-style"), "new-style custom attribute");
final Div div3 = createElement(newPage, body, "div", "DIV with data-attribute as new-style custom html attribute");
div3.setProperty(new GenericProperty<String>("_custom_html_data-test-attribute-new-style"), "new-style custom data-attribute");
final RenderContext renderContext = new RenderContext(securityContext);
newPage.render(renderContext, 0);
final String renderedHtml = StringUtils.join(renderContext.getBuffer().getQueue(), "");
final String expectedHtml = "<!DOCTYPE html>\n" + "<html>\n" + " <head>\n" + " <title>Test Page for custom html attributes</title>\n" + " </head>\n" + " <body>\n" + " <div data-test-attribute-old-style=\"old-style data attribute\">DIV with old-style data attribute</div>\n" + " <div test-attribute-new-style=\"new-style custom attribute\">DIV with new-style custom html attribute</div>\n" + " <div data-test-attribute-new-style=\"new-style custom data-attribute\">DIV with data-attribute as new-style custom html attribute</div>\n" + " </body>\n" + "</html>";
Assert.assertEquals(expectedHtml, renderedHtml);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
}
use of org.structr.web.common.RenderContext in project structr by structr.
the class RenderContextTest method testFunctionEvaluationInDynamicTypes.
@Test
public void testFunctionEvaluationInDynamicTypes() {
NodeInterface item = null;
try (final Tx tx = app.tx()) {
app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"), new NodeAttribute(new StringProperty("_testMethodCalled"), "Boolean"), new NodeAttribute(new StringProperty("___testMethod"), "set(this, 'testMethodCalled', true)"));
// compile the stuff
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class itemClass = config.getNodeEntityClass("Item");
// create parent/child relationship
try (final Tx tx = app.tx()) {
item = app.create(itemClass, new NodeAttribute(SchemaNode.name, "Item"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// check property access in template expressions
try (final Tx tx = app.tx()) {
final RenderContext renderContext = new RenderContext(securityContext);
renderContext.putDataObject("item", item);
assertEquals("Invalid combined array dot syntax result: ", "Item", Scripting.replaceVariables(renderContext, item, "${find('Item')[0].name}"));
Scripting.replaceVariables(renderContext, item, "${item.testMethod()}");
assertEquals("Invalid method evaluation result: ", "true", Scripting.replaceVariables(renderContext, item, "${item.testMethodCalled}"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
}
use of org.structr.web.common.RenderContext in project structr by structr.
the class RenderContextTest method testScriptEvaluation.
@Test
public void testScriptEvaluation() {
Settings.CypherDebugLogging.setValue(true);
try (final Tx tx = app.tx()) {
// create a Project type
final SchemaNode projectNode = createTestNode(SchemaNode.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Project"));
// create a Task type with a string property "task"
final SchemaNode taskNode = createTestNode(SchemaNode.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Task"), new NodeAttribute<>(new StringProperty("_task"), "String"));
// create a schema relationship between them
createTestNode(SchemaRelationshipNode.class, new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "sourceNode"), projectNode), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "targetNode"), taskNode), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "relationshipType"), "has"), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "sourceMultiplicity"), "1"), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "targetMultiplicity"), "*"), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "sourceJsonName"), "project"), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "targetJsonName"), "tasks"));
tx.success();
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
// obtain class objects to create instances of the above types
final Class projectType = StructrApp.getConfiguration().getNodeEntityClass("Project");
final Class taskType = StructrApp.getConfiguration().getNodeEntityClass("Task");
final PropertyKey taskKey = StructrApp.key(taskType, "task");
final PropertyKey tasksKey = StructrApp.key(projectType, "tasks");
final List<NodeInterface> tasks = new LinkedList<>();
tasks.add(app.create(taskType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Task 1"), new NodeAttribute<>(taskKey, "Task 1")));
tasks.add(app.create(taskType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Task 2"), new NodeAttribute<>(taskKey, "Task 2")));
tasks.add(app.create(taskType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Task 3"), new NodeAttribute<>(taskKey, "Task 3")));
// create a project and a task
final NodeInterface project = app.create(projectType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "project"), new NodeAttribute<>(tasksKey, tasks));
// create an additional test task without a project
final NodeInterface testTask = app.create(taskType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "test task"), new NodeAttribute<>(taskKey, "test task"));
final RenderContext renderContext = new RenderContext(securityContext);
renderContext.putDataObject("project", project);
renderContext.putDataObject("task", testTask);
assertEquals("Invalid scripting evaluation result", "", Scripting.replaceVariables(renderContext, null, "${foo.page}"));
assertEquals("Invalid scripting evaluation result", testTask.getUuid(), Scripting.replaceVariables(renderContext, null, "${task}"));
assertEquals("Invalid scripting evaluation result", "test task", Scripting.replaceVariables(renderContext, null, "${task.task}"));
assertEquals("Invalid scripting evaluation result", tasks.toString(), Scripting.replaceVariables(renderContext, null, "${project.tasks}"));
assertEquals("Invalid scripting evaluation result", tasks.get(0).getUuid(), Scripting.replaceVariables(renderContext, null, "${project.tasks[0]}"));
assertEquals("Invalid scripting evaluation result", tasks.get(1).getUuid(), Scripting.replaceVariables(renderContext, null, "${project.tasks[1]}"));
assertEquals("Invalid scripting evaluation result", tasks.get(2).getUuid(), Scripting.replaceVariables(renderContext, null, "${project.tasks[2]}"));
assertEquals("Invalid scripting evaluation result", "", Scripting.replaceVariables(renderContext, null, "${project.tasks[3]}"));
assertEquals("Invalid scripting evaluation result", "Task 1", Scripting.replaceVariables(renderContext, null, "${project.tasks[0].task}"));
assertEquals("Invalid scripting evaluation result", "Task 2", Scripting.replaceVariables(renderContext, null, "${project.tasks[1].task}"));
assertEquals("Invalid scripting evaluation result", "Task 3", Scripting.replaceVariables(renderContext, null, "${project.tasks[2].task}"));
assertEquals("Invalid scripting evaluation result", "", Scripting.replaceVariables(renderContext, null, "${project.tasks[3].task}"));
tx.success();
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
}
use of org.structr.web.common.RenderContext in project structr by structr.
the class UiScriptingTest method testGroupFunctions.
@Test
public void testGroupFunctions() {
Group group = null;
User tester = null;
try (final Tx tx = app.tx()) {
// create test user
tester = createTestNode(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "tester"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "test"));
// create test group
group = createTestNode(Group.class, new NodeAttribute<>(StructrApp.key(Group.class, "name"), "test"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
final RenderContext renderContext = new RenderContext(securityContext, new RequestMockUp(), new ResponseMockUp(), RenderContext.EditMode.NONE);
try (final Tx tx = app.tx()) {
// check that the user is not in the group at first
assertFalse("User should not be in the test group before testing", group.getMembers().contains(tester));
// check that is_in_group returns the correct result
assertEquals("Function is_in_group should return false.", false, Scripting.evaluate(renderContext, null, "${is_in_group(first(find('Group')), first(find('User')))}", "test"));
// add user to group
Scripting.evaluate(renderContext, null, "${add_to_group(first(find('Group')), first(find('User')))}", "test");
// check that the user is in the group after the call to add_to_group
final List<Principal> members = group.getMembers();
assertTrue("User should be in the test group now", members.contains(tester));
// check that is_in_group returns the correct result
assertEquals("Function is_in_group should return true.", true, Scripting.evaluate(renderContext, null, "${is_in_group(first(find('Group')), first(find('User')))}", "test"));
// remove user from group
Scripting.evaluate(renderContext, null, "${remove_from_group(first(find('Group')), first(find('User')))}", "test");
// check that the user is not in the group any more after the call to remove_from_group
assertFalse("User should not be in the test group before testing", group.getMembers().contains(tester));
// check that is_in_group returns the correct result
assertEquals("Function is_in_group should return false.", false, Scripting.evaluate(renderContext, null, "${is_in_group(first(find('Group')), first(find('User')))}", "test"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
}
use of org.structr.web.common.RenderContext in project structr by structr.
the class DOMNode method getContent.
public static String getContent(final DOMNode thisNode, final RenderContext.EditMode editMode) throws FrameworkException {
final RenderContext ctx = new RenderContext(thisNode.getSecurityContext(), null, null, editMode);
final StringRenderBuffer buffer = new StringRenderBuffer();
ctx.setBuffer(buffer);
thisNode.render(ctx, 0);
// extract source
return buffer.getBuffer().toString();
}
Aggregations