From a1d94eef979cd95f27c1167b0e142d98630fd0bb Mon Sep 17 00:00:00 2001 From: Jonathan Putney Date: Mon, 30 Dec 2019 11:33:08 -0500 Subject: [PATCH] Handling non-string values being passed to SetValue/LMSSetValue --- src/BaseAPI.js | 4 ++++ src/constants/field_values.js | 5 +++++ test/Scorm12API.spec.js | 24 +++++++++++++++++++++++- test/Scorm2004API.spec.js | 29 ++++++++++++++++++++++++++++- test/api_helpers.js | 30 ++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 2 deletions(-) diff --git a/src/BaseAPI.js b/src/BaseAPI.js index 2943e2c..2c1e333 100644 --- a/src/BaseAPI.js +++ b/src/BaseAPI.js @@ -169,6 +169,9 @@ export default class BaseAPI { checkTerminated: boolean, CMIElement, value) { + if (value !== undefined) { + value = String(value); + } let returnValue = global_constants.SCORM_FALSE; if (this.checkState(checkTerminated, this.#error_codes.STORE_BEFORE_INIT, @@ -181,6 +184,7 @@ export default class BaseAPI { this.lastErrorCode = e.errorCode; returnValue = global_constants.SCORM_FALSE; } else { + console.error(e.getMessage()); this.throwSCORMError(this.#error_codes.GENERAL); } } diff --git a/src/constants/field_values.js b/src/constants/field_values.js index d88eec6..9f73e68 100644 --- a/src/constants/field_values.js +++ b/src/constants/field_values.js @@ -132,7 +132,12 @@ export const scorm12_values = { validScoreRange: [ '1', '50.25', + '70', '100', + 1, + 50.25, + 70, + 100, ], invalidScoreRange: [ 'invalid', diff --git a/test/Scorm12API.spec.js b/test/Scorm12API.spec.js index 1a0ddc3..5d741b2 100644 --- a/test/Scorm12API.spec.js +++ b/test/Scorm12API.spec.js @@ -3,6 +3,7 @@ import {describe, it} from 'mocha'; import Scorm12API from '../src/Scorm12API'; import * as h from './api_helpers'; import {scorm12_error_codes} from '../src/constants/error_codes'; +import {scorm12_values} from '../src/constants/field_values'; const api = (settings = {}) => { const API = new Scorm12API(settings); @@ -16,6 +17,27 @@ const apiInitialized = (settings = {}) => { }; describe('SCORM 1.2 API Tests', () => { + describe('LMSSetValue()', () => { + h.checkValidValues({ + api: apiInitialized(), + fieldName: 'cmi.core.score.raw', + validValues: scorm12_values.validScoreRange, + invalidValues: scorm12_values.invalidScoreRange, + }); + h.checkValidValues({ + api: apiInitialized(), + fieldName: 'cmi.core.score.min', + validValues: scorm12_values.validScoreRange, + invalidValues: scorm12_values.invalidScoreRange, + }); + h.checkValidValues({ + api: apiInitialized(), + fieldName: 'cmi.core.score.max', + validValues: scorm12_values.validScoreRange, + invalidValues: scorm12_values.invalidScoreRange, + }); + }); + describe('setCMIValue()', () => { describe('Invalid Sets - Should Always Fail', () => { h.checkSetCMIValue({ @@ -280,7 +302,7 @@ describe('SCORM 1.2 API Tests', () => { scorm12API.cmi.core.session_time = '23:59:59'; const cmiExport = scorm12API.renderCommitCMI(true); expect( - cmiExport.cmi.core.total_time + cmiExport.cmi.core.total_time, ).to.equal('36:34:55'); }); }); diff --git a/test/Scorm2004API.spec.js b/test/Scorm2004API.spec.js index 14ee28f..a9058d2 100644 --- a/test/Scorm2004API.spec.js +++ b/test/Scorm2004API.spec.js @@ -3,7 +3,7 @@ import {describe, it} from 'mocha'; import * as h from './api_helpers'; import {scorm2004_error_codes} from '../src/constants/error_codes'; import Scorm2004API from '../src/Scorm2004API'; -import {scorm2004_values} from '../src/constants/field_values'; +import {scorm12_values, scorm2004_values} from '../src/constants/field_values'; const api = () => { const API = new Scorm2004API(); @@ -17,6 +17,33 @@ const apiInitialized = () => { }; describe('SCORM 2004 API Tests', () => { + describe('SetValue()', () => { + h.checkValidValues({ + api: apiInitialized(), + fieldName: 'cmi.score.scaled', + validValues: scorm2004_values.validScaledRange, + invalidValues: scorm2004_values.invalidScaledRange, + }); + h.checkValidValues({ + api: apiInitialized(), + fieldName: 'cmi.score.raw', + validValues: scorm2004_values.validScoreRange, + invalidValues: scorm2004_values.invalidScoreRange, + }); + h.checkValidValues({ + api: apiInitialized(), + fieldName: 'cmi.score.min', + validValues: scorm2004_values.validScoreRange, + invalidValues: scorm2004_values.invalidScoreRange, + }); + h.checkValidValues({ + api: apiInitialized(), + fieldName: 'cmi.score.max', + validValues: scorm2004_values.validScoreRange, + invalidValues: scorm2004_values.invalidScoreRange, + }); + }); + describe('setCMIValue()', () => { describe('Invalid Sets - Should Always Fail', () => { h.checkSetCMIValue({ diff --git a/test/api_helpers.js b/test/api_helpers.js index e8fcc3a..ad7c818 100644 --- a/test/api_helpers.js +++ b/test/api_helpers.js @@ -1,6 +1,36 @@ import {describe, it} from 'mocha'; import {expect} from 'chai'; +export const checkValidValues = ( + { + api, + fieldName, + validValues, + invalidValues, + }) => { + describe(`Field: ${fieldName}`, () => { + for (const idx in validValues) { + if ({}.hasOwnProperty.call(validValues, idx)) { + it(`Should successfully write '${validValues[idx]}' to ${fieldName}`, + () => { + expect(api.lmsSetValue(fieldName, validValues[idx])). + to.equal('true'); + }); + } + } + + for (const idx in invalidValues) { + if ({}.hasOwnProperty.call(invalidValues, idx)) { + it(`Should fail to write '${invalidValues[idx]}' to ${fieldName}`, + () => { + expect(api.lmsSetValue(fieldName, invalidValues[idx])). + to.equal('false'); + }); + } + } + }); +}; + export const checkLMSSetValue = ( { api,