@webship-js/bbpress-steps 0.0.0-stage → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +515 -2
- package/package.json +67 -4
- package/tests/features/accessibility.feature +29 -0
- package/tests/features/administration.feature +126 -0
- package/tests/features/forum-index.feature +96 -0
- package/tests/features/forum-roles.feature +88 -0
- package/tests/features/moderation.feature +223 -0
- package/tests/features/notifications.feature +43 -0
- package/tests/features/profiles.feature +71 -0
- package/tests/features/search-and-tags.feature +72 -0
- package/tests/features/subscriptions-and-favorites.feature +67 -0
- package/tests/features/topics.feature +146 -0
- package/tests/step-definitions/README.md +44 -0
- package/tests/step-definitions/bbpress-admin.steps.js +414 -0
- package/tests/step-definitions/bbpress-content.steps.js +179 -0
- package/tests/step-definitions/bbpress-defaults.js +64 -0
- package/tests/step-definitions/bbpress-forums.steps.js +488 -0
- package/tests/step-definitions/bbpress-helpers.js +668 -0
- package/tests/step-definitions/bbpress-mail.steps.js +95 -0
- package/tests/step-definitions/bbpress-moderation.steps.js +413 -0
- package/tests/step-definitions/bbpress-profile.steps.js +402 -0
- package/tests/step-definitions/bbpress-search.steps.js +235 -0
- package/tests/step-definitions/bbpress-topics.steps.js +577 -0
|
@@ -0,0 +1,668 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Shared helpers for the forums' step files.
|
|
4
|
+
//
|
|
5
|
+
// Not a step file: it registers no step definitions and no hooks. It holds the
|
|
6
|
+
// plumbing the step files share, on top of the WordPress pack's helpers (the
|
|
7
|
+
// page budget, navigation, the users registry, the login): the forums' root,
|
|
8
|
+
// what a page of the forums lists (forums, topics, posts, notices), the way to
|
|
9
|
+
// a forum and a topic, the links of a post, the session of another account for
|
|
10
|
+
// the test content, and the mail catcher of the test site.
|
|
11
|
+
//
|
|
12
|
+
// Everything the forums print sits in `#bbpress-forums`, whatever the theme:
|
|
13
|
+
// the lists are `ul` rows with an id (`bbp-forum-12`, `bbp-topic-34`), a post
|
|
14
|
+
// is a header (`#post-56`) followed by its body (`.post-56`), and the state of
|
|
15
|
+
// each is a class the forums set themselves (`status-closed`, `sticky`,
|
|
16
|
+
// `bbp-forum-visibility-private`).
|
|
17
|
+
|
|
18
|
+
const { friendly, gotoUrl } = require('webship-js/tests/step-definitions/webship');
|
|
19
|
+
const {
|
|
20
|
+
budget,
|
|
21
|
+
settle,
|
|
22
|
+
siteRoot,
|
|
23
|
+
joinUrl,
|
|
24
|
+
navigate,
|
|
25
|
+
resolveUser,
|
|
26
|
+
loginAs,
|
|
27
|
+
isLoggedIn,
|
|
28
|
+
supplied,
|
|
29
|
+
} = require('@webship-js/wordpress-steps/tests/step-definitions/wordpress-helpers');
|
|
30
|
+
|
|
31
|
+
/** Where the forums print everything. */
|
|
32
|
+
const FORUMS = '#bbpress-forums';
|
|
33
|
+
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
// Text
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
/** White space collapsed, typographic dashes and quotes made plain, trimmed. */
|
|
39
|
+
function clean(text) {
|
|
40
|
+
return String(text === null || text === undefined ? '' : text)
|
|
41
|
+
.replace(/[‒-―−]/g, '-')
|
|
42
|
+
.replace(/[“”„‟]/g, '"')
|
|
43
|
+
.replace(/[‘’]/g, "'")
|
|
44
|
+
.replace(/ /g, ' ')
|
|
45
|
+
.replace(/\s+/g, ' ')
|
|
46
|
+
.trim();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A list for a failure message: "a", "b" and "c", or (none). */
|
|
50
|
+
function listed(items) {
|
|
51
|
+
const all = (items || []).map((i) => `"${clean(i)}"`);
|
|
52
|
+
if (!all.length) return '(none)';
|
|
53
|
+
return all.length > 12 ? `${all.slice(0, 12).join(', ')} and ${all.length - 12} more` : all.join(', ');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The same text for a person: letter case, white space, dashes and quotes aside. */
|
|
57
|
+
function same(a, b) {
|
|
58
|
+
return clean(a).toLowerCase() === clean(b).toLowerCase();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Whether a text holds another, letter case, white space, dashes and quotes aside. */
|
|
62
|
+
function holds(text, part) {
|
|
63
|
+
return clean(text).toLowerCase().includes(clean(part).toLowerCase());
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The names of a step argument such as `"A", "B" and "C"`. */
|
|
67
|
+
function quotedList(text) {
|
|
68
|
+
return (String(text).match(/"[^"]*"/g) || []).map((q) => q.slice(1, -1));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// ---------------------------------------------------------------------------
|
|
72
|
+
// Waiting
|
|
73
|
+
// ---------------------------------------------------------------------------
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Read something of the page again and again until `ok` holds for it, or the
|
|
77
|
+
* wait budget (twice the page budget) is spent; then fail with the message
|
|
78
|
+
* `fail` builds from the last reading.
|
|
79
|
+
*/
|
|
80
|
+
async function eventually(world, read, ok, fail, timeout) {
|
|
81
|
+
const deadline = Date.now() + (timeout || budget(world) * 2);
|
|
82
|
+
let value;
|
|
83
|
+
for (;;) {
|
|
84
|
+
try {
|
|
85
|
+
value = await read();
|
|
86
|
+
} catch (err) {
|
|
87
|
+
value = undefined;
|
|
88
|
+
if (Date.now() > deadline) throw err;
|
|
89
|
+
}
|
|
90
|
+
if (value !== undefined && ok(value)) return value;
|
|
91
|
+
if (Date.now() > deadline) break;
|
|
92
|
+
await world.page.waitForTimeout(200);
|
|
93
|
+
}
|
|
94
|
+
throw friendly(fail(value));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Click a link or a button that loads a page (a form of the forums, a
|
|
99
|
+
* moderation link) and wait for that page.
|
|
100
|
+
*/
|
|
101
|
+
async function clickAndLoad(world, locator, what, quiet) {
|
|
102
|
+
try {
|
|
103
|
+
await Promise.all([
|
|
104
|
+
world.page.waitForNavigation({ waitUntil: 'domcontentloaded', timeout: 30000 }),
|
|
105
|
+
locator.click(),
|
|
106
|
+
]);
|
|
107
|
+
} catch (err) {
|
|
108
|
+
throw friendly({
|
|
109
|
+
message: `Could not ${what}: the forums did not answer with a page.`,
|
|
110
|
+
cause: err,
|
|
111
|
+
hint: `The browser is at ${world.page.url()}.`,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
// A screen of the administration keeps timers running (its heartbeat): its page is whole when it arrives.
|
|
115
|
+
if (!quiet) await settle(world);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ---------------------------------------------------------------------------
|
|
119
|
+
// The forums' root and the page that is open
|
|
120
|
+
// ---------------------------------------------------------------------------
|
|
121
|
+
|
|
122
|
+
/** The address the forums start at: `worldParameters.forums.root`, "/forums/" as the plugin sets it. */
|
|
123
|
+
function forumsRoot(world) {
|
|
124
|
+
const own = (world.parameters && world.parameters.forums && world.parameters.forums.root) || '/forums/';
|
|
125
|
+
return '/' + String(own).replace(/^\/+|\/+$/g, '') + '/';
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The handle of the account that prepares and removes test content: `worldParameters.forums.moderator`. */
|
|
129
|
+
function contentModerator(world) {
|
|
130
|
+
return (world.parameters && world.parameters.forums && world.parameters.forums.moderator) || 'moderator';
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Open an address of the site and say what the site answered: the status of
|
|
135
|
+
* the page the browser ended on (after its redirects), 0 when none was read.
|
|
136
|
+
*/
|
|
137
|
+
async function visit(world, url) {
|
|
138
|
+
let status = 0;
|
|
139
|
+
const onResponse = (response) => {
|
|
140
|
+
const request = response.request();
|
|
141
|
+
if (request.isNavigationRequest() && request.frame() === world.page.mainFrame()) status = response.status();
|
|
142
|
+
};
|
|
143
|
+
world.page.on('response', onResponse);
|
|
144
|
+
try {
|
|
145
|
+
await navigate(world, url);
|
|
146
|
+
} finally {
|
|
147
|
+
world.page.off('response', onResponse);
|
|
148
|
+
}
|
|
149
|
+
return status;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Open a screen of the site's administration and say what the site answered.
|
|
154
|
+
* The screens the steps read (the lists, the tools, the users) are rendered
|
|
155
|
+
* on the server: they are whole when the document arrives, so the step does
|
|
156
|
+
* not wait for the page to go quiet, which a screen of the administration
|
|
157
|
+
* never does (its heartbeat keeps a timer running).
|
|
158
|
+
*/
|
|
159
|
+
async function visitAdmin(world, url) {
|
|
160
|
+
let status = 0;
|
|
161
|
+
const onResponse = (response) => {
|
|
162
|
+
const request = response.request();
|
|
163
|
+
if (request.isNavigationRequest() && request.frame() === world.page.mainFrame()) status = response.status();
|
|
164
|
+
};
|
|
165
|
+
world.page.on('response', onResponse);
|
|
166
|
+
try {
|
|
167
|
+
await gotoUrl(world.page, joinUrl(world.launchUrl, url));
|
|
168
|
+
await world.page.waitForSelector('body', { state: 'attached', timeout: 30000 });
|
|
169
|
+
await world.page.waitForLoadState('domcontentloaded');
|
|
170
|
+
} finally {
|
|
171
|
+
world.page.off('response', onResponse);
|
|
172
|
+
}
|
|
173
|
+
return status;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Open the forum index. */
|
|
177
|
+
async function gotoForumIndex(world) {
|
|
178
|
+
await navigate(world, forumsRoot(world));
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* What kind of page of the forums the browser shows, and its heading: the
|
|
183
|
+
* index, a forum, a topic, a profile, the search, a tag, a view, the edit form
|
|
184
|
+
* of a post, a moderation form (merge, split, move), or none of them.
|
|
185
|
+
*/
|
|
186
|
+
async function forumPage(world) {
|
|
187
|
+
return world.page.evaluate((sel) => {
|
|
188
|
+
const box = document.querySelector(sel);
|
|
189
|
+
const body = document.body.classList;
|
|
190
|
+
const heading = [...document.querySelectorAll('h1')].find((h) => h.getClientRects().length && !h.closest('#wp-link-wrap, #wpadminbar'));
|
|
191
|
+
let kind = 'none';
|
|
192
|
+
if (box) {
|
|
193
|
+
if (box.querySelector('#merge_topic, #split_topic, #move_reply')) kind = 'form';
|
|
194
|
+
else if (body.contains('topic-edit') || body.contains('reply-edit')) kind = 'edit';
|
|
195
|
+
else if (body.contains('bbp-user-page') || box.querySelector('#bbp-user-wrapper')) kind = 'profile';
|
|
196
|
+
else if (body.contains('bbp-search') || box.querySelector('#bbp-search-results, .bbp-breadcrumb-search')) kind = 'search';
|
|
197
|
+
else if (body.contains('topic-tag') || body.contains('bbp-topic-tag')) kind = 'tag';
|
|
198
|
+
else if (body.contains('bbp-view')) kind = 'view';
|
|
199
|
+
else if (body.contains('single-topic') || body.contains('topic-edit') || box.querySelector('ul.bbp-replies')) kind = 'topic';
|
|
200
|
+
else if (body.contains('single-forum')) kind = 'forum';
|
|
201
|
+
else if (body.contains('forum-archive')) kind = 'index';
|
|
202
|
+
else kind = 'other';
|
|
203
|
+
}
|
|
204
|
+
return { kind, heading: heading ? heading.textContent.replace(/\s+/g, ' ').trim() : '', url: location.href };
|
|
205
|
+
}, FORUMS).catch(() => ({ kind: 'none', heading: '', url: world.page.url() }));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** The notices the forums show on the page: `{ kind, text }` per line; kind is "info", "error" or "". */
|
|
209
|
+
async function forumNotices(world) {
|
|
210
|
+
return world.page.evaluate((sel) => {
|
|
211
|
+
const out = [];
|
|
212
|
+
for (const notice of document.querySelectorAll(`${sel} .bbp-template-notice`)) {
|
|
213
|
+
if (!notice.getClientRects().length) continue;
|
|
214
|
+
const kind = notice.classList.contains('error') ? 'error' : (notice.classList.contains('info') ? 'info' : '');
|
|
215
|
+
const lines = notice.querySelectorAll('li');
|
|
216
|
+
for (const line of (lines.length ? lines : [notice])) {
|
|
217
|
+
const text = line.textContent.replace(/\s+/g, ' ').trim();
|
|
218
|
+
if (text) out.push({ kind, text });
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return out;
|
|
222
|
+
}, FORUMS).catch(() => []);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** The total a list of the forums counts ("Viewing 4 topics - 1 through 4 (of 4 total)"), or null without one. */
|
|
226
|
+
async function listTotal(world) {
|
|
227
|
+
return world.page.evaluate((sel) => {
|
|
228
|
+
const count = document.querySelector(`${sel} .bbp-pagination-count`);
|
|
229
|
+
if (!count) return null;
|
|
230
|
+
const m = count.textContent.match(/\(of\s+([\d,.]+)\s+total\)/i);
|
|
231
|
+
return { text: count.textContent.replace(/\s+/g, ' ').trim(), total: m ? Number(m[1].replace(/[,.]/g, '')) : null };
|
|
232
|
+
}, FORUMS).catch(() => null);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// ---------------------------------------------------------------------------
|
|
236
|
+
// Forums
|
|
237
|
+
// ---------------------------------------------------------------------------
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* The forums the page lists (the index, a category, a forum with subforums,
|
|
241
|
+
* the subscribed forums of a profile), as a person reads them: the name
|
|
242
|
+
* (without the "Private: " the site puts before a private forum), the
|
|
243
|
+
* counters, whether it is a category, closed, private or hidden, and the
|
|
244
|
+
* subforums listed under it.
|
|
245
|
+
*/
|
|
246
|
+
async function listedForums(world) {
|
|
247
|
+
return world.page.evaluate((sel) => {
|
|
248
|
+
const bare = (text) => text.replace(/\s+/g, ' ').trim().replace(/^(?:(?:Private|Hidden|Protected):\s+)+/i, '');
|
|
249
|
+
const number = (el) => (el ? Number(el.textContent.replace(/[^\d]/g, '')) : null);
|
|
250
|
+
const out = [];
|
|
251
|
+
for (const row of document.querySelectorAll(`${sel} ul[id^="bbp-forum-"]`)) {
|
|
252
|
+
if (!row.closest('li.bbp-body')) continue;
|
|
253
|
+
const title = row.querySelector('a.bbp-forum-title');
|
|
254
|
+
if (!title) continue;
|
|
255
|
+
const visibility = ([...row.classList].find((c) => c.startsWith('bbp-forum-visibility-')) || '').replace('bbp-forum-visibility-', '');
|
|
256
|
+
out.push({
|
|
257
|
+
id: row.id.replace('bbp-forum-', ''),
|
|
258
|
+
name: bare(title.textContent),
|
|
259
|
+
href: title.href,
|
|
260
|
+
topics: number(row.querySelector('.bbp-forum-topic-count')),
|
|
261
|
+
posts: number(row.querySelector('.bbp-forum-reply-count')),
|
|
262
|
+
category: row.classList.contains('status-category'),
|
|
263
|
+
closed: row.classList.contains('bbp-forum-status-closed'),
|
|
264
|
+
private: visibility === 'private',
|
|
265
|
+
hidden: visibility === 'hidden',
|
|
266
|
+
subscribed: Boolean(row.querySelector('.is-subscribed')),
|
|
267
|
+
remove: (row.querySelector('.bbp-row-actions a') || {}).href || '',
|
|
268
|
+
subforums: [...row.querySelectorAll('.bbp-forums-list a.bbp-forum-link')].map((a) => ({
|
|
269
|
+
name: bare(a.textContent.replace(/\s*\([\d,\s]+\)\s*$/, '')),
|
|
270
|
+
href: a.href,
|
|
271
|
+
})),
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
return out;
|
|
275
|
+
}, FORUMS).catch(() => []);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Every forum name the page shows: the rows and the subforums under them. */
|
|
279
|
+
function forumNames(forums) {
|
|
280
|
+
const names = [];
|
|
281
|
+
for (const forum of forums || []) {
|
|
282
|
+
names.push(forum.name);
|
|
283
|
+
for (const sub of forum.subforums) names.push(sub.name);
|
|
284
|
+
}
|
|
285
|
+
return names;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** The address of a forum the page lists, by its name: a row or a subforum under one. */
|
|
289
|
+
function forumAddress(forums, name) {
|
|
290
|
+
for (const forum of forums || []) {
|
|
291
|
+
if (same(forum.name, name)) return forum.href;
|
|
292
|
+
const sub = forum.subforums.find((s) => same(s.name, name));
|
|
293
|
+
if (sub) return sub.href;
|
|
294
|
+
}
|
|
295
|
+
return '';
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Open a forum by its name, as a person does: from the page that is open when
|
|
300
|
+
* it lists the forum, else from the forum index (its forums and the subforums
|
|
301
|
+
* under them), else through the category that holds it.
|
|
302
|
+
*/
|
|
303
|
+
async function openForum(world, name) {
|
|
304
|
+
let href = forumAddress(await listedForums(world), name);
|
|
305
|
+
let seen = [];
|
|
306
|
+
if (!href) {
|
|
307
|
+
await gotoForumIndex(world);
|
|
308
|
+
const index = await listedForums(world);
|
|
309
|
+
seen = forumNames(index);
|
|
310
|
+
href = forumAddress(index, name);
|
|
311
|
+
// A forum deeper than the index shows: look in each forum that has subforums.
|
|
312
|
+
for (const parent of (href ? [] : index.filter((f) => f.subforums.length))) {
|
|
313
|
+
await navigate(world, parent.href);
|
|
314
|
+
const inside = await listedForums(world);
|
|
315
|
+
seen = seen.concat(forumNames(inside));
|
|
316
|
+
href = forumAddress(inside, name);
|
|
317
|
+
if (href) break;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (!href) {
|
|
321
|
+
throw friendly({
|
|
322
|
+
message: `The forums list no forum "${name}". They list: ${listed([...new Set(seen)])}.`,
|
|
323
|
+
hint: 'A forum is found by the name the forum index shows. A private forum is listed for a logged-in participant, a hidden one for a moderator; a blocked account gets no forum at all.',
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
await navigate(world, href);
|
|
327
|
+
const page = await forumPage(world);
|
|
328
|
+
if (!same(page.heading.replace(/^(?:(?:Private|Hidden|Protected):\s+)+/i, ''), name)) {
|
|
329
|
+
throw friendly({
|
|
330
|
+
message: `The forum "${name}" did not open: the page is "${page.heading}" (${page.url}).`,
|
|
331
|
+
hint: 'The account may not be allowed to read this forum.',
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// ---------------------------------------------------------------------------
|
|
337
|
+
// Topics
|
|
338
|
+
// ---------------------------------------------------------------------------
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* The topics the page lists (a forum, a tag, a view, the lists of a profile),
|
|
342
|
+
* as a person reads them: title, author, voices, posts, whether the topic is
|
|
343
|
+
* sticky, its status, its tags, the forum it is in where the list names it.
|
|
344
|
+
*/
|
|
345
|
+
async function listedTopics(world) {
|
|
346
|
+
return world.page.evaluate((sel) => {
|
|
347
|
+
const number = (el) => (el ? Number(el.textContent.replace(/[^\d]/g, '')) : null);
|
|
348
|
+
const text = (el) => (el ? el.textContent.replace(/\s+/g, ' ').trim() : '');
|
|
349
|
+
const out = [];
|
|
350
|
+
for (const row of document.querySelectorAll(`${sel} ul[id^="bbp-topic-"]`)) {
|
|
351
|
+
if (!row.closest('li.bbp-body')) continue;
|
|
352
|
+
const title = row.querySelector('a.bbp-topic-permalink');
|
|
353
|
+
if (!title) continue;
|
|
354
|
+
out.push({
|
|
355
|
+
id: row.id.replace('bbp-topic-', ''),
|
|
356
|
+
title: text(title),
|
|
357
|
+
href: title.href,
|
|
358
|
+
author: text(row.querySelector('.bbp-topic-started-by .bbp-author-name')),
|
|
359
|
+
forum: text(row.querySelector('.bbp-topic-started-in a')),
|
|
360
|
+
voices: number(row.querySelector('.bbp-topic-voice-count')),
|
|
361
|
+
posts: number(row.querySelector('.bbp-topic-reply-count')),
|
|
362
|
+
sticky: row.classList.contains('sticky') || row.classList.contains('super-sticky'),
|
|
363
|
+
superSticky: row.classList.contains('super-sticky'),
|
|
364
|
+
status: ([...row.classList].find((c) => c.startsWith('status-')) || '').replace('status-', ''),
|
|
365
|
+
tags: [...row.classList].filter((c) => c.startsWith('topic-tag-')).map((c) => c.replace('topic-tag-', '')),
|
|
366
|
+
remove: (row.querySelector('.bbp-row-actions a') || {}).href || '',
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
return out;
|
|
370
|
+
}, FORUMS).catch(() => []);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** The address of the next page of a list of the forums, or an empty string on the last one. */
|
|
374
|
+
async function nextPageAddress(world) {
|
|
375
|
+
return world.page.evaluate((sel) => {
|
|
376
|
+
const next = document.querySelector(`${sel} .bbp-pagination-links a.next`);
|
|
377
|
+
return next ? next.href : '';
|
|
378
|
+
}, FORUMS).catch(() => '');
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** A topic of the list on the page by its title, looking through the pages of the list; null when it lists none. */
|
|
382
|
+
async function findListedTopic(world, title) {
|
|
383
|
+
const seen = [];
|
|
384
|
+
for (let page = 0; page < 20; page += 1) {
|
|
385
|
+
const topics = await listedTopics(world);
|
|
386
|
+
const found = topics.find((t) => same(t.title, title));
|
|
387
|
+
if (found) return { topic: found, seen };
|
|
388
|
+
seen.push(...topics.map((t) => t.title));
|
|
389
|
+
const next = await nextPageAddress(world);
|
|
390
|
+
if (!next) break;
|
|
391
|
+
await navigate(world, next);
|
|
392
|
+
}
|
|
393
|
+
return { topic: null, seen };
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Open a topic by its title: in a forum (by the forum's name, through the
|
|
398
|
+
* pages of its list), or without one from the list on the page, else through
|
|
399
|
+
* the forum search.
|
|
400
|
+
*/
|
|
401
|
+
async function openTopic(world, title, forumName) {
|
|
402
|
+
let href = '';
|
|
403
|
+
if (supplied(forumName)) {
|
|
404
|
+
await openForum(world, forumName);
|
|
405
|
+
const { topic, seen } = await findListedTopic(world, title);
|
|
406
|
+
if (!topic) {
|
|
407
|
+
throw friendly({
|
|
408
|
+
message: `The forum "${forumName}" lists no topic "${title}". It lists: ${listed(seen)}.`,
|
|
409
|
+
hint: 'A topic is found by its title in the forum\'s list. A pending, spammed or trashed topic is left out of it, for a moderator too until it asks for them: When I show the hidden topics.',
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
href = topic.href;
|
|
413
|
+
} else {
|
|
414
|
+
const known = (world.forumTopics || []).find((t) => same(t.title, title));
|
|
415
|
+
const here = (await listedTopics(world)).find((t) => same(t.title, title));
|
|
416
|
+
if (here) href = here.href;
|
|
417
|
+
else if (known) href = known.url;
|
|
418
|
+
else {
|
|
419
|
+
await navigate(world, `${forumsRoot(world)}search/${encodeURIComponent(title)}/`);
|
|
420
|
+
href = await world.page.evaluate(([sel, wanted]) => {
|
|
421
|
+
const norm = (t) => t.replace(/\s+/g, ' ').trim().toLowerCase();
|
|
422
|
+
const link = [...document.querySelectorAll(`${sel} .bbp-topic-header .bbp-topic-title a, ${sel} a.bbp-topic-permalink`)]
|
|
423
|
+
.find((a) => norm(a.textContent) === norm(wanted));
|
|
424
|
+
return link ? link.href : '';
|
|
425
|
+
}, [FORUMS, title]).catch(() => '');
|
|
426
|
+
if (!href) {
|
|
427
|
+
throw friendly({
|
|
428
|
+
message: `The forum search finds no topic "${title}" for this account.`,
|
|
429
|
+
hint: 'Name the forum: When I open the topic "…" in "…". Without one the topic is looked up in the list on the page, then through the forum search, which the settings of the forums can turn off.',
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
await navigate(world, href);
|
|
435
|
+
await readingTopic(world, title);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/** Assert the page is a topic, with a title when one is given; returns the page. */
|
|
439
|
+
async function readingTopic(world, title) {
|
|
440
|
+
const page = await forumPage(world);
|
|
441
|
+
if (page.kind !== 'topic') {
|
|
442
|
+
throw friendly({
|
|
443
|
+
message: `The page is not a topic: it is ${page.heading ? `"${page.heading}"` : 'no page of the forums'} (${page.url}).`,
|
|
444
|
+
hint: 'Open one first: When I open the topic "…" in "…".',
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
if (supplied(title) && !same(page.heading, title)) {
|
|
448
|
+
throw friendly({
|
|
449
|
+
message: `The page is not the topic "${title}": it is the topic "${page.heading}".`,
|
|
450
|
+
hint: `The browser is at ${page.url}.`,
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
return page;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// ---------------------------------------------------------------------------
|
|
457
|
+
// Posts
|
|
458
|
+
// ---------------------------------------------------------------------------
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* The posts of the topic page, in order: the lead post of the topic, then
|
|
462
|
+
* the replies of this page. Each with its author, the author's forum role,
|
|
463
|
+
* its text (the edit log aside), its HTML, the lines of its edit log, its
|
|
464
|
+
* status and the links the forums offer on it.
|
|
465
|
+
*/
|
|
466
|
+
async function topicPosts(world) {
|
|
467
|
+
return world.page.evaluate((sel) => {
|
|
468
|
+
const text = (el) => (el ? el.innerText.replace(/\s+/g, ' ').trim() : '');
|
|
469
|
+
const out = [];
|
|
470
|
+
for (const header of document.querySelectorAll(`${sel} ul.bbp-replies div.bbp-reply-header[id^="post-"]`)) {
|
|
471
|
+
const id = header.id.replace('post-', '');
|
|
472
|
+
const body = header.parentElement.querySelector(`div.post-${id}`);
|
|
473
|
+
if (!body) continue;
|
|
474
|
+
const content = body.querySelector('.bbp-reply-content');
|
|
475
|
+
const copy = content ? content.cloneNode(true) : null;
|
|
476
|
+
const log = copy ? [...copy.querySelectorAll('.bbp-topic-revision-log li, .bbp-reply-revision-log li')].map((li) => li.textContent.replace(/\s+/g, ' ').trim()) : [];
|
|
477
|
+
if (copy) copy.querySelectorAll('.bbp-topic-revision-log, .bbp-reply-revision-log').forEach((n) => n.remove());
|
|
478
|
+
out.push({
|
|
479
|
+
id,
|
|
480
|
+
lead: body.classList.contains('type-topic'),
|
|
481
|
+
status: ([...body.classList].find((c) => c.startsWith('status-')) || '').replace('status-', ''),
|
|
482
|
+
author: text(body.querySelector('.bbp-reply-author .bbp-author-name')),
|
|
483
|
+
role: text(body.querySelector('.bbp-reply-author .bbp-author-role')),
|
|
484
|
+
text: copy ? copy.textContent.replace(/\s+/g, ' ').trim() : '',
|
|
485
|
+
html: copy ? copy.innerHTML : '',
|
|
486
|
+
log,
|
|
487
|
+
links: [...header.querySelectorAll('.bbp-admin-links a')].map((a) => ({
|
|
488
|
+
text: a.textContent.replace(/\s+/g, ' ').trim(),
|
|
489
|
+
name: [...a.classList].find((c) => /^bbp-(topic|reply)-/.test(c)) || '',
|
|
490
|
+
href: a.href,
|
|
491
|
+
confirms: a.hasAttribute('onclick'),
|
|
492
|
+
})),
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
return out;
|
|
496
|
+
}, FORUMS).catch(() => []);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* A post of the topic page by its number on the page (1 is the first), or
|
|
501
|
+
* the last one when no number is given.
|
|
502
|
+
*/
|
|
503
|
+
async function postOnPage(world, number) {
|
|
504
|
+
await readingTopic(world);
|
|
505
|
+
const posts = await topicPosts(world);
|
|
506
|
+
if (!posts.length) {
|
|
507
|
+
throw friendly({ message: `The topic shows no post (${world.page.url()}).`, hint: 'A blocked account, or a visitor of a private forum, gets no post.' });
|
|
508
|
+
}
|
|
509
|
+
if (!supplied(number)) return { post: posts[posts.length - 1], posts, label: 'The last post' };
|
|
510
|
+
const index = Number(number);
|
|
511
|
+
if (index < 1 || index > posts.length) {
|
|
512
|
+
throw friendly({
|
|
513
|
+
message: `The page has no post ${index}: it shows ${posts.length} post(s).`,
|
|
514
|
+
hint: 'Posts are counted on the page that is open, the topic\'s own post first; a topic shows 15 posts a page.',
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
return { post: posts[index - 1], posts, label: `Post ${index}` };
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Follow a link the forums offer on a post (Close, Stick, Trash, Spam,
|
|
522
|
+
* Approve, …) by the class the forums give it; a link that asks "are you
|
|
523
|
+
* sure" gets its yes.
|
|
524
|
+
*/
|
|
525
|
+
async function followPostLink(world, post, names, what) {
|
|
526
|
+
const link = post.links.find((l) => names.includes(l.name));
|
|
527
|
+
if (!link) {
|
|
528
|
+
throw friendly({
|
|
529
|
+
message: `Could not ${what}: the post offers no such link. It offers: ${listed(post.links.map((l) => l.text))}.`,
|
|
530
|
+
hint: 'The moderation links are offered to a moderator and a keymaster; a participant gets Edit on its own posts and Reply.',
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
if (link.confirms) world.page.once('dialog', (dialog) => dialog.accept().catch(() => {}));
|
|
534
|
+
await clickAndLoad(world, world.page.locator(`#post-${post.id} .bbp-admin-links a.${link.name}`).first(), what);
|
|
535
|
+
return link;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// ---------------------------------------------------------------------------
|
|
539
|
+
// Another account, for the test content
|
|
540
|
+
// ---------------------------------------------------------------------------
|
|
541
|
+
|
|
542
|
+
// The session of each account that logged in for the test content, by site and handle: the next Given of
|
|
543
|
+
// the run takes it over instead of filling in the login form again.
|
|
544
|
+
const SESSIONS = new Map();
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* Do something as another account, then give the scenario's own session
|
|
548
|
+
* back. The account logs in through the login page once in a run; later calls
|
|
549
|
+
* take its session over when the site still knows it.
|
|
550
|
+
*/
|
|
551
|
+
async function asAccount(world, handle, work) {
|
|
552
|
+
resolveUser(world, handle);
|
|
553
|
+
const key = `${siteRoot(world.launchUrl)}|${handle}`;
|
|
554
|
+
const before = await world.context.cookies();
|
|
555
|
+
try { await world.page.goto('about:blank'); } catch { /* nothing open yet */ }
|
|
556
|
+
await world.context.clearCookies();
|
|
557
|
+
let ready = false;
|
|
558
|
+
if (SESSIONS.has(key)) {
|
|
559
|
+
await world.context.addCookies(SESSIONS.get(key));
|
|
560
|
+
ready = await isLoggedIn(world).catch(() => false);
|
|
561
|
+
if (!ready) await world.context.clearCookies();
|
|
562
|
+
}
|
|
563
|
+
if (!ready) {
|
|
564
|
+
await loginAs(world, handle);
|
|
565
|
+
SESSIONS.set(key, await world.context.cookies());
|
|
566
|
+
}
|
|
567
|
+
try {
|
|
568
|
+
return await work();
|
|
569
|
+
} finally {
|
|
570
|
+
try { await world.page.goto('about:blank'); } catch { /* the page is gone */ }
|
|
571
|
+
await world.context.clearCookies();
|
|
572
|
+
if (before.length) await world.context.addCookies(before);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// ---------------------------------------------------------------------------
|
|
577
|
+
// The mail catcher
|
|
578
|
+
// ---------------------------------------------------------------------------
|
|
579
|
+
|
|
580
|
+
/** The address of a handle of the users registry, or the address itself. */
|
|
581
|
+
function addressOf(world, who) {
|
|
582
|
+
if (/@/.test(who)) return who;
|
|
583
|
+
const users = (world.parameters && world.parameters.users) || {};
|
|
584
|
+
const entry = users[who];
|
|
585
|
+
if (!entry || !entry.email) {
|
|
586
|
+
throw friendly({
|
|
587
|
+
message: `"${who}" has no e-mail address in the users registry.`,
|
|
588
|
+
hint: `Give worldParameters.users.${who} an "email" in cucumber.js, or write the address itself in the step.`,
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
return entry.email;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/** The mail catcher's API address: `worldParameters.mailUrl`, else port 8026 of the site (a DDEV project's Mailpit). */
|
|
595
|
+
function mailRoot(world) {
|
|
596
|
+
const own = world.parameters && world.parameters.mailUrl;
|
|
597
|
+
return String(own || `${siteRoot(world.launchUrl)}:8026`).replace(/\/+$/, '');
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* The mail that arrived since the scenario began, to an address in any of
|
|
602
|
+
* To, Cc and Bcc (the forums send one mail to a no-reply address with the
|
|
603
|
+
* subscribers in Bcc), newest first.
|
|
604
|
+
*/
|
|
605
|
+
async function mailTo(world, address) {
|
|
606
|
+
const url = `${mailRoot(world)}/api/v1/messages?limit=200`;
|
|
607
|
+
let response;
|
|
608
|
+
try {
|
|
609
|
+
response = await world.context.request.get(url, { failOnStatusCode: false, ignoreHTTPSErrors: true });
|
|
610
|
+
} catch (err) {
|
|
611
|
+
throw friendly({ message: `Could not read the mail catcher at ${url}.`, cause: err, hint: 'Check worldParameters.mailUrl in cucumber.js: the API of Mailpit, port 8026 of a DDEV project.' });
|
|
612
|
+
}
|
|
613
|
+
if (response.status() !== 200) {
|
|
614
|
+
throw friendly({ message: `The mail catcher at ${url} answered ${response.status()}.`, hint: 'Check worldParameters.mailUrl in cucumber.js: the API of Mailpit, port 8026 of a DDEV project.' });
|
|
615
|
+
}
|
|
616
|
+
const since = (world.forumScenarioStartedAt || 0) - 2000;
|
|
617
|
+
const wanted = address.toLowerCase();
|
|
618
|
+
const body = await response.json();
|
|
619
|
+
return (body.messages || []).filter((m) => Date.parse(m.Created) >= since
|
|
620
|
+
&& [].concat(m.To || [], m.Cc || [], m.Bcc || []).some((a) => String(a.Address || '').toLowerCase() === wanted));
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/** The text of a mail of the mail catcher: its text version, else its HTML without the markup. */
|
|
624
|
+
async function mailText(world, id) {
|
|
625
|
+
const response = await world.context.request.get(`${mailRoot(world)}/api/v1/message/${id}`, { failOnStatusCode: false, ignoreHTTPSErrors: true });
|
|
626
|
+
const body = await response.json();
|
|
627
|
+
return clean(body.Text || String(body.HTML || '').replace(/<[^>]*>/g, ' '));
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
module.exports = {
|
|
631
|
+
FORUMS,
|
|
632
|
+
budget,
|
|
633
|
+
settle,
|
|
634
|
+
navigate,
|
|
635
|
+
visit,
|
|
636
|
+
visitAdmin,
|
|
637
|
+
joinUrl,
|
|
638
|
+
supplied,
|
|
639
|
+
clean,
|
|
640
|
+
listed,
|
|
641
|
+
same,
|
|
642
|
+
holds,
|
|
643
|
+
quotedList,
|
|
644
|
+
eventually,
|
|
645
|
+
clickAndLoad,
|
|
646
|
+
forumsRoot,
|
|
647
|
+
contentModerator,
|
|
648
|
+
gotoForumIndex,
|
|
649
|
+
forumPage,
|
|
650
|
+
forumNotices,
|
|
651
|
+
listTotal,
|
|
652
|
+
listedForums,
|
|
653
|
+
forumNames,
|
|
654
|
+
forumAddress,
|
|
655
|
+
openForum,
|
|
656
|
+
listedTopics,
|
|
657
|
+
nextPageAddress,
|
|
658
|
+
findListedTopic,
|
|
659
|
+
openTopic,
|
|
660
|
+
readingTopic,
|
|
661
|
+
topicPosts,
|
|
662
|
+
postOnPage,
|
|
663
|
+
followPostLink,
|
|
664
|
+
asAccount,
|
|
665
|
+
addressOf,
|
|
666
|
+
mailTo,
|
|
667
|
+
mailText,
|
|
668
|
+
};
|