-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
order-emails.spec.js
244 lines (221 loc) · 7.25 KB
/
order-emails.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* eslint-disable no-console */
const { test, expect } = require( '@playwright/test' );
const { admin } = require( '../../test-data/data' );
const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default;
test.describe( 'Merchant > Order Action emails received', () => {
test.use( { storageState: process.env.ADMINSTATE } );
const customerBilling = {
email: 'john.doe.merchant.test@example.com',
};
const storeName = 'WooCommerce Core E2E Test Suite';
let orderId, newOrderId, cancelledOrderId, completedOrderId;
test.beforeAll( async ( { baseURL } ) => {
const api = new wcApi( {
url: baseURL,
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET,
version: 'wc/v3',
} );
await api
.post( 'orders', {
status: 'processing',
billing: customerBilling,
} )
.then( ( response ) => {
orderId = response.data.id;
} );
} );
test.beforeEach( async ( { page } ) => {
await page.goto(
`wp-admin/tools.php?page=wpml_plugin_log&s=${ encodeURIComponent(
customerBilling.email
) }`
);
// clear out the email logs before each test
while (
await page.locator( '#bulk-action-selector-top' ).isVisible()
) {
// In WP 6.3, label intercepts check action. Need to force.
await page
.getByLabel( 'Select All' )
.first()
.check( { force: true } );
await page
.locator( '#bulk-action-selector-top' )
.selectOption( 'delete' );
await page.locator( '#doaction' ).click();
}
} );
test.afterAll( async ( { baseURL } ) => {
const api = new wcApi( {
url: baseURL,
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET,
version: 'wc/v3',
} );
await api.post( `orders/batch`, {
delete: [ orderId, newOrderId, completedOrderId, cancelledOrderId ],
} );
} );
test( 'can receive new order email', async ( { page, baseURL } ) => {
// New order emails are sent automatically when we create a simple order. Verify that we get these.
// Need to create a new order for this test because we clear logs before each run.
const api = new wcApi( {
url: baseURL,
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET,
version: 'wc/v3',
} );
await api
.post( 'orders', {
status: 'processing',
billing: customerBilling,
} )
.then( ( response ) => {
newOrderId = response.data.id;
} );
// search to narrow it down to just the messages we want
await page.goto(
`wp-admin/tools.php?page=wpml_plugin_log&s=${ encodeURIComponent(
customerBilling.email
) }`
);
await expect(
page.locator( 'td.column-receiver >> nth=1' )
).toContainText( admin.email );
await expect(
page.locator( 'td.column-subject >> nth=1' )
).toContainText( `[${ storeName }]: New order #${ newOrderId }` );
} );
test( 'can receive completed email', async ( { page, baseURL } ) => {
// Completed order emails are sent automatically when an order's payment is completed.
// Verify that the email is sent, and that the content is the expected one
const emailContent = '#wp-mail-logging-modal-content-body-content';
const emailContentJson = '#wp-mail-logging-modal-format-json';
const api = new wcApi( {
url: baseURL,
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET,
version: 'wc/v3',
} );
await api
.post( 'orders', {
status: 'completed',
billing: customerBilling,
} )
.then( ( response ) => {
completedOrderId = response.data.id;
} );
// Search to narrow it down to just the messages we want
await page.goto(
`wp-admin/tools.php?page=wpml_plugin_log&s=${ encodeURIComponent(
customerBilling.email
) }`
);
await page.selectOption( 'select[name="search[place]"]', 'subject' );
await page.fill( 'input[name="search[term]"]', 'complete' );
await page.click( 'input#search-submit' );
// Verify that the email has been sent
await expect(
page.getByText( `Your ${ storeName } order is now complete` )
).toBeVisible();
// Enter email log and select to view the content in JSON
await page.click( 'button[title^="View log"]' );
await page.locator( emailContentJson ).click();
// Verify that the message includes an order processing confirmation
await expect(
page.locator( emailContent )
).toContainText( 'We have finished processing your order.' );
// Verify that the email address is the correct one
await expect(
page.locator( emailContent )
).toContainText( customerBilling.email );
// Verify that the email contains the order ID
await expect(
page.locator( emailContent )
).toContainText( `[Order #${ completedOrderId.toString() }]` );
// Verify that the email contains a "Thanks" note
await expect(
page.locator( emailContent )
).toContainText( 'Thanks for shopping with us' );
} );
test( 'can receive cancelled order email', async ( { page, baseURL } ) => {
const api = new wcApi( {
url: baseURL,
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET,
version: 'wc/v3',
} );
await api
.post( 'orders', {
status: 'processing',
billing: customerBilling,
} )
.then( ( response ) => {
cancelledOrderId = response.data.id;
api.put( `orders/${ cancelledOrderId }`, {
status: 'cancelled',
} );
} );
await page.waitForTimeout( 1000 );
// search to narrow it down to just the messages we want
await page.goto(
`wp-admin/tools.php?page=wpml_plugin_log&s=${ encodeURIComponent(
customerBilling.email
) }`
);
await expect(
page.getByText(
`[${ storeName }]: Order #${ cancelledOrderId } has been cancelled`
)
).toBeVisible();
} );
test( 'can resend new order notification', async ( { page } ) => {
// resend the new order notification
await page.goto( `wp-admin/post.php?post=${ orderId }&action=edit` );
await page
.locator( 'li#actions > select' )
.selectOption( 'send_order_details_admin' );
await page.locator( 'button.wc-reload' ).click();
await page.waitForLoadState( 'networkidle' );
// search to narrow it down to just the messages we want
await page.goto(
`wp-admin/tools.php?page=wpml_plugin_log&s=${ encodeURIComponent(
customerBilling.email
) }`
);
// Expect row containing the admin email and the correct subject to be listed.
await expect(
page
.getByRole( 'row' )
.filter( { hasText: admin.email } )
.filter( {
hasText: `[${ storeName }]: New order #${ orderId }`,
} )
).toBeVisible();
} );
test( 'can email invoice/order details to customer', async ( { page } ) => {
// send the customer order details
await page.goto( `wp-admin/post.php?post=${ orderId }&action=edit` );
await page
.locator( 'li#actions > select' )
.selectOption( 'send_order_details' );
await page.locator( 'button.wc-reload' ).click();
await page.waitForLoadState( 'networkidle' );
// confirm the message was delivered in the logs
await page.goto(
`wp-admin/tools.php?page=wpml_plugin_log&s=${ encodeURIComponent(
customerBilling.email
) }`
);
// Expect row containing the billing email and the correct subject to be listed.
await expect(
page
.getByRole( 'row' )
.filter( { hasText: customerBilling.email } )
.filter( {
hasText: `Order #${ orderId } from ${ storeName }`,
} )
).toBeVisible();
} );
} );