Skip to content

Commit bd386ab

Browse files
committed
Run the E2E tests as a user with the author role
1 parent 273da09 commit bd386ab

8 files changed

+86
-13
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

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ 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+
# Set up an author, and run the tests with that user, and don't error if the user is already there
12+
WP_PASSWORD=authpass WP_USERNAME=author npm run test-e2e
13+
else
14+
npm run test-e2e
15+
fi

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

+9-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,14 @@ describe( 'templates', () => {
6062
const STANDARD_FORMAT_VALUE = '0';
6163

6264
async function setPostFormat( format ) {
65+
await switchToAdminUser();
6366
await visitAdmin( 'options-writing.php' );
6467
await page.select( '#default_post_format', format );
65-
return Promise.all( [
68+
await Promise.all( [
6669
page.waitForNavigation(),
6770
page.click( '#submit' ),
6871
] );
72+
await switchToTestUser();
6973
}
7074

7175
beforeAll( async () => await setPostFormat( 'image' ) );
@@ -93,9 +97,13 @@ describe( 'templates', () => {
9397
} );
9498

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

98105
expect( await getEditedPostContent() ).toMatchSnapshot();
106+
await switchToTestUser();
99107
} );
100108
} );
101109
} );

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
@@ -9,10 +9,15 @@ import { URL } from 'url';
99
*/
1010
import { times, castArray } from 'lodash';
1111

12+
const WP_ADMIN_USER = {
13+
username: 'admin',
14+
password: 'password',
15+
};
16+
1217
const {
1318
WP_BASE_URL = 'http://localhost:8889',
14-
WP_USERNAME = 'admin',
15-
WP_PASSWORD = 'password',
19+
WP_USERNAME = WP_ADMIN_USER.username,
20+
WP_PASSWORD = WP_ADMIN_USER.password,
1621
} = process.env;
1722

1823
/**
@@ -76,16 +81,44 @@ async function goToWPPath( WPPath, query ) {
7681
await page.goto( getUrl( WPPath, query ) );
7782
}
7883

79-
async function login() {
80-
await page.type( '#user_login', WP_USERNAME );
81-
await page.type( '#user_pass', WP_PASSWORD );
84+
async function login( username = WP_USERNAME, password = WP_PASSWORD ) {
85+
await page.focus( '#user_login' );
86+
await pressWithModifier( META_KEY, 'a' );
87+
await page.type( '#user_login', username );
88+
await page.focus( '#user_pass' );
89+
await pressWithModifier( META_KEY, 'a' );
90+
await page.type( '#user_pass', password );
8291

8392
await Promise.all( [
8493
page.waitForNavigation(),
8594
page.click( '#wp-submit' ),
8695
] );
8796
}
8897

98+
/**
99+
* Switches the logged in user to the admin user, if the user
100+
* running the test is not already the admin user.
101+
*/
102+
export async function switchToAdminUser() {
103+
if ( WP_USERNAME === WP_ADMIN_USER.username ) {
104+
return;
105+
}
106+
await goToWPPath( 'wp-login.php' );
107+
await login( WP_ADMIN_USER.username, WP_ADMIN_USER.password );
108+
}
109+
110+
/**
111+
* Switches the logged in user to the user the tests are running as,
112+
* if we're not already that user.
113+
*/
114+
export async function switchToTestUser() {
115+
if ( WP_USERNAME === WP_ADMIN_USER.username ) {
116+
return;
117+
}
118+
await goToWPPath( 'wp-login.php' );
119+
await login();
120+
}
121+
89122
export async function visitAdmin( adminPath, query ) {
90123
await goToWPPath( join( 'wp-admin', adminPath ), query );
91124

0 commit comments

Comments
 (0)