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

Ddfbra 222 update webform module #1808

Open
wants to merge 4 commits into
base: develop
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
21 changes: 11 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions cypress/e2e/webforms.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
describe('Webforms', () => {
before(() => {
cy.enableDevelMailLog();
});

beforeEach(() => {
Cypress.session.clearAllSavedSessions();
});

it('Go to the default contact webform page and fill it out successfully.', () => {
cy.visit('/kontakt');
cy.url().should('match', /kontakt/);
cy.get('[data-cy="name"]').type('John Doe');
cy.get('[data-cy="email"]').type('[email protected]');
cy.get('[data-cy="category"]').select(1);
cy.get('[data-cy="subject"]').type('Test');
cy.get('[data-cy="message"]').type('Lorem ipsum');
// We bypass the linting here, as we need to force waiting as we need to
// wait for the honeypot timer to run out.
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(6000);
cy.get('[data-cy="op"]').click();
cy.url().should('match', /kontakt/);
cy.get('.status-message__description').contains(
'Your submission has been received. You will receive a confirmation mail soon.',
);
});

it('Go to the default contact webform page and check that required fields are working.', () => {
cy.visit('/kontakt');
cy.url().should('match', /kontakt/);
cy.get('[data-cy="op"]').click();
cy.url().should('match', /kontakt/);
});

it('Check that antibot_key and honeypot_time input elements exists.', () => {
cy.visit('/kontakt');
cy.url().should('match', /kontakt/);
cy.get('[data-cy="honeypot_time"]').should('exist');
cy.get('[data-cy="antibot_key"]').should('exist');
});

it('Check that the honeypot timer is working by submitting form before timer (5000ms) is up.', () => {
cy.visit('/kontakt');
cy.url().should('match', /kontakt/);
cy.get('[data-cy="name"]').type('John Doe');
cy.get('[data-cy="email"]').type('[email protected]');
cy.get('[data-cy="category"]').select(1);
cy.get('[data-cy="subject"]').type('Test');
cy.get('[data-cy="message"]').type('Lorem ipsum');
cy.get('[data-cy="op"]').click();
cy.get('.error-message__description').contains(
'There was a problem with your form submission.',
);
});

it('Check that the honeypot field is working by filling in hidden honeypot field.', () => {
cy.visit('/kontakt');
cy.url().should('match', /kontakt/);
cy.get('[data-cy="name"]').type('John Doe');
cy.get('[data-cy="email"]').type('[email protected]');
cy.get('[data-cy="category"]').select(1);
cy.get('[data-cy="subject"]').type('Test');
cy.get('[data-cy="message"]').type('Lorem ipsum');
cy.get('[data-cy="url"]').type('John Doe', { force: true });
// We bypass the linting here, as we need to force waiting as we need to
// wait for the honeypot timer to run out.
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(6000);
cy.get('[data-cy="op"]').click();
cy.get('.error-message__description').contains(
'There was a problem with your form submission.',
);
});

beforeEach(() => {
cy.resetMappings();
});

afterEach(() => {
cy.logMappingRequests();
});
});
16 changes: 13 additions & 3 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,26 @@ Cypress.Commands.add('drupalLogout', () => {

Cypress.Commands.add('drupalCron', () => {
// Because we run Wiremock as a proxy only services configured with the
// proxy will use it. We need to proxy requests during cron and only the
// web container is configured to use the proxy and thus we have to run
// proxy will use it. We need to proxy requests during cron and only the
// web container is configured to use the proxy, and thus we have to run
// cron through the web frontend. Using the proxy with the CLI container would
// cause too many irrelevant requests to pass throuh the proxy.
// cause too many irrelevant requests to pass through the proxy.
cy.drupalLogin();
cy.visit('/admin/config/system/cron');
cy.get('[value="Run cron"]').click();
cy.contains('Cron ran successfully.');
cy.drupalLogout();
});

Cypress.Commands.add('enableDevelMailLog', () => {
cy.drupalLogin();
cy.visit('/admin/config/system/mailsystem');
cy.get('[data-cy="mailsystem[default_formatter]"]').select('devel_mail_log');
cy.get('[data-cy="mailsystem[default_sender]"]').select('devel_mail_log');
cy.get('[data-drupal-selector="edit-submit"]').click();
cy.drupalLogout();
});

const adgangsplatformenLoginOauthMappings = ({
userIsAlreadyRegistered,
authorizationCode,
Expand Down Expand Up @@ -346,6 +355,7 @@ declare global {
drupalLogin(url?: string): Chainable<null>;
drupalLogout(): Chainable<null>;
drupalCron(): Chainable<null>;
enableDevelMailLog(): Chainable<null>;
adgangsplatformenLogin(params: {
authorizationCode: string;
accessToken: string;
Expand Down
38 changes: 38 additions & 0 deletions web/modules/custom/dpl_webform/dpl_webform.module
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,39 @@ function dpl_webform_webform_create(Webform $webform): void {
$webform->setSetting('confirmation_type', 'message');
}

/**
* Implements hook_preprocess_HOOK().
*/
function dpl_webform_preprocess_input(array &$variables): void {

// Add data-cy attributes to all input elements for cypress testing.
if (isset($variables['attributes']['name'])) {
$variables['attributes']['data-cy'] = $variables['attributes']['name'];
}
}

/**
* Implements hook_preprocess_HOOK().
*/
function dpl_webform_preprocess_textarea(array &$variables): void {

// Add data-cy attributes to all input elements for cypress testing.
if (isset($variables['attributes']['name'])) {
$variables['attributes']['data-cy'] = $variables['attributes']['name'];
}
}

/**
* Implements hook_preprocess_HOOK().
*/
function dpl_webform_preprocess_select(array &$variables): void {

// Add data-cy attributes to all input elements for cypress testing.
if (isset($variables['attributes']['name'])) {
$variables['attributes']['data-cy'] = $variables['attributes']['name'];
}
}

/**
* Implements hook_preprocess_HOOK().
*/
Expand All @@ -38,6 +71,11 @@ function dpl_webform_preprocess_input__submit(array &$variables): void {
'dpl-button',
]);
}

// Add data-cy attributes to all input elements for cypress testing.
if (isset($variables['attributes']['name'])) {
$variables['attributes']['data-cy'] = $variables['attributes']['name'];
}
}

/**
Expand Down
Loading