diff --git a/javascript-modules-engine-java/src/main/java/org/jahia/modules/javascript/modules/engine/js/server/RenderHelper.java b/javascript-modules-engine-java/src/main/java/org/jahia/modules/javascript/modules/engine/js/server/RenderHelper.java index c22d1e7..21d7ec1 100644 --- a/javascript-modules-engine-java/src/main/java/org/jahia/modules/javascript/modules/engine/js/server/RenderHelper.java +++ b/javascript-modules-engine-java/src/main/java/org/jahia/modules/javascript/modules/engine/js/server/RenderHelper.java @@ -35,6 +35,7 @@ import org.jahia.services.render.RenderException; import org.jahia.services.render.RenderService; import org.jahia.services.render.Resource; +import org.jahia.services.usermanager.JahiaUser; import org.jahia.taglibs.template.include.*; import org.jahia.taglibs.uicomponents.Functions; import org.slf4j.Logger; @@ -121,7 +122,12 @@ public JCRNodeWrapper getBoundNode(JCRNodeWrapper node, RenderContext context) { } public String renderComponent(Map attr, RenderContext renderContext) throws RepositoryException { - return jcrTemplate.doExecuteWithSystemSessionAsUser(jcrSessionFactory.getCurrentUser(), renderContext.getWorkspace(), + // first try to get the aliased user from the session (when using customized preview for instance), otherwise use the current user + JahiaUser user = jcrSessionFactory.getCurrentAliasedUser(); + if (user == null) { + user = jcrSessionFactory.getCurrentUser(); + } + return jcrTemplate.doExecuteWithSystemSessionAsUser(user, renderContext.getWorkspace(), renderContext.getMainResource().getLocale(), session -> { JCRNodeWrapper node = JSNodeMapper.toVirtualNode((Map) attr.get("content"), session, renderContext); diff --git a/javascript-modules-engine/tests/cypress/e2e/ui/virtualNodeTest.cy.ts b/javascript-modules-engine/tests/cypress/e2e/ui/virtualNodeTest.cy.ts new file mode 100644 index 0000000..d38bcf5 --- /dev/null +++ b/javascript-modules-engine/tests/cypress/e2e/ui/virtualNodeTest.cy.ts @@ -0,0 +1,44 @@ +import {addNode, createUser, deleteUser} from '@jahia/cypress'; +import {addSimplePage} from '../../utils/Utils'; + +describe('Test virtual nodes', () => { + const pageName = 'testVirtualNode'; + + before('Create test page and contents', () => { + addSimplePage('/sites/npmTestSite/home', pageName, pageName, 'en', 'simple', [ + { + name: 'pagecontent', + primaryNodeType: 'jnt:contentList' + } + ]).then(() => { + addNode({ + parentPathOrId: `/sites/npmTestSite/home/${pageName}/pagecontent`, + name: 'test', + primaryNodeType: 'npmExample:testVirtualNode', + properties: [ + {name: 'jcr:title', value: 'Test Virtual Node'} + ] + }); + }); + }); + + it(`${pageName}: Check virtual nodes are correctly rendered in preview mode`, function () { + cy.login(); + cy.visit(`/cms/render/default/en/sites/npmTestSite/home/${pageName}.html`); + cy.get('div[data-testid="testVirtualNodeSample_myProperty"]').contains('this is a virtual node property'); + cy.get('div[data-testid="virtualNode_aliasedUser"]').should('be.empty'); // logged as root, no alias + cy.logout(); + }); + it(`${pageName}: Check virtual nodes are correctly rendered in customized preview mode`, function () { + cy.login(); + let users = ['fooUser', 'barUser']; + users.forEach(user => { + createUser(user, 'testPassword'); + cy.visit(`/cms/render/default/en/sites/npmTestSite/home/${pageName}.html?alias=${user}`); + cy.get('div[data-testid="testVirtualNodeSample_myProperty"]').should('have.text', 'this is a virtual node property'); + cy.get('div[data-testid="virtualNode_aliasedUser"]').should('have.text', user); + deleteUser('testUser'); + }); + cy.logout(); + }); +}); diff --git a/javascript-modules-engine/tests/jahia-module/settings/definitions.cnd b/javascript-modules-engine/tests/jahia-module/settings/definitions.cnd index ad57343..9891cd6 100644 --- a/javascript-modules-engine/tests/jahia-module/settings/definitions.cnd +++ b/javascript-modules-engine/tests/jahia-module/settings/definitions.cnd @@ -137,3 +137,8 @@ [npmExample:testRule] > jnt:content, npmExampleMix:npmExampleComponent, mix:title - triggerProp (string) - resultProp (string) + +[npmExample:testVirtualNode] > jnt:content, npmExampleMix:npmExampleComponent, mix:title + +[npmExample:testVirtualNodeSample] > jnt:content, npmExampleMix:npmExampleComponent, mix:title + - myProperty (string) diff --git a/javascript-modules-engine/tests/jahia-module/src/react/server/views/index.js b/javascript-modules-engine/tests/jahia-module/src/react/server/views/index.js index 0e90cfe..5927e0a 100644 --- a/javascript-modules-engine/tests/jahia-module/src/react/server/views/index.js +++ b/javascript-modules-engine/tests/jahia-module/src/react/server/views/index.js @@ -1,29 +1,31 @@ +export * from './contentList/ContentListParameters'; export * from './hydratedNavMenu/HydratedNavMenu'; export * from './navMenu/NavMenu'; -export * from './testCurrentContent/TestCurrentContent'; +export * from './testAbsoluteAreas/TestAbsoluteAreas'; export * from './testAreaColumns/TestAreaColumns'; export * from './testAreas/TestAreas'; -export * from './testAbsoluteAreas/TestAbsoluteAreas'; +export * from './testBoundComponent/TestBoundComponent'; export * from './testConfig/TestConfig'; -export * from './testFindDisplayableContent/TestFindDisplayableContent'; -export * from './testGQL/TestGQL'; -export * from './testRender'; -export * from './testUrl/TestUrl'; -export * from './testGetNodeProps/TestGetNodeProps'; -export * from './testGetChildNodes/TestGetChildNodes'; export * from './testContentTemplate/TestContentTemplate'; -export * from './testContentTemplate/TestContentTemplateView'; export * from './testContentTemplate/TestContentTemplateOtherView'; -export * from './testReactViewRegistration/TestReactViewRegistration'; -export * from './testJCRQuery/TestJCRQuery'; -export * from './testReactClientSide/TestReactClientSide'; -export * from './contentList/ContentListParameters'; +export * from './testContentTemplate/TestContentTemplateView'; +export * from './testCrashingView/TestCrashingView'; +export * from './testCurrentContent/TestCurrentContent'; export * from './testCurrentUser/TestCurrentUser'; -export * from './testIsNodeType/TestIsNodeType'; +export * from './testFindDisplayableContent/TestFindDisplayableContent'; +export * from './testGQL/TestGQL'; +export * from './testGetChildNodes/TestGetChildNodes'; +export * from './testGetNodeProps/TestGetNodeProps'; export * from './testHasPermission/TestHasPermission'; -export * from './testUrlParameters/TestUrlParameters'; -export * from './testLocale/TestLocale'; -export * from './testCrashingView/TestCrashingView'; -export * from './testBoundComponent/TestBoundComponent'; export * from './testI18n/TestI18n'; +export * from './testIsNodeType/TestIsNodeType'; +export * from './testJCRQuery/TestJCRQuery'; +export * from './testLocale/TestLocale'; +export * from './testReactClientSide/TestReactClientSide'; +export * from './testReactViewRegistration/TestReactViewRegistration'; +export * from './testRender'; export * from './testRule/TestRule'; +export * from './testUrl/TestUrl'; +export * from './testUrlParameters/TestUrlParameters'; +export * from './testVirtualNode'; + diff --git a/javascript-modules-engine/tests/jahia-module/src/react/server/views/testVirtualNode/TestVirtualNode.jsx b/javascript-modules-engine/tests/jahia-module/src/react/server/views/testVirtualNode/TestVirtualNode.jsx new file mode 100644 index 0000000..e68f884 --- /dev/null +++ b/javascript-modules-engine/tests/jahia-module/src/react/server/views/testVirtualNode/TestVirtualNode.jsx @@ -0,0 +1,33 @@ +import React from 'react'; +import {defineJahiaComponent, Render, useServerContext} from '@jahia/javascript-modules-library'; + + +export const TestVirtualNode = () => { + const {currentNode, renderContext} = useServerContext(); + const aliasedUser = renderContext.getMainResource().getNode().getSession().getAliasedUser(); + // create a virtual node (TestVirtualNodeSample) + const testVirtualNodeSample = { + name: 'testVirtualNodeSample', + nodeType: 'npmExample:testVirtualNodeSample', + properties: { + 'myProperty': 'this is a virtual node property' + } + }; + return ( + <> +

test virtual node

+
+
{aliasedUser == null ? "" : aliasedUser.getName()}
+ +
+
+ + ) +} + +TestVirtualNode.jahiaComponent = defineJahiaComponent({ + nodeType: 'npmExample:testVirtualNode', + name: 'default', + displayName: 'test virtual node', + componentType: 'view' +}); diff --git a/javascript-modules-engine/tests/jahia-module/src/react/server/views/testVirtualNode/TestVirtualNodeSample.jsx b/javascript-modules-engine/tests/jahia-module/src/react/server/views/testVirtualNode/TestVirtualNodeSample.jsx new file mode 100644 index 0000000..be8e707 --- /dev/null +++ b/javascript-modules-engine/tests/jahia-module/src/react/server/views/testVirtualNode/TestVirtualNodeSample.jsx @@ -0,0 +1,16 @@ +import React from 'react'; +import {defineJahiaComponent, getNodeProps, useServerContext} from '@jahia/javascript-modules-library'; + +export const TestVirtualNodeSample = () => { + const {currentNode} = useServerContext(); + const props = getNodeProps(currentNode, ['myProperty']); + + return ( +
{props['myProperty']}
+ ); +}; + +TestVirtualNodeSample.jahiaComponent = defineJahiaComponent({ + nodeType: 'npmExample:testVirtualNodeSample', + componentType: 'view', +}); \ No newline at end of file diff --git a/javascript-modules-engine/tests/jahia-module/src/react/server/views/testVirtualNode/index.js b/javascript-modules-engine/tests/jahia-module/src/react/server/views/testVirtualNode/index.js new file mode 100644 index 0000000..6380709 --- /dev/null +++ b/javascript-modules-engine/tests/jahia-module/src/react/server/views/testVirtualNode/index.js @@ -0,0 +1,2 @@ +export * from './TestVirtualNode'; +export * from './TestVirtualNodeSample'; \ No newline at end of file