Search in sources :

Example 1 with RenderContext

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);
    }
}
Also used : Div(org.structr.web.entity.html.Div) RenderContext(org.structr.web.common.RenderContext) Head(org.structr.web.entity.html.Head) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Html(org.structr.web.entity.html.Html) Title(org.structr.web.entity.html.Title) Page(org.structr.web.entity.dom.Page) Body(org.structr.web.entity.html.Body) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 2 with RenderContext

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");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) RenderContext(org.structr.web.common.RenderContext) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) StringProperty(org.structr.core.property.StringProperty) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 3 with RenderContext

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");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) RenderContext(org.structr.web.common.RenderContext) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) StringProperty(org.structr.core.property.StringProperty) LinkedList(java.util.LinkedList) SchemaNode(org.structr.core.entity.SchemaNode) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 4 with RenderContext

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.");
    }
}
Also used : Group(org.structr.core.entity.Group) NodeAttribute(org.structr.core.graph.NodeAttribute) RenderContext(org.structr.web.common.RenderContext) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 5 with RenderContext

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();
}
Also used : RenderContext(org.structr.web.common.RenderContext) StringRenderBuffer(org.structr.web.common.StringRenderBuffer)

Aggregations

RenderContext (org.structr.web.common.RenderContext)15 FrameworkException (org.structr.common.error.FrameworkException)12 Tx (org.structr.core.graph.Tx)11 Test (org.junit.Test)9 StructrUiTest (org.structr.web.StructrUiTest)9 SecurityContext (org.structr.common.SecurityContext)5 NodeAttribute (org.structr.core.graph.NodeAttribute)5 NodeInterface (org.structr.core.graph.NodeInterface)5 DOMNode (org.structr.web.entity.dom.DOMNode)5 Page (org.structr.web.entity.dom.Page)5 App (org.structr.core.app.App)4 StructrApp (org.structr.core.app.StructrApp)4 LinkedList (java.util.LinkedList)3 AbstractNode (org.structr.core.entity.AbstractNode)3 Principal (org.structr.core.entity.Principal)3 PropertyKey (org.structr.core.property.PropertyKey)3 PropertyMap (org.structr.core.property.PropertyMap)3 StringProperty (org.structr.core.property.StringProperty)3 StringRenderBuffer (org.structr.web.common.StringRenderBuffer)3 File (org.structr.web.entity.File)3