From 9dee6f0a4d6446dfc3b73417d7cb6e77ca1c37aa Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Fri, 14 Nov 2025 13:42:53 +0000 Subject: [PATCH] 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) --- t/cypress/plugins/index.js | 5 ++++ t/cypress/plugins/xml.js | 51 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 t/cypress/plugins/xml.js diff --git a/t/cypress/plugins/index.js b/t/cypress/plugins/index.js index 5970890dc02..47ab304cb14 100644 --- a/t/cypress/plugins/index.js +++ b/t/cypress/plugins/index.js @@ -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 }); diff --git a/t/cypress/plugins/xml.js b/t/cypress/plugins/xml.js new file mode 100644 index 00000000000..b7811ddbe14 --- /dev/null +++ b/t/cypress/plugins/xml.js @@ -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}>[^<]*`, "i"); + const replacement = `<${element}>${value}`; + + 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}>([^<]*)`, "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 }; -- 2.39.5