Skip to content

Commit

Permalink
Page Contributions are only injected after page reload #7301
Browse files Browse the repository at this point in the history
  • Loading branch information
ashklianko committed Jan 10, 2025
1 parent 9188151 commit af8dfb9
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.enonic.xp.page.PageTemplateService;
import com.enonic.xp.portal.PortalRequest;
import com.enonic.xp.portal.PortalResponse;
import com.enonic.xp.portal.RenderMode;
import com.enonic.xp.portal.handler.WebHandlerHelper;
import com.enonic.xp.portal.impl.ContentResolver;
import com.enonic.xp.portal.impl.ContentResolverResult;
Expand Down Expand Up @@ -112,7 +113,10 @@ public WebResponse handle( final WebRequest webRequest )

private PortalResponse doHandle( final PortalRequest portalRequest )
{
final PortalResponse response = rendererDelegate.render( portalRequest.getComponent(), portalRequest );
final PortalResponse result = rendererDelegate.render( portalRequest.getComponent(), portalRequest );
final boolean isEditMode = portalRequest.getMode() == RenderMode.EDIT;
final PortalResponse response = ( isEditMode && result.hasContributions() ) ? addHasContributionsHeader( result ) : result;

return this.postProcessor.processResponseInstructions( portalRequest, response );
}

Expand Down Expand Up @@ -226,4 +230,9 @@ private com.enonic.xp.region.Component getFragmentComponent( final FragmentCompo
return null;
}
}

private PortalResponse addHasContributionsHeader( final PortalResponse source )
{
return PortalResponse.create( source ).header( "X-Has-Contributions", "true" ).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

import com.google.common.net.MediaType;
Expand All @@ -16,7 +17,9 @@
import com.enonic.xp.page.PageTemplateKey;
import com.enonic.xp.portal.PortalRequest;
import com.enonic.xp.portal.PortalResponse;
import com.enonic.xp.portal.RenderMode;
import com.enonic.xp.portal.impl.handler.render.RenderBaseHandlerTest;
import com.enonic.xp.portal.postprocess.HtmlTag;
import com.enonic.xp.region.Component;
import com.enonic.xp.region.FragmentComponent;
import com.enonic.xp.region.LayoutComponent;
Expand All @@ -37,6 +40,7 @@
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ComponentHandlerTest
Expand Down Expand Up @@ -88,8 +92,9 @@ public void testComponentFound()

final PortalResponse portalResponse =
PortalResponse.create().body( "component rendered" ).header( "some-header", "some-value" ).status( HttpStatus.OK ).build();

Mockito.when( this.postProcessor.processResponseInstructions( Mockito.any(), Mockito.any() ) ).thenReturn( portalResponse );
final ArgumentCaptor<PortalResponse> portalResponseCaptor = ArgumentCaptor.forClass( PortalResponse.class );
Mockito.when( this.postProcessor.processResponseInstructions( Mockito.any(), portalResponseCaptor.capture() ) )
.thenReturn( portalResponse );

setRendererResult( portalResponse );

Expand All @@ -101,6 +106,7 @@ public void testComponentFound()
assertEquals( MediaType.PLAIN_TEXT_UTF_8, res.getContentType() );
assertEquals( "some-value", res.getHeaders().get( "some-header" ) );
assertEquals( "component rendered", res.getBody() );
assertNull( portalResponseCaptor.getValue().getHeaders().get( "Has-Contributions" ) );
}

@Test
Expand Down Expand Up @@ -162,6 +168,78 @@ public void getSiteNotFound()
assertEquals( "Site for [/site/somepath/content] not found", e.getMessage() );
}

@Test
public void testHasContributionsHeaderAdded() throws Exception
{
setupSite();
setupContent();
setupTemplates();

final PortalResponse portalResponse =
PortalResponse.create().contribution( HtmlTag.HEAD_END, "contribute" ).status( HttpStatus.OK ).build();

final ArgumentCaptor<PortalResponse> portalResponseCaptor = ArgumentCaptor.forClass( PortalResponse.class );
Mockito.when( this.postProcessor.processResponseInstructions( Mockito.any(), portalResponseCaptor.capture() ) )
.thenReturn( portalResponse );

setRendererResult( portalResponse );

this.request.setMode( RenderMode.EDIT );
this.request.setEndpointPath( "/_/component/main-region/0" );

final WebResponse res = this.handler.handle( this.request );
assertNotNull( res );
assertEquals( HttpStatus.OK, res.getStatus() );
assertEquals( "true", portalResponseCaptor.getValue().getHeaders().get( "X-Has-Contributions" ) );
}

@Test
public void testHasContributionsHeaderNotAddedInNonEditMode() throws Exception
{
setupSite();
setupContent();
setupTemplates();

final PortalResponse portalResponse =
PortalResponse.create().contribution( HtmlTag.HEAD_END, "contribute" ).status( HttpStatus.OK ).build();

final ArgumentCaptor<PortalResponse> portalResponseCaptor = ArgumentCaptor.forClass( PortalResponse.class );
Mockito.when( this.postProcessor.processResponseInstructions( Mockito.any(), portalResponseCaptor.capture() ) )
.thenReturn( portalResponse );

setRendererResult( portalResponse );

this.request.setEndpointPath( "/_/component/main-region/0" );

final WebResponse res = this.handler.handle( this.request );
assertNotNull( res );
assertEquals( HttpStatus.OK, res.getStatus() );
assertNull( portalResponseCaptor.getValue().getHeaders().get( "X-Has-Contributions" ) );
}

@Test
public void testHasContributionsHeaderNotAddedInEditModeWithoutContributions() throws Exception
{
setupSite();
setupContent();
setupTemplates();

final PortalResponse portalResponse = PortalResponse.create().status( HttpStatus.OK ).build();

final ArgumentCaptor<PortalResponse> portalResponseCaptor = ArgumentCaptor.forClass( PortalResponse.class );
Mockito.when( this.postProcessor.processResponseInstructions( Mockito.any(), portalResponseCaptor.capture() ) )
.thenReturn( portalResponse );

setRendererResult( portalResponse );
this.request.setMode( RenderMode.EDIT );
this.request.setEndpointPath( "/_/component/main-region/0" );

final WebResponse res = this.handler.handle( this.request );
assertNotNull( res );
assertEquals( HttpStatus.OK, res.getStatus() );
assertNull( portalResponseCaptor.getValue().getHeaders().get( "X-Has-Contributions" ) );
}

@Test
public void testEmptyFragmentIsResolved()
throws Exception
Expand Down

0 comments on commit af8dfb9

Please sign in to comment.