Skip to content

Commit

Permalink
Dont replace xml if entities-version doesnt match expected old version
Browse files Browse the repository at this point in the history
  • Loading branch information
ktuite committed Oct 3, 2024
1 parent 5abdf61 commit 7878c43
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
11 changes: 6 additions & 5 deletions lib/data/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,11 +610,11 @@ const _addBranchIdAndTrunkVersion = (xml) => new Promise((pass, fail) => {
parser.end();
});

const _updateEntityVersion = (xml, newVersion) => new Promise((pass, fail) => {
const _updateEntityVersion = (xml, oldVersion, newVersion) => new Promise((pass, fail) => {
const stack = [];
const parser = new hparser.Parser({
onattribute: (name, value) => {
if ((stripNamespacesFromPath(name) === 'entities-version') && (value === '2023.1.0')
if ((stripNamespacesFromPath(name) === 'entities-version') && (value === oldVersion)
&& (stack.length) === 2 && (stack[0] === 'html') && (stack[1] === 'head')) {
const idx = parser._tokenizer._index;
parser.reset();
Expand All @@ -636,10 +636,11 @@ const _updateEntityVersion = (xml, newVersion) => new Promise((pass, fail) => {
parser.end();
});

const updateEntityForm = (xml, newVersion, suffix) =>
_updateEntityVersion(xml, newVersion)
const updateEntityForm = (xml, oldVersion, newVersion, suffix) =>
_updateEntityVersion(xml, oldVersion, newVersion)
.then(_addBranchIdAndTrunkVersion)
.then(x => addVersionSuffix(x, suffix));
.then(x => addVersionSuffix(x, suffix))
.catch(() => xml);

module.exports = {
getFormFields,
Expand Down
8 changes: 7 additions & 1 deletion test/unit/data/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2089,7 +2089,7 @@ describe('form schema', () => {

describe('updateEntityForm', () => {
it('should change version 2023->2024, add trunkVersion, and add branchId', () =>
updateEntityForm(testData.forms.updateEntity, '2024.1.0', '_upgrade').then((result) => result.should.equal(`<?xml version="1.0"?>
updateEntityForm(testData.forms.updateEntity, '2023.1.0', '2024.1.0', '_upgrade').then((result) => result.should.equal(`<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:entities="http://www.opendatakit.org/xforms">
<h:head>
<model entities:entities-version="2024.1.0">
Expand All @@ -2110,6 +2110,12 @@ describe('form schema', () => {
</model>
</h:head>
</h:html>`)));

it('should not alter a version 2022.1.0 form when the old version to replace is 2023.1.0', () =>
updateEntityForm(testData.forms.simpleEntity, '2023.1.0', '2024.1.0', '_upgrade').then((result) => result.should.equal(testData.forms.simpleEntity)));

it('should not alter a version 2024.1.0 form when the old version to replace is 2023.1.0', () =>
updateEntityForm(testData.forms.offlineEntity, '2023.1.0', '2024.1.0', '_upgrade').then((result) => result.should.equal(testData.forms.offlineEntity)));
});
});

0 comments on commit 7878c43

Please sign in to comment.