Search in sources :

Example 6 with ProcessHtmlParams

use of com.enonic.xp.portal.url.ProcessHtmlParams in project xp by enonic.

the class PortalUrlServiceImpl_processHtmlTest method process_image_with_styles.

@Test
public void process_image_with_styles() {
    // Creates a content
    final Media media = ContentFixtures.newMedia();
    Mockito.when(this.contentService.getById(media.getId())).thenReturn(media);
    Mockito.when(this.contentService.getBinaryKey(media.getId(), media.getMediaAttachment().getBinaryReference())).thenReturn("binaryHash");
    final ImageStyle imageStyle = ImageStyle.create().name("mystyle").aspectRatio("2:1").filter("myfilter").build();
    final StyleDescriptor styleDescriptor = StyleDescriptor.create().application(ApplicationKey.from("myapp")).addStyleElement(imageStyle).build();
    Mockito.when(styleDescriptorService.getByApplications(Mockito.any())).thenReturn(StyleDescriptors.from(styleDescriptor));
    // Process an html text containing a style
    final String link1 = "<a href=\"image://" + media.getId() + "?style=mystyle\">Image</a>";
    final String link2 = "<a href=\"image://" + media.getId() + "?style=missingstyle\">Image</a>";
    final ProcessHtmlParams params1 = new ProcessHtmlParams().portalRequest(this.portalRequest).value(link1);
    final ProcessHtmlParams params2 = new ProcessHtmlParams().portalRequest(this.portalRequest).value(link2);
    final String processedLink1 = this.service.processHtml(params1);
    final String processedLink2 = this.service.processHtml(params2);
    // Checks that the page URL of the content is returned
    final String expectedResult1 = "<a href=\"/site/default/draft/context/path/_/image/" + media.getId() + ":8cf45815bba82c9711c673c9bb7304039a790026/" + "block-768-384" + "/" + media.getName() + "?filter=myfilter\">Image</a>";
    final String expectedResult2 = "<a href=\"/site/default/draft/context/path/_/image/" + media.getId() + ":8cf45815bba82c9711c673c9bb7304039a790026/" + "width-768" + "/" + media.getName() + "\">Image</a>";
    assertEquals(expectedResult1, processedLink1);
    assertEquals(expectedResult2, processedLink2);
}
Also used : StyleDescriptor(com.enonic.xp.style.StyleDescriptor) Media(com.enonic.xp.content.Media) ProcessHtmlParams(com.enonic.xp.portal.url.ProcessHtmlParams) ImageStyle(com.enonic.xp.style.ImageStyle) Test(org.junit.jupiter.api.Test)

Example 7 with ProcessHtmlParams

use of com.enonic.xp.portal.url.ProcessHtmlParams in project xp by enonic.

the class PortalUrlServiceImpl_processHtmlTest method process_image_with_scale.

@Test
public void process_image_with_scale() {
    // Creates a content
    final Media media = ContentFixtures.newMedia();
    Mockito.when(this.contentService.getById(media.getId())).thenReturn(media);
    Mockito.when(this.contentService.getBinaryKey(media.getId(), media.getMediaAttachment().getBinaryReference())).thenReturn("binaryHash");
    // Process an html text containing a link to this content
    final ProcessHtmlParams params = new ProcessHtmlParams().portalRequest(this.portalRequest).value("<a href=\"image://" + media.getId() + "?scale=21:9\">Image</a>");
    // Checks that the page URL of the content is returned
    final String processedHtml = this.service.processHtml(params);
    assertEquals("<a href=\"/site/default/draft/context/path/_/image/" + media.getId() + ":8cf45815bba82c9711c673c9bb7304039a790026/" + "block-768-324" + "/" + media.getName() + "\">Image</a>", processedHtml);
}
Also used : Media(com.enonic.xp.content.Media) ProcessHtmlParams(com.enonic.xp.portal.url.ProcessHtmlParams) Test(org.junit.jupiter.api.Test)

Example 8 with ProcessHtmlParams

use of com.enonic.xp.portal.url.ProcessHtmlParams in project xp by enonic.

the class PortalUrlServiceImpl_processHtmlTest method process_unknown_image.

@Test
public void process_unknown_image() {
    when(contentService.getById(isA(ContentId.class))).thenAnswer((params) -> {
        final ContentId id = params.getArgument(0);
        throw new ContentNotFoundException(id, ContextAccessor.current().getBranch());
    });
    // Process an html text containing a link to an unknown media
    final ProcessHtmlParams params = new ProcessHtmlParams().portalRequest(this.portalRequest).value("<a href=\"image://123\">Image</a>");
    // Checks that the error 500 page is returned
    final String processedHtml = this.service.processHtml(params);
    assertEquals("<a href=\"/site/default/draft/context/path/_/error/500?message=Image+with+%5B123%5D+id+not+found\">Image</a>", processedHtml);
}
Also used : ContentNotFoundException(com.enonic.xp.content.ContentNotFoundException) ProcessHtmlParams(com.enonic.xp.portal.url.ProcessHtmlParams) ContentId(com.enonic.xp.content.ContentId) Test(org.junit.jupiter.api.Test)

Example 9 with ProcessHtmlParams

use of com.enonic.xp.portal.url.ProcessHtmlParams in project xp by enonic.

the class PortalUrlServiceImpl_processHtmlTest method process_single_content.

@Test
public void process_single_content() {
    // Creates a content
    final Content content = ContentFixtures.newContent();
    Mockito.when(this.contentService.getById(content.getId())).thenReturn(content);
    // Process an html text containing a link to this content
    final ProcessHtmlParams params = new ProcessHtmlParams().portalRequest(this.portalRequest).value("<a href=\"content://" + content.getId() + "\">Content</a>");
    // Checks that the page URL of the content is returned
    final String processedHtml = this.service.processHtml(params);
    assertEquals("<a href=\"/site/default/draft" + content.getPath() + "\">Content</a>", processedHtml);
}
Also used : Content(com.enonic.xp.content.Content) ProcessHtmlParams(com.enonic.xp.portal.url.ProcessHtmlParams) Test(org.junit.jupiter.api.Test)

Example 10 with ProcessHtmlParams

use of com.enonic.xp.portal.url.ProcessHtmlParams in project xp by enonic.

the class TextRenderer method render.

@Override
public PortalResponse render(final TextComponent textComponent, final PortalRequest portalRequest) {
    final RenderMode renderMode = portalRequest.getMode();
    final PortalResponse.Builder portalResponseBuilder = PortalResponse.create();
    portalResponseBuilder.contentType(MediaType.create("text", "html")).postProcess(false);
    if (textComponent.getText().isEmpty()) {
        renderEmptyTextComponent(textComponent, renderMode, portalResponseBuilder);
    } else {
        if (renderMode == RenderMode.EDIT) {
            portalResponseBuilder.body(MessageFormat.format(COMPONENT_EDIT_MODE_HTML, textComponent.getType().toString(), textComponent.getText()));
        } else {
            ProcessHtmlParams params = new ProcessHtmlParams().portalRequest(portalRequest).value(textComponent.getText());
            final String processedHtml = removeEmptyFigCaptionTags(service.processHtml(params));
            portalResponseBuilder.body(MessageFormat.format(COMPONENT_PREVIEW_MODE_HTML, textComponent.getType().toString(), processedHtml));
        }
    }
    return portalResponseBuilder.build();
}
Also used : RenderMode(com.enonic.xp.portal.RenderMode) PortalResponse(com.enonic.xp.portal.PortalResponse) ProcessHtmlParams(com.enonic.xp.portal.url.ProcessHtmlParams)

Aggregations

ProcessHtmlParams (com.enonic.xp.portal.url.ProcessHtmlParams)15 Test (org.junit.jupiter.api.Test)13 Media (com.enonic.xp.content.Media)5 Content (com.enonic.xp.content.Content)4 ContentId (com.enonic.xp.content.ContentId)3 ContentNotFoundException (com.enonic.xp.content.ContentNotFoundException)3 Attachment (com.enonic.xp.attachment.Attachment)2 Attachments (com.enonic.xp.attachment.Attachments)2 PortalResponse (com.enonic.xp.portal.PortalResponse)1 RenderMode (com.enonic.xp.portal.RenderMode)1 ImageStyle (com.enonic.xp.style.ImageStyle)1 StyleDescriptor (com.enonic.xp.style.StyleDescriptor)1 InputStream (java.io.InputStream)1