feat(pages): card sections, CTA band, section looks and optional hero image in services-counter
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
1 parent
2ff117e41d
commit
cc962ebfcf
6 files changed
+234
-5
No files matched your search
+5
-1
@@ -11,7 +11,8 @@ import java.util.List;
|
||||
/**
|
||||
* One landing-page block. Which fields apply depends on {@code type}:
|
||||
* HERO (imageId, heading, text, button), RICH_TEXT (html), IMAGE_TEXT (imageId, imagePosition, heading, html, button),
|
||||
* FAQ (heading, items), FORM (heading, eventUuid).
|
||||
* FAQ (heading, items), FORM (heading, eventUuid), HTML (html), FEATURES (heading, text, cards), STEPS (heading, cards),
|
||||
* TRUST (cards), CTA (heading, text, button). Any block may carry anchor and look.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@@ -27,7 +28,10 @@ public class CmsBlock {
|
||||
private String buttonUrl;
|
||||
/** Optional link name: the block's id on the public page, the target of a #name link (add-page-anchor-links). */
|
||||
private String anchor;
|
||||
/** DEFAULT (or absent), MUTED, DARK or ACCENT (add-page-sections). */
|
||||
private String look;
|
||||
private List<CmsFaqItem> items;
|
||||
private List<CmsCard> cards;
|
||||
/** A Snowflake value beyond JavaScript number precision: written as a string, read from either. */
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long eventUuid;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.solvway.services.counter.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
|
||||
/** One card of a FEATURES (icon, title, text), STEPS (title, text) or TRUST (title) block (add-page-sections). */
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public record CmsCard(String icon, String title, String text) {}
|
||||
+68
-1
@@ -1,6 +1,7 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.dto.CmsBlock;
|
||||
import com.solvway.services.counter.dto.CmsCard;
|
||||
import com.solvway.services.counter.dto.CmsFaqItem;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
@@ -16,12 +17,17 @@ import java.util.regex.Pattern;
|
||||
* Co-Lite changes: images are uploaded ids of the page's company, FORM points at an event of that company,
|
||||
* buttons may also use mailto: and tel:, and rich-text HTML holding scripts is refused (the public page sanitises as well).
|
||||
* The HTML block (custom HTML) is exempt from the script check: it is served only inside a sandboxed frame.
|
||||
* add-page-sections adds FEATURES, STEPS, TRUST and CTA, a look on any block, and makes the HERO image optional.
|
||||
* Throws 422 on the first violation, with the reason "path|MessageCode".
|
||||
*/
|
||||
public final class CmsBlockValidator {
|
||||
private static final int MAX_BLOCKS = 30;
|
||||
/** A block link name (add-page-anchor-links); a same-page link is "#" plus one. "reg-" is the sign-up form's own ids. */
|
||||
static final Pattern ANCHOR = Pattern.compile("(?!reg-)[a-z0-9][a-z0-9-]{0,39}");
|
||||
/** Section looks and feature icons (add-page-sections). */
|
||||
private static final Set<String> LOOKS = Set.of("DEFAULT", "MUTED", "DARK", "ACCENT");
|
||||
static final Set<String> ICONS = Set.of("wifi", "phone", "tv", "monitor", "lock", "shield-check", "cloud", "wrench", "headset", "users", "calendar", "star");
|
||||
private static final Set<String> CARD_TYPES = Set.of("FEATURES", "STEPS", "TRUST");
|
||||
private static final Pattern SCRIPT = Pattern.compile("(?i)<\\s*script|\\son[a-z]+\\s*=|javascript\\s*:");
|
||||
|
||||
private CmsBlockValidator() {
|
||||
@@ -50,9 +56,17 @@ public final class CmsBlockValidator {
|
||||
throw refuse(path + ".anchor", "PagesAnchorTaken");
|
||||
}
|
||||
}
|
||||
if (b.getLook() != null && !LOOKS.contains(b.getLook())) {
|
||||
throw refuse(path + ".look", "PagesBadLook");
|
||||
}
|
||||
if (b.getCards() != null && !CARD_TYPES.contains(b.getType())) {
|
||||
throw refuse(path + ".cards", "PagesUnusedField");
|
||||
}
|
||||
switch (b.getType()) {
|
||||
case "HERO" -> {
|
||||
image(b, path, ownImage);
|
||||
if (b.getImageId() != null) {
|
||||
image(b, path, ownImage);
|
||||
}
|
||||
required(b.getHeading(), path + ".heading");
|
||||
max(b.getHeading(), path + ".heading", 120);
|
||||
max(b.getText(), path + ".text", 300);
|
||||
@@ -80,6 +94,30 @@ public final class CmsBlockValidator {
|
||||
throw refuse(path + ".eventUuid", "PagesBadEvent");
|
||||
}
|
||||
}
|
||||
case "FEATURES" -> {
|
||||
required(b.getHeading(), path + ".heading");
|
||||
max(b.getHeading(), path + ".heading", 120);
|
||||
max(b.getText(), path + ".text", 300);
|
||||
cards(b, path, true, true);
|
||||
}
|
||||
case "STEPS" -> {
|
||||
required(b.getHeading(), path + ".heading");
|
||||
max(b.getHeading(), path + ".heading", 120);
|
||||
cards(b, path, false, true);
|
||||
}
|
||||
case "TRUST" -> {
|
||||
if (b.getHeading() != null) {
|
||||
throw refuse(path + ".heading", "PagesUnusedField");
|
||||
}
|
||||
cards(b, path, false, false);
|
||||
}
|
||||
case "CTA" -> {
|
||||
required(b.getHeading(), path + ".heading");
|
||||
max(b.getHeading(), path + ".heading", 120);
|
||||
max(b.getText(), path + ".text", 300);
|
||||
required(b.getButtonLabel(), path + ".buttonLabel");
|
||||
button(b, path);
|
||||
}
|
||||
default -> throw refuse(path + ".type", "PagesUnknownType");
|
||||
}
|
||||
}
|
||||
@@ -131,6 +169,35 @@ public final class CmsBlockValidator {
|
||||
}
|
||||
}
|
||||
|
||||
/** 2 to 6 cards; an icon from the list where the type uses one, a title (80), a text (300) where used, nothing else. */
|
||||
private static void cards(CmsBlock b, String path, boolean icon, boolean text) {
|
||||
List<CmsCard> cards = b.getCards();
|
||||
if (cards == null || cards.size() < 2 || cards.size() > 6) {
|
||||
throw refuse(path + ".cards", "PagesCardCount");
|
||||
}
|
||||
for (int i = 0; i < cards.size(); i++) {
|
||||
CmsCard card = cards.get(i);
|
||||
String cardPath = path + ".cards[" + i + "]";
|
||||
if (card == null) {
|
||||
throw refuse(cardPath + ".title", "PagesRequired");
|
||||
}
|
||||
if (icon && (card.icon() == null || !ICONS.contains(card.icon()))) {
|
||||
throw refuse(cardPath + ".icon", "PagesBadIcon");
|
||||
}
|
||||
if (!icon && card.icon() != null) {
|
||||
throw refuse(cardPath + ".icon", "PagesUnusedField");
|
||||
}
|
||||
required(card.title(), cardPath + ".title");
|
||||
max(card.title(), cardPath + ".title", 80);
|
||||
if (text) {
|
||||
required(card.text(), cardPath + ".text");
|
||||
max(card.text(), cardPath + ".text", 300);
|
||||
} else if (card.text() != null) {
|
||||
throw refuse(cardPath + ".text", "PagesUnusedField");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void button(CmsBlock b, String path) {
|
||||
boolean hasLabel = b.getButtonLabel() != null && !b.getButtonLabel().isBlank();
|
||||
boolean hasUrl = b.getButtonUrl() != null && !b.getButtonUrl().isBlank();
|
||||
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.dto.CmsBlock;
|
||||
import com.solvway.services.counter.dto.CmsCard;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
/** add-page-sections: card sections (FEATURES, STEPS, TRUST), the CTA band, section looks and the optional hero image. */
|
||||
class CmsBlockValidatorSectionsTest {
|
||||
|
||||
private static void validate(List<CmsBlock> blocks) {
|
||||
CmsBlockValidator.validate(blocks, id -> id == 5L, uuid -> uuid == 900L);
|
||||
}
|
||||
|
||||
private static void ok(CmsBlock... blocks) {
|
||||
assertThatCode(() -> validate(List.of(blocks))).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
private static void refused(CmsBlock b, String path, String code) {
|
||||
assertThatThrownBy(() -> validate(List.of(b)))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class, e -> assertThat(e.getReason()).isEqualTo(path + "|" + code));
|
||||
}
|
||||
|
||||
private static CmsBlock block(String type, String heading, CmsCard... cards) {
|
||||
CmsBlock b = new CmsBlock();
|
||||
b.setType(type);
|
||||
b.setHeading(heading);
|
||||
if (cards.length > 0) b.setCards(new ArrayList<>(Arrays.asList(cards)));
|
||||
return b;
|
||||
}
|
||||
|
||||
private static CmsCard feature(String icon) {
|
||||
return new CmsCard(icon, "Managed WiFi", "Guests connect at once.");
|
||||
}
|
||||
|
||||
private static CmsCard step(String title) {
|
||||
return new CmsCard(null, title, "We visit your property.");
|
||||
}
|
||||
|
||||
private static CmsCard trust(String title) {
|
||||
return new CmsCard(null, title, null);
|
||||
}
|
||||
|
||||
private static CmsBlock cta() {
|
||||
CmsBlock b = block("CTA", "Ready to upgrade?");
|
||||
b.setText("Talk to our team today.");
|
||||
b.setButtonLabel("Get a quote");
|
||||
b.setButtonUrl("#signup");
|
||||
return b;
|
||||
}
|
||||
|
||||
@Test
|
||||
void validSectionsOfEveryNewTypePass() {
|
||||
CmsBlock features = block("FEATURES", "What we do", feature("wifi"), feature("tv"), feature("lock"));
|
||||
features.setText("One partner for every system.");
|
||||
ok(features,
|
||||
block("STEPS", "How it works", step("Survey"), step("Install")),
|
||||
block("TRUST", null, trust("Certified with LG"), trust("Local support")),
|
||||
cta());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cardSectionsHaveTwoToSixCards() {
|
||||
refused(block("STEPS", "How", step("One")), "blocks[0].cards", "PagesCardCount");
|
||||
refused(block("TRUST", null), "blocks[0].cards", "PagesCardCount");
|
||||
CmsCard[] seven = new CmsCard[7];
|
||||
for (int i = 0; i < 7; i++) seven[i] = feature("wifi");
|
||||
refused(block("FEATURES", "Too many", seven), "blocks[0].cards", "PagesCardCount");
|
||||
CmsCard[] six = new CmsCard[6];
|
||||
for (int i = 0; i < 6; i++) six[i] = feature("star");
|
||||
ok(block("FEATURES", "Six", six));
|
||||
}
|
||||
|
||||
@Test
|
||||
void featureCardsNeedAKnownIconATitleAndAText() {
|
||||
refused(block("FEATURES", "X", feature("rocket-launcher"), feature("wifi")), "blocks[0].cards[0].icon", "PagesBadIcon");
|
||||
refused(block("FEATURES", "X", feature("wifi"), feature(null)), "blocks[0].cards[1].icon", "PagesBadIcon");
|
||||
refused(block("FEATURES", "X", feature("wifi"), new CmsCard("tv", " ", "t")), "blocks[0].cards[1].title", "PagesRequired");
|
||||
refused(block("FEATURES", "X", feature("wifi"), new CmsCard("tv", "T", "")), "blocks[0].cards[1].text", "PagesRequired");
|
||||
refused(block("FEATURES", null, feature("wifi"), feature("tv")), "blocks[0].heading", "PagesRequired");
|
||||
for (String icon : List.of("wifi", "phone", "tv", "monitor", "lock", "shield-check", "cloud", "wrench", "headset", "users", "calendar", "star")) {
|
||||
ok(block("FEATURES", "Icons", feature(icon), feature(icon)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void cardLimitsAndFieldsATypeDoesNotUse() {
|
||||
refused(block("STEPS", "X", step("a".repeat(81)), step("b")), "blocks[0].cards[0].title", "PagesTooLong");
|
||||
refused(block("STEPS", "X", step("a"), new CmsCard(null, "b", "c".repeat(301))), "blocks[0].cards[1].text", "PagesTooLong");
|
||||
refused(block("STEPS", "X", step("a"), new CmsCard("wifi", "b", "c")), "blocks[0].cards[1].icon", "PagesUnusedField");
|
||||
refused(block("TRUST", null, trust("a"), new CmsCard(null, "b", "c")), "blocks[0].cards[1].text", "PagesUnusedField");
|
||||
refused(block("TRUST", "Heading", trust("a"), trust("b")), "blocks[0].heading", "PagesUnusedField");
|
||||
refused(block("STEPS", null, step("a"), step("b")), "blocks[0].heading", "PagesRequired");
|
||||
refused(block("STEPS", "X", step("a"), null), "blocks[0].cards[1].title", "PagesRequired");
|
||||
CmsBlock faqWithCards = block("FAQ", "Q", trust("a"), trust("b"));
|
||||
refused(faqWithCards, "blocks[0].cards", "PagesUnusedField");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCtaNeedsAHeadingAndAButton() {
|
||||
CmsBlock noButton = cta();
|
||||
noButton.setButtonLabel(null);
|
||||
noButton.setButtonUrl(null);
|
||||
refused(noButton, "blocks[0].buttonLabel", "PagesRequired");
|
||||
CmsBlock badLink = cta();
|
||||
badLink.setButtonUrl("javascript:alert(1)");
|
||||
refused(badLink, "blocks[0].buttonUrl", "PagesBadLink");
|
||||
CmsBlock noHeading = cta();
|
||||
noHeading.setHeading(" ");
|
||||
refused(noHeading, "blocks[0].heading", "PagesRequired");
|
||||
CmsBlock longText = cta();
|
||||
longText.setText("x".repeat(301));
|
||||
refused(longText, "blocks[0].text", "PagesTooLong");
|
||||
}
|
||||
|
||||
@Test
|
||||
void anyBlockMayCarryAKnownLook() {
|
||||
for (String look : List.of("DEFAULT", "MUTED", "DARK", "ACCENT")) {
|
||||
CmsBlock b = cta();
|
||||
b.setLook(look);
|
||||
ok(b);
|
||||
}
|
||||
CmsBlock neon = cta();
|
||||
neon.setLook("NEON");
|
||||
refused(neon, "blocks[0].look", "PagesBadLook");
|
||||
CmsBlock html = new CmsBlock();
|
||||
html.setType("HTML");
|
||||
html.setHtml("<p>x</p>");
|
||||
html.setLook("dark");
|
||||
refused(html, "blocks[0].look", "PagesBadLook");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aHeroImageIsOptionalButMustBeTheCompanysWhenGiven() {
|
||||
CmsBlock hero = block("HERO", "Hotel technology");
|
||||
ok(hero);
|
||||
hero.setImageId(6L);
|
||||
refused(hero, "blocks[0].imageId", "PagesBadImage");
|
||||
}
|
||||
}
|
||||
+5
-2
@@ -145,8 +145,11 @@ class CmsBlockValidatorTest {
|
||||
CmsBlock foreignImage = hero();
|
||||
foreignImage.setImageId(6L);
|
||||
refused(List.of(foreignImage), "blocks[0].imageId", "PagesBadImage");
|
||||
CmsBlock noImage = hero();
|
||||
noImage.setImageId(null);
|
||||
// add-page-sections: a hero may have no image (CmsBlockValidatorSectionsTest); an image-with-text block still needs one.
|
||||
CmsBlock noImage = new CmsBlock();
|
||||
noImage.setType("IMAGE_TEXT");
|
||||
noImage.setImagePosition("LEFT");
|
||||
noImage.setHtml("<p>x</p>");
|
||||
refused(List.of(noImage), "blocks[0].imageId", "PagesBadImage");
|
||||
refused(List.of(form(901)), "blocks[0].eventUuid", "PagesBadEvent");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user