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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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import { Page } from 'playwright';
import envVariables from '../../env-variables';
import { EditorComponent } from './editor-component';
import type { ArticlePublishSchedule, EditorSidebarTab, ArticlePrivacyOptions } from './types';

const panel = '[aria-label="Editor settings"]';

const selectors = {
	section: ( name: string ) =>
		`${ panel } .components-panel__body-title button:has-text("${ name }")`,

	// Revisions (after 18.7.0)
	showRevisionButton: '.editor-private-post-last-revision__button',

	// Status & Visibility
	visibilityButton: '.edit-post-post-visibility__toggle',
	visibilityPopover: 'fieldset.editor-post-visibility__dialog-fieldset',
	visibilityOption: ( option: ArticlePrivacyOptions ) => `input[value="${ option.toLowerCase() }"]`,
	postPasswordInput: '.editor-post-visibility__password-input',

	// Schedule
	scheduleButton: 'button.editor-post-schedule__dialog-toggle',
	schedulePopoverCloseButton:
		'[data-wp-component="Popover"][aria-label="Change publish date"] [aria-label="Close"]',
	scheduleInput: ( name: string ) => `.editor-post-schedule__dialog label:has-text("${ name }")`,
	scheduleMeridianButton: ( meridian: 'AM' | 'PM' ) =>
		`button[role=radio]:has-text("${ meridian }")`,
	// Category
	categoryCheckbox: ( categoryName: string ) =>
		`${ panel } div[aria-label=Categories] label:text("${ categoryName }")`,

	// Add tag.
	// String was changed for WP 6.8, so we need both for a bit: https://core.trac.wordpress.org/changeset/59784
	tagInput: `${ panel } .components-form-token-field:is(:has-text("Add New Tag"),:has-text("Add Tag")) input`,
	addedTag: ( tag: string ) =>
		`${ panel } .components-form-token-field__token-text:has-text("${ tag }")`,
};

/**
 * Represents an instance of the WordPress.com Editor's Settings sidebar.
 */
export class EditorSettingsSidebarComponent {
	private page: Page;
	private editor: EditorComponent;

	/**
	 * Constructs an instance of the component.
	 *
	 * @param {Page} page The underlying page.
	 * @param {EditorComponent} editor The EditorComponent instance.
	 */
	constructor( page: Page, editor: EditorComponent ) {
		this.page = page;
		this.editor = editor;
	}

	//#region Generic methods

	/**
	 * Returns the root element of the sidebar.
	 */
	async getRoot() {
		const editorParent = await this.editor.parent();

		return editorParent.getByRole( 'region', { name: 'Editor settings' } );
	}

	/**
	 * Returns the root element (panel body) of the section.
	 */
	async getSection( name: string ) {
		const sidebar = await this.getRoot();

		return sidebar.locator( '.components-panel__body' ).filter( {
			has: this.page.locator( `h2.components-panel__body-title button:has-text("${ name }")` ),
		} );
	}

	/**
	 * Clicks a button matching the accessible name.
	 *
	 * @param {string} name Accessible name of the button.
	 */
	async clickButton( name: string ): Promise< void > {
		const editorParent = await this.editor.parent();

		await editorParent.getByRole( 'button', { name: name } ).click();
	}

	/**
	 * Enters the specified text to an input field, specified by a label.
	 *
	 * In the future, this method may support other methods of locating a
	 * text box.
	 *
	 * @param {string} text Text to enter.
	 * @param param1 Keyed object parameter.
	 * @param {string} param1.label Locate text field by label.
	 */
	async enterText( text: string, { label }: { label: string } ): Promise< void > {
		const editorParent = await this.editor.parent();

		if ( label ) {
			return await editorParent.getByRole( 'textbox', { name: label } ).fill( text );
		}

		throw new Error( 'Must specify a method to locate the text field.' );
	}

	//#endregion

	/**
	 * Closes the sidebar for mobile viewport.
	 *
	 * This method can close both the post/page settings as well as the Jetpack
	 * sidebar.
	 */
	async closeSidebarForMobile(): Promise< void > {
		if ( envVariables.VIEWPORT_NAME !== 'mobile' ) {
			return;
		}
		const editorParent = await this.editor.parent();
		await editorParent.getByRole( 'button', { name: /Close Settings|Close plugin/ } ).click();
	}

	/**
	 * Clicks on one of the top tabs (e.g. 'Post' or 'Block') in the sidebar.
	 *
	 * @param {EditorSidebarTab} tabName Name of tab to click.
	 * @returns {Promise<void>} No return value.
	 */
	async clickTab( tabName: EditorSidebarTab ): Promise< void > {
		const editorParent = await this.editor.parent();
		const settingsPanel = editorParent.locator( panel );

		await settingsPanel.getByRole( 'tab', { name: tabName } ).click();
	}

	/**
	 * Expands a collapsed section of the sidebar.
	 *
	 * If the section is already open, this method will pass.
	 *
	 * @param {string} name Name of section to be expanded.
	 *
	 * @returns The section element.
	 */
	async expandSection( name: string ) {
		const section = await this.getSection( name );
		if ( await this.targetIsOpen( selectors.section( name ) ) ) {
			return section;
		}

		const editorParent = await this.editor.parent();
		const sectionLocator = editorParent.locator( selectors.section( name ) );
		await sectionLocator.click( { position: { x: 5, y: 5 } } );

		const expandedLocator = editorParent.locator(
			`${ selectors.section( name ) }[aria-expanded="true"]`
		);
		await expandedLocator.waitFor();

		return section;
	}

	/**
	 * Expands a collapsed `Summary` section of the sidebar if it exists.
	 * The `Summary` section is no longer collapsible in recent GB iterations
	 * @see https://github.com/WordPress/gutenberg/commit/201099408131e2abe3cd094f7a1e7e539a350c12
	 * @deprecated To discourage the adoption of this function
	 * @todo Remove when all platforms have eventually been migrated
	 *
	 * If the section is already open, this method will pass.
	 *
	 * @param {string} name Name of section to be expanded.
	 */
	async expandSummary( name: string ): Promise< void > {
		const editorParent = await this.editor.parent();
		const sectionLocator = editorParent.locator( selectors.section( name ) );

		if ( ! ( await sectionLocator.isVisible() ) ) {
			return;
		}

		this.expandSection( name );
	}

	/**
	 * Given a selector, determines whether the target button/toggle is
	 * in an expanded state.
	 *
	 * If the toggle is in the on state or otherwise in an expanded
	 * state, this method will return true. Otherwise, false.
	 *
	 * @param {string} selector Target selector.
	 * @returns {Promise<boolean>} True if target is in an expanded state. False otherwise.
	 */
	private async targetIsOpen( selector: string ): Promise< boolean > {
		const editorParent = await this.editor.parent();
		const locator = editorParent.locator( selector );
		const state = await locator.getAttribute( 'aria-expanded' );

		return state === 'true';
	}

	/* Post Privacy/Visibility */

	/**
	 * Opens the Post Visibility popover.
	 *
	 * If the popover is already toggled open, this method will pass.
	 *
	 * @throws {Error} If popover failed to open.
	 */
	async openVisibilityOptions(): Promise< void > {
		if ( await this.targetIsOpen( selectors.visibilityButton ) ) {
			return;
		}

		const editorParent = await this.editor.parent();
		const buttonLocator = editorParent.locator( selectors.visibilityButton );
		await buttonLocator.click();

		const expandedLocator = editorParent.locator(
			`${ selectors.visibilityButton }[aria-expanded="true"]`
		);
		await expandedLocator.waitFor();
	}

	/**
	 * Closes the Post Visibility popover.
	 *
	 * If the popover is already closed, this method will pass.
	 *
	 * @throws {Error} If popover failed to be closed.
	 */
	async closeVisibilityOptions(): Promise< void > {
		if ( ! ( await this.targetIsOpen( selectors.visibilityButton ) ) ) {
			return;
		}

		const editorParent = await this.editor.parent();
		const buttonLocator = editorParent.locator( selectors.visibilityButton );
		await buttonLocator.click();

		const closedLocator = editorParent.locator(
			`${ selectors.visibilityButton }[aria-expanded="false"]`
		);
		await closedLocator.waitFor();
	}

	/**
	 * Sets the post visibility to the provided visibility setting.
	 *
	 * @param {ArticlePrivacyOptions} visibility Desired post visibility setting.
	 * @param param1 Object parameter.
	 * @param {string} param1.password Password for the post. Normally an optinal value, this
	 * 	must be set if the `visibility` parameter is set to `Password`.
	 */
	async selectVisibility(
		visibility: ArticlePrivacyOptions,
		{ password }: { password?: string } = {}
	): Promise< void > {
		const editorParent = await this.editor.parent();
		const optionLocator = editorParent.locator( selectors.visibilityOption( visibility ) );
		await optionLocator.click();

		if ( visibility === 'Private' ) {
			// Private articles are posted immediately and thus we must break the
			// single responsibility principle for this case.
			// @TODO: eventually refactor this out to a ConfirmationDialogComponent.
			const dialogConfirmLocator = editorParent.locator(
				'div[role="dialog"] button:has-text("OK")'
			);
			await dialogConfirmLocator.click();
		}

		// For Password-protected posts, the password field needs to be filled.
		if ( visibility === 'Password' ) {
			if ( ! password ) {
				throw new Error( 'Post password is undefined.' );
			}
			await this.setPostPassword( password );
		}
	}

	/**
	 * Sets the article password, for password-protected articles.
	 *
	 * @param {string} password Password to be used.
	 */
	private async setPostPassword( password: string ): Promise< void > {
		const editorParent = await this.editor.parent();
		const inputLocator = editorParent.locator( selectors.postPasswordInput );
		await inputLocator.fill( password );
	}

	/* Schedule */

	/**
	 * Opens the Schedule picker.
	 */
	async openSchedule(): Promise< void > {
		if ( await this.targetIsOpen( selectors.scheduleButton ) ) {
			return;
		}

		const editorParent = await this.editor.parent();
		const buttonLocator = editorParent.locator( selectors.scheduleButton );
		await buttonLocator.click();
	}

	/**
	 * Closes the Schedule picker.
	 */
	async closeSchedule(): Promise< void > {
		// Under certain circumstances, the scheduler is auto-dismissed at the end
		// of interaction and thus the closure is not required.
		// For instance, if the current time is in the 'am' and the article is
		// scheduled for 'pm', the act of clicking on the 'pm' button dismisses the
		// scheduler.
		if ( ! ( await this.targetIsOpen( selectors.scheduleButton ) ) ) {
			return;
		}

		const editorParent = await this.editor.parent();

		if ( envVariables.VIEWPORT_NAME === 'mobile' ) {
			const buttonLocator = editorParent.locator( selectors.schedulePopoverCloseButton );
			await buttonLocator.click();
		} else {
			const buttonLocator = editorParent.locator( selectors.scheduleButton );
			await buttonLocator.click();
		}
	}

	/**
	 * Schedules the page/post.
	 *
	 * @param {ArticlePublishSchedule} date Date of the article to be scheduled.
	 */
	async setScheduleDetails( date: ArticlePublishSchedule ): Promise< void > {
		const editorParent = await this.editor.parent();
		let key: keyof ArticlePublishSchedule;

		for ( key in date ) {
			if ( key === 'meridian' ) {
				// am/pm is a button.
				const meridianButtonLocator = editorParent.locator(
					selectors.scheduleMeridianButton( date[ key ] )
				);
				await meridianButtonLocator.click();
				continue;
			}
			if ( key === 'month' ) {
				// For month numbers less than 10, pad the digit to be
				// 2 digits as required by the select.
				const monthSelectLocator = editorParent.locator( selectors.scheduleInput( 'month' ) );
				await monthSelectLocator.selectOption( ( date[ key ] + 1 ).toString().padStart( 2, '0' ) );
				continue;
			}
			if ( key === 'date' ) {
				const daySelector = editorParent.locator( selectors.scheduleInput( 'day' ) );
				await daySelector.fill( date[ key ].toString() );
				continue;
			}

			// Regular input fields.
			const inputLocator = editorParent.locator( selectors.scheduleInput( key ) );
			await inputLocator.fill( date[ key ].toString() );
		}
	}

	/**
	 * Opens the Revisions modal
	 * via summary button for Gutenberg 18.7.0
	 */
	async showRevisions(): Promise< void > {
		const editorParent = await this.editor.parent();
		const locator = editorParent.locator( selectors.showRevisionButton );

		await locator.click();
	}

	/**
	 * Check a category checkbox for an article.
	 *
	 * @param {string} name Category name.
	 * @throws {Error} If requested cateogry is not found.
	 */
	async checkCategory( name: string ): Promise< void > {
		const editorParent = await this.editor.parent();
		//TODO: Categories can be slow because we never do any cleanup. Remove extended timeout once we start doing cleanup.
		const locator = editorParent.locator( selectors.categoryCheckbox( name ) );

		try {
			await locator.click( { timeout: 60 * 1000 } );
		} catch {
			throw new Error( `No category matching ${ name } found.` );
		}
	}

	/**
	 * Enters a tag in the tag field, presses enter to submit it
	 * and validates it shows up in field.
	 *
	 * This method does not partial match; if a tag does not exist with the
	 * provided name, a new one is added.
	 *
	 * @param {string} name Tag name to enter.
	 */
	async enterTag( name: string ): Promise< void > {
		const editorParent = await this.editor.parent();
		const inputLocator = editorParent.locator( selectors.tagInput );
		await inputLocator.fill( name );
		await this.page.keyboard.press( 'Enter' );

		const addedTagLocator = editorParent.locator( selectors.addedTag( name ) );
		await addedTagLocator.waitFor();
	}

	/**
	 * Enter the URL slug for the page/post.
	 *
	 * @param {string} slug URL slug to set.
	 */
	async enterUrlSlug( slug: string ) {
		const editorParent = await this.editor.parent();
		// TODO: Once WordPress/gutenberg#63669 is everywhere, remove the alternation.
		await editorParent.getByRole( 'button', { name: /Change link:/ } ).click();
		await editorParent.getByRole( 'textbox', { name: /^(Link|Slug)$/ } ).fill( slug );
		await editorParent.getByRole( 'button', { name: 'Close', exact: true } ).click();
	}
}