-
Notifications
You must be signed in to change notification settings - Fork 0
Introduce some tools for users #7
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
71137ed
Introduced some tools for users
Screenfeed 13a040c
Lint
Screenfeed e951bc6
Global export
Screenfeed a78b437
Removed unnecessary checks after `execSync()`
Screenfeed 32423f5
Simpler user creation
Screenfeed e7d041d
CS
Screenfeed 61cdb97
Better default value
Screenfeed 67ae0f7
Improved and fixed the doc
Screenfeed b62615c
Added doc to `README.md`
Screenfeed 0fe3655
ffs
Screenfeed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // @ts-check | ||
| import { | ||
| expect, | ||
| Admin, | ||
| RequestUtils, | ||
| } from '@wordpress/e2e-test-utils-playwright'; | ||
| import { execSync } from 'child_process'; | ||
|
|
||
| /** | ||
| * @typedef {import('@playwright/test').Page} Page | ||
| * @typedef {Object} User | ||
| * @property {string} username The user name. | ||
| * @property {string} password The user's password. | ||
| */ | ||
|
|
||
| /** | ||
| * Creates a translator user. | ||
| * | ||
| * @param {Array<string>} langSlugs Language slugs. | ||
| * @param {string} userName Optional. User name. | ||
| * Defaults to `XX-YY-translator`, where `XX` and `YY` are language slugs. | ||
| * @return {Promise<User&{id: number}>} Promise resolving to a user object containing ID, user name, and password. | ||
| */ | ||
| export async function createTranslator( langSlugs, userName = '' ) { | ||
| userName = | ||
| '' === userName | ||
| ? `${ langSlugs.join( '-' ).toUpperCase() }-translator` | ||
| : userName; | ||
| const email = `${ userName.toLowerCase() }@example.com`; | ||
| const userId = parseInt( | ||
| execSync( | ||
| `npx wp-env run tests-cli wp user create ${ userName } ${ email } --role=editor --user_pass=password --porcelain`, | ||
| { encoding: 'utf8' } | ||
| ).trim(), | ||
| 10 | ||
| ); | ||
|
|
||
| langSlugs.forEach( ( langSlug ) => { | ||
| execSync( | ||
| `npx wp-env run tests-cli wp user add-cap ${ userId } translate_${ langSlug }`, | ||
| { encoding: 'utf8' } | ||
| ); | ||
| } ); | ||
|
|
||
| return { id: userId, username: userName, password: 'password' }; | ||
| } | ||
|
|
||
| /** | ||
| * Switches to the given user. | ||
| * Inspired from https://github.com/WordPress/gutenberg/blob/9ee534a42cd546fc2da23ce0f31607467c78c94c/test/e2e/specs/editor/collaboration/fixtures/collaboration-utils.ts#L104. | ||
| * | ||
| * @param {User} user The user to switch to. | ||
| * @param {Admin} admin Instance of `Admin`. | ||
| * @param {RequestUtils} requestUtils Gutenberg request utils object. | ||
| * @return {Promise<Page>} Promise resolving to the `Page` object. | ||
| */ | ||
| export async function switchToUser( user, admin, requestUtils ) { | ||
| const translatorContext = await admin.browser.newContext( { | ||
| baseURL: requestUtils.baseURL, | ||
| } ); | ||
| const page = await translatorContext.newPage(); | ||
|
|
||
| await page.goto( '/wp-login.php' ); | ||
| await page.locator( '#user_login' ).fill( user.username ); | ||
| await page.locator( '#user_pass' ).fill( user.password ); | ||
| await page.getByRole( 'button', { name: 'Log In' } ).click(); | ||
| await page.waitForURL( '**/wp-admin/**' ); | ||
|
|
||
| expect( | ||
| page.getByRole( 'menuitem', { | ||
| name: `Howdy, ${ user.username }`, | ||
| } ) | ||
| ).toBeVisible(); | ||
|
|
||
| await page.waitForFunction( () => window?.wp?.data && window?.wp?.blocks ); | ||
| await page.evaluate( () => { | ||
| window.wp.data | ||
| .dispatch( 'core/preferences' ) | ||
| .set( 'core/edit-post', 'welcomeGuide', false ); | ||
| window.wp.data | ||
| .dispatch( 'core/preferences' ) | ||
| .set( 'core/edit-post', 'fullscreenMode', false ); | ||
| } ); | ||
|
Screenfeed marked this conversation as resolved.
|
||
|
|
||
| return page; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't check types here.
The reason is that we strongly document function in this package, with TS-like checks.
If someone passes dog shit, I expect the code to fail. Here it goes on silently.
Moreover, you coded a default value but didn't document it that way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type check was my way to provide a default value. Improved with 61cdb97.