Skip to content

Commit

Permalink
Merge pull request #17 from 10up/command/set-permalink-structure
Browse files Browse the repository at this point in the history
Set Permalink Structure command
  • Loading branch information
cadic authored Mar 23, 2022
2 parents 8f7642d + f67f5fb commit 330d8f2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/commands/set-permalink-structure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Set Permalink Structure
*
* @param type - Permalink structure, should contain structure tags or be an empty string (which means "Plain" structure)
*
* @example
* ```
* cy.setPermalinkStructure('/%year%/%postname%/')
* ```
*/
export const setPermalinkStructure = (type: string): void => {
cy.visit('/wp-admin/options-permalink.php');
cy.get('#permalink_structure').click().clear();
if ('' !== type) {
cy.get('#permalink_structure').click().type(type);
}
cy.get('#submit').click();
};
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/// <reference types="cypress" />

// Import commands.
import { setPermalinkStructure } from './commands/set-permalink-structure';
import { openDocumentSettingsPanel } from './commands/open-document-settings-panel';
import { openDocumentSettingsSidebar } from './commands/open-document-settings-sidebar';
import { deleteAllTerms } from './commands/delete-all-terms';
Expand All @@ -12,6 +13,7 @@ import { login } from './commands/login';
declare global {
namespace Cypress {
interface Chainable<Subject> {
setPermalinkStructure: typeof setPermalinkStructure;
openDocumentSettingsPanel: typeof openDocumentSettingsPanel;
openDocumentSettingsSidebar: typeof openDocumentSettingsSidebar;
deleteAllTerms: typeof deleteAllTerms;
Expand All @@ -23,6 +25,7 @@ declare global {
}

// Register commands
Cypress.Commands.add('setPermalinkStructure', setPermalinkStructure);
Cypress.Commands.add('openDocumentSettingsPanel', openDocumentSettingsPanel);
Cypress.Commands.add(
'openDocumentSettingsSidebar',
Expand Down
48 changes: 48 additions & 0 deletions tests/cypress/integration/set-permalink-structure.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
describe('Command: setPermalinkStructure', () => {
beforeEach(() => {
cy.login();
});

const structures = [
{ name: 'Plain', value: '' },
{ name: 'Day and name', value: '/%year%/%monthnum%/%day%/%postname%/' },
{ name: 'Month and name', value: '/%year%/%monthnum%/%postname%/' },
{ name: 'Numeric', value: '/archives/%post_id%' },
{ name: 'Post name', value: '/%postname%/' },
];

structures.forEach(structure => {
it(`Should be able to set predefined ${structure.name} permalinks`, () => {
cy.setPermalinkStructure(structure.value);
cy.get('.notice-success').should(
'contain',
'Permalink structure updated.'
);
cy.get('.form-table.permalink-structure :checked').should(
'have.value',
structure.value
);
});
});

it('Should be able to set custom permalinks', () => {
const structure = '/custom/%second%/';
cy.setPermalinkStructure(structure);
cy.get('.notice-success').should('contain', 'Permalink structure updated.');
cy.get('.form-table.permalink-structure :checked').should(
'have.value',
'custom'
);
cy.get('#permalink_structure').should('have.value', structure);
});

it('Should receive error if no tag added', ()=>{
cy.setPermalinkStructure('no-tag');
cy.get('.notice-error').should('contain', 'A structure tag is required when using custom permalinks.');
});

after(() => {
// Set permalinks back to plain
cy.setPermalinkStructure('');
});
});

0 comments on commit 330d8f2

Please sign in to comment.