| 1 |
import { safeJsonStringify } from './shared'; |
| 2 |
|
| 3 |
// CI runs cypress against a native wp-cli install (see .github/workflows/e2e.yml's |
| 4 |
// "Set up WordPress" step + the wp-cli.yml it writes). Locally we still go through |
| 5 |
// wp-env's containerized cli. Both forms run from the repo root. |
| 6 |
const wp = (args) => |
| 7 |
Cypress.env('CI') ? `wp ${args}` : `npx wp-env run cli wp ${args}`; |
| 8 |
|
| 9 |
Cypress.Commands.add('text', { prevSubject: true }, (subject, text) => { |
| 10 |
subject.val(text); |
| 11 |
return cy.wrap(subject); |
| 12 |
}); |
| 13 |
|
| 14 |
Cypress.Commands.add('activateTheme', (theme) => { |
| 15 |
cy.log(`Activate ${theme} theme`); |
| 16 |
cy.exec(wp(`theme activate ${theme}`), { |
| 17 |
failOnNonZeroExit: false, |
| 18 |
}); |
| 19 |
}); |
| 20 |
|
| 21 |
Cypress.Commands.add( |
| 22 |
'updateWpConfig', |
| 23 |
(constant, val, flags = { raw: true }) => { |
| 24 |
cy.log(`Update WordPress config: ${constant}`); |
| 25 |
cy.exec(wp(`config set ${constant} ${val} ${flags.raw ? '--raw' : ''}`), { |
| 26 |
failOnNonZeroExit: false, |
| 27 |
}).then(({ stdout }) => cy.log(stdout)); |
| 28 |
}, |
| 29 |
); |
| 30 |
|
| 31 |
Cypress.Commands.add('resetAll', () => { |
| 32 |
cy.log('Reset WordPress'); |
| 33 |
cy.resetWpDb(); |
| 34 |
cy.clearBrowserStorage(); |
| 35 |
cy.clearCookies(); |
| 36 |
cy.updateWpConfig('EXTENDIFY_PARTNER_ID', null); |
| 37 |
cy.activateTheme('extendable'); |
| 38 |
|
| 39 |
// Close pattern modal and welcome guide |
| 40 |
cy.exec( |
| 41 |
wp( |
| 42 |
'user meta add 1 wp_persisted_preferences \'{"core/edit-post":{"welcomeGuide":false,"core/edit-post/pattern-modal":false,"pattern-modal":false,"edit-post/pattern-modal":false,"patternModal":false},"core":{"enableChoosePatternModal":false},"_modified":"2025-03-23T02:16:33.561Z"}\' --format=json', |
| 43 |
), |
| 44 |
); |
| 45 |
}); |
| 46 |
|
| 47 |
Cypress.Commands.add('resetWpDb', () => { |
| 48 |
cy.log('Reset database'); |
| 49 |
if (Cypress.env('CI')) { |
| 50 |
cy.exec('wp db reset --yes', { failOnNonZeroExit: false }); |
| 51 |
cy.exec( |
| 52 |
'wp core install --url=http://localhost:8888 --title=Test --admin_user=admin --admin_password=password --admin_email=admin@example.org --skip-email', |
| 53 |
{ failOnNonZeroExit: false }, |
| 54 |
); |
| 55 |
// `wp db reset` wipes active_plugins. Re-activate so |
| 56 |
// PluginRedirectDisabler hooks `activated_plugin` before the spec |
| 57 |
// activates the third-party plugins under test. |
| 58 |
cy.exec('wp plugin activate extendify-sdk', { failOnNonZeroExit: false }); |
| 59 |
return; |
| 60 |
} |
| 61 |
cy.exec('npx wp-env clean all', { |
| 62 |
failOnNonZeroExit: false, |
| 63 |
}); |
| 64 |
}); |
| 65 |
|
| 66 |
Cypress.Commands.add('updateOption', (key, val, options = { json: false }) => { |
| 67 |
cy.log(`Update WordPress database option: ${key}`); |
| 68 |
|
| 69 |
const command = options.json |
| 70 |
? `'${safeJsonStringify(val)}' --format=json` |
| 71 |
: val; |
| 72 |
cy.exec(wp(`option update ${key} ${command}`), { |
| 73 |
failOnNonZeroExit: false, |
| 74 |
}); |
| 75 |
}); |
| 76 |
|
| 77 |
Cypress.Commands.add('enableLaunch', (partnerId = 'github') => { |
| 78 |
cy.log('Enable Launch'); |
| 79 |
cy.updateWpConfig('EXTENDIFY_PARTNER_ID', `"${partnerId}"`, { raw: false }); |
| 80 |
|
| 81 |
// Required only when using the default partnerId, due to the new way of managing the showLaunch status. |
| 82 |
// Active partners will have this set to true by default. |
| 83 |
// More details: https://github.com/extendify/company-partnerships/issues/109 |
| 84 |
if (partnerId === 'github') { |
| 85 |
cy.setPartner(partnerId, { |
| 86 |
showLaunch: true, |
| 87 |
}); |
| 88 |
} |
| 89 |
}); |
| 90 |
|
| 91 |
Cypress.Commands.add('setPartner', (name, options = {}) => { |
| 92 |
cy.updateWpConfig('EXTENDIFY_PARTNER_ID', `"${name}"`, { raw: false }); |
| 93 |
const defaultOptions = { |
| 94 |
deactivated: false, |
| 95 |
}; |
| 96 |
cy.addPartnerData({ ...defaultOptions, ...options }); |
| 97 |
}); |
| 98 |
|
| 99 |
Cypress.Commands.add('addPartnerData', (payload = {}) => { |
| 100 |
const serialized = JSON.stringify(payload); |
| 101 |
cy.exec( |
| 102 |
wp(`option update extendify_partner_data_v2 '${serialized}' --format=json`), |
| 103 |
{ raw: false }, |
| 104 |
); |
| 105 |
cy.exec( |
| 106 |
wp("option update _transient_extendify_partner_data_cache_check '1'"), |
| 107 |
{ raw: false }, |
| 108 |
); |
| 109 |
}); |
| 110 |
|
| 111 |
Cypress.Commands.add('clearBrowserStorage', () => { |
| 112 |
cy.log('Clear browser local storage (including session storage)'); |
| 113 |
cy.reload(); |
| 114 |
cy.window().then((win) => { |
| 115 |
win.localStorage.clear(); |
| 116 |
win.sessionStorage.clear(); |
| 117 |
}); |
| 118 |
// https://github.com/cypress-io/cypress/issues/2573#issuecomment-1339618812 |
| 119 |
cy.clearAllLocalStorage(); |
| 120 |
}); |
| 121 |
|
| 122 |
Cypress.Commands.add('endTestCleanly', (path = '') => { |
| 123 |
cy.visitAdminPage(path); |
| 124 |
}); |
| 125 |
|