Skip to content

Commit

Permalink
Component Service mapping should not apply page contributions #10845
Browse files Browse the repository at this point in the history
  • Loading branch information
ashklianko committed Jan 13, 2025
1 parent d8f1227 commit 1634324
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,20 @@ public Class<ControllerMappingDescriptor> getType()
}

@Override
public PortalResponse doRender( final ControllerMappingDescriptor controllerMappingDescriptor, final PortalRequest portalRequest )
public PortalResponse doRender( final ControllerMappingDescriptor descriptor, final PortalRequest portalRequest )
{
return portalRequest.getControllerScript().execute( portalRequest );
final PortalResponse portalResponse = portalRequest.getControllerScript().execute( portalRequest );
return isComponentService( descriptor ) ? PortalResponse.create( portalResponse ).applyFilters( false ).build() : portalResponse;
}

@Override
protected boolean isPageContributionsAllowed( final ControllerMappingDescriptor controllerMappingDescriptor, final PortalRequest portalRequest )
{
return !isComponentService( controllerMappingDescriptor );
}

private boolean isComponentService( final ControllerMappingDescriptor controllerMappingDescriptor )
{
return "component".equals( controllerMappingDescriptor.getService() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@ public PortalResponse render( final R component, final PortalRequest portalReque
PortalResponse portalResponse = doRender( component, portalRequest );
portalResponse = this.postProcessor.processResponseInstructions( portalRequest, portalResponse );
portalResponse = executeResponseProcessors( portalRequest, portalResponse );
portalResponse = this.postProcessor.processResponseContributions( portalRequest, portalResponse );
return portalResponse;
return isPageContributionsAllowed(component, portalRequest)
? this.postProcessor.processResponseContributions( portalRequest, portalResponse )
: portalResponse;
}

protected abstract PortalResponse doRender( R component, PortalRequest portalRequest );

protected boolean isPageContributionsAllowed( final R component, final PortalRequest portalRequest )
{
return true;
}

private PortalResponse executeResponseProcessors( final PortalRequest portalRequest, final PortalResponse portalResponse )
{
if ( !portalResponse.applyFilters() )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.enonic.xp.portal.impl.rendering;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import com.google.common.net.MediaType;

import com.enonic.xp.portal.PortalRequest;
import com.enonic.xp.portal.PortalResponse;
import com.enonic.xp.portal.controller.ControllerScript;
import com.enonic.xp.portal.controller.ControllerScriptFactory;
import com.enonic.xp.portal.impl.postprocess.PostProcessorImpl;
import com.enonic.xp.portal.impl.postprocess.TestPostProcessInjection;
import com.enonic.xp.portal.impl.processor.ProcessorChainResolver;
import com.enonic.xp.portal.script.PortalScriptService;
import com.enonic.xp.resource.ResourceKey;
import com.enonic.xp.site.mapping.ControllerMappingDescriptor;
import com.enonic.xp.site.processor.ResponseProcessorDescriptors;
import com.enonic.xp.web.HttpStatus;
import com.enonic.xp.web.websocket.WebSocketEvent;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ControllerMappingRendererTest
{
private PortalRequest portalRequest;

private ControllerMappingRenderer renderer;

ControllerScriptFactory controllerScriptFactory;

ProcessorChainResolver processorChainResolver;

PortalScriptService portalScriptService;

PostProcessorImpl postProcessor;

@BeforeEach
public void before()
{

final PostProcessorImpl processorImpl = new PostProcessorImpl();
postProcessor = Mockito.spy( processorImpl );
postProcessor.addInjection( new TestPostProcessInjection() );
controllerScriptFactory = mock( ControllerScriptFactory.class );
processorChainResolver = mock( ProcessorChainResolver.class );
portalScriptService = mock( PortalScriptService.class );

renderer = new ControllerMappingRenderer( postProcessor, portalScriptService, processorChainResolver );

this.portalRequest = new PortalRequest();
this.portalRequest.setControllerScript( makeControllerScript() );

when( processorChainResolver.resolve( this.portalRequest ) ).thenReturn( ResponseProcessorDescriptors.empty() );
}

@Test
public void pageContributionsAndResponseProcessorsAreIgnoredForComponentService()
{
// setup
final ControllerMappingDescriptor mappingDescriptor = ControllerMappingDescriptor.create()
.controller( ResourceKey.from( "com.enonic.app.myapp:/site/foobar/component_override.js" ) )
.service( "component" )
.build();

// exercise
final PortalResponse portalResponse = renderer.render( mappingDescriptor, portalRequest );

// verify
assertEquals( getComponentHtml(), portalResponse.getAsString() );
Mockito.verify( processorChainResolver, Mockito.times( 0 ) ).resolve( Mockito.any( PortalRequest.class) );
Mockito.verify( postProcessor, Mockito.times( 0 ) )
.processResponseContributions( Mockito.any( PortalRequest.class), Mockito.any( PortalResponse.class ) );
}

@Test
public void pageContributionsAndResponseProcessorsAreNotIgnored()
{
// setup
final ControllerMappingDescriptor mappingDescriptor = ControllerMappingDescriptor.create()
.controller( ResourceKey.from( "com.enonic.app.myapp:/site/foobar/component_override.js" ) )
.build();

// exercise
final PortalResponse portalResponse = renderer.render( mappingDescriptor, portalRequest );

// verify
assertEquals( getComponentHtml(), portalResponse.getAsString() );
Mockito.verify( processorChainResolver, Mockito.times( 1 ) ).resolve( Mockito.any( PortalRequest.class) );
Mockito.verify( postProcessor, Mockito.times( 1 ) )
.processResponseContributions( Mockito.any( PortalRequest.class), Mockito.any( PortalResponse.class ) );
}

private ControllerScript makeControllerScript()
{
return new ControllerScript()
{
@Override
public PortalResponse execute( final PortalRequest portalRequest )
{
return PortalResponse.create()
.body(getComponentHtml() )
.contentType( MediaType.HTML_UTF_8 )
.status( HttpStatus.OK )
.build();
}

@Override
public void onSocketEvent( final WebSocketEvent event )
{
}
};
}

private String getComponentHtml()
{
return "<div class=\"row\"><div data-portal-region=\"left\" class=\"col-left\"></div><div data-portal-region=\"right\" class=\"col-right\"></div></div>";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import com.enonic.xp.app.ApplicationKey;
import com.enonic.xp.branch.Branch;
Expand Down Expand Up @@ -52,11 +53,14 @@ public class PageRendererTest

PortalScriptService portalScriptService;

PostProcessorImpl postProcessor;

@BeforeEach
public void before()
{

final PostProcessorImpl postProcessor = new PostProcessorImpl();
final PostProcessorImpl processorImpl = new PostProcessorImpl();
postProcessor = Mockito.spy( processorImpl );
postProcessor.addInjection( new TestPostProcessInjection() );
controllerScriptFactory = mock( ControllerScriptFactory.class );
processorChainResolver = mock( ProcessorChainResolver.class );
Expand Down Expand Up @@ -136,6 +140,9 @@ public void contentWithRenderModeEdit()
final String response =
"<html><head><title>My Content</title></head><body data-portal-component-type=\"page\"></body></html>";
assertEquals( response, portalResponse.getAsString() );
Mockito.verify( processorChainResolver, Mockito.times( 1 ) ).resolve( Mockito.any( PortalRequest.class) );
Mockito.verify( postProcessor, Mockito.times( 1 ) )
.processResponseContributions( Mockito.any( PortalRequest.class), Mockito.any( PortalResponse.class ) );
}

@Test
Expand Down

0 comments on commit 1634324

Please sign in to comment.