Browse Source

fix(question-sheet): prevent country dataset injection on political stance question

master
mortezaei 3 weeks ago
parent
commit
3df66ef591
  1. 96
      src/components/Componentes/question-sheet.test.tsx
  2. 32
      src/components/Componentes/question-sheet.tsx

96
src/components/Componentes/question-sheet.test.tsx

@ -1183,7 +1183,103 @@ describe("QuestionSheet component", () => {
expect(longBtn.style.minHeight).toBe(""); expect(longBtn.style.minHeight).toBe("");
}); });
}); });
it("renders exact 6 political stance options and NOT countries for real DB question beliefs_lifestyle.political_stance_towards_governance (FA & EN)", async () => {
const { useI18n } = await import("@/translations/provider");
(useI18n as any).mockReturnValue({
locale: "fa",
dictionary: { Confirm: "تایید" },
});
const politicalQuestionFa: QuestionField = {
id: "beliefs_lifestyle.political_stance_towards_governance",
title: "موضع کلی شما نسبت به حکومت مستقر در کشور محل زندگی‌تان چیست؟",
type: "dropdown",
order: 10,
required: false,
baseRequired: false,
isVisible: true,
private: true,
description: "",
tooltip: "این سؤال محرمانه است و فقط برای سنجش سازگاری ارزشی استفاده می‌شود.",
ui_config: {
private: true,
noSearch: false,
placeholder_en: "Select one option",
placeholder_fa: "یک گزینه را انتخاب کنید",
},
extras: {
placeHolder: "یک گزینه را انتخاب کنید",
range: [0, 0],
options: [],
noSearch: false,
},
options: [
{
id: "beliefs_lifestyle.political_stance_towards_governance.strongly_supportive",
value: "strongly_supportive",
label: "قویاً موافق و حامی هستم",
order: 1,
},
{
id: "beliefs_lifestyle.political_stance_towards_governance.generally_supportive_with_critiques",
value: "generally_supportive_with_critiques",
label: "در مجموع موافقم، اما نقدهای مشخصی دارم",
order: 2,
},
{
id: "beliefs_lifestyle.political_stance_towards_governance.mixed_or_moderate_view",
value: "mixed_or_moderate_view",
label: "دیدگاه ترکیبی یا میانه دارم",
order: 3,
},
{
id: "beliefs_lifestyle.political_stance_towards_governance.generally_critical",
value: "generally_critical",
label: "در مجموع منتقد هستم",
order: 4,
},
{
id: "beliefs_lifestyle.political_stance_towards_governance.strongly_opposed",
value: "strongly_opposed",
label: "قویاً مخالف هستم",
order: 5,
},
{
id: "beliefs_lifestyle.political_stance_towards_governance.no_political_interest_or_stance",
value: "no_political_interest_or_stance",
label: "علاقه یا موضع سیاسی مشخصی ندارم",
order: 6,
},
],
};
render(
<QueryClientProvider client={queryClient}>
<QuestionAnswersProvider slug="beliefs_lifestyle" questions={[politicalQuestionFa]}>
<QuestionSheet question={politicalQuestionFa} />
</QuestionAnswersProvider>
</QueryClientProvider>,
);
fireEvent.click(screen.getByRole("button", { name: "یک گزینه را انتخاب کنید" }));
// Should render all 6 political stance options
expect(screen.getByRole("button", { name: "قویاً موافق و حامی هستم" })).toBeDefined();
expect(screen.getByRole("button", { name: "در مجموع موافقم، اما نقدهای مشخصی دارم" })).toBeDefined();
expect(screen.getByRole("button", { name: "دیدگاه ترکیبی یا میانه دارم" })).toBeDefined();
expect(screen.getByRole("button", { name: "در مجموع منتقد هستم" })).toBeDefined();
expect(screen.getByRole("button", { name: "قویاً مخالف هستم" })).toBeDefined();
expect(screen.getByRole("button", { name: "علاقه یا موضع سیاسی مشخصی ندارم" })).toBeDefined();
// Should NOT render countries or search bar (compact 6-option sheet)
expect(screen.queryByRole("button", { name: "افغانستان" })).toBeNull();
expect(screen.queryByRole("button", { name: "ایران" })).toBeNull();
expect(screen.queryByRole("button", { name: "Afghanistan" })).toBeNull();
expect(screen.queryByPlaceholderText("جستجو...")).toBeNull();
});
}); });

32
src/components/Componentes/question-sheet.tsx

@ -211,7 +211,13 @@ export function QuestionSheet({ question, disabled }: QuestionSheetProps) {
question.id?.toLowerCase().includes("status") || question.id?.toLowerCase().includes("status") ||
question.id?.toLowerCase().includes("responsibility") || question.id?.toLowerCase().includes("responsibility") ||
question.id?.toLowerCase().includes("difference") || question.id?.toLowerCase().includes("difference") ||
question.id?.toLowerCase().includes("impact");
question.id?.toLowerCase().includes("impact") ||
question.id?.toLowerCase().includes("political") ||
question.id?.toLowerCase().includes("governance") ||
question.id?.toLowerCase().includes("stance") ||
question.id?.toLowerCase().includes("government");
const hasStructuredBackendId = Boolean(question.id?.includes("."));
const isLanguageQuestion = Boolean( const isLanguageQuestion = Boolean(
question.ui_config?.dataset === "languages" || question.ui_config?.dataset === "languages" ||
@ -220,8 +226,8 @@ export function QuestionSheet({ question, disabled }: QuestionSheetProps) {
question.id?.endsWith(".other_languages") || question.id?.endsWith(".other_languages") ||
question.id?.endsWith(".languages") || question.id?.endsWith(".languages") ||
question.id?.endsWith(".language") || question.id?.endsWith(".language") ||
(!question.id?.includes("impact") &&
!question.id?.includes("difference") &&
(!hasStructuredBackendId &&
!isExcludedFromAutoDatasets &&
Boolean( Boolean(
question.title?.toLowerCase().includes("mother tongue") || question.title?.toLowerCase().includes("mother tongue") ||
question.title?.toLowerCase().includes("native language") || question.title?.toLowerCase().includes("native language") ||
@ -239,17 +245,21 @@ export function QuestionSheet({ question, disabled }: QuestionSheetProps) {
question.ui_config?.dataset === "nationalities" || question.ui_config?.dataset === "nationalities" ||
question.id?.includes("nationality") || question.id?.includes("nationality") ||
question.id?.includes("citizenship") || question.id?.includes("citizenship") ||
question.id?.includes("country_of_") ||
question.id?.endsWith(".birthplace") || question.id?.endsWith(".birthplace") ||
question.id?.endsWith(".current_residence") || question.id?.endsWith(".current_residence") ||
question.id?.endsWith(".country") || question.id?.endsWith(".country") ||
Boolean(
question.title?.toLowerCase().includes("nationality") ||
question.title?.toLowerCase().includes("citizenship") ||
question.title?.toLowerCase().includes("country") ||
question.title?.includes("تابعیت") ||
question.title?.includes("ملیت") ||
question.title?.includes("کشور")
))
(!hasStructuredBackendId &&
!question.title?.includes("؟") &&
!question.title?.includes("?") &&
Boolean(
question.title?.toLowerCase().includes("nationality") ||
question.title?.toLowerCase().includes("citizenship") ||
question.title?.toLowerCase().includes("country") ||
question.title?.includes("تابعیت") ||
question.title?.includes("ملیت") ||
question.title?.includes("کشور")
)))
); );
const options = useMemo(() => { const options = useMemo(() => {

Loading…
Cancel
Save