Bug 41247: Preparation: Cypress XML plugin

Add a way to access and manipulate koha-conf.xml (we need to ensure plugins_restricted is 0 for the actual tests)

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
This commit is contained in:
Pedro Amorim 2025-11-14 13:42:53 +00:00 committed by Martin Renvoize
parent b591b668d2
commit 0387ebfb97
No known key found for this signature in database
GPG Key ID: 2D86E0EAEC83711B
2 changed files with 56 additions and 0 deletions

View File

@ -26,6 +26,8 @@ const { getBasicAuthHeader } = require("./auth.js");
const { query } = require("./db.js");
const { modifyXmlElement, readXmlElementValue } = require("./xml.js");
const { apiGet, apiPost, apiPut, apiDelete } = require("./api-client.js");
/**
@ -52,6 +54,7 @@ const { apiGet, apiPost, apiPut, apiDelete } = require("./api-client.js");
* - API Access: apiGet, apiPost, apiPut, apiDelete
* - Database Access: query
* - Authentication: getBasicAuthHeader
* - XML files Access: modifyXmlElement, readXmlElementValue
*
* @example
* // Usage in Cypress tests
@ -104,6 +107,8 @@ module.exports = (on, config) => {
},
deleteSampleObjects,
query,
modifyXmlElement,
readXmlElementValue,
apiGet(args) {
return apiGet({ ...args, baseUrl, authHeader });

51
t/cypress/plugins/xml.js Normal file
View File

@ -0,0 +1,51 @@
const fs = require("fs");
const path = require("path");
const { exec } = require("child_process");
const { promisify } = require("util");
const execPromise = promisify(exec);
async function modifyXmlElement({ filePath, element, value }) {
const targetPath = path.resolve(filePath);
let xmlContent;
try {
xmlContent = fs.readFileSync(targetPath, "utf8");
const regex = new RegExp(`<${element}>[^<]*</${element}>`, "i");
const replacement = `<${element}>${value}</${element}>`;
if (!regex.test(xmlContent)) {
throw new Error(`Element <${element}> not found in ${targetPath}`);
}
const newContent = xmlContent.replace(regex, replacement);
fs.writeFileSync(targetPath, newContent, "utf8");
await execPromise("flush_memcached");
await execPromise("koha-plack --restart kohadev");
return null;
} catch (err) {
return err.message;
}
}
async function readXmlElementValue({ filePath, element }) {
const targetPath = path.resolve(filePath);
let xmlContent;
try {
xmlContent = fs.readFileSync(targetPath, "utf8");
const regex = new RegExp(`<${element}>([^<]*)</${element}>`, "i");
const match = xmlContent.match(regex);
if (!match) {
throw new Error(`Element <${element}> not found in ${targetPath}`);
}
// Return the value of the element
return match[1]; // match[1] contains the value between the tags
} catch (err) {
return err.message;
}
}
module.exports = { readXmlElementValue, modifyXmlElement };