@Test public void testCreateContentWithBinary() { fixture.createAndStoreNormalUserWithUserGroup("testuser", "Test user", "testuserstore"); fixture.save( factory.createContentHandler( "Custom content", ContentHandlerName.CUSTOM.getHandlerClassShortName())); fixture.save( factory.createContentType( "MyContentType", ContentHandlerName.CUSTOM.getHandlerClassShortName(), standardConfig)); fixture.save(factory.createUnit("MyUnit", "en")); fixture.save( factory.createCategory("MyCategory", "MyContentType", "MyUnit", "testuser", "testuser")); fixture.save(factory.createCategoryAccessForUser("MyCategory", "testuser", "read,create")); fixture.flushAndClearHibernateSesssion(); UserEntity runningUser = fixture.findUserByName("testuser"); PortalSecurityHolder.setImpersonatedUser(runningUser.getKey()); ContentDataInput contentData = new ContentDataInput("MyContentType"); contentData.add(new TextInput("myTitle", "testtitle")); contentData.add(new BinaryInput("myBinaryfile", dummyBinary, "dummyBinary")); CreateContentParams params = new CreateContentParams(); params.categoryKey = fixture.findCategoryByName("MyCategory").getKey().toInt(); params.contentData = contentData; params.publishFrom = new Date(); params.publishTo = null; params.status = ContentStatus.STATUS_DRAFT; int contentKey = internalClient.createContent(params); fixture.flushAndClearHibernateSesssion(); ContentEntity persistedContent = fixture.findContentByKey(new ContentKey(contentKey)); assertNotNull(persistedContent); assertEquals("MyCategory", persistedContent.getCategory().getName()); ContentVersionEntity persistedVersion = persistedContent.getMainVersion(); assertNotNull(persistedVersion); assertEquals("testtitle", persistedVersion.getTitle()); assertEquals( com.enonic.cms.core.content.ContentStatus.DRAFT.getKey(), persistedVersion.getStatus().getKey()); // verify binary was saved Set<ContentBinaryDataEntity> contentBinaryDatas = persistedVersion.getContentBinaryData(); assertEquals(1, contentBinaryDatas.size()); ContentBinaryDataEntity contentBinaryData = contentBinaryDatas.iterator().next(); BinaryDataEntity binaryData = contentBinaryData.getBinaryData(); assertEquals("dummyBinary", binaryData.getName()); CustomContentData customContentData = (CustomContentData) persistedVersion.getContentData(); assertNotNull(customContentData); }
@Before public void before() { factory = fixture.getFactory(); fixture.initSystemData(); fixture.createAndStoreUserAndUserGroup( "testuser", "testuser fullname", UserType.NORMAL, "testuserstore"); ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(httpServletRequest); RequestContextHolder.setRequestAttributes(servletRequestAttributes); httpServletRequest.setCharacterEncoding("UTF-8"); ServletRequestAccessor.setRequest(httpServletRequest); loginUserInAdmin(fixture.findUserByName("testuser").getKey()); loginUserInPortal(fixture.findUserByName("testuser").getKey()); imageController.setGroupDao(groupDao); imageController.setContentDao(contentDao); imageController.setSecurityService(securityService); imageController.setImageService(imageService); imageController.setDisableParamEncoding(true); previewService = Mockito.mock(PreviewService.class); Mockito.when(previewService.isInPreview()).thenReturn(false); Mockito.when(previewService.getPreviewContext()).thenReturn(PreviewContext.NO_PREVIEW); site1 = factory.createSite("MySite", new Date(), null, "en"); fixture.save(site1); MenuItemEntity firstPage = createPage("Firstpage", null, "MySite"); fixture.save(firstPage); site1.setFirstPage(firstPage); fixture.flushAndClearHibernateSesssion(); fixture.save( factory.createContentHandler( "Image content", ContentHandlerName.IMAGE.getHandlerClassShortName())); fixture.save( factory.createContentType( "ImageContentType", ContentHandlerName.IMAGE.getHandlerClassShortName())); fixture.save(factory.createUnit("ImageUnit")); fixture.save( factory.createCategory( "ImageCategory", "ImageContentType", "ImageUnit", "testuser", "testuser")); fixture.save( factory.createCategoryAccessForUser("ImageCategory", "testuser", "read, create, approve")); fixture.flushAndClearHibernateSesssion(); }
@Test public void request_user_image() throws Exception { byte[] bytes = loadImage("Arn.JPG"); UserEntity testUser = fixture.findUserByName("testuser"); testUser.setPhoto(bytes); fixture.flushAndClearHibernateSesssion(); String imageRequestPath = "_image/user/" + testUser.getKey() + ".jpg"; setPathInfoAndRequestURI(httpServletRequest, imageRequestPath); httpServletRequest.setParameter("_background", "0xffffff"); httpServletRequest.setParameter("_quality", "100"); imageController.handleRequestInternal(httpServletRequest, httpServletResponse); assertEquals(HttpServletResponse.SC_OK, httpServletResponse.getStatus()); assertEquals("image/jpg", httpServletResponse.getContentType()); }
@Test public void response_ok_for_request_to_content_image_that_binary_is_not_on_main_version() throws Exception { // setup: content byte[] bytes = loadImage("Arn.JPG"); ContentKey contentKey = createImageContent( "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 10, 0, 0, 0), null); // setup: draft version of content ContentEntity content = fixture.findContentByKey(contentKey); ContentVersionEntity draftVersion = createDraftVersion("Arn.JPG"); draftVersion.setContentDataXml(content.getMainVersion().getContentDataAsXmlString()); content.setDraftVersion(draftVersion); content.addVersion(draftVersion); fixture.save(draftVersion); BinaryDataEntity binaryDataForDraftVersion = factory.createBinaryData("Arn.JPG", bytes.length); binaryDataForDraftVersion.setBlobKey( content.getMainVersion().getBinaryData("source").getBlobKey()); fixture.save(binaryDataForDraftVersion); ContentBinaryDataEntity contentBinaryDataForDraftVersion = factory.createContentBinaryData("source", binaryDataForDraftVersion, draftVersion); draftVersion.addContentBinaryData(contentBinaryDataForDraftVersion); fixture.save(contentBinaryDataForDraftVersion); fixture.flushAndClearHibernateSesssion(); // exercise & verify String imageRequestPath = "_image/" + contentKey + "/label/source.jpg"; setPathInfoAndRequestURI(httpServletRequest, imageRequestPath); httpServletRequest.setParameter("_version", draftVersion.getKey().toString()); httpServletRequest.setParameter("_background", "0xffffff"); httpServletRequest.setParameter("_quality", "100"); imageController.handleRequestInternal(httpServletRequest, httpServletResponse); assertEquals(HttpServletResponse.SC_OK, httpServletResponse.getStatus()); assertTrue("Content Length", httpServletResponse.getContentLength() > 0); assertEquals("image/jpg", httpServletResponse.getContentType()); }