Skip to content

Commit

Permalink
[cypress] nested set table #__menu (#43456)
Browse files Browse the repository at this point in the history
* nested set table

* nested set table

* cs

* cs

* cs

* cs

* mainmenu

* before

* drone

* feedback

* feedback

* chain

* async

* cs

* cs

* return

* easier2read

Co-authored-by: Allon Moritz <allon.moritz@digital-peak.com>

---------

Co-authored-by: Allon Moritz <allon.moritz@digital-peak.com>
  • Loading branch information
alikon and laoneo authored Jun 13, 2024
1 parent 6c45033 commit 8ae5039
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
11 changes: 6 additions & 5 deletions tests/System/integration/api/com_menus/SiteMenuItems.cy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe('Test that menu items site API endpoint', () => {
afterEach(() => cy.task('queryDB', "DELETE FROM #__menu WHERE title = 'automated test site menu item' "));
beforeEach(() => cy.db_deleteMenuItem({ title: 'automated test site menu item' }));

it('can deliver a list of site menu items types', () => {
cy.api_get('/menus/site/items/types')
Expand Down Expand Up @@ -43,15 +43,16 @@ describe('Test that menu items site API endpoint', () => {
});

it('can update a site menu item', () => {
cy.db_createMenuItem({ title: 'automated test site menu item', type: 'component' })
.then((id) => cy.api_patch(`/menus/site/items/${id}`, { title: 'updated automated test site menu item', type: 'component' }))
cy.db_createMenuItem({ title: 'updated automated test site menu item', type: 'component' })
.then((id) => cy.api_patch(`/menus/site/items/${id}`, { title: 'automated test site menu item', type: 'component' }))
.then((response) => cy.wrap(response).its('body').its('data').its('attributes')
.its('title')
.should('include', 'updated automated test site menu item'));
.should('include', 'automated test site menu item'));
});

it('can delete a site menu item', () => {
cy.db_createMenuItem({ title: 'automated test site menu item', published: -2 })
.then((id) => cy.api_delete(`/menus/site/items/${id}`));
.then((id) => cy.api_delete(`/menus/site/items/${id}`))
.then((response) => cy.wrap(response).its('status').should('equal', 204));
});
});
45 changes: 33 additions & 12 deletions tests/System/support/commands/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,21 +439,42 @@ Cypress.Commands.add('db_createMenuItem', (menuItemData) => {
language: '*',
params: '',
img: '',
lft: 0,
rgt: 0,
};

// Create the data to insert
const menuItem = { ...defaultMenuItemOptions, ...menuItemData };

// Extract the component from the link
const component = (new URLSearchParams(menuItem.link.replace('index.php', ''))).get('option');

// Search for the component
return cy.task('queryDB', `SELECT extension_id FROM #__extensions WHERE name = '${component}'`).then((id) => {
// Get the correct component id from the extensions record
menuItem.component_id = id[0].extension_id;
// Create space for rgt and lft
cy.task('queryDB', 'SELECT rgt FROM #__menu WHERE id = 1').then((myrgt) => {
defaultMenuItemOptions.lft = myrgt[0].rgt;
defaultMenuItemOptions.rgt = myrgt[0].rgt + 1;

const menuItem = { ...defaultMenuItemOptions, ...menuItemData };
// Extract the component from the link
const component = (new URLSearchParams(menuItem.link.replace('index.php', ''))).get('option');
return cy.task('queryDB', `SELECT extension_id FROM #__extensions WHERE name = '${component}'`).then((id) => {
// Get the correct component id from the extensions record
menuItem.component_id = id[0].extension_id;
return cy.task('queryDB', `UPDATE #__menu SET rgt = rgt + 2 WHERE rgt >= '${defaultMenuItemOptions.lft}'`)
.then(() => cy.task('queryDB', `UPDATE #__menu SET lft = lft + 2 WHERE lft > '${defaultMenuItemOptions.rgt}'`))
.then(() => cy.task('queryDB', createInsertQuery('menu', menuItem)))
.then(async (info) => info.insertId);
});
});
});

// Create the menu item
return cy.task('queryDB', createInsertQuery('menu', menuItem)).then(async (info) => info.insertId);
/**
* Delete a menu item in the database with the given title.
*
* @param {Object} menuItemTitle The menu item tile to delete
*
*/
Cypress.Commands.add('db_deleteMenuItem', (menuItemTitle) => {
cy.task('queryDB', `SELECT lft, rgt, (rgt - lft) +1 AS width FROM #__menu WHERE title = '${menuItemTitle.title}'`).then((record) => {
if (record.length > 0) {
cy.task('queryDB', `DELETE FROM #__menu WHERE lft BETWEEN '${record[0].lft}' AND '${record[0].rgt}'`)
.then(() => cy.task('queryDB', `UPDATE #__menu SET lft = lft - '${record[0].width}' WHERE lft > '${record[0].rgt}'`))
.then(() => cy.task('queryDB', `UPDATE #__menu SET rgt = rgt - '${record[0].width}' WHERE rgt > '${record[0].rgt}'`));
}
});
});

Expand Down

0 comments on commit 8ae5039

Please sign in to comment.