diff --git a/tests/System/integration/plugins/system/sef/SefPlugin.cy.js b/tests/System/integration/plugins/system/sef/SefPlugin.cy.js index 35b27448284b0..b4a81da32df4e 100644 --- a/tests/System/integration/plugins/system/sef/SefPlugin.cy.js +++ b/tests/System/integration/plugins/system/sef/SefPlugin.cy.js @@ -1,77 +1,99 @@ describe('Test that the sef system plugin', () => { - afterEach(() => { - cy.task('deleteRelativePath', '.htaccess'); - cy.exec(`php ${Cypress.env('cmsPath')}/cli/joomla.php config:set sef=true sef_suffix=false sef_rewrite=false`); - cy.db_updateExtensionParameter('enforcesuffix', '1', 'plg_system_sef'); - cy.db_updateExtensionParameter('indexphp', '1', 'plg_system_sef'); - cy.db_updateExtensionParameter('trailingslash', '0', 'plg_system_sef'); - cy.db_updateExtensionParameter('strictrouting', '1', 'plg_system_sef'); - }); + const setSefDefaults = () => cy.task('deleteRelativePath', '.htaccess') + .then(() => cy.config_setParameter('sef', true)) + .then(() => cy.config_setParameter('sef_suffix', false)) + .then(() => cy.config_setParameter('sef_rewrite', false)) + .then(() => cy.db_updateExtensionParameter('enforcesuffix', '1', 'plg_system_sef')) + .then(() => cy.db_updateExtensionParameter('indexphp', '1', 'plg_system_sef')) + .then(() => cy.db_updateExtensionParameter('trailingslash', '0', 'plg_system_sef')) + .then(() => cy.db_updateExtensionParameter('strictrouting', '1', 'plg_system_sef')); + + // Ensure that we always start with a clean SEF default state + beforeEach(() => setSefDefaults()); + + // Return to the clean SEF default state for subsequent Joomla System Tests + afterEach(() => setSefDefaults()); it('can process if option \'sef\' disabled', () => { - cy.exec(`php ${Cypress.env('cmsPath')}/cli/joomla.php config:set sef=false`); - cy.request({ url: '/index.php?option=com_users&view=login', followRedirect: false }).then((response) => { - expect(response.status).to.eq(200); - }); - cy.request({ url: '/index.php/component/users/login', failOnStatusCode: false, followRedirect: false }).then((response) => { - expect(response.status).to.eq(404); - }); + cy.config_setParameter('sef', false) + .then(() => { + cy.request({ url: '/index.php?option=com_users&view=login', followRedirect: false }) + .then((response) => { + expect(response.status).to.eq(200); + }); + cy.request({ url: '/index.php/component/users/login', failOnStatusCode: false, followRedirect: false }) + .then((response) => { + expect(response.status).to.eq(404); + }); + }); }); it('can process if option \'enforcesuffix\' enabled', () => { - cy.exec(`php ${Cypress.env('cmsPath')}/cli/joomla.php config:set sef_suffix=true`); - cy.request({ url: '/index.php/component/users/login', followRedirect: false }).then((response) => { - expect(response.status).to.eq(301); - expect(response.redirectedToUrl).to.match(/\/index\.php\/component\/users\/login\.html$/); - }); - cy.request({ url: '/index.php/component/users/login.html', followRedirect: false }).then((response) => { - expect(response.status).to.eq(200); - }); + cy.config_setParameter('sef_suffix', true) + .then(() => { + cy.request({ url: '/index.php/component/users/login', followRedirect: false }) + .then((response) => { + expect(response.status).to.eq(301); + expect(response.redirectedToUrl).to.match(/\/index\.php\/component\/users\/login\.html$/); + }); + cy.request({ url: '/index.php/component/users/login.html', followRedirect: false }) + .then((response) => { + expect(response.status).to.eq(200); + }); + }); }); it('can process if option \'enforcesuffix\' disabled', () => { - cy.exec(`php ${Cypress.env('cmsPath')}/cli/joomla.php config:set sef_suffix=true`); - cy.db_updateExtensionParameter('enforcesuffix', '0', 'plg_system_sef'); - cy.request({ url: '/index.php/component/users/login', followRedirect: false }).then((response) => { - expect(response.status).to.eq(200); - }); - cy.request({ url: '/index.php/component/users/login.html', followRedirect: false }).then((response) => { - expect(response.status).to.eq(200); - }); + cy.config_setParameter('sef_suffix', true) + .then(() => cy.db_updateExtensionParameter('enforcesuffix', '0', 'plg_system_sef')) + .then(() => cy.request({ url: '/index.php/component/users/login', followRedirect: false })) + .then((response) => { + expect(response.status).to.eq(200); + }) + .then(() => cy.request({ url: '/index.php/component/users/login.html', followRedirect: false })) + .then((response) => { + expect(response.status).to.eq(200); + }); }); it('can process if option \'indexphp\' enabled', () => { - cy.exec(`php ${Cypress.env('cmsPath')}/cli/joomla.php config:set sef_rewrite=true`); - cy.task('copyRelativeFile', { source: 'htaccess.txt', destination: '.htaccess' }); - cy.request({ url: '/index.php/component/users/login', followRedirect: false }).then((response) => { - expect(response.status).to.eq(301); - expect(response.redirectedToUrl).to.match(/(? { - expect(response.status).to.eq(200); - }); + cy.config_setParameter('sef_rewrite', true) + .then(() => cy.task('copyRelativeFile', { source: 'htaccess.txt', destination: '.htaccess' })) + .then(() => cy.request({ url: '/index.php/component/users/login', followRedirect: false })) + .then((response) => { + expect(response.status).to.eq(301); + expect(response.redirectedToUrl).to.match(/(? cy.request({ url: '/component/users/login', followRedirect: false })) + .then((response) => { + expect(response.status).to.eq(200); + }); }); it('can process if option \'indexphp\' disabled', () => { - cy.exec(`php ${Cypress.env('cmsPath')}/cli/joomla.php config:set sef_rewrite=true`); - cy.task('copyRelativeFile', { source: 'htaccess.txt', destination: '.htaccess' }); - cy.db_updateExtensionParameter('indexphp', '0', 'plg_system_sef'); - cy.request({ url: '/index.php/component/users/login', followRedirect: false }).then((response) => { - expect(response.status).to.eq(200); - }); - cy.request({ url: '/component/users/login', followRedirect: false }).then((response) => { - expect(response.status).to.eq(200); - }); + cy.config_setParameter('sef_rewrite', true) + .then(() => cy.task('copyRelativeFile', { source: 'htaccess.txt', destination: '.htaccess' })) + .then(() => cy.db_updateExtensionParameter('indexphp', '0', 'plg_system_sef')) + .then(() => cy.request({ url: '/index.php/component/users/login', followRedirect: false })) + .then((response) => { + expect(response.status).to.eq(200); + }) + .then(() => cy.request({ url: '/component/users/login', followRedirect: false })) + .then((response) => { + expect(response.status).to.eq(200); + }); }); it('can process if option \'trailingslash\' disabled', () => { - cy.request({ url: '/index.php/component/users/login/', followRedirect: false }).then((response) => { - expect(response.status).to.eq(301); - expect(response.redirectedToUrl).to.match(/\/index\.php\/component\/users\/login$/); - }); - cy.request({ url: '/index.php/component/users/login', followRedirect: false }).then((response) => { - expect(response.status).to.eq(200); - }); + cy.request({ url: '/index.php/component/users/login/', followRedirect: false }) + .then((response) => { + expect(response.status).to.eq(301); + expect(response.redirectedToUrl).to.match(/\/index\.php\/component\/users\/login$/); + }); + cy.request({ url: '/index.php/component/users/login', followRedirect: false }) + .then((response) => { + expect(response.status).to.eq(200); + }); cy.visit('/'); cy.get('li.nav-item').contains('Home') .should('have.attr', 'href') @@ -79,14 +101,16 @@ describe('Test that the sef system plugin', () => { }); it('can process if option \'trailingslash\' enabled', () => { - cy.db_updateExtensionParameter('trailingslash', '1', 'plg_system_sef'); - cy.request({ url: '/index.php/component/users/login', followRedirect: false }).then((response) => { - expect(response.status).to.eq(301); - expect(response.redirectedToUrl).to.match(/\/index\.php\/component\/users\/login\/$/); - }); - cy.request({ url: '/index.php/component/users/login/', followRedirect: false }).then((response) => { - expect(response.status).to.eq(200); - }); + cy.db_updateExtensionParameter('trailingslash', '1', 'plg_system_sef') + .then(() => cy.request({ url: '/index.php/component/users/login', followRedirect: false })) + .then((response) => { + expect(response.status).to.eq(301); + expect(response.redirectedToUrl).to.match(/\/index\.php\/component\/users\/login\/$/); + }) + .then(() => cy.request({ url: '/index.php/component/users/login/', followRedirect: false })) + .then((response) => { + expect(response.status).to.eq(200); + }); cy.visit('/'); cy.get('li.nav-item').contains('Home') .should('have.attr', 'href') @@ -94,22 +118,26 @@ describe('Test that the sef system plugin', () => { }); it('can process if option \'strictrouting\' enabled', () => { - cy.request({ url: '/index.php?option=com_users&view=login', followRedirect: false }).then((response) => { - expect(response.status).to.eq(301); - expect(response.redirectedToUrl).to.match(/\/index\.php\/component\/users\/login$/); - }); - cy.request({ url: '/index.php/component/users/login', followRedirect: false }).then((response) => { - expect(response.status).to.eq(200); - }); + cy.request({ url: '/index.php?option=com_users&view=login', followRedirect: false }) + .then((response) => { + expect(response.status).to.eq(301); + expect(response.redirectedToUrl).to.match(/\/index\.php\/component\/users\/login$/); + }); + cy.request({ url: '/index.php/component/users/login', followRedirect: false }) + .then((response) => { + expect(response.status).to.eq(200); + }); }); it('can process if option \'strictrouting\' disabled', () => { - cy.db_updateExtensionParameter('strictrouting', '0', 'plg_system_sef'); - cy.request({ url: '/index.php?option=com_users&view=login', followRedirect: false }).then((response) => { - expect(response.status).to.eq(200); - }); - cy.request({ url: '/index.php/component/users/login', followRedirect: false }).then((response) => { - expect(response.status).to.eq(200); - }); + cy.db_updateExtensionParameter('strictrouting', '0', 'plg_system_sef') + .then(() => cy.request({ url: '/index.php?option=com_users&view=login', followRedirect: false })) + .then((response) => { + expect(response.status).to.eq(200); + }) + .then(() => cy.request({ url: '/index.php/component/users/login', followRedirect: false })) + .then((response) => { + expect(response.status).to.eq(200); + }); }); }); diff --git a/tests/System/plugins/fs.mjs b/tests/System/plugins/fs.mjs index 5dd1a23e3a59c..9bbd65be11321 100644 --- a/tests/System/plugins/fs.mjs +++ b/tests/System/plugins/fs.mjs @@ -36,24 +36,29 @@ function deleteRelativePath(relativePath, config) { * @returns null */ function writeRelativeFile(relativePath, content, config, mode = 0o444) { - const fullPath = join(config.env.cmsPath, relativePath); - // Prologue: Reset process file mode creation mask to ensure the umask value is not subtracted - const oldmask = umask(0); - // Create missing parent directories with 'rwxrwxrwx' - mkdirSync(dirname(fullPath), { recursive: true, mode: 0o777 }); - // Check if the file exists - if (existsSync(fullPath)) { - // Set 'rw-rw-rw-' to be able to overwrite the file - chmodSync(fullPath, 0o666); - } - // Write or overwrite the file on relative path with given content - writeFileSync(fullPath, content); - // Finally set given file mode or default 'r--r--r--' - chmodSync(fullPath, mode); - // Epilogue: Restore process file mode creation mask - umask(oldmask); - - return null; + return new Promise((resolve, reject) => { + try { + const fullPath = join(config.env.cmsPath, relativePath); + // Prologue: Reset process file mode creation mask to ensure the umask value is not subtracted + const oldmask = umask(0); + // Create missing parent directories with 'rwxrwxrwx' + mkdirSync(dirname(fullPath), { recursive: true, mode: 0o777 }); + // Check if the file exists + if (existsSync(fullPath)) { + // Set 'rw-rw-rw-' to be able to overwrite the file + chmodSync(fullPath, 0o666); + } + // Write or overwrite the file on relative path with given content + writeFileSync(fullPath, content); + // Finally set given file mode or default 'r--r--r--' + chmodSync(fullPath, mode); + // Epilogue: Restore process file mode creation mask + umask(oldmask); + resolve(`File successfully written: ${fullPath}`); + } catch (error) { + reject(new Error(`Failed to write file: ${error.message}`)); + } + }); } /** diff --git a/tests/System/support/commands/config.mjs b/tests/System/support/commands/config.mjs index 4987273e8ee49..b044974663cae 100644 --- a/tests/System/support/commands/config.mjs +++ b/tests/System/support/commands/config.mjs @@ -1,7 +1,8 @@ Cypress.Commands.add('config_setParameter', (parameter, value) => { const configPath = `${Cypress.env('cmsPath')}/configuration.php`; - cy.readFile(configPath).then((fileContent) => { + // Return a Cypress chainable for chaining + return cy.readFile(configPath).then((fileContent) => { // Setup the new value const newValue = typeof value === 'string' ? `'${value}'` : value; @@ -11,7 +12,11 @@ Cypress.Commands.add('config_setParameter', (parameter, value) => { // Replace the whole line with the new value const content = fileContent.replace(regex, `public $${parameter} = ${newValue};`); - // Write the modified content back to the configuration file relative to the CMS root folder - cy.task('writeRelativeFile', { path: 'configuration.php', content }); + /* Write the modified content back to the configuration file relative to the CMS root folder and + * wait for the task to complete and chain it. + */ + return cy.task('writeRelativeFile', { path: 'configuration.php', content }).then((result) => { + cy.log(result); // Log success message for debugging + }); }); });