From 3bfa83220f07bee082a68539cebb543a880ecd1c Mon Sep 17 00:00:00 2001 From: connorhaugh <49422820+connorhaugh@users.noreply.github.com> Date: Fri, 24 Mar 2023 10:05:21 -0400 Subject: [PATCH] feat: error on funky multiple choice tags. (#288) This change increases the frequency at which odd tags (like shuffle) on multiple choice problems don't go to the visual editor. Instead, they will now divert to the advanced editor. There might be other places where we need to follow a similar pattern, but we don't know those yet. --- .../containers/ProblemEditor/data/OLXParser.js | 4 ++++ .../ProblemEditor/data/OLXParser.test.js | 18 ++++++++++++++++++ .../ProblemEditor/data/mockData/olxTestData.js | 14 ++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/src/editors/containers/ProblemEditor/data/OLXParser.js b/src/editors/containers/ProblemEditor/data/OLXParser.js index b5467b838..39b43ef71 100644 --- a/src/editors/containers/ProblemEditor/data/OLXParser.js +++ b/src/editors/containers/ProblemEditor/data/OLXParser.js @@ -72,6 +72,10 @@ export class OLXParser { const answers = []; let data = {}; const widget = _.get(this.problem, `${problemType}.${widgetName}`); + const permissableTags = ['choice', '@_type', 'compoundhint', 'option', '#text']; + if (_.keys(widget).some((tag) => !permissableTags.includes(tag))) { + throw new Error('Misc Tags, reverting to Advanced Editor'); + } const choice = _.get(widget, option); const isComplexAnswer = [ProblemTypeKeys.SINGLESELECT, ProblemTypeKeys.MULTISELECT].includes(problemType); if (_.isEmpty(choice)) { diff --git a/src/editors/containers/ProblemEditor/data/OLXParser.test.js b/src/editors/containers/ProblemEditor/data/OLXParser.test.js index 21468bd83..99b4580f8 100644 --- a/src/editors/containers/ProblemEditor/data/OLXParser.test.js +++ b/src/editors/containers/ProblemEditor/data/OLXParser.test.js @@ -15,6 +15,7 @@ import { blankProblemOLX, blankQuestionOLX, styledQuestionOLX, + shuffleProblemOLX, } from './mockData/olxTestData'; import { ProblemTypeKeys } from '../../../data/constants/problem'; @@ -110,6 +111,23 @@ describe('Check OLXParser for answer parsing', () => { const answer = olxparser.parseMultipleChoiceAnswers('choiceresponse', 'checkboxgroup', 'choice'); expect(answer).toEqual(checkboxesOLXWithFeedbackAndHintsOLX.data); }); + + test('Test checkbox answer', () => { + const olxparser = new OLXParser(checkboxesOLXWithFeedbackAndHintsOLX.rawOLX); + const answer = olxparser.parseMultipleChoiceAnswers('choiceresponse', 'checkboxgroup', 'choice'); + expect(answer).toEqual(checkboxesOLXWithFeedbackAndHintsOLX.data); + }); + + test('Test checkboxs with extraneous tags error out', () => { + const olxparser = new OLXParser(shuffleProblemOLX.rawOLX); + try { + olxparser.parseMultipleChoiceAnswers('choiceresponse', 'checkboxgroup', 'choice'); + } catch (e) { + expect(e).toBeInstanceOf(Error); + expect(e.message).toBe('Misc Tags, reverting to Advanced Editor'); + } + }); + test('Test dropdown answer', () => { const olxparser = new OLXParser(dropdownOLXWithFeedbackAndHintsOLX.rawOLX); const answer = olxparser.parseMultipleChoiceAnswers('optionresponse', 'optioninput', 'option'); diff --git a/src/editors/containers/ProblemEditor/data/mockData/olxTestData.js b/src/editors/containers/ProblemEditor/data/mockData/olxTestData.js index 3cbd61a92..89bde5b7f 100644 --- a/src/editors/containers/ProblemEditor/data/mockData/olxTestData.js +++ b/src/editors/containers/ProblemEditor/data/mockData/olxTestData.js @@ -607,3 +607,17 @@ export const styledQuestionOLX = { `, question: '

test

', }; + +export const shuffleProblemOLX = { + rawOLX: ` + + + + The iPad + Napster + The iPod + The vegetable peeler + + +`, +};