Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cypress] Remove Joomla command line client tool dependency #44656

Open
wants to merge 2 commits into
base: 5.2-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 106 additions & 78 deletions tests/System/integration/plugins/system/sef/SefPlugin.cy.js
Original file line number Diff line number Diff line change
@@ -1,115 +1,143 @@
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(/(?<!index\.php)\/component\/users\/login$/);
});
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.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: '/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')
.and('match', /\/index\.php$/);
});

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')
.and('match', /\/index\.php\/$/);
});

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);
});
});
});
41 changes: 23 additions & 18 deletions tests/System/plugins/fs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`));
}
});
}

/**
Expand Down
11 changes: 8 additions & 3 deletions tests/System/support/commands/config.mjs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
});
});
});