diff --git a/contentcuration/contentcuration/frontend/shared/views/QTIEditor/README.md b/contentcuration/contentcuration/frontend/shared/views/QTIEditor/README.md index 06f8615e01..db298be9d8 100644 --- a/contentcuration/contentcuration/frontend/shared/views/QTIEditor/README.md +++ b/contentcuration/contentcuration/frontend/shared/views/QTIEditor/README.md @@ -39,7 +39,9 @@ Prompts, choices, items and hints are edited in `TipTapEditor` with `format="htm > [!IMPORTANT] > In `parse`, read any field TipTap loads as HTML with `getContentHTML` (or `getPromptHTML`) from `serialization/xml.js`, never `innerHTML` or `XMLSerializer`. XML serialization self-closes empty elements (``); the HTML parser doesn't treat `/>` as closing, so everything after it is nested inside and lost. > -> On build, pass the HTML to `buildXmlNode({ innerHTML })`, which parses it as HTML and re-creates it as XML. +> On build, pass the HTML to `buildXmlNode({ innerHTML })`, which parses it as HTML and re-creates it as XML. Stored MathML travels through TipTap in `span[data-mathml]`; pasted carriers keep LaTeX or text instead. + +List the HTML fields and their editor modes in the interaction descriptor's `richTextFields`. `isEditableItem` checks them against the TipTap schema; a lossy field makes the item read-only without suppressing `validateQtiItem` errors. > [!NOTE] > A node view that shows itself as selected, like the inline choice chip, uses `--brand-secondary-v100`, the background of KDS's global `::selection`, with `--tokens-text` for its text. diff --git a/contentcuration/contentcuration/frontend/shared/views/QTIEditor/__tests__/QTIEditor.spec.js b/contentcuration/contentcuration/frontend/shared/views/QTIEditor/__tests__/QTIEditor.spec.js index 13e17d418f..48d997cf6c 100644 --- a/contentcuration/contentcuration/frontend/shared/views/QTIEditor/__tests__/QTIEditor.spec.js +++ b/contentcuration/contentcuration/frontend/shared/views/QTIEditor/__tests__/QTIEditor.spec.js @@ -45,6 +45,20 @@ afterAll(() => { }); describe('QTIEditor', () => { + it('enables Edit on the first render of a list of editable rich-text questions', () => { + const raw_data = VALID_CHOICE_ITEM_DOCUMENT.replace('Pick one.', 'Pick one.'); + renderComponent( + Array.from({ length: 24 }, (_, index) => ({ + ...QTI_ITEM, + raw_data, + assessment_id: `question-${index}`, + })), + ); + const editActions = screen.getAllByRole('button', { name: toolbarLabelEdit$() }); + expect(editActions).toHaveLength(24); + editActions.forEach(action => expect(action).toBeEnabled()); + }); + describe('opening a question', () => { it('opens a question from its Edit action', async () => { renderComponent([QTI_ITEM]); diff --git a/contentcuration/contentcuration/frontend/shared/views/QTIEditor/__tests__/validateItem.spec.js b/contentcuration/contentcuration/frontend/shared/views/QTIEditor/__tests__/validateItem.spec.js index cbd4769568..a70a4fb884 100644 --- a/contentcuration/contentcuration/frontend/shared/views/QTIEditor/__tests__/validateItem.spec.js +++ b/contentcuration/contentcuration/frontend/shared/views/QTIEditor/__tests__/validateItem.spec.js @@ -1,11 +1,16 @@ +import { DOMParser as SchemaParser } from '@tiptap/pm/model'; import { isEditableItem, validateItemShape, validateQtiItem } from '../validateItem'; -import { QuestionType, ValidationError } from '../constants'; +import { QuestionType, ResponseProcessingTemplate, ValidationError } from '../constants'; import { assembleItemXml } from '../serialization/assembleItem'; import { parseItem } from '../serialization/parseItem'; +import { choiceInteractionDescriptor } from '../interactions/choice/Descriptor'; import { textEntryInteractionDescriptor } from '../interactions/textEntry/Descriptor'; +import { inlineChoiceInteractionDescriptor } from '../interactions/inlineChoice/Descriptor'; import { validateTextEntryInteraction } from '../interactions/textEntry/validation'; import { + CHOICE_ITEM_DOCUMENT_WITH_CATALOG_REFERENCE, VALID_CHOICE_ITEM_DOCUMENT, + NUMERIC_ITEM_DOCUMENT, CHOICE_ITEM_DOCUMENT_NO_PROMPT, CHOICE_ITEM_DOCUMENT_NO_CORRECT_ANSWER, CHOICE_ITEM_DOCUMENT_NO_CORRECT_ANSWER_WITH_STIMULUS, @@ -15,6 +20,8 @@ import { NO_INTERACTION_ITEM_DOCUMENT, INLINE_CHOICE_ITEM_DOCUMENT, VALID_MATCH_ITEM_DOCUMENT, + VALID_ASSOCIATE_ITEM_DOCUMENT, + ORDERING_ITEM_DOCUMENT_NO_PROMPT, MATCH_THREE_SETS_XML, MATCH_XML, MULTI_TEXT_ENTRY_ITEM_DOCUMENT, @@ -22,6 +29,19 @@ import { MULTI_INTERACTION_ITEM_DOCUMENT, UNRECOGNIZED_INTERACTION_ITEM_DOCUMENT, } from '../utils/testingFixtures'; +import { XSI_NS } from '../serialization/xml'; + +it('reports duplicate associate choices even with a large match capacity', () => { + const xml = VALID_ASSOCIATE_ITEM_DOCUMENT.replace( + /match-max="1">Antonio/, + 'match-max="6">Antonio', + ).replace('>Capulet<', '>Antonio<'); + expect(validateQtiItem(xml)).toEqual( + expect.arrayContaining([ + expect.objectContaining({ code: ValidationError.DUPLICATE_DISTRACTOR_CONTENT }), + ]), + ); +}); const codesOf = errors => errors.map(error => error.code); @@ -30,6 +50,14 @@ describe('validateQtiItem', () => { expect(validateQtiItem(VALID_CHOICE_ITEM_DOCUMENT)).toEqual([]); }); + it('reports a missing correct answer for a catalog reference', () => { + const xml = CHOICE_ITEM_DOCUMENT_WITH_CATALOG_REFERENCE.replace( + /[\s\S]*?<\/qti-correct-response>/, + '', + ); + expect(codesOf(validateQtiItem(xml))).toContain(ValidationError.NO_CORRECT_ANSWER); + }); + it('reports a missing prompt', () => { expect(codesOf(validateQtiItem(CHOICE_ITEM_DOCUMENT_NO_PROMPT))).toContain( ValidationError.PROMPT_REQUIRED, @@ -339,6 +367,193 @@ describe('validateQtiItem on numeric answers typed in a language', () => { describe('isEditableItem', () => { const isEditable = xml => isEditableItem(parseItem(xml), xml); + + it('reuses parsed hints and resolves the descriptor only once for a supported item', () => { + const item = parseItem(CHOICE_ITEM_DOCUMENT_WITH_HINTS); + const parseHints = jest.spyOn(require('../serialization/hints'), 'parseHints'); + const resolveType = jest.spyOn(choiceInteractionDescriptor, 'getQuestionType'); + try { + expect(isEditableItem(item, CHOICE_ITEM_DOCUMENT_WITH_HINTS)).toBe(true); + expect(parseHints).not.toHaveBeenCalled(); + expect(resolveType).toHaveBeenCalledTimes(1); + } finally { + parseHints.mockRestore(); + resolveType.mockRestore(); + } + }); + + it('rejects an uneditable associate prompt without serializing its answer pool', () => { + const xml = VALID_ASSOCIATE_ITEM_DOCUMENT.replace( + 'Match each character to his adversary.', + 'Question', + ); + const item = parseItem(xml); + const getContentHTML = jest.spyOn(require('../serialization/xml'), 'getContentHTML'); + try { + expect(isEditableItem(item, xml)).toBe(false); + expect(getContentHTML).not.toHaveBeenCalled(); + } finally { + getContentHTML.mockRestore(); + } + }); + + it('rejects a table in a choice prompt before serializing its fields', () => { + const xml = VALID_CHOICE_ITEM_DOCUMENT.replace( + /[\s\S]*?<\/qti-prompt>/, + `${'

Context

'.repeat(2048)}
A
`, + ); + const item = parseItem(xml); + const getContentHTML = jest.spyOn(require('../serialization/xml'), 'getContentHTML'); + try { + expect(isEditableItem(item, xml)).toBe(false); + expect(getContentHTML).not.toHaveBeenCalled(); + } finally { + getContentHTML.mockRestore(); + } + }); + + it('does not extract hints after rejecting root attributes', () => { + const xml = CHOICE_ITEM_DOCUMENT_WITH_HINTS.replace('adaptive="false"', 'adaptive="true"'); + const item = parseItem(xml); + const parseHints = jest.spyOn(require('../serialization/hints'), 'parseHints'); + try { + expect(isEditableItem(item, xml)).toBe(false); + expect(parseHints).not.toHaveBeenCalled(); + } finally { + parseHints.mockRestore(); + } + }); + + it('does not extract Associate options after rejecting its prompt', () => { + const xml = VALID_ASSOCIATE_ITEM_DOCUMENT.replace( + /[\s\S]*?<\/qti-prompt>/, + 'Read this', + ); + const item = parseItem(xml); + const getContentHTML = jest.spyOn(require('../serialization/xml'), 'getContentHTML'); + try { + expect(isEditableItem(item, xml)).toBe(false); + expect(getContentHTML).not.toHaveBeenCalled(); + } finally { + getContentHTML.mockRestore(); + } + }); + + it('does not extract hints after rejecting a stylesheet', () => { + const xml = CHOICE_ITEM_DOCUMENT_WITH_HINTS.replace( + '', + '', + ); + const item = parseItem(xml); + const parseHints = jest.spyOn(require('../serialization/hints'), 'parseHints'); + try { + expect(isEditableItem(item, xml)).toBe(false); + expect(parseHints).not.toHaveBeenCalled(); + } finally { + parseHints.mockRestore(); + } + }); + + it('rejects a lossy span before round-tripping the inline choice passage', () => { + const xml = INLINE_CHOICE_ITEM_DOCUMENT.replace( + 'Contexte
{ + const xml = VALID_CHOICE_ITEM_DOCUMENT.replace( + '>A', + '>A', + ).replace( + '', + `Perte`, + ); + const item = parseItem(xml); + const parse = jest.spyOn(SchemaParser.prototype, 'parse'); + try { + expect(isEditableItem(item, xml)).toBe(false); + expect(parse).not.toHaveBeenCalled(); + } finally { + parse.mockRestore(); + } + }, + ); + + it.each([ + ['', [3, 1234.5]], + [' lower-bound="0" upper-bound="1"', [1234.5, 3]], + ])('allows an equivalent numeric mapping with attributes %j', (bounds, keys) => { + const xml = NUMERIC_ITEM_DOCUMENT.replace( + '', + ` + ${keys.map(key => ``).join('')} + `, + ).replace( + '', + ``, + ); + expect(isEditable(xml)).toBe(true); + }); + + it('rejects MathML cross-references to prompt IDs lost on rebuild', () => { + const xml = VALID_CHOICE_ITEM_DOCUMENT.replace( + 'Pick one.', + 'Pick x.', + ); + expect(isEditable(xml.replace(' xref="worked"', ''))).toBe(true); + expect(isEditable(xml)).toBe(false); + }); + + it.each([ + [ + 'associate', + VALID_ASSOCIATE_ITEM_DOCUMENT.replace( + 'match-max="1">Lysander', + 'match-max="1200">

Lysander

', + ).replace( + '', + 'Pericles', + ), + 'Lysander', + ], + [ + 'match', + VALID_MATCH_ITEM_DOCUMENT.replace( + '>Mammal', + '>

Mammal

', + ) + .replace( + 'row_frog choice_amphibian', + 'row_frog choice_mammal', + ) + .replace( + '>Reptile', + '>Reptile', + ), + 'Mammal', + ], + ])( + 'checks repeated %s rich text once without skipping lossy distractors', + (_name, xml, repeated) => { + const parse = jest.spyOn(SchemaParser.prototype, 'parse'); + try { + expect(isEditable(xml)).toBe(false); + expect(parse.mock.calls.filter(([root]) => root.textContent === repeated)).toHaveLength(1); + } finally { + parse.mockRestore(); + } + }, + ); const withRoot = attrs => CHOICE_ITEM_DOCUMENT_WITH_HINTS.replace('xml:lang="en"', `xml:lang="en" ${attrs}`); const withScoring = (outcomeDeclarations, responseProcessing = '') => @@ -413,6 +628,55 @@ describe('isEditableItem', () => { }); it.each([ + [ + 'audio in a prompt', + CHOICE_ITEM_DOCUMENT_NO_CORRECT_ANSWER.replace( + '
', + '
', + ), + ], + [ + 'video in a choice', + CHOICE_ITEM_DOCUMENT_NO_CORRECT_ANSWER.replace( + '', + '