Skip to content

Commit 1673755

Browse files
Finish unsubscribe tests
Add support for fetching more contacts Add support for fetching contacts by status Add utility function to set boolean settings
1 parent 07e1ec6 commit 1673755

File tree

5 files changed

+143
-69
lines changed

5 files changed

+143
-69
lines changed

.vscode/launch.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "WP Env",
9+
"type": "php",
10+
"request": "launch",
11+
"port": 9003,
12+
"pathMappings": {
13+
"/var/www/html/wp-content/plugins/wordpress": "${workspaceFolder}/"
14+
}
15+
}
16+
]
17+
}

tests/cypress/e2e/submission/unsubscribe.test.js

Lines changed: 79 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,29 @@ describe('Unsubscribe form', () => {
1717

1818
cy.selectList('10up'); // Ensure list is selected, refreshes Mailchimp data with WP
1919

20+
// Single Opt-in
21+
cy.setDoubleOptInOption(false);
22+
2023
// Check unsubscription link
21-
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_options');
22-
cy.get('#mc_use_unsub_link').check();
23-
cy.get('input[value="Update Subscribe Form Settings"]').first().click();
24+
cy.setSettingsOption('#mc_use_unsub_link', true);
2425
});
2526

2627
after(() => {
2728
// I don't know why we have to login again, but we do
2829
cy.login(); // WP
2930

3031
// Uncheck unsubscription link
31-
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_options');
32-
cy.get('#mc_use_unsub_link').uncheck();
33-
cy.get('input[value="Update Subscribe Form Settings"]').first().click();
32+
cy.setSettingsOption('#mc_use_unsub_link', false);
33+
});
34+
35+
it('unsubscribe link appears on both shortcode and block pages', () => {
36+
[shortcodePostURL, blockPostPostURL].forEach((url) => {
37+
// Visit the mailchimp block page
38+
cy.visit(url);
39+
40+
// Assert unsubscribe link exists
41+
cy.get('a[href*="/unsubscribe"]').should('exist');
42+
});
3443
})
3544

3645
it('unsubscribes valid emails that were previously subscribed to a list', () => {
@@ -41,81 +50,90 @@ describe('Unsubscribe form', () => {
4150
cy.subscribeToList(listId, email);
4251
});
4352

44-
[shortcodePostURL, blockPostPostURL].forEach((url) => {
45-
let baseUrl;
53+
let baseUrl;
4654

47-
// Visit the mailchimp block page
48-
cy.visit(url);
55+
// Visit the mailchimp block page
56+
cy.visit(blockPostPostURL);
4957

50-
// Get baseUrl to use for later assertion
51-
cy.url().then((url) => {
52-
// Extract the base URL
53-
const urlObject = new URL(url);
54-
baseUrl = `${urlObject.protocol}//${urlObject.host}`;
55-
});
56-
58+
// Get baseUrl to use for later assertion
59+
cy.url().then((url) => {
60+
// Extract the base URL
61+
const urlObject = new URL(url);
62+
baseUrl = `${urlObject.protocol}//${urlObject.host}`;
63+
});
5764

58-
// Assert unsubscribe link exists
59-
cy.get('a[href*="/unsubscribe"]').should('exist');
6065

61-
// Visit unsubscribe link
62-
cy.get('a[href*="/unsubscribe"]')
63-
.invoke('removeAttr', 'target') // Prevent opening in new window so that Cypress can test
64-
.click();
66+
// Assert unsubscribe link exists
67+
cy.get('a[href*="/unsubscribe"]').should('exist');
6568

66-
// Unsubscribe
67-
cy.get('#email-address').type(email);
68-
cy.get('input[type="submit"]').click();
69-
cy.get('body').should('contain', 'Unsubscribe Successful');
69+
// Visit unsubscribe link
70+
cy.get('a[href*="/unsubscribe"]')
71+
.invoke('removeAttr', 'target') // Prevent opening in new window so that Cypress can test
72+
.click();
7073

71-
// Navigate back to the website button exists
72-
cy.contains('a', 'return to our website')
73-
.should('exist');
74+
// Unsubscribe
75+
cy.get('#email-address').type(email);
76+
cy.get('input[type="submit"]').click();
77+
cy.get('body').should('contain', 'Unsubscribe Successful');
78+
79+
// Navigate back to the website button exists
80+
cy.contains('a', 'return to our website')
81+
.should('exist');
82+
83+
// Verify contact exists in Mailchimp with status 'unsubscribed'
84+
cy.verifyContactInMailchimp(email, '10up', 'unsubscribed').then((contact) => {
85+
cy.verifyContactStatus(contact, 'unsubscribed');
7486

7587
// Delete contact to clean up
7688
cy.deleteContactFromList(email);
77-
78-
// Navigate to website
79-
// NOTE: The website URL is site in Mailchimp and it won't accept localhost or our test URL
80-
// TODO: Assert that we're back on our website (we currently have no way to set this)
81-
// cy.contains('a', 'return to our website').click();
82-
// cy.url().should('include', baseUrl); // TODO: Do we want to assert a specific landing page?
83-
8489
});
90+
91+
// Navigate to back website
92+
// NOTE: The website URL is site in Mailchimp and it won't accept localhost or our test URL
93+
// TODO: Assert that we're back on our website (we currently have no way to set this)
94+
// cy.contains('a', 'return to our website').click();
95+
// cy.url().should('include', baseUrl); // TODO: Do we want to assert a specific landing page?
8596
});
86-
97+
8798
it('throws an error when unsubscribing an email that was never subscribed to a list', () => {
8899
const email = generateRandomEmail('never-subscribed-user');
89100

90-
[shortcodePostURL, blockPostPostURL].forEach((url) => {
91-
// Visit the mailchimp block page
92-
cy.visit(url);
101+
// Visit the mailchimp block page
102+
cy.visit(blockPostPostURL);
93103

94-
// Assert unsubscribe link exists
95-
cy.get('a[href*="/unsubscribe"]').should('exist');
104+
// Assert unsubscribe link exists
105+
cy.get('a[href*="/unsubscribe"]').should('exist');
96106

97-
// Visit unsubscribe link
98-
cy.get('a[href*="/unsubscribe"]')
99-
.invoke('removeAttr', 'target') // Prevent opening in new window so that Cypress can test
100-
.click();
107+
// Visit unsubscribe link
108+
cy.get('a[href*="/unsubscribe"]')
109+
.invoke('removeAttr', 'target') // Prevent opening in new window so that Cypress can test
110+
.click();
101111

102-
// Unsubscribe
103-
cy.get('#email-address').type(email);
104-
cy.get('input[type="submit"]').click();
112+
// Unsubscribe
113+
cy.get('#email-address').type(email);
114+
cy.get('input[type="submit"]').click();
105115

106-
// Assert that the unsubscribe didn't work because the email isn't subscribed
107-
cy.get('.errorText').should('contain', 'this email is not subscribed');
108-
109-
});
116+
// Assert that the unsubscribe didn't work because the email isn't subscribed
117+
cy.get('.errorText').should('contain', 'this email is not subscribed');
110118
});
111119

112-
// TODO: Test not written yet
113-
it.skip('does not display an unsubscribe link when the unsubscribe option is disabled', () => {
114-
// Test here...
115-
});
120+
it('does not display an unsubscribe link when the unsubscribe option is disabled', () => {
121+
// Not sure why we have to login for this test, but we do
122+
cy.login(); // WP
116123

117-
// TODO: Test not written yet
118-
it.skip('redirects the user back to the website when the user is finished unsubscribing and clicks the back link', () => {
119-
// Test here...
124+
// Uncheck unsubscription link
125+
cy.setSettingsOption('#mc_use_unsub_link', false);
126+
127+
[shortcodePostURL, blockPostPostURL].forEach((url) => {
128+
// Visit the mailchimp block page
129+
cy.visit(url);
130+
131+
// Assert unsubscribe link exists
132+
cy.get('a[href*="/unsubscribe"]').should('not.exist');
133+
});
120134
});
135+
136+
// NOTE: We can not set the "return to website" URL from the Mailchimp plugin or through the API.
137+
// Alternative proposals on issue #91 and #92 to add a user tutorial
138+
// it.skip('redirects the user back to the website when the user is finished unsubscribing and clicks the back link', () => {});
121139
});

tests/cypress/support/commands/mailchimpApi.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,36 @@ async function getListId(listName) {
4747
* Gets lists from the account of the API token set in the mailchimp config
4848
*/
4949
Cypress.Commands.add('getContactsFromAList', getContactsFromAList);
50-
async function getContactsFromAList(listId) {
51-
const response = await mailchimp.lists.getListMembersInfo(listId);
52-
return response.members;
50+
async function getContactsFromAList(listId, status = null) {
51+
let members = [];
52+
let offset = 0;
53+
const count = 100; // Number of members to fetch per request (Mailchimp's limit is usually 1000 per page)
54+
55+
while (true) {
56+
const options = {
57+
count,
58+
offset,
59+
};
60+
61+
// Add status filter if provided
62+
if (status) {
63+
options.status = status;
64+
}
65+
66+
const response = await mailchimp.lists.getListMembersInfo(listId, options);
67+
68+
members = members.concat(response.members);
69+
70+
// Break the loop if we've fetched all members
71+
if (members.length >= response.total_items) {
72+
break;
73+
}
74+
75+
// Increment the offset for the next page
76+
offset += count;
77+
}
78+
79+
return members;
5380
}
5481

5582
/**
@@ -125,7 +152,7 @@ async function updateMergeFieldByTag(listId, tag, data) {
125152
async function getMergeFields(listId) {
126153
let mergeFields = [];
127154
let offset = 0;
128-
const count = 25; // Number of fields to fetch per request
155+
const count = 100; // Number of fields to fetch per request
129156

130157
while (true) {
131158
const response = await mailchimp.lists.getListMergeFields(listId, {

tests/cypress/support/commands/settings.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ function setDoubleOptInOption(enabled) {
7575
cy.get('input[value="Update Subscribe Form Settings"]').first().click();
7676
}
7777

78+
Cypress.Commands.add('setSettingsOption', setSettingsOption);
79+
function setSettingsOption(selector, enabled) {
80+
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_options');
81+
if (enabled) {
82+
cy.get(selector).check();
83+
} else {
84+
cy.get(selector).uncheck();
85+
}
86+
cy.get('input[value="Update Subscribe Form Settings"]').first().click();
87+
}
88+
7889
/**
7990
* Custom Cypress command to enable or disable Mailchimp merge fields in the WordPress admin settings.
8091
*

tests/cypress/support/commands/submission.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@ Cypress.Commands.add('submitFormAndVerifyError', () => {
2323
/**
2424
* Custom command to verify that a contact was added to a specified list in Mailchimp
2525
*/
26-
Cypress.Commands.add('verifyContactInMailchimp', (email, listName = '10up') => {
26+
Cypress.Commands.add('verifyContactInMailchimp', (email, listName = '10up', status = null) => {
2727
// Step 1: Get the list ID for the specified list name
2828
cy.getListId(listName).then((listId) => {
2929
// Step 2: Retrieve the contacts from the specified list
30-
cy.getContactsFromAList(listId).then((contacts) => {
31-
cy.log('Contacts retrieved:', contacts); // Log the contacts for debugging
30+
cy.getContactsFromAList(listId, status).then((contacts) => {
31+
// Log the contacts for debugging
32+
console.log(contacts)
3233

3334
// Step 3: Verify that the contact with the provided email exists in the list
3435
const contact = contacts.find((c) => c.email_address === email);
3536
expect(contact).to.exist;
36-
return contact;
37+
cy.wrap(contact); // Wrap the contact to allow further chaining
3738
});
3839
});
3940
});

0 commit comments

Comments
 (0)