Search in sources :

Example 1 with ProcessHtmlParams

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

the class PortalUrlServiceImpl_processHtmlTest method processHtml_image_imageWidths_with_imageSizes.

@Test
public void processHtml_image_imageWidths_with_imageSizes() {
    // 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("<p><figure class=\"editor-align-justify\"><img alt=\"Alt text\" src=\"image://" + media.getId() + "\"/><figcaption>Caption text</figcaption></figure></p>").imageWidths(List.of(660, 1024)).imageSizes("(max-width: 960px) 660px");
    // Checks that the page URL of the content is returned
    final String processedHtml = this.service.processHtml(params);
    assertEquals("<p><figure class=\"editor-align-justify\">" + "<img alt=\"Alt text\" src=\"/site/default/draft/context/path/_/image/" + media.getId() + ":8cf45815bba82c9711c673c9bb7304039a790026/width-768/mycontent\" " + "srcset=\"/site/default/draft/context/path/_/image/" + media.getId() + ":8cf45815bba82c9711c673c9bb7304039a790026/width-660/mycontent 660w," + "/site/default/draft/context/path/_/image/" + media.getId() + ":8cf45815bba82c9711c673c9bb7304039a790026/width-1024/mycontent 1024w\" sizes=\"(max-width: 960px) 660px\"/><figcaption>Caption text</figcaption></figure></p>", processedHtml);
}
Also used : Media(com.enonic.xp.content.Media) ProcessHtmlParams(com.enonic.xp.portal.url.ProcessHtmlParams) Test(org.junit.jupiter.api.Test)

Example 2 with ProcessHtmlParams

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

the class PortalUrlServiceImpl_processHtmlTest method assertProcessHtml.

private void assertProcessHtml(String inputName, String expectedOutputName) throws IOException {
    final String input;
    final String expected;
    // Reads the input and output files
    try (InputStream is = this.getClass().getResourceAsStream(inputName)) {
        input = new String(is.readAllBytes(), StandardCharsets.UTF_8);
    }
    try (InputStream is = this.getClass().getResourceAsStream(expectedOutputName)) {
        expected = new String(is.readAllBytes(), StandardCharsets.UTF_8);
    }
    // Processes the input file
    final ProcessHtmlParams processHtmlParams = new ProcessHtmlParams().value(input);
    final String processedHtml = this.service.processHtml(processHtmlParams);
    // Checks that the processed text is equal to the expected output
    assertEquals(expected, processedHtml);
}
Also used : InputStream(java.io.InputStream) ProcessHtmlParams(com.enonic.xp.portal.url.ProcessHtmlParams)

Example 3 with ProcessHtmlParams

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

the class PortalUrlServiceImpl_processHtmlTest method process_absolute.

@Test
public void process_absolute() {
    // 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().type(UrlTypeConstants.ABSOLUTE).portalRequest(this.portalRequest).value("<a href=\"content://" + content.getId() + "\">Content</a>");
    when(req.getServerName()).thenReturn("localhost");
    when(req.getScheme()).thenReturn("http");
    when(req.getServerPort()).thenReturn(80);
    // Checks that the page URL of the content is returned
    final String processedHtml = this.service.processHtml(params);
    assertEquals("<a href=\"http://localhost/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 4 with ProcessHtmlParams

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

the class PortalUrlServiceImpl_processHtmlTest method process_single_image.

@Test
public void process_single_image() {
    // 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() + "\">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/" + "width-768" + "/" + 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 5 with ProcessHtmlParams

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

the class PortalUrlServiceImpl_processHtmlTest method process_unknown_media.

@Test
public void process_unknown_media() {
    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=\"media://inline/123\">Media</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/404?message=Content+with+id+%5B123%5D+was+not+found+in+branch+%5Bdraft%5D\">Media</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)

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