|
10 | 10 | import com.dotcms.contenttype.model.type.ContentType; |
11 | 11 | import com.dotcms.datagen.*; |
12 | 12 | import com.dotcms.mock.request.MockAttributeRequest; |
13 | | -import com.dotcms.mock.request.MockHttpRequestIntegrationTest; |
14 | | -import com.dotcms.mock.response.MockHttpResponse; |
15 | | -import com.dotcms.rendering.velocity.viewtools.content.util.ContentUtils; |
| 13 | +import com.dotcms.rendering.velocity.viewtools.VelocityRequestWrapper; |
16 | 14 | import com.dotcms.util.CollectionsUtils; |
17 | 15 | import com.dotcms.util.IntegrationTestInitService; |
18 | 16 | import com.dotcms.util.JsonUtil; |
19 | 17 | import com.dotmarketing.business.APILocator; |
20 | | -import com.dotmarketing.db.LocalTransaction; |
21 | 18 | import com.dotmarketing.exception.DotDataException; |
22 | 19 | import com.dotmarketing.exception.DotSecurityException; |
23 | 20 | import com.dotmarketing.portlets.contentlet.model.Contentlet; |
|
29 | 26 | import com.fasterxml.jackson.core.JsonProcessingException; |
30 | 27 | import com.liferay.util.StringPool; |
31 | 28 | import com.tngtech.java.junit.dataprovider.DataProvider; |
32 | | -import com.tngtech.java.junit.dataprovider.DataProviderRunner; |
33 | 29 | import com.tngtech.java.junit.dataprovider.UseDataProvider; |
34 | 30 | import io.vavr.control.Try; |
35 | 31 | import org.junit.Assert; |
36 | 32 | import org.junit.BeforeClass; |
37 | | -import org.junit.Ignore; |
38 | 33 | import org.junit.Test; |
39 | 34 | import org.junit.runner.RunWith; |
40 | 35 |
|
41 | 36 | import javax.enterprise.context.ApplicationScoped; |
42 | 37 | import javax.servlet.http.HttpServletRequest; |
43 | 38 | import javax.servlet.http.HttpServletResponse; |
| 39 | +import java.io.IOException; |
44 | 40 | import java.util.LinkedHashMap; |
45 | 41 | import java.util.List; |
46 | 42 | import java.util.Map; |
47 | 43 | import java.util.Optional; |
48 | 44 |
|
49 | | -import static com.dotcms.util.CollectionsUtils.list; |
50 | 45 | import static org.junit.Assert.*; |
51 | 46 | import static org.mockito.Mockito.mock; |
52 | 47 | import static org.mockito.Mockito.when; |
@@ -435,7 +430,7 @@ public void test_get_dependencies_with_bad_content_value() { |
435 | 430 | assertNotNull(contentletIdList); |
436 | 431 | assertTrue(contentletIdList.isEmpty()); |
437 | 432 | } |
438 | | - |
| 433 | + |
439 | 434 | @Test |
440 | 435 | public void test_get_refreshStoryBlockValueReferences_with_bad_content_value() { |
441 | 436 |
|
@@ -469,6 +464,143 @@ public void test_refreshStoryBlockValueReferences_with_json_value() { |
469 | 464 |
|
470 | 465 | } |
471 | 466 |
|
| 467 | + /** |
| 468 | + * Method to test: {@link StoryBlockAPI#refreshReferences(Contentlet)} |
| 469 | + * Given Scenario: This will create 2 block contents, adds a rich content to each block content and retrieve the json. |
| 470 | + * ExpectedResult: The new json will contain the rich text data map for each block content. |
| 471 | + */ |
| 472 | + @Test |
| 473 | + public void test_refresh_references_multiple_blocks() |
| 474 | + throws DotDataException, DotSecurityException, JsonProcessingException, IOException { |
| 475 | + |
| 476 | + ContentType storyBlockType = null; |
| 477 | + |
| 478 | + final HttpServletRequest oldThreadRequest = HttpServletRequestThreadLocal.INSTANCE.getRequest(); |
| 479 | + final HttpServletResponse oldThreadResponse = HttpServletResponseThreadLocal.INSTANCE.getResponse(); |
| 480 | + |
| 481 | + try { |
| 482 | + // 1) get the default language |
| 483 | + final Language defaultLanguage = APILocator.getLanguageAPI().getDefaultLanguage(); |
| 484 | + |
| 485 | + // 2) create 2 rich text contentlets with some initial values |
| 486 | + final ContentType contentTypeRichText = APILocator.getContentTypeAPI(APILocator.systemUser()).find("webPageContent"); |
| 487 | + final Contentlet richTextContentlet1 = new ContentletDataGen(contentTypeRichText) |
| 488 | + .languageId(defaultLanguage.getId()) |
| 489 | + .setProperty("title","Title1") |
| 490 | + .setProperty("body", TestDataUtils.BLOCK_EDITOR_DUMMY_CONTENT) |
| 491 | + .nextPersistedAndPublish(); |
| 492 | + final Contentlet richTextContentlet2 = new ContentletDataGen(contentTypeRichText) |
| 493 | + .languageId(defaultLanguage.getId()) |
| 494 | + .setProperty("title","Title2") |
| 495 | + .setProperty("body", TestDataUtils.BLOCK_EDITOR_DUMMY_CONTENT) |
| 496 | + .nextPersistedAndPublish(); |
| 497 | + |
| 498 | + // 3) create a StoryBlockField and a ContentType with it |
| 499 | + final Field storyBlockField = new FieldDataGen() |
| 500 | + .type(StoryBlockField.class) |
| 501 | + .name("StoryBlockTestField") |
| 502 | + .velocityVarName("storyBlockTestField") |
| 503 | + .next(); |
| 504 | + storyBlockType = new ContentTypeDataGen().field(storyBlockField).nextPersisted(); |
| 505 | + |
| 506 | + // 4) create first block content with first rich content |
| 507 | + final Contentlet firstBlockContentlet = new ContentletDataGen(storyBlockType) |
| 508 | + .languageId(defaultLanguage.getId()) |
| 509 | + .nextPersisted(); |
| 510 | + final Contentlet firstBlockCheckout = ContentletDataGen.checkout(firstBlockContentlet); |
| 511 | + setBlockEditorField(firstBlockCheckout, storyBlockField, richTextContentlet1); |
| 512 | + final Contentlet firstBlockComplete = APILocator.getContentletAPI().checkin( |
| 513 | + firstBlockCheckout, APILocator.systemUser(), false); |
| 514 | + ContentletDataGen.publish(firstBlockComplete); |
| 515 | + |
| 516 | + // 5) create second block content with second rich content |
| 517 | + final Contentlet secondBlockContentlet = new ContentletDataGen(storyBlockType) |
| 518 | + .languageId(defaultLanguage.getId()) |
| 519 | + .nextPersisted(); |
| 520 | + final Contentlet secondBlockCheckout = ContentletDataGen.checkout(secondBlockContentlet); |
| 521 | + setBlockEditorField(secondBlockCheckout, storyBlockField, richTextContentlet2); |
| 522 | + final Contentlet secondBlockComplete = APILocator.getContentletAPI().checkin( |
| 523 | + secondBlockCheckout, APILocator.systemUser(), false); |
| 524 | + ContentletDataGen.publish(secondBlockComplete); |
| 525 | + |
| 526 | + // 6) now we have 2 block contents, each one with a rich content, we are going to refresh the references |
| 527 | + final HttpServletRequest attrRequest = new MockAttributeRequest(mock(HttpServletRequest.class)); |
| 528 | + attrRequest.setAttribute("USER", APILocator.systemUser()); |
| 529 | + attrRequest.setAttribute(WebKeys.PAGE_MODE_PARAMETER, PageMode.LIVE); |
| 530 | + attrRequest.setAttribute(WebKeys.HTMLPAGE_LANGUAGE, "1"); |
| 531 | + |
| 532 | + final HttpServletRequest request = VelocityRequestWrapper.wrapVelocityRequest(attrRequest); |
| 533 | + HttpServletRequestThreadLocal.INSTANCE.setRequest(request); |
| 534 | + |
| 535 | + final HttpServletResponse response = mock(HttpServletResponse.class); |
| 536 | + HttpServletResponseThreadLocal.INSTANCE.setResponse(response); |
| 537 | + |
| 538 | + // 7) verify first rich content |
| 539 | + final Contentlet firstBlockPublished = APILocator.getContentletAPI().find( |
| 540 | + firstBlockComplete.getInode(), APILocator.systemUser(), false); |
| 541 | + assertNotNull(firstBlockPublished); |
| 542 | + |
| 543 | + final Map<?, ?> firstDataMap = getDataMap(firstBlockPublished, storyBlockField.variable()); |
| 544 | + assertNotNull(firstDataMap); |
| 545 | + |
| 546 | + assertEquals(richTextContentlet1.getIdentifier(), firstDataMap.get("identifier")); |
| 547 | + assertEquals(richTextContentlet1.getStringProperty("title"), firstDataMap.get("title")); |
| 548 | + |
| 549 | + // 8) verify second rich content |
| 550 | + final Contentlet secondBlockPublished = APILocator.getContentletAPI().find( |
| 551 | + secondBlockComplete.getInode(), APILocator.systemUser(), false); |
| 552 | + assertNotNull(secondBlockPublished); |
| 553 | + |
| 554 | + final Map<?, ?> secondDataMap = getDataMap(secondBlockPublished, storyBlockField.variable()); |
| 555 | + assertNotNull(secondDataMap); |
| 556 | + |
| 557 | + assertEquals(richTextContentlet2.getIdentifier(), secondDataMap.get("identifier")); |
| 558 | + assertEquals(richTextContentlet2.getStringProperty("title"), secondDataMap.get("title")); |
| 559 | + |
| 560 | + } finally { |
| 561 | + HttpServletRequestThreadLocal.INSTANCE.setRequest(oldThreadRequest); |
| 562 | + HttpServletResponseThreadLocal.INSTANCE.setResponse(oldThreadResponse); |
| 563 | + if (storyBlockType != null) { |
| 564 | + ContentTypeDataGen.remove(storyBlockType); |
| 565 | + } |
| 566 | + } |
| 567 | + } |
| 568 | + |
| 569 | + /** |
| 570 | + * Helper method to get the data map from the first Contentlet that is referenced in a StoryBlockField. |
| 571 | + * @param storyBlockContentlet The Contentlet that contains the StoryBlockField. |
| 572 | + * @param storyBlockField The StoryBlockField variable name. |
| 573 | + * @return A map containing the data from the referenced Contentlet, or null if not found. |
| 574 | + */ |
| 575 | + private Map<?, ?> getDataMap(final Contentlet storyBlockContentlet, final String storyBlockField) |
| 576 | + throws JsonProcessingException { |
| 577 | + |
| 578 | + if (storyBlockContentlet == null || storyBlockField == null) return null; |
| 579 | + |
| 580 | + final Object storyBlockValue = storyBlockContentlet.getStringProperty(storyBlockField); |
| 581 | + if (storyBlockValue == null) return null; |
| 582 | + |
| 583 | + final Map<String, Object> blockEditorMap = |
| 584 | + APILocator.getStoryBlockAPI().toMap(storyBlockValue); |
| 585 | + if (blockEditorMap == null || blockEditorMap.isEmpty()) return null; |
| 586 | + |
| 587 | + final List<?> contentsMap = (List<?>) blockEditorMap.get(StoryBlockAPI.CONTENT_KEY); |
| 588 | + if (contentsMap == null || contentsMap.isEmpty()) return null; |
| 589 | + |
| 590 | + final Optional<?> firstContentletMap = contentsMap.stream() |
| 591 | + .filter(contentMap -> "dotContent".equals( |
| 592 | + ((Map<?,?>)contentMap).get("type"))) |
| 593 | + .findFirst(); |
| 594 | + if (firstContentletMap.isEmpty()) return null; |
| 595 | + |
| 596 | + final Map<?, ?> contentletMap = (Map<?, ?>) firstContentletMap.get(); |
| 597 | + final Map<?, ?> attrsMap = (Map<?, ?>) contentletMap.get(StoryBlockAPI.ATTRS_KEY); |
| 598 | + if (attrsMap == null) return null; |
| 599 | + |
| 600 | + return (Map<?, ?>) attrsMap.get(StoryBlockAPI.DATA_KEY); |
| 601 | + |
| 602 | + } |
| 603 | + |
472 | 604 | /** |
473 | 605 | * Method to test: {@link StoryBlockAPIImpl#refreshReferences(Contentlet)} |
474 | 606 | * When: |
|
0 commit comments