Skip to content
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

Run the E2E tests as a user with the author role #11359

Merged
merged 8 commits into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,9 @@ jobs:
- stage: test
env: WP_VERSION=latest
script:
- npm install || exit 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presume this is run by Travis and thus not needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's run as part of the e2e tests. It didn't used to be, but now it is.

- ./bin/run-e2e-tests.sh || exit 1

- stage: test
env: WP_VERSION=latest E2E_ROLE=author
script:
- ./bin/run-e2e-tests.sh || exit 1
10 changes: 10 additions & 0 deletions bin/install-wordpress.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ echo -e $(status_message "Installing WordPress...")
# prevents permissions errors. See: https://github.com/WordPress/gutenberg/pull/8427#issuecomment-410232369
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

if [ "$E2E_ROLE" = "author" ]; then
# Create an additional author user for testsing.
docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run --rm -u 33 $CLI user create author [email protected] --role=author --user_pass=authpass
# Assign the existing Hello World post to the author.
docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run --rm -u 33 $CLI post update 1 --post_author=2
fi

if [ "$WP_VERSION" == "latest" ]; then
# Check for WordPress updates, to make sure we're running the very latest version.
docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run --rm -u 33 $CLI core update >/dev/null
Expand All @@ -69,3 +76,6 @@ fi
# Activate Gutenberg.
echo -e $(status_message "Activating Gutenberg...")
docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run --rm $CLI plugin activate gutenberg >/dev/null

# Install a dummy favicon to avoid 404 errors.
docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run --rm $CONTAINER touch /var/www/html/favicon.ico
7 changes: 5 additions & 2 deletions bin/run-e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ cd "$(dirname "$0")/../"
# Setup local environement
( ./bin/setup-local-env.sh )

# Run the tests
npm run test-e2e
if [ "$E2E_ROLE" = "author" ]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the role we want to use could be passed to the script rather than hardcoding an "author-or-admin" choice, but if you want to do that as a follow-up it's fine with me 😄

(It would also let you avoid the if/else here, and instead just pass the role we want, with "admin" being the default. 🤷‍♂️)

WP_PASSWORD=authpass WP_USERNAME=author npm run test-e2e
else
npm run test-e2e
fi
42 changes: 36 additions & 6 deletions packages/block-library/src/latest-posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
ToggleControl,
Toolbar,
} from '@wordpress/components';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';
import { __ } from '@wordpress/i18n';
import { dateI18n, format, __experimentalGetSettings } from '@wordpress/date';
import { decodeEntities } from '@wordpress/html-entities';
Expand All @@ -27,15 +29,46 @@ import {
} from '@wordpress/editor';
import { withSelect } from '@wordpress/data';

/**
* Module Constants
*/
const CATEGORIES_LIST_QUERY = {
per_page: 100,
};
const MAX_POSTS_COLUMNS = 6;

class LatestPostsEdit extends Component {
constructor() {
super( ...arguments );

this.state = {
categoriesList: [],
};
this.toggleDisplayPostDate = this.toggleDisplayPostDate.bind( this );
}

componentWillMount() {
this.isStillMounted = true;
this.fetchRequest = apiFetch( {
path: addQueryArgs( `/wp/v2/categories`, CATEGORIES_LIST_QUERY ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this need to iterate the entire list? What if I have 102 categories?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷‍♀️ this is just how it was in the original code, I was just fixing it so that it didn't break for authors

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I'll mention on #10873 and we can resolve with that.

} ).then(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if this request fails? We don't use catch here...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a catch which sets the list to an empty list.

( categoriesList ) => {
if ( this.isStillMounted ) {
this.setState( { categoriesList } );
}
}
).catch(
() => {
if ( this.isStillMounted ) {
this.setState( { categoriesList: [] } );
}
}
);
}

componentWillUnmount() {
this.isStillMounted = false;
}

toggleDisplayPostDate() {
const { displayPostDate } = this.props.attributes;
const { setAttributes } = this.props;
Expand All @@ -44,7 +77,8 @@ class LatestPostsEdit extends Component {
}

render() {
const { attributes, categoriesList, setAttributes, latestPosts } = this.props;
const { attributes, setAttributes, latestPosts } = this.props;
const { categoriesList } = this.state;
const { displayPostDate, align, postLayout, columns, order, orderBy, categories, postsToShow } = attributes;

const inspectorControls = (
Expand Down Expand Up @@ -163,11 +197,7 @@ export default withSelect( ( select, props ) => {
orderby: orderBy,
per_page: postsToShow,
}, ( value ) => ! isUndefined( value ) );
const categoriesListQuery = {
per_page: 100,
};
return {
latestPosts: getEntityRecords( 'postType', 'post', latestPostsQuery ),
categoriesList: getEntityRecords( 'taxonomy', 'category', categoriesListQuery ),
};
} )( LatestPostsEdit );
6 changes: 3 additions & 3 deletions test/e2e/specs/change-detection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ describe( 'Change detection', () => {
it( 'Should prompt to confirm unsaved changes for autosaved draft for non-content fields', async () => {
await page.type( '.editor-post-title__input', 'Hello World' );

// Toggle post as sticky (not persisted for autosave).
// Toggle post as needing review (not persisted for autosave).
await ensureSidebarOpened();

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

// Force autosave to occur immediately.
await Promise.all( [
Expand Down
11 changes: 10 additions & 1 deletion test/e2e/specs/templates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
pressWithModifier,
visitAdmin,
clickBlockAppender,
switchToAdminUser,
switchToTestUser,
} from '../support/utils';
import { activatePlugin, deactivatePlugin } from '../support/plugins';

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

async function setPostFormat( format ) {
// To set the post format, we need to be the admin user.
await switchToAdminUser();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switching user will not actually do anything if we're running as the admin user. We'll only switch if we're running as a different user, because the author role cannot manage plugins.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good to know; could you leave a comment explaining that in the code? 😄

await visitAdmin( 'options-writing.php' );
await page.select( '#default_post_format', format );
return Promise.all( [
await Promise.all( [
page.waitForNavigation(),
page.click( '#submit' ),
] );
await switchToTestUser();
}

beforeAll( async () => await setPostFormat( 'image' ) );
Expand Down Expand Up @@ -93,9 +98,13 @@ describe( 'templates', () => {
} );

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

expect( await getEditedPostContent() ).toMatchSnapshot();
await switchToTestUser();
} );
} );
} );
10 changes: 9 additions & 1 deletion test/e2e/support/plugins.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Node dependencies
*/
import { visitAdmin } from './utils';
import { visitAdmin, switchToAdminUser, switchToTestUser } from './utils';

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

/**
Expand All @@ -21,9 +23,11 @@ export async function installPlugin( slug, searchTerm ) {
* @param {string} slug Plugin slug.
*/
export async function activatePlugin( slug ) {
await switchToAdminUser();
await visitAdmin( 'plugins.php' );
await page.click( 'tr[data-slug="' + slug + '"] .activate a' );
await page.waitForSelector( 'tr[data-slug="' + slug + '"] .deactivate a' );
await switchToTestUser();
}

/**
Expand All @@ -32,9 +36,11 @@ export async function activatePlugin( slug ) {
* @param {string} slug Plugin slug.
*/
export async function deactivatePlugin( slug ) {
await switchToAdminUser();
await visitAdmin( 'plugins.php' );
await page.click( 'tr[data-slug="' + slug + '"] .deactivate a' );
await page.waitForSelector( 'tr[data-slug="' + slug + '"] .delete a' );
await switchToTestUser();
}

/**
Expand All @@ -43,6 +49,7 @@ export async function deactivatePlugin( slug ) {
* @param {string} slug Plugin slug.
*/
export async function uninstallPlugin( slug ) {
await switchToAdminUser();
await visitAdmin( 'plugins.php' );
const confirmPromise = new Promise( ( resolve ) => {
page.once( 'dialog', () => resolve() );
Expand All @@ -52,4 +59,5 @@ export async function uninstallPlugin( slug ) {
page.click( 'tr[data-slug="' + slug + '"] .delete a' ),
] );
await page.waitForSelector( 'tr[data-slug="' + slug + '"].deleted' );
await switchToTestUser();
}
6 changes: 6 additions & 0 deletions test/e2e/support/setup-test-framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ function observeConsoleLogging() {
return;
}

// Viewing posts on the front end can result in this error, which
// has nothing to do with Gutenberg.
if ( text.includes( 'net::ERR_UNKNOWN_URL_SCHEME' ) ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a jQuery thing that happens on the front end, and we sometimes would get hit by it, but sometimes the test assertions would complete quickly enough for it not to make the test fail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an issue anywhere that tracks that? I don't quite follow.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what causes it yet, it seems to be unrelated to Gutenberg, and happens when you view the published post on the front end. I'll investigate further after this merge.

return;
}

const logFunction = OBSERVED_CONSOLE_MESSAGE_TYPES[ type ];

// Disable reason: We intentionally bubble up the console message
Expand Down
43 changes: 38 additions & 5 deletions test/e2e/support/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import { URL } from 'url';
*/
import { times, castArray } from 'lodash';

const WP_ADMIN_USER = {
username: 'admin',
password: 'password',
};

const {
WP_BASE_URL = 'http://localhost:8889',
WP_USERNAME = 'admin',
WP_PASSWORD = 'password',
WP_USERNAME = WP_ADMIN_USER.username,
WP_PASSWORD = WP_ADMIN_USER.password,
} = process.env;

/**
Expand Down Expand Up @@ -76,16 +81,44 @@ async function goToWPPath( WPPath, query ) {
await page.goto( getUrl( WPPath, query ) );
}

async function login() {
await page.type( '#user_login', WP_USERNAME );
await page.type( '#user_pass', WP_PASSWORD );
async function login( username = WP_USERNAME, password = WP_PASSWORD ) {
await page.focus( '#user_login' );
await pressWithModifier( META_KEY, 'a' );
await page.type( '#user_login', username );
await page.focus( '#user_pass' );
await pressWithModifier( META_KEY, 'a' );
await page.type( '#user_pass', password );

await Promise.all( [
page.waitForNavigation(),
page.click( '#wp-submit' ),
] );
}

/**
* Switches the current user to the admin user (if the user
* running the test is not already the admin user).
*/
export async function switchToAdminUser() {
if ( WP_USERNAME === WP_ADMIN_USER.username ) {
return;
}
await goToWPPath( 'wp-login.php' );
await login( WP_ADMIN_USER.username, WP_ADMIN_USER.password );
}

/**
* Switches the current user to whichever user we should be
* running the tests as (if we're not already that user).
*/
export async function switchToTestUser() {
if ( WP_USERNAME === WP_ADMIN_USER.username ) {
return;
}
await goToWPPath( 'wp-login.php' );
await login();
}

export async function visitAdmin( adminPath, query ) {
await goToWPPath( join( 'wp-admin', adminPath ), query );

Expand Down