Skip to content

Commit d6ba8c2

Browse files
authored
Run the E2E tests as a user with the author role (#11359)
1 parent 489eb79 commit d6ba8c2

File tree

9 files changed

+122
-19
lines changed

9 files changed

+122
-19
lines changed

.travis.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,9 @@ jobs:
6767
- stage: test
6868
env: WP_VERSION=latest
6969
script:
70-
- npm install || exit 1
70+
- ./bin/run-e2e-tests.sh || exit 1
71+
72+
- stage: test
73+
env: WP_VERSION=latest E2E_ROLE=author
74+
script:
7175
- ./bin/run-e2e-tests.sh || exit 1

bin/install-wordpress.sh

+10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ echo -e $(status_message "Installing WordPress...")
5454
# prevents permissions errors. See: https://github.com/WordPress/gutenberg/pull/8427#issuecomment-410232369
5555
docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run --rm -u 33 $CLI core install --title="$SITE_TITLE" --admin_user=admin --admin_password=password [email protected] --skip-email --url=http://localhost:$HOST_PORT >/dev/null
5656

57+
if [ "$E2E_ROLE" = "author" ]; then
58+
# Create an additional author user for testsing.
59+
docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run --rm -u 33 $CLI user create author [email protected] --role=author --user_pass=authpass
60+
# Assign the existing Hello World post to the author.
61+
docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run --rm -u 33 $CLI post update 1 --post_author=2
62+
fi
63+
5764
if [ "$WP_VERSION" == "latest" ]; then
5865
# Check for WordPress updates, to make sure we're running the very latest version.
5966
docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run --rm -u 33 $CLI core update >/dev/null
@@ -69,3 +76,6 @@ fi
6976
# Activate Gutenberg.
7077
echo -e $(status_message "Activating Gutenberg...")
7178
docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run --rm $CLI plugin activate gutenberg >/dev/null
79+
80+
# Install a dummy favicon to avoid 404 errors.
81+
docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run --rm $CONTAINER touch /var/www/html/favicon.ico

bin/run-e2e-tests.sh

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ cd "$(dirname "$0")/../"
77
# Setup local environement
88
( ./bin/setup-local-env.sh )
99

10-
# Run the tests
11-
npm run test-e2e
10+
if [ "$E2E_ROLE" = "author" ]; then
11+
WP_PASSWORD=authpass WP_USERNAME=author npm run test-e2e
12+
else
13+
npm run test-e2e
14+
fi

packages/block-library/src/latest-posts/edit.js

+36-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
ToggleControl,
1818
Toolbar,
1919
} from '@wordpress/components';
20+
import apiFetch from '@wordpress/api-fetch';
21+
import { addQueryArgs } from '@wordpress/url';
2022
import { __ } from '@wordpress/i18n';
2123
import { dateI18n, format, __experimentalGetSettings } from '@wordpress/date';
2224
import { decodeEntities } from '@wordpress/html-entities';
@@ -27,15 +29,46 @@ import {
2729
} from '@wordpress/editor';
2830
import { withSelect } from '@wordpress/data';
2931

32+
/**
33+
* Module Constants
34+
*/
35+
const CATEGORIES_LIST_QUERY = {
36+
per_page: 100,
37+
};
3038
const MAX_POSTS_COLUMNS = 6;
3139

3240
class LatestPostsEdit extends Component {
3341
constructor() {
3442
super( ...arguments );
35-
43+
this.state = {
44+
categoriesList: [],
45+
};
3646
this.toggleDisplayPostDate = this.toggleDisplayPostDate.bind( this );
3747
}
3848

49+
componentWillMount() {
50+
this.isStillMounted = true;
51+
this.fetchRequest = apiFetch( {
52+
path: addQueryArgs( `/wp/v2/categories`, CATEGORIES_LIST_QUERY ),
53+
} ).then(
54+
( categoriesList ) => {
55+
if ( this.isStillMounted ) {
56+
this.setState( { categoriesList } );
57+
}
58+
}
59+
).catch(
60+
() => {
61+
if ( this.isStillMounted ) {
62+
this.setState( { categoriesList: [] } );
63+
}
64+
}
65+
);
66+
}
67+
68+
componentWillUnmount() {
69+
this.isStillMounted = false;
70+
}
71+
3972
toggleDisplayPostDate() {
4073
const { displayPostDate } = this.props.attributes;
4174
const { setAttributes } = this.props;
@@ -44,7 +77,8 @@ class LatestPostsEdit extends Component {
4477
}
4578

4679
render() {
47-
const { attributes, categoriesList, setAttributes, latestPosts } = this.props;
80+
const { attributes, setAttributes, latestPosts } = this.props;
81+
const { categoriesList } = this.state;
4882
const { displayPostDate, align, postLayout, columns, order, orderBy, categories, postsToShow } = attributes;
4983

5084
const inspectorControls = (
@@ -163,11 +197,7 @@ export default withSelect( ( select, props ) => {
163197
orderby: orderBy,
164198
per_page: postsToShow,
165199
}, ( value ) => ! isUndefined( value ) );
166-
const categoriesListQuery = {
167-
per_page: 100,
168-
};
169200
return {
170201
latestPosts: getEntityRecords( 'postType', 'post', latestPostsQuery ),
171-
categoriesList: getEntityRecords( 'taxonomy', 'category', categoriesListQuery ),
172202
};
173203
} )( LatestPostsEdit );

test/e2e/specs/change-detection.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ describe( 'Change detection', () => {
9191
it( 'Should prompt to confirm unsaved changes for autosaved draft for non-content fields', async () => {
9292
await page.type( '.editor-post-title__input', 'Hello World' );
9393

94-
// Toggle post as sticky (not persisted for autosave).
94+
// Toggle post as needing review (not persisted for autosave).
9595
await ensureSidebarOpened();
9696

97-
const postStickyToggleButton = ( await page.$x( "//label[contains(text(), 'Stick to the Front Page')]" ) )[ 0 ];
98-
await postStickyToggleButton.click( 'button' );
97+
const postPendingReviewButton = ( await page.$x( "//label[contains(text(), 'Pending Review')]" ) )[ 0 ];
98+
await postPendingReviewButton.click( 'button' );
9999

100100
// Force autosave to occur immediately.
101101
await Promise.all( [

test/e2e/specs/templates.test.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
pressWithModifier,
1010
visitAdmin,
1111
clickBlockAppender,
12+
switchToAdminUser,
13+
switchToTestUser,
1214
} from '../support/utils';
1315
import { activatePlugin, deactivatePlugin } from '../support/plugins';
1416

@@ -60,12 +62,15 @@ describe( 'templates', () => {
6062
const STANDARD_FORMAT_VALUE = '0';
6163

6264
async function setPostFormat( format ) {
65+
// To set the post format, we need to be the admin user.
66+
await switchToAdminUser();
6367
await visitAdmin( 'options-writing.php' );
6468
await page.select( '#default_post_format', format );
65-
return Promise.all( [
69+
await Promise.all( [
6670
page.waitForNavigation(),
6771
page.click( '#submit' ),
6872
] );
73+
await switchToTestUser();
6974
}
7075

7176
beforeAll( async () => await setPostFormat( 'image' ) );
@@ -93,9 +98,13 @@ describe( 'templates', () => {
9398
} );
9499

95100
it( 'should not populate new page with default block for format', async () => {
101+
// This test always needs to run as the admin user, because other roles can't create pages.
102+
// It can't be skipped, because then it failed because of not testing the snapshot.
103+
await switchToAdminUser();
96104
await newPost( { postType: 'page' } );
97105

98106
expect( await getEditedPostContent() ).toMatchSnapshot();
107+
await switchToTestUser();
99108
} );
100109
} );
101110
} );

test/e2e/support/plugins.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Node dependencies
33
*/
4-
import { visitAdmin } from './utils';
4+
import { visitAdmin, switchToAdminUser, switchToTestUser } from './utils';
55

66
/**
77
* Install a plugin from the WP.org repository.
@@ -10,9 +10,11 @@ import { visitAdmin } from './utils';
1010
* @param {string?} searchTerm If the plugin is not findable by its slug use an alternative term to search.
1111
*/
1212
export async function installPlugin( slug, searchTerm ) {
13+
await switchToAdminUser();
1314
await visitAdmin( 'plugin-install.php?s=' + encodeURIComponent( searchTerm || slug ) + '&tab=search&type=term' );
1415
await page.click( '.install-now[data-slug="' + slug + '"]' );
1516
await page.waitForSelector( '.activate-now[data-slug="' + slug + '"]' );
17+
await switchToTestUser();
1618
}
1719

1820
/**
@@ -21,9 +23,11 @@ export async function installPlugin( slug, searchTerm ) {
2123
* @param {string} slug Plugin slug.
2224
*/
2325
export async function activatePlugin( slug ) {
26+
await switchToAdminUser();
2427
await visitAdmin( 'plugins.php' );
2528
await page.click( 'tr[data-slug="' + slug + '"] .activate a' );
2629
await page.waitForSelector( 'tr[data-slug="' + slug + '"] .deactivate a' );
30+
await switchToTestUser();
2731
}
2832

2933
/**
@@ -32,9 +36,11 @@ export async function activatePlugin( slug ) {
3236
* @param {string} slug Plugin slug.
3337
*/
3438
export async function deactivatePlugin( slug ) {
39+
await switchToAdminUser();
3540
await visitAdmin( 'plugins.php' );
3641
await page.click( 'tr[data-slug="' + slug + '"] .deactivate a' );
3742
await page.waitForSelector( 'tr[data-slug="' + slug + '"] .delete a' );
43+
await switchToTestUser();
3844
}
3945

4046
/**
@@ -43,6 +49,7 @@ export async function deactivatePlugin( slug ) {
4349
* @param {string} slug Plugin slug.
4450
*/
4551
export async function uninstallPlugin( slug ) {
52+
await switchToAdminUser();
4653
await visitAdmin( 'plugins.php' );
4754
const confirmPromise = new Promise( ( resolve ) => {
4855
page.once( 'dialog', () => resolve() );
@@ -52,4 +59,5 @@ export async function uninstallPlugin( slug ) {
5259
page.click( 'tr[data-slug="' + slug + '"] .delete a' ),
5360
] );
5461
await page.waitForSelector( 'tr[data-slug="' + slug + '"].deleted' );
62+
await switchToTestUser();
5563
}

test/e2e/support/setup-test-framework.js

+6
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ function observeConsoleLogging() {
116116
return;
117117
}
118118

119+
// Viewing posts on the front end can result in this error, which
120+
// has nothing to do with Gutenberg.
121+
if ( text.includes( 'net::ERR_UNKNOWN_URL_SCHEME' ) ) {
122+
return;
123+
}
124+
119125
const logFunction = OBSERVED_CONSOLE_MESSAGE_TYPES[ type ];
120126

121127
// Disable reason: We intentionally bubble up the console message

test/e2e/support/utils.js

+38-5
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ import fetch from 'node-fetch';
1515
*/
1616
import { addQueryArgs } from '@wordpress/url';
1717

18+
const WP_ADMIN_USER = {
19+
username: 'admin',
20+
password: 'password',
21+
};
22+
1823
const {
1924
WP_BASE_URL = 'http://localhost:8889',
20-
WP_USERNAME = 'admin',
21-
WP_PASSWORD = 'password',
25+
WP_USERNAME = WP_ADMIN_USER.username,
26+
WP_PASSWORD = WP_ADMIN_USER.password,
2227
} = process.env;
2328

2429
/**
@@ -82,16 +87,44 @@ async function goToWPPath( WPPath, query ) {
8287
await page.goto( getUrl( WPPath, query ) );
8388
}
8489

85-
async function login() {
86-
await page.type( '#user_login', WP_USERNAME );
87-
await page.type( '#user_pass', WP_PASSWORD );
90+
async function login( username = WP_USERNAME, password = WP_PASSWORD ) {
91+
await page.focus( '#user_login' );
92+
await pressWithModifier( META_KEY, 'a' );
93+
await page.type( '#user_login', username );
94+
await page.focus( '#user_pass' );
95+
await pressWithModifier( META_KEY, 'a' );
96+
await page.type( '#user_pass', password );
8897

8998
await Promise.all( [
9099
page.waitForNavigation(),
91100
page.click( '#wp-submit' ),
92101
] );
93102
}
94103

104+
/**
105+
* Switches the current user to the admin user (if the user
106+
* running the test is not already the admin user).
107+
*/
108+
export async function switchToAdminUser() {
109+
if ( WP_USERNAME === WP_ADMIN_USER.username ) {
110+
return;
111+
}
112+
await goToWPPath( 'wp-login.php' );
113+
await login( WP_ADMIN_USER.username, WP_ADMIN_USER.password );
114+
}
115+
116+
/**
117+
* Switches the current user to whichever user we should be
118+
* running the tests as (if we're not already that user).
119+
*/
120+
export async function switchToTestUser() {
121+
if ( WP_USERNAME === WP_ADMIN_USER.username ) {
122+
return;
123+
}
124+
await goToWPPath( 'wp-login.php' );
125+
await login();
126+
}
127+
95128
export async function visitAdmin( adminPath, query ) {
96129
await goToWPPath( join( 'wp-admin', adminPath ), query );
97130

0 commit comments

Comments
 (0)